package com.zcxk.charge.web.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.charge.api.dto.*;
import com.zcxk.charge.api.dto.query.ReportQueryPageDto;
import com.zcxk.charge.api.enums.ChargeErrorEnum;
import com.zcxk.charge.api.vo.DevicePressReportAttributeVo;
import com.zcxk.charge.api.vo.DeviceWaterReportAttributeVo;
import com.zcxk.charge.api.vo.ReportVo;
import com.zcxk.charge.web.common.Pagination;
import com.zcxk.charge.web.enums.SceneEnum;
import com.zcxk.charge.web.service.ReportDimensionSettingService;
import com.zcxk.charge.web.service.ReportService;
import com.zcxk.charge.web.util.ExcelUtil;
import com.zcxk.charge.web.util.ReportExcelUtil;
import com.zcxk.core.common.enums.RespCodeEnum;
import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.utils.export.EasyExcelUtil;
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.servlet.http.HttpServletResponse;
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 = "/getReportBySceneIdsNews", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage 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());
        return AjaxMessage.success(new Pagination<>(reportService.getReportBySceneIdsNew(iPage, queryDto)));
    }

    @ApiOperation(value = "【统计分析】--按设备统计压力及流量年月日统计报表")
    @RequestMapping(value = "/getDevicePressReportByDeviceIdsNew", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage 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());
        Pagination<ReportVo> pages = new Pagination<>(reportService.getPipeDevicePressReportByDeviceIds(iPage, queryDto));
        return AjaxMessage.success(pages);
    }

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

    @ApiOperation(value = "【历史数据】--按日期获取历史数据统计报表")
    @RequestMapping(value = "/getHistoryDataReportBySceneIdsNews", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage 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());
        Pagination<ReportVo> pages = new Pagination<>(reportService.getHistoryDataReportBySceneIdsNews(iPage, queryDto));
        return AjaxMessage.success(pages);
    }

    @ApiOperation(value = "【历史数据】--按设备统计压力及流量年月日统计报表")
    @RequestMapping(value = "/getHistoryDataDevicePressReportByDeviceIdsNew", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage 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());
        Pagination<ReportVo> pages = new Pagination<>(reportService.getPipeHistoryDataDevicePressReportByDeviceIds(iPage, queryDto));
        return AjaxMessage.success(pages);
    }

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

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

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

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

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

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

    @RequestMapping(value = "/export/excel", method = RequestMethod.GET)
    @ApiOperation(value = "【统计分析】--报表导出")
    public void exportExcel(ReportQueryDto queryDto, HttpServletResponse response) throws Exception {
        IPage<ReportVo> iPage;
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())){
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        if (queryDto.getType() == null ){
            iPage = reportService.getReportBySceneIdsNew(new Page<>(1, 1000000), queryDto);
        } else  {
            iPage = reportService.getPipeDevicePressReportByDeviceIds(new Page<>(1, 1000000), queryDto);
        }
        List<ReportVo> list = iPage.getRecords();
        if (CollectionUtil.isEmpty(list)){
            throw BusinessException.builder(ChargeErrorEnum.OBJECT_ERROR);
        }
        List<List<String>> reportTitle = ReportExcelUtil.toExcelTitleList(list.get(0).getReportTitle(), queryDto.getSceneType(),0);
        EasyExcelUtil.excelWriteWithHeader(response, SceneEnum.getMsg(queryDto.getSceneType()) +"报表", reportTitle, ReportExcelUtil.toReportExcelDto(list,queryDto.getSceneType()));
    }

    @RequestMapping(value = "/export/historyDataExcel", method = RequestMethod.GET)
    @ApiOperation(value = "【历史数据】--报表导出")
    public void historyDataExcel(ReportQueryDto queryDto, HttpServletResponse response) throws Exception {
        IPage<ReportVo> iPage;
        if (StringUtils.isEmpty(queryDto.getStartTime()) || StringUtils.isEmpty(queryDto.getEndTime())) {
            queryDto.setStartTime(null);
            queryDto.setEndTime(null);
        }
        if (queryDto.getType() == null) {
            iPage = reportService.getHistoryDataReportBySceneIdsNews(new Page<>(1, 1000000), queryDto);
        } else {
            iPage = reportService.getPipeHistoryDataDevicePressReportByDeviceIds(new Page<>(1, 1000000), queryDto);
        }
        List<ReportVo> list = iPage.getRecords();
        if (CollectionUtil.isEmpty(list)) {
            throw BusinessException.builder(ChargeErrorEnum.OBJECT_ERROR);
        }
        List<List<String>> reportTitle = ReportExcelUtil.toHistoryDataExcelTitleList(list.get(0).getReportTitle(), queryDto.getSceneType(),1);
        EasyExcelUtil.excelWriteWithHeader(response, SceneEnum.getMsg(queryDto.getSceneType()) +"报表", reportTitle, ReportExcelUtil.toHisoryDataReportExcelDto(list, queryDto.getSceneType()));
    }

}
