package com.zcxk.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.dto.*;
import com.zcxk.enums.SceneEnum;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.ReportDimensionSettingService;
import com.zcxk.service.ReportService;
import com.zcxk.util.ExcelUtil;
import com.zcxk.util.ReportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 标签信息页面控制器
 * @author WYY
 * @date 2020-12-03 16:50
 */
@RestController
@RequestMapping("/report")
@Api(tags = "日月年统计报表信息")
public class DayMonthYearReportController {

    @Value("${SMS.sys_excel_path}")
    private String baseDir;

    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportDimensionSettingService reportDimensionSettingService;

    @ApiOperation(value = "按年月日统计报表")
    @RequestMapping(value = "/getReportBySceneIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getReportBySceneIds(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @ApiParam(value = "场景IDS", required = true) @RequestParam(required = true) Long[] ids,
            @ApiParam(value = "查询年", required = false) @RequestParam(required = false) Integer year ,
            @ApiParam(value = "查询月", required = false) @RequestParam(required = false) Integer month ,
            @ApiParam(value = "查询日", required = false) @RequestParam(required = false) Integer day ,
            @ApiParam(value = "报表类型(0日 1月 2年)", required = true) @RequestParam(required = true) Integer reportType
    ) {
        IPage<ReportDto> iPage = new Page<>(pageNum, pageSize);
        iPage = reportService.getReportBySceneIds(iPage,ids,year,month,day,reportType);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }
    @ApiOperation(value = "按设备统计压力及流量年月日统计报表")
    @RequestMapping(value = "/getDevicePressReportByDeviceIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<DevicePressReportAttributeDto>> getDevicePressReportByDeviceIds(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @ApiParam(value = "设备IDS", required = true) @RequestParam(required = true) Long[] ids,
            @ApiParam(value = "查询年", required = false) @RequestParam(required = false) Integer year ,
            @ApiParam(value = "查询月", required = false) @RequestParam(required = false) Integer month ,
            @ApiParam(value = "查询日", required = false) @RequestParam(required = false) Integer day ,
            @ApiParam(value = "类型(1压力 2流量)", required = false) @RequestParam(required = false) Integer type ,
            @ApiParam(value = "报表类型(0日 1月 2年)", required = true) @RequestParam(required = true) Integer reportType
    ) {
        IPage<DevicePressReportAttributeDto> iPage = new Page<>(pageNum, pageSize);
        iPage = reportService.getDevicePressReportByDeviceIds(iPage,ids,year,month,day,reportType,type);
        Pagination<DevicePressReportAttributeDto> pages = new Pagination<>(iPage);
        for(DevicePressReportAttributeDto item : pages.getList())
        {
            item.setAvgValue((double)Math.round(item.getAvgValue()==null?0:item.getAvgValue()*1000)/1000);
            item.setMinValue((double)Math.round(item.getMinValue()==null?0:item.getMinValue()*1000)/1000);
            item.setMaxValue((double)Math.round(item.getMaxValue()==null?0:item.getMaxValue()*1000)/1000);
            item.setSumValue((double)Math.round(item.getSumValue()==null?0:item.getSumValue()*1000)/1000);
            item.setLatestValue((double)Math.round(item.getLatestValue()==null?0:item.getLatestValue()*1000)/1000);
        }

        return new AjaxMessage<>(ResultStatus.OK,pages);
    }
    @ApiOperation(value = "按设备统计水质年月日统计报表")
    @RequestMapping(value = "/getDevicWaterReportByDeviceIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<DeviceWaterReportAttributeDto>> getDevicWaterReportByDeviceIds(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @ApiParam(value = "设备IDS", required = true) @RequestParam(required = true) Long[] ids,
            @ApiParam(value = "查询年", required = false) @RequestParam(required = false) Integer year ,
            @ApiParam(value = "查询月", required = false) @RequestParam(required = false) Integer month ,
            @ApiParam(value = "查询日", required = false) @RequestParam(required = false) Integer day ,
            @ApiParam(value = "报表类型(0日 1月 2年)", required = true) @RequestParam(required = true) Integer reportType
    ) {
        IPage<DeviceWaterReportAttributeDto> iPage = new Page<>(pageNum, pageSize);
        iPage = reportService.getDevicWaterReportByDeviceIds(iPage,ids,year,month,day,reportType);
        Pagination<DeviceWaterReportAttributeDto> pages = new Pagination<>(iPage);
        for(DeviceWaterReportAttributeDto item : pages.getList()) {

            item.setAvgCLValue((double)Math.round(item.getAvgCLValue()==null?0:item.getAvgCLValue()*1000)/1000);
            item.setAvgPHValue((double)Math.round(item.getAvgPHValue()==null?0:item.getAvgPHValue()*1000)/1000);
            item.setAvgQuValue((double)Math.round(item.getAvgQuValue()==null?0:item.getAvgQuValue()*1000)/1000);

            item.setMinCLValue((double)Math.round(item.getMinCLValue()==null?0:item.getMinCLValue()*1000)/1000);
            item.setMinPHValue((double)Math.round(item.getMinPHValue()==null?0:item.getMinPHValue()*1000)/1000);
            item.setMinQuValue((double)Math.round(item.getMinQuValue()==null?0:item.getMinQuValue()*1000)/1000);

            item.setMaxCLValue((double)Math.round(item.getMaxCLValue()==null?0:item.getMaxCLValue()*1000)/1000);
            item.setMaxPHValue((double)Math.round(item.getMaxPHValue()==null?0:item.getMaxPHValue()*1000)/1000);
            item.setMaxQuValue((double)Math.round(item.getMaxQuValue()==null?0:item.getMaxQuValue()*1000)/1000);

            item.setSumCLValue((double)Math.round(item.getSumCLValue()==null?0:item.getSumCLValue()*1000)/1000);
            item.setSumPHValue((double)Math.round(item.getSumPHValue()==null?0:item.getSumPHValue()*1000)/1000);
            item.setSumQuValue((double)Math.round(item.getSumQuValue()==null?0:item.getSumQuValue()*1000)/1000);

            item.setLatestCLValue((double)Math.round(item.getLatestCLValue()==null?0:item.getLatestCLValue()*1000)/1000);
            item.setLatestPHValue((double)Math.round(item.getLatestPHValue()==null?0:item.getLatestPHValue()*1000)/1000);
            item.setLatestQuValue((double)Math.round(item.getLatestQuValue()==null?0:item.getLatestQuValue()*1000)/1000);
        }
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }


