package com.njcn.event.controller.majornetwork;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.vo.LineDetailDataVO;
import com.njcn.event.common.pojo.dto.LineDetailDataCommDTO;
import com.njcn.event.common.service.CommMonitorEventReportService;
import com.njcn.event.enums.EventResponseEnum;
import com.njcn.event.pojo.param.*;
import com.njcn.event.pojo.vo.DetailVO;
import com.njcn.event.pojo.vo.GeneralVO;
import com.njcn.event.pojo.vo.ReasonsVO;
import com.njcn.event.pojo.vo.WaveTypeVO;
import com.njcn.event.service.majornetwork.ReportService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.web.controller.BaseController;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author xxy
 * @version 1.0.0
 * @date 2022年07月28日 11:36
 */
@Validated
@Slf4j
@Api(tags = "暂态报告")
@RestController
@RequestMapping("/report")
@RequiredArgsConstructor
public class ReportController extends BaseController {

    private final ReportService reportService;

    private final CommMonitorEventReportService commMonitorEventReportService;

    private final LineFeignClient lineFeignClient;

    private final MonitorClient monitorClient;

    private final DicDataFeignClient dicDataFeignClient;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGeneralSituation")
    @ApiOperation("暂态总体概况(区域)")
    @ApiImplicitParam(name = "businessParam", value = "暂态总体概况区域参数", required = true)
    public HttpResult<List<GeneralVO>> getGeneralSituation(@RequestBody @Validated DeviceInfoParam.BusinessParam businessParam) {
        String methodDescribe = getMethodDescribe("getGeneralSituation");
        List<GeneralVO> page = reportService.getGeneralSituation(businessParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getVoltage")
    @ApiOperation("暂态总体概况(电压)")
    @ApiImplicitParam(name = "businessParam", value = "暂态总体概况电压参数", required = true)
    public HttpResult<List<GeneralVO>> getVoltage(@RequestBody @Validated DeviceInfoParam.BusinessParam businessParam) {
        String methodDescribe = getMethodDescribe("getVoltage");
        List<GeneralVO> page = reportService.getVoltage(businessParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSeverity")
    @ApiOperation("暂态严重度")
    @ApiImplicitParam(name = "businessParam", value = "暂态严重度参数", required = true)
    public HttpResult<List<WaveTypeVO>> getSeverity(@RequestBody @Validated DeviceInfoParam.BusinessParam businessParam) {
        String methodDescribe = getMethodDescribe("getSeverity");
        List<WaveTypeVO> page = reportService.getSeverity(businessParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getReason")
    @ApiOperation("暂态原因统计表")
    @ApiImplicitParam(name = "businessParam", value = "暂态原因统计表参数", required = true)
    public HttpResult<List<ReasonsVO>> getReason(@RequestBody @Validated DeviceInfoParam.BusinessParam businessParam) {
        String methodDescribe = getMethodDescribe("getSeverity");
        List<ReasonsVO> page = reportService.getReason(businessParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getContinueTime")
    @ApiOperation("详细事件列表")
    @ApiImplicitParam(name = "businessParam", value = "详细时间列表参数", required = true)
    public HttpResult<Page<DetailVO>> getContinueTime(@RequestBody @Validated WaveTypeParam businessParam) {
        String methodDescribe = getMethodDescribe("getContinueTime");
        Page<DetailVO> page = reportService.getContinueTime(businessParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getSagTimes")
    @ApiOperation("暂降次数统计")
    @ApiImplicitParam(name = "waveTypeParam", value = "暂降次数统计参数", required = true)
    public HttpResult<Page<DetailVO>> getSagTimes(@RequestBody @Validated WaveTypeParam waveTypeParam) {
        String methodDescribe = getMethodDescribe("getSagTimes");
        Page<DetailVO> page = reportService.getSagTimes(waveTypeParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getUpTimes")
    @ApiOperation("暂升次数统计")
    @ApiImplicitParam(name = "waveTypeParam", value = "暂升次数统计参数", required = true)
    public HttpResult<Page<DetailVO>> getUpTimes(@RequestBody @Validated WaveTypeParam waveTypeParam) {
        String methodDescribe = getMethodDescribe("getUpTimes");
        Page<DetailVO> page = reportService.getUpTimes(waveTypeParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getBreakTimes")
    @ApiOperation("中断次数统计")
    @ApiImplicitParam(name = "waveTypeParam", value = "中断次数统计参数", required = true)
    public HttpResult<Page<DetailVO>> getBreakTimes(@RequestBody @Validated WaveTypeParam waveTypeParam) {
        String methodDescribe = getMethodDescribe("getBreakTimes");
        Page<DetailVO> page = reportService.getBreakTimes(waveTypeParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getExport")
    @ApiOperation("周报导出")
    @ApiImplicitParam(name = "businessParam", value = "周报导出参数", required = true)
    public void getExport(@RequestBody @Validated DeviceInfoParam.BusinessParam businessParam, HttpServletResponse response) throws TemplateException, IOException {
        reportService.getExport(businessParam, response);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/appEventExport")
    @ApiOperation("灿能云暂态报告导出")
    @ApiImplicitParam(name = "businessParam", value = "灿能云暂态导出参数", required = true)
    public HttpResult<String> appEventExport(@RequestBody @Validated DeviceInfoParam.BusinessParam businessParam) throws TemplateException, IOException {
        String methodDescribe = getMethodDescribe("appEventExport");
        String path = reportService.appEventExport(businessParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, path, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLineExport")
    @ApiOperation("监测点报告导出")
    @ApiImplicitParam(name = "exportParam", value = "监测点报告导出参数", required = true)
    public void getLineExport(@RequestBody @Validated ExportParam exportParam, HttpServletResponse response) throws IOException, InvalidFormatException, TemplateException, ParseException {

        LineDetailDataCommDTO lineDetailDataCommDTO = new LineDetailDataCommDTO();
        if(exportParam.getType() == 0) {
            LineDetailDataVO lineDetailData = lineFeignClient.getLineDetailData(exportParam.getLineId()).getData();
            if (ObjectUtil.isNull(lineDetailData)) {
                throw new BusinessException(CommonResponseEnum.FAIL, "查询监测点失败");
            }
            BeanUtil.copyProperties(lineDetailData,lineDetailDataCommDTO);
        }else {
            HttpResult<List<Monitor>> monitorList = monitorClient.getMonitorList(Arrays.asList(exportParam.getLineId()));
            if (ObjectUtil.isNull(monitorList)) {
                throw new BusinessException(EventResponseEnum.NOT_FOUND);
            }
            lineDetailDataCommDTO.setLineName(monitorList.getData().get(0).getName());
            lineDetailDataCommDTO.setScale(dicDataFeignClient.getDicDataById(monitorList.getData().get(0).getVoltageLevel()).getData().getName());
            lineDetailDataCommDTO.setPt(monitorList.getData().get(0).getPt1() + "/" + monitorList.getData().get(0).getPt2());
            lineDetailDataCommDTO.setPt(monitorList.getData().get(0).getCt1() + "/" + monitorList.getData().get(0).getCt2());
            lineDetailDataCommDTO.setDealCapacity(monitorList.getData().get(0).getUserAgreementCapacity());
//                insertRow(doc, table, centerParagraph, false, "基准容量", monitorList.getData().get(0).getMinShortCircuitCapacity() + "");
            lineDetailDataCommDTO.setDevCapacity(monitorList.getData().get(0).getPowerSupplyEqCapacity());
            lineDetailDataCommDTO.setShortCapacity(monitorList.getData().get(0).getMinShortCircuitCapacity());
            lineDetailDataCommDTO.setPtType(dicDataFeignClient.getDicDataById(monitorList.getData().get(0).getTerminalWiringMethod()).getData().getName());
        }

        commMonitorEventReportService.getLineExport(exportParam, lineDetailDataCommDTO,response);
    }

    /**
     * 获取事件报告
     *
     * @author zbj
     * @date 2022/7/29
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventReport")
    @ApiOperation("获取事件报告")
    @ApiImplicitParam(name = "deviceInfoParam", value = "参数体", required = true)
    public HttpResult<Page<WaveTypeVO>> getEventReport(@RequestBody @Validated WaveTypeParam deviceInfoParam) {
        String methodDescribe = getMethodDescribe("getEventReport");
        Page<WaveTypeVO> res = reportService.getEventReport(deviceInfoParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
    }

    /**
     * 获取区域报告
     *
     * @author zbj
     * @date 2022/8/4
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAreaReport")
    @ApiOperation("获取区域报告")
    @ApiImplicitParam(name = "areaReportParam", value = "参数体", required = true)
    public void getAreaReport(@RequestBody @Validated AreaReportParam areaReportParam, HttpServletResponse response) throws Exception {
        reportService.getAreaReport(areaReportParam, response);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPmsAreaReport")
    @ApiOperation("获取Pms区域报告")
    @ApiImplicitParam(name = "areaReportParam", value = "参数体", required = true)
    public void getPmsAreaReport(@RequestBody @Validated AreaReportParam areaReportParam, HttpServletResponse response) throws Exception {
        reportService.getPmsAreaReport(areaReportParam, response);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/createEventReport")
    @ApiOperation("暂态事件报告导出")
    public void createEventReport(@RequestBody @Validated List<String> index, HttpServletResponse response) throws IOException, InvalidFormatException, ExecutionException, InterruptedException {
        commMonitorEventReportService.createEventReport(index,response);
    }
}
