/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;


import com.tofly.xrtygis.commons.Constant;
import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.entity.AbnormalWaterConfig;
import com.tofly.xrtygis.entity.AbnormalWaterUse;
import com.tofly.xrtygis.service.AbnormalWaterConfigService;
import com.tofly.xrtygis.service.AbnormalWaterUseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 分析用户用水量记录表
 *
 * @author admin
 * @date Thu Jul 28 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/abnormalwateruse")
@Api(tags = "分析用户用水量记录表接口")
public class AbnormalWaterUseController {

    private final AbnormalWaterUseService abnormalWaterUseService;
    private final AbnormalWaterConfigService configService;
    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param abnormalWaterUse 分析用户用水量记录表
     * @return
     */
    @GetMapping("/getAbnormalWaterUsePage")
    @ApiOperation(value = "分页查询")
    public ResultRespone getAbnormalWaterUsePage(Page page, @ModelAttribute AbnormalWaterUse abnormalWaterUse) {
        return ResultRespone.success(abnormalWaterUseService.getAbnormalWaterUserPage(page,abnormalWaterUse.getStartTime(),abnormalWaterUse.getEndTime(),abnormalWaterUse.getState(),abnormalWaterUse.getLikeStr()));
    }

    @PostMapping("/getAbnormalWaterUseAnalyze")
    @ApiOperation(value = "用户用水异常分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dates", value = "时间格式 yyyy-MM-dd hh:mm:ss,多个以,分隔", required = true, dataType = "String")
    })
    public ResultRespone getAbnormalWaterUseAnalyze(String dates) {
        List<Date> deviceIdList = Arrays.stream(dates.split(Constant.CSV)).map(s -> DateTimeUtil.convertLDTToDate(DateTimeUtil.fromString2LocalDateTime(s.trim(), DateTimeUtil.FULL_FORMAT)))
                .collect(Collectors.toList());
        return ResultRespone.success(abnormalWaterUseService.send(deviceIdList));
    }

    @GetMapping("/getUserMeterReading")
    @ApiOperation(value = "获取用户抄表详情信息")
    public ResultRespone getUserMeterReading(@ModelAttribute AbnormalWaterUse dto) {
        return ResultRespone.success(abnormalWaterUseService.getUserMeterReading(dto));
    }

    @GetMapping("/getConfig")
    @ApiOperation(value = "获取当前用户抄表分析配置信息")
    public ResultRespone getConfig() {
        return ResultRespone.success(configService.getConfig());
    }

    @PostMapping("/updateConfig")
    @ApiOperation(value = "修改分析用户用水量配置表")
    public ResultRespone getConfig(@RequestBody AbnormalWaterConfig config) {
        return ResultRespone.success(configService.updateConfig(config));
    }
}