    @ApiOperation(value = "【统计分析】--按年月日统计报表")
    @RequestMapping(value = "/getReportBySceneIdsNews", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getReportBySceneIdsNews(@RequestBody ReportQueryDto queryDto) {
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())){
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        IPage<ReportDto> iPage = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        iPage = reportService.getReportBySceneIdsNew(iPage, queryDto);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }


    @ApiOperation(value = "【历史数据】--按日期获取历史数据统计报表")
    @RequestMapping(value = "/getHistoryDataReportBySceneIdsNews", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getHistoryDataReportBySceneIdsNews(@RequestBody ReportQueryDto queryDto) {
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())){
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        IPage<ReportDto> iPage = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        iPage = reportService.getHistoryDataReportBySceneIdsNews(iPage, queryDto);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }

    @ApiOperation(value = "【统计分析】--按设备统计压力及流量年月日统计报表")
    @RequestMapping(value = "/getDevicePressReportByDeviceIdsNew", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getDevicePressReportByDeviceIdsNew(@RequestBody ReportQueryDto queryDto) {
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())){
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        IPage<ReportDto> iPage = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        iPage = reportService.getPipeDevicePressReportByDeviceIds(iPage, queryDto);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }

    @ApiOperation(value = "【历史数据】--按设备统计压力及流量年月日统计报表")
    @RequestMapping(value = "/getHistoryDataDevicePressReportByDeviceIdsNew", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getHistoryDataDevicePressReportByDeviceIdsNew(@RequestBody ReportQueryDto queryDto) {
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())){
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        IPage<ReportDto> iPage = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        iPage = reportService.getPipeHistoryDataDevicePressReportByDeviceIds(iPage, queryDto);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }

