package com.zcxk.controller;

import com.zcxk.common.StringUtils;
import com.zcxk.dto.MonitorDataChartReportDeviceDto;
import com.zcxk.dto.generalView.*;
import com.zcxk.entity.DeviceEntity;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.MonitorDataReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description
 * @auto wangli
 * @data 2020-12-02 8:19
 */
@RestController
@RequestMapping("/MonitorDataReport")
@Api(tags = "统计分析")
public class MonitorDataReportController {

    @Autowired
    private MonitorDataReportService monitorDataReportService;

    @RequestMapping(value = "MonitorDataChartReport",method = RequestMethod.GET)
    @ApiOperation(value = "统计分析——图表")
    public AjaxMessage<List<MonitorDataChartReportDeviceDto>> MonitorDataChartReport(
            @ApiParam(value = "场景id", required = true) @RequestParam Long sceneId,
            @ApiParam(value = "时间") @RequestParam(name = "reportDate",required = false) String reportDate,
            @ApiParam(value = "类型,1年2月3日4自定义", required = true) @RequestParam Integer reportType,
            @ApiParam(value = "开始时间") @RequestParam(name = "startTime",required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(name = "endTime",required = false) String endTime
    ){
        LocalDate localDate = getLocalDate(reportDate, reportType);
        if (sceneId == null || reportType == null){
            return new AjaxMessage<>(ResultStatus.OK);
        }
        if (reportType ==1){
            return new AjaxMessage<>(ResultStatus.OK, monitorDataReportService.monitorDataChartReportByYear(sceneId,localDate.getYear()));
        }
        if (reportType ==2 ){
            return new AjaxMessage<>(ResultStatus.OK, monitorDataReportService.monitorDataChartReportByMonth(sceneId,localDate.getYear(),localDate.getMonthValue()));
        }
        if (reportType ==3){
            return new AjaxMessage<>(ResultStatus.OK, monitorDataReportService.monitorDataChartReportByDay(sceneId,localDate.getYear(),localDate.getMonthValue(),localDate.getDayOfMonth()));
        }
        if (reportType ==4 && StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)){
            return new AjaxMessage<>(ResultStatus.OK, monitorDataReportService.monthMonitorData(sceneId, startTime, endTime));
        }
        return new AjaxMessage<>(ResultStatus.OK);
    }

