package com.njcn.harmonic.controller;

import cn.hutool.core.date.DateUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.device.biz.pojo.dto.PollutionLineDTO;
import com.njcn.device.pq.pojo.dto.PollutionSubstationDTO;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.harmonic.pojo.excel.pollution.AreaPollution;
import com.njcn.harmonic.pojo.excel.pollution.LinePollution;
import com.njcn.harmonic.pojo.excel.pollution.SubstationPollution;
import com.njcn.harmonic.pojo.excel.pollution.UserLinePollution;
import com.njcn.harmonic.pojo.param.HarmonicPublicParam;
import com.njcn.harmonic.pojo.param.PollutionSubstationQuryParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.vo.PollutionSubstationVO;
import com.njcn.harmonic.pojo.vo.PollutionVO;
import com.njcn.harmonic.pojo.vo.SubstationVo;
import com.njcn.harmonic.service.PollutionSubstationService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author clam
 * @version V1.0.0
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/PollutionSubstation")
@Api(tags = "变电站污染统计")
@AllArgsConstructor
public class PollutionSubstationController extends BaseController {

    private final PollutionSubstationService pollutionSubstationService;


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPollutionSubstationData")
    @ApiOperation("按变电站及指标类型展示污染")
    @ApiImplicitParam(name = "pollutionSubstationQuryParam", value = "污染指标查询参数", required = true)
    public HttpResult<List<PollutionSubstationVO>> getPollutionSubstationData(@RequestBody @Validated PollutionSubstationQuryParam pollutionSubstationQuryParam) {
        String methodDescribe = getMethodDescribe ("getPollutionSubstationData");
        LogUtil.njcnDebug(log, "{}，实体参数：{}", methodDescribe, pollutionSubstationQuryParam);
        List<PollutionSubstationVO> list = pollutionSubstationService.getPollutionSubstationData(pollutionSubstationQuryParam);
        return HttpResultUtil.assembleCommonResponseResult (CommonResponseEnum.SUCCESS, list, methodDescribe);
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/deptSubstationRelations")
    @ApiOperation("污区图-部门变电站关系")
    @ApiImplicitParam(name = "param", value = "实体参数", required = true)
    public HttpResult<List<PollutionVO>> deptSubstationRelations(@RequestBody HarmonicPublicParam param) {
        String methodDescribe = getMethodDescribe("deptSubstationRelations");
        LogUtil.njcnDebug(log, "{}，实体参数：{}", methodDescribe, param);
        List<PollutionVO> list = pollutionSubstationService.getDeptSubstationRelations(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSubstationInfoById")
    @ApiOperation("污区图-根据部门获取变电站详情")
    @ApiImplicitParam(name = "param", value = "部门参数", required = true)
    public HttpResult<List<PollutionSubstationDTO>> getSubstationInfoById(@RequestBody HarmonicPublicParam param) {
        String methodDescribe = getMethodDescribe("getSubstationInfoById");
        LogUtil.njcnDebug(log, "{}，部门参数：{}", methodDescribe, param);
        List<PollutionSubstationDTO> list = pollutionSubstationService.getSubstationInfoById(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineInfoById")
    @ApiOperation("污区图-根据变电站获取监测点详情")
    @ApiImplicitParam(name = "param", value = "变电站参数", required = true)
    public HttpResult<List<PollutionLineDTO>> getLineInfoById(@RequestBody HarmonicPublicParam param) {
        String methodDescribe = getMethodDescribe("getLineInfoById");
        LogUtil.njcnDebug(log, "{}，变电站参数：{}", methodDescribe, param);
        List<PollutionLineDTO> list = pollutionSubstationService.getLineInfoById(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineRank")
    @ApiOperation("污区图-获取前十监测点排名")
    @ApiImplicitParam(name = "param", value = "实体参数", required = true)
    public HttpResult<List<PollutionLineDTO>> getLineRank(@RequestBody HarmonicPublicParam param) {
        String methodDescribe = getMethodDescribe("getLineRank");
        LogUtil.njcnDebug(log, "{}，实体参数：{}", methodDescribe, param);
        List<PollutionLineDTO> list = pollutionSubstationService.getLineRankTop10(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/substationInfo")
    @ApiOperation("变电站台账信息(包含稳态数据统计)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptIndex", value = "部门id"),
            @ApiImplicitParam(name = "searchValue", value = "搜索值"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")
    })
    public HttpResult<List<SubstationVo>> getSubstationInfo(@RequestParam(value = "deptIndex") String deptIndex,
                                                            @RequestParam(value = "searchValue",required=false) String searchValue,
                                                            @RequestParam(value = "startTime") String startTime,
                                                            @RequestParam(value = "endTime") String endTime
                                                            ) {
        String methodDescribe = getMethodDescribe("getSubstationInfo");
        LogUtil.njcnDebug(log, "{}，实体参数：{},{}", methodDescribe, deptIndex, searchValue);
        List<SubstationVo> list = pollutionSubstationService.getSubstationInfo(deptIndex,searchValue,startTime,endTime);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation(value ="监测点谐波污染值列表")
    @PostMapping(value ="/downPollutionLineCalc")
    public HttpResult<List<LinePollution>> downPollutionLineCalc(@RequestBody StatSubstationBizBaseParam param) {
        String methodDescribe = getMethodDescribe("downPollutionLineCalc");
        param.setStartTime(DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())).toString());
        param.setEndTime(DateUtil.endOfDay(DateUtil.parse(param.getEndTime())).toString());
        List<LinePollution> linePollutions = pollutionSubstationService.downPollutionLineCalc(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, linePollutions, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation(value ="导出用户谐波电压污染值",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping(value ="/downPollutionUserCalc",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downPollutionUserCalc(@RequestParam(value = "startTime") String startTime,
                                      @RequestParam(value = "endTime") String endTime) {
        StatSubstationBizBaseParam param=new StatSubstationBizBaseParam();
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        pollutionSubstationService.downPollutionUserCalc(param);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation(value ="变电站谐波污染值列表")
    @PostMapping(value ="/downPollutionSubCalc")
    public HttpResult<List<SubstationPollution>> downPollutionSubCalc(@RequestBody StatSubstationBizBaseParam param) {
        String methodDescribe = getMethodDescribe("downPollutionSubCalc");
        param.setStartTime(DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())).toString());
        param.setEndTime(DateUtil.endOfDay(DateUtil.parse(param.getEndTime())).toString());
        List<SubstationPollution> gdPollutions = pollutionSubstationService.downPollutionSubCalc(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, gdPollutions, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation(value = "监测点和敏感用户谐波污染值列表")
    @PostMapping(value = "/getSumList")
    public HttpResult<Map<String, UserLinePollution>> getSumList(@RequestBody OnlineRateParam.Info param) {
        String methodDescribe = getMethodDescribe("getSumList");
        param.setStartTime(DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())).toString());
        param.setEndTime(DateUtil.endOfDay(DateUtil.parse(param.getEndTime())).toString());
        List<UserLinePollution> sumList = pollutionSubstationService.getSumList(param);
        Map<String, UserLinePollution> sumMap = sumList.stream().collect(Collectors.toMap(UserLinePollution::getLineId, Function.identity()));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, sumMap, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation(value = "区域污染值统计")
    @PostMapping(value = "/getAreaPollution")
    public HttpResult<List<AreaPollution>> getAreaPollution(@RequestBody StatSubstationBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getAreaPollution");
        param.setStartTime(DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())).toString());
        param.setEndTime(DateUtil.endOfDay(DateUtil.parse(param.getEndTime())).toString());
        List<AreaPollution> areaPollution = pollutionSubstationService.getAreaPollution(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, areaPollution, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation(value ="敏感用户谐波污染值列表")
    @PostMapping(value ="/downPollutionSensitiveUser")
    public HttpResult<List<SubstationPollution>> downPollutionSensitiveUser(@RequestBody StatSubstationBizBaseParam param) {
        String methodDescribe = getMethodDescribe("downPollutionSensitiveUser");
        param.setStartTime(DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())).toString());
        param.setEndTime(DateUtil.endOfDay(DateUtil.parse(param.getEndTime())).toString());
        List<SubstationPollution> linePollutions = pollutionSubstationService.downPollutionSensitiveUser(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, linePollutions, methodDescribe);
    }


}