    @ApiOperation(value = "【统计分析】--按设备统计水质年月日统计报表")
    @RequestMapping(value = "/getDevicWaterReportByDeviceIdsNew", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getDevicWaterReportByDeviceIdsNew(@RequestBody ReportQueryDto queryDto) {
        queryDto.setType(3);
        IPage<ReportDto> iPage = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        iPage = reportService.getPipeDevicePressReportByDeviceIds(iPage,queryDto);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }

    @ApiOperation(value = "【历史数据】--按设备统计水质年月日统计报表")
    @RequestMapping(value = "/getHistoryDataDevicWaterReportByDeviceIdsNew", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<ReportDto>> getHistoryDataDevicWaterReportByDeviceIdsNew(@RequestBody ReportQueryDto queryDto) {
        queryDto.setType(3);
        IPage<ReportDto> iPage = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        iPage = reportService.getPipeHistoryDataDevicePressReportByDeviceIds(iPage,queryDto);
        Pagination<ReportDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,pages);
    }

    @ApiOperation(value = "【统计分析】--获取自定义报表统计维度")
    @RequestMapping(value = "/getCustomDimension", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<List<ReportAttributeDto>> getCustomDimension(
            @ApiParam(value = "场景IDS", required = true) @RequestParam(required = true) Long[] ids
    ) {
        return new AjaxMessage<>(ResultStatus.OK, reportService.findAttributeNameList(ids, null,0));
    }

    @ApiOperation(value = "【历史数据】--获取历史数据自定义报表统计维度")
    @RequestMapping(value = "/getHistoryDataCustomDimension", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<List<ReportAttributeDto>> getHistoryDataCustomDimension(
            @ApiParam(value = "场景IDS", required = true) @RequestParam(required = true) Long[] ids
    ) {
        return new AjaxMessage<>(ResultStatus.OK, reportService.findAttributeNameList(ids, null, 1));
    }

    @ApiOperation(value = "【统计分析】--获取管网自定义报表统计维度")
    @RequestMapping(value = "/getPipelineCustomDimension", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<List<ReportAttributeDto>> getPipelineCustomDimension(
            @ApiParam(value = "设备ID", required = true) @RequestParam(required = true) Long deviceId,
            @ApiParam(value = "类型：1压力，2流量，3水质", required = true) @RequestParam(required = true) Integer type) {
        return new AjaxMessage<>(ResultStatus.OK, reportService.findPipeAttributeNameList(deviceId, type,0));
    }

    @ApiOperation(value = "【历史数据】--获取管网自定义报表统计维度")
    @RequestMapping(value = "/getHistoryDataPipelineCustomDimension", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<List<ReportAttributeDto>> getHistoryDataPipelineCustomDimension(
            @ApiParam(value = "设备ID", required = true) @RequestParam(required = true) Long deviceId,
            @ApiParam(value = "类型：1压力，2流量，3水质", required = true) @RequestParam(required = true) Integer type) {
        return new AjaxMessage<>(ResultStatus.OK, reportService.findPipeAttributeNameList(deviceId, type,1));
    }

    @ApiOperation(value = "【统计分析/历史数据】--修改自定义报表统计维度")
    @RequestMapping(value = "/updateCustomDimension", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<List<ReportAttributeDto>> updateCustomDimension(@RequestBody @Valid List<ReportStatisticalDimensionDto> dataList) {
        reportDimensionSettingService.batchUpdateStatisticalDimension(dataList);
        return new AjaxMessage<>(ResultStatus.OK);
    }

    @RequestMapping(value = "/export/excel", method = RequestMethod.POST)
    @ApiOperation(value = "【统计分析】--报表导出")
    public AjaxMessage<String> exportExcel(@RequestBody  @Valid ReportQueryDto queryDto) {
        IPage<ReportDto> iPage = new Page<>(1, 1000000);
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())){
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        if (queryDto.getType() == null ){
            iPage = reportService.getReportBySceneIdsNew(iPage, queryDto);
        } else  {
            iPage = reportService.getPipeDevicePressReportByDeviceIds(iPage, queryDto);
        }
        List<ReportDto> list = iPage.getRecords();
        if (CollectionUtil.isEmpty(list)){
            return new AjaxMessage<>(ResultStatus.OK);
        }
        List<List<String>> reportTitle = ReportExcelUtil.toExcelTitleList(list.get(0).getReportTitle(), queryDto.getSceneType(),0);
        String filePath = ExcelUtil.writeSimpleExcelWithHeader(baseDir, SceneEnum.getMsg(queryDto.getSceneType()) +"报表", reportTitle, ReportExcelUtil.toReportExcelDto(list,queryDto.getSceneType()));
        return new AjaxMessage<>(ResultStatus.OK, filePath);
    }

    @RequestMapping(value = "/export/historyDataExcel", method = RequestMethod.POST)
    @ApiOperation(value = "【历史数据】--报表导出")
    public AjaxMessage<String> historyDataExcel(@RequestBody  @Valid ReportQueryDto queryDto) {
        IPage<ReportDto> iPage = new Page<>(1, 1000000);
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())) {
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        if (queryDto.getType() == null) {
            iPage = reportService.getHistoryDataReportBySceneIdsNews(iPage, queryDto);
        } else {
            iPage = reportService.getPipeHistoryDataDevicePressReportByDeviceIds(iPage, queryDto);
        }
        List<ReportDto> list = iPage.getRecords();
        if (CollectionUtil.isEmpty(list)) {
            return new AjaxMessage<>(ResultStatus.OK);
        }
        List<List<String>> reportTitle = ReportExcelUtil.toHistoryDataExcelTitleList(list.get(0).getReportTitle(), queryDto.getSceneType(),1);
        String filePath = ExcelUtil.writeSimpleExcelWithHeader(baseDir, SceneEnum.getMsg(queryDto.getSceneType()) + "报表", reportTitle, ReportExcelUtil.toHisoryDataReportExcelDto(list, queryDto.getSceneType()));
        return new AjaxMessage<>(ResultStatus.OK, filePath);
    }

}
