package com.njcn.harmonic.controller.report;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.device.pq.api.DeviceUnitClient;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.pq.pojo.vo.LineDetailDataVO;
import com.njcn.event.enums.EventResponseEnum;
import com.njcn.harmonic.common.pojo.dto.DeviceUnitCommDTO;
import com.njcn.harmonic.common.pojo.dto.HarmLineDetailDataCommDTO;
import com.njcn.harmonic.common.pojo.dto.OverLimitInfoCommDTO;
import com.njcn.harmonic.common.service.MonitorHarmonicReportService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;


/**
 * @author wr
 */
@Slf4j
@RestController
@RequestMapping("/exportmodel")
@Api(tags = "word报告")
@AllArgsConstructor
public class ExportModelController extends BaseController {

   private final MonitorHarmonicReportService monitorHarmonicReportService;

   private final LineFeignClient lineFeignClient;

   private final MonitorClient monitorClient;

   private final DeviceUnitClient deviceUnitClient;

   private final DicDataFeignClient dicDataFeignClient;

    /**
     * @param response
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param type         区分pq:0 pms:1
     * @param lineIndex    监测点id
     * @param name         监测点名称
     * @param reportNumber 客户编号
     * @param crmName      客户名称
     * @param file         接线图
     * @param isUrl        是否地址栏返回
     * @return
     * @throws IOException
     */
    @PostMapping("/exportModel")
    @ApiOperation("word报告")
    public void exportWorld(HttpServletResponse response,
                                          @RequestParam(value ="startTime",required = false) String startTime,
                                          @RequestParam(value ="endTime",required = false)  String endTime,
                                          @RequestParam(value ="type",required = false) Integer type,
                                          @RequestParam(value ="lineIndex",required = false)  String lineIndex,
                                          @RequestParam(value ="name",required = false)  String name,
                                          @RequestParam(value ="reportNumber",required = false) String reportNumber,
                                          @RequestParam(value ="crmName",required = false) String crmName,
                                          @RequestParam(value = "isUrl",required = false)  Boolean isUrl,
                                          @RequestPart(value ="file",required = false)   MultipartFile file) throws IOException {
        String methodDescribe = getMethodDescribe("exportWorld");

        HarmLineDetailDataCommDTO harmLineDetailDataCommDTO;
        OverLimitInfoCommDTO overLimitInfoCommDTO;
        DeviceUnitCommDTO deviceUnitCommDTO;

        LineDetailDataVO lineDto = new LineDetailDataVO();
        Overlimit overLimit = new Overlimit();
        PqsDeviceUnit deviceUnit = new PqsDeviceUnit();
        if(type == 1) {
            //河北pms
            List<Monitor> monitorList = monitorClient.getMonitorList(Collections.singletonList(lineIndex)).getData();
            if (ObjectUtil.isNull(monitorList)) {
                throw new BusinessException(EventResponseEnum.NOT_FOUND);
            }
            Monitor monitor = monitorList.get(0);
            lineDto.setBdName(monitor.getPowerrName());

            lineDto.setAreaName(monitor.getOrgName());

            lineDto.setPt(monitor.getPt1() + "/" + monitor.getPt2());
            lineDto.setCt(monitor.getCt1() + "/" + monitor.getCt2());
            //基准容量
            lineDto.setStandardCapacity(monitor.getStandShortCapacity());
            //短路容量
            lineDto.setShortCapacity(monitor.getMinShortCircuitCapacity());
            //协议容量
            lineDto.setDealCapacity(monitor.getUserAgreementCapacity());
            //设备容量
            lineDto.setDevCapacity(monitor.getPowerSupplyEqCapacity());
            //电压等级
            lineDto.setScale(dicDataFeignClient.getDicDataById(monitor.getVoltageLevel()).getData().getName());

            overLimit = monitorClient.getOverLimitData(monitor.getId()).getData();
        }else {
            //通用
            lineDto = lineFeignClient.getLineDetailData(lineIndex).getData();
            overLimit = lineFeignClient.getOverLimitData(lineIndex).getData();

        }
        //数据单位
        deviceUnit = deviceUnitClient.lineUnitDetail(lineIndex).getData();

        harmLineDetailDataCommDTO = BeanUtil.copyProperties(lineDto, HarmLineDetailDataCommDTO.class);
        overLimitInfoCommDTO = BeanUtil.copyProperties(overLimit, OverLimitInfoCommDTO.class);
        deviceUnitCommDTO = BeanUtil.copyProperties(deviceUnit, DeviceUnitCommDTO.class);



        monitorHarmonicReportService.exportWorld(response,startTime,endTime,type,lineIndex,name,reportNumber,crmName,isUrl,file, harmLineDetailDataCommDTO,overLimitInfoCommDTO,deviceUnitCommDTO,null);
    }
}
