package com.huatek.busi.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.List;

import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.cable.CableCirculationtemperIncreaseRateCacheMapper;
import com.huatek.busi.service.cable.CableCirculationtemperIncreaseRateCacheService;
import com.huatek.busi.service.cable.CableCirculationtemperIncreaseRateService;
import com.huatek.busi.vo.*;
import com.huatek.busi.vo.cable.BusiCableCirculationWarningVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.huatek.busi.common.BusiConstant.EqType;
import com.huatek.busi.common.BusiConstant.Phase;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.common.util.FieldUtil;
import com.huatek.busi.dao.BusiEqmWarningDao;
import com.huatek.busi.mapper.GisSf6IncreaseRateCacheMapper;
import com.huatek.busi.service.BusiCommonService;
import com.huatek.busi.service.BusiEqmWarningInfoService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiExternalEquipmentService;
import com.huatek.busi.service.BusiSensorService;
import com.huatek.busi.service.BusiSubPageService;
import com.huatek.busi.service.temperature.BusiTemperPointService;
import com.huatek.busi.service.temperature.TemperIncreaseRateService;
import com.huatek.busi.service.transformer.BusiOfflineTransformerBrandService;
import com.huatek.busi.service.transformer.TransformerEpaDataOriginService;
import com.huatek.busi.service.transformer.TransformerEpaDeviationService;
import com.huatek.busi.service.transformer.TransformerOfflineExperimentReportService;
import com.huatek.busi.vo.warning.BusiArresterVo;
import com.huatek.busi.vo.warning.BusiCapaVo;
import com.huatek.busi.vo.warning.BusiGisSf6Vo;
import com.huatek.busi.vo.warning.BusiIcgeVo;
import com.huatek.busi.vo.warning.BusiOiTmplVo;
import com.huatek.busi.vo.warning.BusiOilVo;
import com.huatek.busi.vo.warning.BusiPdVo;
import com.huatek.busi.vo.warning.BusiPhotoVo;
import com.huatek.busi.vo.warning.BusiRunStateVo;
import com.huatek.busi.vo.warning.BusiWcgVo;