    @RequestMapping(value = "MonitorDataCompareReport",method = RequestMethod.GET)
    @ApiOperation(value = "统计分析——对比,按集合顺序取，能耗——水质——报警")
    public AjaxMessage<List<List<MonitorDataChartReportDeviceDto>>> MonitorDataCompareReport(
            @ApiParam(value = "场景ids", required = true) @RequestParam List<Long> sceneIds,
            @ApiParam(value = "日期，格式：2020-12-12") @RequestParam(name = "reportDate",required = false) String reportDate,
            @ApiParam(value = "类型,1年2月3日4自定义", required = true) @RequestParam Integer reportType,
            @ApiParam(value = "场景类型：1水源 2水厂 3泵站 0其他",defaultValue = "0") @RequestParam(required = false ,defaultValue = "0") Integer sceneType,
            @ApiParam(value = "开始时间") @RequestParam(name = "startTime",required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(name = "endTime",required = false) String endTime
    ){
        LocalDate localDate = getLocalDate(reportDate, reportType);
        List<List<MonitorDataChartReportDeviceDto>> reportList=new ArrayList<>();
        if(sceneIds != null && sceneIds.size() > 0){

            //能耗（供水量3、耗电量5 ）（4取水量、5耗电量）
            List<MonitorDataChartReportDeviceDto> energy = monitorDataReportService.monitorDataEnergyReport(reportType,sceneIds,localDate.getYear(),localDate.getMonthValue(),localDate.getDayOfMonth(),sceneType,startTime, endTime);
            //水质（PH8、余氯11、浊度10）
            List<MonitorDataChartReportDeviceDto> quality = monitorDataReportService.monitorDataQualityReport(reportType,sceneIds,localDate.getYear(),localDate.getMonthValue(),localDate.getDayOfMonth(),startTime, endTime);
            // 水电耗
            List<MonitorDataChartReportDeviceDto> consumption = monitorDataReportService.monitorWaterPowerConsumptionReport(reportType,sceneIds,localDate.getYear(),localDate.getMonthValue(),localDate.getDayOfMonth(),sceneType, startTime, endTime);
            //报警（报警次数）
            List<MonitorDataChartReportDeviceDto> alarm = monitorDataReportService.deviceAlarmReport(reportType,sceneIds,localDate.getYear(),localDate.getMonthValue(),localDate.getDayOfMonth(), startTime,  endTime);

            Map<String,MonitorDataChartReportDeviceDto> map = consumption.stream().collect(Collectors.toMap(MonitorDataChartReportDeviceDto::getDeviceName, a -> a,(k1, k2)->k1));
            for (MonitorDataChartReportDeviceDto energyDto: energy) {
                MonitorDataChartReportDeviceDto reportDeviceDto =  map.get(energyDto.getDeviceName());
                energyDto.getAttributeData().addAll(reportDeviceDto.getAttributeData());
            }
            reportList.add(energy);
            reportList.add(quality);
            reportList.add(alarm);
        }
        return new AjaxMessage<>(ResultStatus.OK, reportList);
    }

    @RequestMapping(value = "getLocalDate",method = RequestMethod.GET)
    @ApiOperation(value = "补充完整时间")
    private LocalDate getLocalDate(@RequestParam @ApiParam(value = "日期，格式：2020-12-12", required = true) String reportDate,
                                   @RequestParam @ApiParam(value = "类型,1年2月3日", required = true) Integer reportType) {
        LocalDate localDate = LocalDate.now();
        if (reportType != null && reportType == 1) {
            reportDate = reportDate + "-01-01";
        } else if (reportType != null && reportType == 2) {
            reportDate = reportDate + "-01";
        }
        if (null != reportDate) {
            localDate = LocalDate.parse(reportDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        return localDate;
    }


    @RequestMapping(value = "getDeviceBySceneTypeName",method = RequestMethod.GET)
    @ApiOperation(value = "按场景类型查询设备（压力、流量、水质等）")
    public AjaxMessage<List<DeviceEntity>> getDeviceBySceneTypeName(
            @ApiParam(value = "场景类型", required = true) @RequestParam String sceneTypeName
    ){

        return new AjaxMessage<>(ResultStatus.OK, monitorDataReportService.getDeviceBySceneTypeName(sceneTypeName));
    }


    @RequestMapping(value = "MonitorDataChartReportByDevice",method = RequestMethod.GET)
    @ApiOperation(value = "统计分析——管网图表")
    public AjaxMessage<List<MonitorDataChartReportDeviceDto>> MonitorDataChartReportByDevice(
            @ApiParam(value = "设备id", required = true) @RequestParam List<Long> deviceIds,
            @ApiParam(value = "时间") @RequestParam(name = "reportDate",required = false) String reportDate,
            @ApiParam(value = "日期类型,1年报2月报3日报4自定义", required = true) @RequestParam Integer reportType,
            @ApiParam(value = "统计类型,1压力2流量3水质", required = true) @RequestParam Integer searchType,
            @ApiParam(value = "开始时间") @RequestParam(name = "startTime",required = false) String startTime,
            @ApiParam(value = "结束时间") @RequestParam(name = "endTime",required = false) String endTime
    ){
        LocalDate localDate = getLocalDate(reportDate, reportType);
        if(deviceIds != null && deviceIds.size()>0){
            return new AjaxMessage<>(ResultStatus.OK,  monitorDataReportService.monitorDataDeviceReport(reportType,deviceIds,searchType,
                    localDate.getYear(),localDate.getMonthValue(),localDate.getDayOfMonth(),startTime,endTime));
        }
        return new AjaxMessage<>(ResultStatus.OK);
    }



    @RequestMapping(value = "MonitorDataGeneralView",method = RequestMethod.GET)
    @ApiOperation(value = "统计分析——管网图表")
    public AjaxMessage<GeneralViewResults> MonitorDataGeneralView(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.monitorDataGeneralView());
    }



    @RequestMapping(value = "getDeviceWaterSupplie",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——近15天水量")
    public AjaxMessage<List<DeviceWaterSupply>> getDeviceWaterSupplie(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.deviceWaterSupplie());
    }

    @RequestMapping(value = "deviceWaterAmount",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——供水详情数据")
    public AjaxMessage<DeviceWaterAmount> deviceWaterAmount(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.deviceWaterAmount());
    }

    @RequestMapping(value = "sceneAlarms",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——供水实时报警信息")
    public AjaxMessage<List<SceneAlarm>> sceneAlarms(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.sceneAlarms());
    }

    @RequestMapping(value = "sceneNormalRates",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——管网水质情况")
    public AjaxMessage<List<SceneNormalRate>> sceneNormalRates(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.SceneNormalRates());
    }

    @RequestMapping(value = "sceneWaterPressure",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——压力分布区间")
    public AjaxMessage<SceneWaterPressure> sceneWaterPressure(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.sceneWaterPressure("水厂"));
    }

    @RequestMapping(value = "sceneWaterAmounts",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——水量排行")
    public AjaxMessage<List<SceneWaterAmount>> sceneWaterAmounts(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.sceneWaterAmounts());
    }

    @RequestMapping(value = "waterSupplyCharts",method = RequestMethod.GET)
    @ApiOperation(value = "概览首页——对比曲线图")
    public AjaxMessage<List<WaterSupplyChart>> waterSupplyCharts(){
        return new AjaxMessage<>(ResultStatus.OK,monitorDataReportService.waterSupplyCharts("水厂"));
    }

}