@Component
@Service(interfaceClass = BusiSubPageService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiSubPageServiceImpl implements BusiSubPageService {
    @Autowired
    private BusiEqmWarningDao busiEqmWarningDao;

    @Autowired
    private BusiExternalEquipmentService busiExternalEquipmentService;
    @Autowired
    private BusiEqmWarningService busiEqmWarningService;
    @Autowired
    private BusiEqmWarningInfoService busiEqmWarningInfoService;
    @Autowired
    private BusiOfflineTransformerBrandService busiOfflineTransformerBrandService;
    @Autowired
    private BusiTemperPointService busiTemperPointService;
    @Autowired
    private BusiCommonService busiCommonService;
    @Autowired
    private TransformerEpaDataOriginService epaDataOriginService;
    @Autowired
    private BusiSensorService busiSensorService;
    @Autowired
    private GisSf6IncreaseRateCacheMapper gisSf6IncreaseRateCacheMapper;
    @Autowired
    private CableCirculationtemperIncreaseRateCacheMapper cableCirculationtemperIncreaseRateCacheMapper;
    @Autowired
    private TemperIncreaseRateService temperIncreaseRateService;
    @Autowired
    private CableCirculationtemperIncreaseRateService cableCirculationtemperIncreaseRateService;
    @Reference(check = false)
    private TransformerEpaDeviationService transformerEpaDeviationService;
    @Reference(check = false)
    private TransformerOfflineExperimentReportService transformerOfflineExperimentReportService;
    @Reference(check = false)
    private TransformerOilThresholdService transformerOilThresholdService;

    @Override
    public BusiIcgeVo getIcgeWarningInfo(String warningId, String sensorCode) {
        BusiIcgeVo vo = null;
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (null != warningInfo) {
            vo = JSON.parseObject(warningInfo.getWarningInfo(), BusiIcgeVo.class);
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    /**
     * 油色谱告警二级界面
     */
    @Override
    public BusiOilVo getOilWarnigInfo(String warningId, String sensorCode) {
        BusiOilVo vo = null;
        Map<String, Object> typeMap = transformerOilThresholdService.findType(sensorCode);
//        String oilType = typeMap.get("type").toString();
        String oilDetailType = typeMap.get("oilType").toString();

        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (null != warningInfo) {
            String warningMsg = warningInfo.getWarningInfo();
            vo = JSON.parseObject(warningMsg, BusiOilVo.class);
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            if (busiSensorVo != null) {
                this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
            }
        }
        // 获取增长率
        TransformerEpaAbsIncreaseCache absIncreaseCache = epaDataOriginService.getEpaAbsIncreaseCacheInfo(sensorCode);
        // 获取偏差率数据
        TransformerEpaDeviation epaDeviation = transformerEpaDeviationService.getEpaDeviation(sensorCode);
        if (ObjectUtils.isNotEmpty(absIncreaseCache)) {
            List<BusiOiTmplVo> oilList = vo.getList();
            for (int i = 0; i < oilList.size(); i++) {
                if (oilList.get(i).getGasName().equals("三比值")) {
                    oilList.remove(i);
                }
            }
            // 获取气体增长率
            oilList.stream().forEach(oiTmplVo -> {
                oiTmplVo.setIncreaseRate(
                        FieldUtil.getFieldValue(absIncreaseCache, oiTmplVo.getGasName().toLowerCase()) + "");
                if (ObjectUtils.isNotEmpty(epaDeviation)) {
                    oiTmplVo.setIncreaseRate(FieldUtil.getFieldValue(epaDeviation,
                            String.format("%s%s", oiTmplVo.getGasName().toLowerCase(), "Deviation")) + "");
                }
                if (oiTmplVo.getGasName().equals("总烃")) {
                    oiTmplVo.setIncreaseRate(StringUtils.isNotEmpty(absIncreaseCache.getTotalHydrocarbon() + "")
                            ? absIncreaseCache.getTotalHydrocarbon() + "" : "-");
                    if (ObjectUtils.isNotEmpty(epaDeviation)) {
                        oiTmplVo
                                .setDeviationRate(StringUtils.isNotEmpty(epaDeviation.getTotalHydrocarbonDeviation() + "")
                                        ? epaDeviation.getTotalHydrocarbonDeviation() + "" : "-");
                    }
                }
                if(oilDetailType.equals(SiteConstants.oilDetailType.OIL_DETAIL_TYPE_1.getValue())) {
                    if (oiTmplVo.getGasName().equals("载气")) {
                        if (oiTmplVo.getGasValue().equals("1")) {
                            oiTmplVo.setGasValue("油色谱载气压力异常");
                        }
                        oiTmplVo.setIncreaseRate("");
                        oiTmplVo.setDeviationRate("");
                    }
                }

            });
        } else {
            List<BusiOiTmplVo> oilList = vo.getList();
            // 获取气体增长率
            oilList.stream().forEach(oiTmplVo -> {
                if (oiTmplVo.getGasName().equals("载气")) {
                    if (oiTmplVo.getGasValue().equals("1")) {
                        oiTmplVo.setAlarmValue("油色谱载气压力异常");
                    }
                }

            });
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    @Override
    public BusiCapaVo getCapacitanceInfo(String warningId, String sensorCode) {
        BusiCapaVo vo = null;
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (null != warningInfo) {
            String warningMsg = warningInfo.getWarningInfo();
            vo = JSON.parseObject(warningMsg, BusiCapaVo.class);
            BusiEqmWarning busiEqmWarning = busiEqmWarningDao.get(warningId);
            if (busiEqmWarning != null) {
                if (EqType.EQ_TYPE1.equals(busiEqmWarning.getEqType())) {
                    BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
                    this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
                } else if (EqType.EQ_TYPE3.equals(busiEqmWarning.getEqType())) {
                    BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BLQ, sensorCode);
                    this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BLQ);
                }
            }
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    @Override
    public BusiArresterVo getArresterInfo(String warningId, String sensorCode) {
        BusiArresterVo vo = null;
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (null != warningInfo) {
            String warningMsg = warningInfo.getWarningInfo();
            vo = JSON.parseObject(warningMsg, BusiArresterVo.class);
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BLQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BLQ);
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }


    /**
     * 电缆局放
     */
    @Override
    public BusiPdVo getPdWarningInfo(String warningId, String sensorCode, String pdType) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        BusiPdVo vo = JSON.parseObject(warningInfo.getWarningInfo(), BusiPdVo.class);
        if (ExternalTypeEnum.BYQ.getValue().equals(pdType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
        } else if (ExternalTypeEnum.GIS.getValue().equals(pdType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.GIS, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.GIS);
        }
        else
        {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.OTHER, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.OTHER);
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    /**
     * 电缆环流
     */
    @Override
    public BusiCableCirculationWarningVo getcableCirculationWarningInfo(String warningId, String sensorCode, String eqType) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        BusiCableCirculationWarningVo vo = JSON.parseObject(warningInfo.getWarningInfo(), BusiCableCirculationWarningVo.class);
        BusiCableCirculationIncreaseRate busiCableCirculationIncreaseRate = cableCirculationtemperIncreaseRateCacheMapper.getItem(sensorCode, vo.getDtime());
        if(busiCableCirculationIncreaseRate != null) {
//            vo.setIncreaseRate(busiCableCirculationIncreaseRate.getDailyGrowthRateVal());
//            vo.setMonthlyGrowthRate(busiCableCirculationIncreaseRate.getMonthlyGrowthRateVal());
            vo.setMonthlyincreaseRate(busiCableCirculationIncreaseRate.getMonthlyGrowthRate());
//            vo.setPredailyGrowthRate(busiCableCirculationIncreaseRate.getPredailyGrowthRate());
//            vo.setPremonthlyGrowthRate(busiCableCirculationIncreaseRate.getPremonthlyGrowthRate());
        }
        if (ExternalTypeEnum.BYQ.getValue().equals(eqType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
        } else if (ExternalTypeEnum.GIS.getValue().equals(eqType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.GIS, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.GIS);
        } else if (ExternalTypeEnum.OTHER.getValue().equals(eqType)){
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.OTHER, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.OTHER);
        } else if (ExternalTypeEnum.DL.getValue().equals(eqType)){
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.DL, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.DL);
        } else if (ExternalTypeEnum.GEN.getValue().equals(eqType)){
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.GEN, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.GEN);
        }
        return vo;
    }

    @Override
    public BusiPhotoVo getPhotoWarningInfo(String warningId, String sensorCode, String pdType) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        BusiPhotoVo vo = JSON.parseObject(warningInfo.getWarningInfo(), BusiPhotoVo.class);
        if (EqType.EQ_TYPE1.equals(pdType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            vo.setPhase(busiSensorVo.getPosition());
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
        } else if (EqType.EQ_TYPE2.equals(pdType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.GIS, sensorCode);
            vo.setPhase(busiSensorVo.getPosition());
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.GIS);
        } else if (EqType.EQ_TYPE3.equals(pdType)) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BLQ, sensorCode);
            vo.setPhase(busiSensorVo.getPosition());
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BLQ);
        }
        Phase phase = Phase.fromValue(vo.getPhase());
        if (phase != null) {
            vo.setPhase(phase.getName());
        }
        return vo;
    }

    @Override
    public BusiGisSf6Vo getGisSf6Info(String warningId, String sensorCode) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        String warningMsg = warningInfo.getWarningInfo();
        BusiGisSf6Vo vo = JSON.parseObject(warningMsg, BusiGisSf6Vo.class);
        BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.GIS, sensorCode);
        this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.GIS);

        GisSf6IncreaseRateCache gisSf6IncreaseRateCache =
                gisSf6IncreaseRateCacheMapper.getItem(sensorCode, vo.getCollectDate().toLocalDate().minusDays(1));
        vo.getList().forEach(gisSf6ItemVo -> {
            gisSf6ItemVo.setEquipmentName(vo.getEquipmentName());
//                if(gisSf6IncreaseRateCache!=null){
//	                // 设置日泄露率
//	                DecimalFormat df = new DecimalFormat("0.00%");
//	                gisSf6ItemVo.setDaylyLeakageRate(df.format(Double.parseDouble(gisSf6IncreaseRateCache.getIncreasePressure20C())).replace("%",""));
//	                // 设置月泄露率
//	                gisSf6ItemVo.setMonthlyLeakageRate(df.format(Double.parseDouble(gisSf6IncreaseRateCache.getMonthIncreasePressure20C())).replace("%",""));
//	                // 设置增长率
//	                gisSf6ItemVo.setMontylyGrowthRate(df.format(Double.parseDouble(gisSf6IncreaseRateCache.getIncreaseMoisture())).replace("%",""));
//                }
            if(gisSf6IncreaseRateCache!=null){
                // 设置日泄露率
                DecimalFormat df = new DecimalFormat("0.00");
                if (gisSf6IncreaseRateCache.getIncreasePressure20C() != null) {
                    gisSf6ItemVo
                            .setDaylyLeakageRate(df.format(new BigDecimal(gisSf6IncreaseRateCache.getIncreasePressure20C())
                                    .multiply(BigDecimal.valueOf(100))));

                }
                // 设置月泄露率
                if (gisSf6IncreaseRateCache.getMonthIncreasePressure20C() != null) {
                    gisSf6ItemVo.setMonthlyLeakageRate(
                            df.format(new BigDecimal(gisSf6IncreaseRateCache.getMonthIncreasePressure20C())
                                    .multiply(BigDecimal.valueOf(100))));
                }
                // 设置增长率
                // 设置月泄露率
                if (gisSf6IncreaseRateCache.getIncreaseMoisture() != null) {
                    gisSf6ItemVo
                            .setMontylyGrowthRate(df.format(new BigDecimal(gisSf6IncreaseRateCache.getIncreaseMoisture())
                                    .multiply(BigDecimal.valueOf(100))));
                }
            }
        });
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    @Override
    public BusiRunStateVo getRunStateWarningInfo(String warningId, String mainTreeId) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        String warningMsg = warningInfo.getWarningInfo();
        BusiRunStateVo vo = JSON.parseObject(warningMsg, BusiRunStateVo.class);
        BusiOfflineTransformerBrandVo busiOfflineTransformerBrand =
                busiOfflineTransformerBrandService.getBusiOfflineTransformerBrandVo("", mainTreeId);
        if (busiOfflineTransformerBrand != null) {
            vo.setEquipmentName(busiOfflineTransformerBrand.getTransformerName());
            // 标牌
            vo.setBrand(busiOfflineTransformerBrand.getEquipmentModel());
            // 生产厂家
            vo.setManufactor(busiOfflineTransformerBrand.getManufacturer());
            // 安装位置
            vo.setInstallLocation("/");
            // 投运时间
            vo.setUseDate(busiOfflineTransformerBrand.getEquipmentDeliveryTime());
            // 维修次数
            vo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(ExternalTypeEnum.BYQ,
                    busiOfflineTransformerBrand.getId()));
            // 误报率
            vo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateByMainTreeId(mainTreeId, null, null));
            //例行试验次数
            vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(busiOfflineTransformerBrand.getId()));
        }
        vo.setExternalType("1");
        return vo;
    }

    @Override
    public BusiWcgVo getCwgInfo(String warningId, String sensorCode) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        String warningMsg = warningInfo.getWarningInfo();
        BusiWcgVo vo = JSON.parseObject(warningMsg, BusiWcgVo.class);
        BusiTemperPoint point = busiTemperPointService.findSensorBySensorCode(sensorCode);
        if (null != point) {
            vo.setInstallLocation(point.getPositionDes());
            vo.setManufactor(point.getManufactor());
            vo.setUseDate(point.getUseDate());
            vo.setBrand(point.getSensorBrand());
            vo.setEquipmentName(point.getSensorName());
            // TODO 增长率
            TemperIncreaseRate temperIncreaseRate =
                    temperIncreaseRateService.getTemperIncreaseRateBySensorCode(sensorCode);
            if (temperIncreaseRate != null) {
                DecimalFormat df = new DecimalFormat("0.00");
                if (temperIncreaseRate.getDailyGrowthRate() != null) {
                    vo.setDayIncreaseRate(df.format(
                            new BigDecimal(temperIncreaseRate.getDailyGrowthRate()).multiply(BigDecimal.valueOf(100))));
                }
                if (temperIncreaseRate.getMonthlyGrowthRate() != null) {
                    vo.setMonthIncreaseRate(df.format(
                            new BigDecimal(temperIncreaseRate.getMonthlyGrowthRate()).multiply(BigDecimal.valueOf(100))));
                }
            }
        }
        return vo;
    }

    /**
     * 红外检测告警
     */
    @Override
    public BusiInfraredVo getBusiInfrared(String warningId, String sensorCode, String pdType) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        String warningMsg = warningInfo.getWarningInfo();
        BusiInfraredVo vo = JSON.parseObject(warningMsg, BusiInfraredVo.class);
        if (pdType.equals("1")) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
        } else if (pdType.equals("2")) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.GIS, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.GIS);
        } else if (pdType.equals("3")) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BLQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BLQ);
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    /**
     * 绕组测温告警
     */
    @Override
    public BusiWindingVo getBusiWinding(String warningId, String sensorCode, String pdType) {
        BusiEqmWarningInfo warningInfo = busiEqmWarningInfoService.findByWarningId(warningId);
        if (warningInfo == null) {
            return null;
        }
        String warningMsg = warningInfo.getWarningInfo();
        BusiWindingVo vo = JSON.parseObject(warningMsg, BusiWindingVo.class);
        if (pdType.equals("1")) {
            BusiSensorVo busiSensorVo = busiSensorService.getBusiSensorVo(ExternalTypeEnum.BYQ, sensorCode);
            this.buildExternalMsg(vo, busiSensorVo, ExternalTypeEnum.BYQ);
        }
        //例行试验次数
        vo.setOfflineReportNumber(transformerOfflineExperimentReportService.findByTransformerId(transformerOfflineExperimentReportService.getTransformerId(sensorCode)));
        return vo;
    }

    /**
     * @Title: buildExternalMsg
     * @Description: 设置外挂设备信息
     * @createDate: Aug 9, 2019 3:13:02 PM
     * @param vo
     * @param
     *            传感器编码
     * @param externalTypeEnum
     *            外挂设备设备类型
     * @return void
     */
    private void buildExternalMsg(BusiSubInfoVo vo, BusiSensorVo busiSensorVo, ExternalTypeEnum externalTypeEnum) {
        BusiExternalEquipment externalEquipment =
                busiExternalEquipmentService.findBySensorCode(busiSensorVo.getSensorCode(), externalTypeEnum.getValue());
        if (externalEquipment == null) {
            return;
        }
        vo.setExternalType(externalEquipment.getExternalType());
        // 监测装置名称
        vo.setEquipmentName(externalEquipment.getEquipmentName());
        // 标牌
        vo.setBrand(StringUtils.isNotEmpty(busiSensorVo.getSensorBrand()) ? busiSensorVo.getSensorBrand() : "");
        // 生产厂家
        vo.setManufactor(StringUtils.isNotEmpty(busiSensorVo.getManufactor()) ? busiSensorVo.getManufactor() : "");
        // 安装位置
        vo.setInstallLocation(
                StringUtils.isNotEmpty(busiSensorVo.getPositionDes()) ? busiSensorVo.getPositionDes() : "");
        // 投运时间
        vo.setUseDate(busiSensorVo.getUseDate());
        // 维修次数
        vo.setRepairCount(busiCommonService.getEquipHalfYearRepairCount(externalTypeEnum, externalEquipment.getId()));
        // 误报率
        vo.setFalseAlarmRate(busiEqmWarningService.getFalseAlarmRateBySensor(busiSensorVo.getSensorCode(),
                DateUtil.getMonthStartDay(), DateUtil.getMonthLastDay()));
    }
}
