package com.huatek.busi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.util.StringUtil;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.UploadCycle;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.dao.EquipmentTemperatureCacheDao;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.*;
import com.huatek.busi.mapper.cable.EquipmentTemperatureCacheMapper;
import com.huatek.busi.service.*;
import com.huatek.busi.service.impl.cac.AbstractCacEtlService;
import com.huatek.busi.task.curve.CurveExecutorTask;
import com.huatek.busi.task.transformer.part.TransfromEqmTask;
import com.huatek.busi.util.CalcUtil;
import com.huatek.busi.vo.EquipmentOnlineRateVo;
import com.huatek.busi.vo.EquipmentTemperatureCacheVo;
import com.huatek.busi.vo.SensiorIdExportVo;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@Slf4j
@Component
@Service(interfaceClass = TransformerNumberService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerNumberServiceImpl  implements TransformerNumberService {
    @Autowired
    private TransformerNumberMapper transformerNumberMapper;

    @Autowired
    private BusiSiteTreeMapper busiSiteTreeMapper;

    @Autowired
    private EquipmentTemperatureCacheMapper equipmentTemperatureCacheMapper;

    @Autowired
    private SiteUploadCycleMapper siteUploadCycleMapper;
    @Autowired
    EquipmentTemperatureCacheDao equipmentTemperatureCacheDao;

    @Autowired
    TransformerNumberService transformerNumberService;

    @Autowired
    GisNumberService gisNumberService;

    @Autowired
    CubicleNumberService cubicleNumberService;

    @Autowired
    CableNumberService cableNumberService;

    @Autowired
    ArresterNumberService arresterNumberService;

    @Autowired
    GeneratorNumberService generatorNumberService;

    @Autowired
    TransfromEqmTask transfromEqmTask;

    @Autowired
    CurveExecutorTask curveExecutorTask;

    @Autowired
    ArresterNumberMapper arresterNumberMapper;

    @Autowired
    GisNumberMapper gisNumberMapper;

    @Autowired
    CableNumberMapper cableNumberMapper;

    @Autowired
    CubicleNumberMapper cubicleNumberMapper;

    @Autowired
    GeneratorNumberMapper generatorNumberMapper;


    @Override
    public List<SensiorIdExportVo>  findAllEquipment() {

            // false: 没找到
            boolean  isOpter = false;
            //查询所有sensor编码
            List<SensiorIdExportVo> list = busiSiteTreeMapper.findSensorListBySiteId();
            EquipmentTemperatureCache equip = new EquipmentTemperatureCache();
            //查询缓存上来的sensor
            List<EquipmentTemperatureCache> equipmentList = equipmentTemperatureCacheMapper.findEquipmentList();
            for (SensiorIdExportVo sensor : list) {
                String sensorId = sensor.getSensorCode();
                for (EquipmentTemperatureCache equipmentTemperatureCache : equipmentList) {
                    String equipmentSensorId = equipmentTemperatureCache.getSensorId();
                    if (sensorId != null) {
                        if (sensorId.equals(equipmentSensorId)) {
//                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                            String three_days_after = sdf.format(new Date());
//                            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                            LocalDateTime ldt = LocalDateTime.parse(three_days_after, df);
//                            equipmentTemperatureCache.setTimeStamp(ldt);
//                            equipmentTemperatureCacheMapper.updateSensorVo(equipmentTemperatureCache);
                            isOpter = true;
                            break;
                        }
                    }
                }
                if (sensorId != null) {
                    if (isOpter == false) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String three_days_after = sdf.format(new Date());
                        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        LocalDateTime ldt = LocalDateTime.parse(three_days_after, df);
                        equip.setId(Uuid.getUUID());
                        equip.setSensorId(sensor.getSensorCode());
                        equip.setTimeStamp(ldt);
                        equip.setSubEquipmentId(sensor.getEquipmentId());
                        equip.setMainEquipmentId(sensor.getMainEquipmentId());
                        equip.setExtEquipmentCode(sensor.getPendantType());
                        equip.setSensorName(sensor.getSensorName());
                        equip.setType(convertType(sensor.getType()));
                        equipmentTemperatureCacheMapper.addSensorVo(equip);
                    }
                    isOpter = false;
                }
            }
        return list;

    }

    String convertType(String type) {
        if (SiteConstants.SiteTreeEnum.BYQ.getValue().equals(type)) {
            return "01";
        } else if (SiteConstants.SiteTreeEnum.GIS.getValue().equals(type)) {
            return "02";
        } else if (SiteConstants.SiteTreeEnum.DL.getValue().equals(type)) {
            return "03";
        } else if (SiteConstants.SiteTreeEnum.BLQ.getValue().equals(type)) {
            return "04";
        } else if (SiteConstants.SiteTreeEnum.CW.getValue().equals(type)) {
            return "05";
        } else if (SiteConstants.SiteTreeEnum.GEN.getValue().equals(type)) {
            return "06";
        } else if (SiteConstants.SiteTreeEnum.OTHER.getValue().equals(type)){
            return "07";
        } else {
            return null;
        }
    }

    @Override
    public List<EquipmentTemperatureCache> findEquipmentAndDelete() {
        //查询所有缓存表外挂
        List<EquipmentTemperatureCache> equipmentList = equipmentTemperatureCacheMapper.findEquipmentList();
        //查询所有sensor编码
        List<SensiorIdExportVo> list =busiSiteTreeMapper.findSensorListBySiteId();
        for (EquipmentTemperatureCache equipmentCache:equipmentList) {
            boolean flag = false;
            String sensorCacheId =equipmentCache.getSensorId();
            for (SensiorIdExportVo sensor:list) {
                String sensorId = sensor.getSensorCode();
                if (sensorId!=null){
                    if (sensorCacheId.equals(sensorId)){
                        flag = true;
                        break;
                    }
                }

            }
            if (!flag){
                equipmentTemperatureCacheMapper.deleteEntity(sensorCacheId);
            }
        }
        return equipmentList;
    }

    //获取变压器外挂设备
    @Override
    public int findTransformerNumber() throws Exception {
        //查询变压器所有设备并获取到变压器设备Id
        List<EquipmentSensorNumber> findEquipment = transformerNumberMapper.findEquipment();
        //查询所有站点Id
        List<SiteData> findSite = transformerNumberMapper.findSiteData() ;
        //循环所有站点
        for (SiteData siteData:findSite) {
            String siteId = siteData.getId();
        for (EquipmentSensorNumber equipmentMainId: findEquipment) {
            String mainEquipmentId = equipmentMainId.getMainEquipmentId();
            //通过变压器id查询cache表中外挂装置
            List<EquipmentSensorNumber> findNumber = transformerNumberMapper.findTransformerNumber(mainEquipmentId);
            for (EquipmentSensorNumber equipmentTemperatureCache:findNumber) {
                    String sensorId = equipmentTemperatureCache.getSensorId();
                    String sensorCode = equipmentTemperatureCache.getExtEquipmentCode();
                    String subSensorID = sensorId.substring(15, 17);
                    //获取当前系统时间
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String newDate = sdf.format(new Date());
                if (subSensorID.equals("01")) {
                    boolean flag = false;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.yspzz + "");
                    if (cycle != null) {
                        int count = 0;
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.HOUR, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();
                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }

                }
                if (subSensorID.equals("02")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.txzz + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("03")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqtg + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("04")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqqx + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("05")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqhw + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("06")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.pzzz + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("07")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.jfzz + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }

                if (subSensorID.equals("09")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqtg + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("13")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.jjzz + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }

                if (subSensorID.equals("15")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqrzcw + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }

                if (subSensorID.equals("0D")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zxdcgq + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("0E")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zscgq + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("0F")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zdcgqup + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }
                if (subSensorID.equals("1F")) {
                    boolean flag = false;
                    int count = 0;
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zdcgqdown + "");
                    if (cycle != null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 1;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                        }
                        //查询记录计算出来值的表
                        List<EquipmentSensorCache> numberList= transformerNumberMapper.findSensorCache();
                        // false: 没找到
                        equipmentTemperatureCache.setId(Uuid.getUUID());
                        equipmentTemperatureCache.setNormalNumber(count);
                        // 将int型转换为BigDecimal
                        BigDecimal countVo = new BigDecimal(0);
                        int value = count;
                        countVo = BigDecimal.valueOf((int) value);
                        //获取sensorCode总数
                        BigDecimal number = transformerNumberMapper.findSesorCodeNumber(sensorCode, mainEquipmentId);
                        equipmentTemperatureCache.setNumber(number);
                        if (!number.equals(BigDecimal.ZERO)) {
                            BigDecimal rate = CalcUtil.calcRate(countVo, number);
                            //计算百分比
                            DecimalFormat df = new DecimalFormat();
                            String online = df.format(rate);
                            equipmentTemperatureCache.setOnlineRate(online);
                        }
                        BigDecimal faultNumber = number.subtract(countVo);
                        equipmentTemperatureCache.setFaultNumber(faultNumber);
                        for (EquipmentSensorCache equipmentSensorCache:numberList) {
                            String SensorCodeId = equipmentSensorCache.getSensorId();

                            if (sensorId.equals(SensorCodeId)){
                                transformerNumberMapper.updateTransformerSensor(equipmentTemperatureCache);
                                flag = true;
                                break;
                            }
                        }
                        if (!flag){
                            equipmentTemperatureCache.setId(Uuid.getUUID());
                            transformerNumberMapper.addTransformerSensor(equipmentTemperatureCache);
                        }
                    }
                }

           }
        }

     }
        return 0;
    }


    @Override
    public List<EquipmentSensorCache> findTransformerSensorNumber(String mainEquipmentId) {
        List<EquipmentSensorCache> online = transformerNumberMapper.findOnlineRate(mainEquipmentId);
        for (EquipmentSensorCache equipmentSensorCache:online) {
            int normal = equipmentSensorCache.getNormalNumber();
            BigDecimal sum = equipmentSensorCache.getNumber();
             //将int型转换为BigDecimal
            BigDecimal normalVo = new BigDecimal(0);
            int value = normal;
            normalVo = BigDecimal.valueOf((int) value);
            BigDecimal fault = sum.subtract(normalVo);
            if (!sum.equals(BigDecimal.ZERO)) {
                    BigDecimal rate = CalcUtil.calcRate(normalVo, sum);
                    //计算百分比
                    DecimalFormat df = new DecimalFormat();
                    String onlineRate = df.format(rate);
                    equipmentSensorCache.setOnlineRate(onlineRate);
                }
            equipmentSensorCache.setFaultNumber(fault);

        }

        return online;
    }

    @Override
    public List<TransformerStatus> findTransformerStatus() {
        List< TransformerStatus > findStatus = transformerNumberMapper.findTransformerStatus();
        for (TransformerStatus transformerStatus: findStatus) {
            String transformerId = transformerStatus.getMainEquipmentId();
            TAndTransformerStatus findTransformerStype = transformerNumberMapper.findTransformerStype(transformerId);
            if(findTransformerStype !=null){
                transformerStatus.setStype(findTransformerStype.getStype());
            }
            String mainEquipmentId = transformerStatus.getMainEquipmentId();
            List<EquipmentSensorCache> list = transformerNumberService.findTransformerSensorNumber(mainEquipmentId);
            for (EquipmentSensorCache sensor: list) {
                if (sensor!=null){
                    if (sensor.getFaultNumber()!=BigDecimal.ZERO){
                        transformerStatus.setPrior("0");
                    }
                    if (sensor.getSensorName()!=null&&sensor.getFaultNumber().equals(BigDecimal.ZERO)){
                        transformerStatus.setPrior("1");
                    }
                }

            }

        }
        return findStatus;
    }
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public EquipmentSumNumber findSumTransformerNumber() throws Exception {
        //查询所有站点Id
        List<SiteData> findSite = transformerNumberMapper.findSiteData() ;
        EquipmentSumNumber finSumNumber = new EquipmentSumNumber();
        //查詢所有裝置
        List<EquipmentSensor> equipmentList = equipmentTemperatureCacheMapper.findEquipmentVoList();
        int count = 0;
        boolean flag =false;
        String ids="";
        String faultIds="";
        for (SiteData siteData:findSite) {
            String siteId = siteData.getId();
        for (EquipmentSensor equipmentTemperatureCache:equipmentList) {
            String sensorId = equipmentTemperatureCache.getSensorId();
            String subSensorID = sensorId.substring(15, 17);
            //获取当前系统时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String newDate = sdf.format(new Date());
            if (subSensorID.equals("01")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.yspzz + "");
                if (cycle!= null){
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 1;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.HOUR, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }

            }
            if (subSensorID.equals("02")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.txzz + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }
            if (subSensorID.equals("03")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqtg + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }
            if (subSensorID.equals("04")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqqx + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }
            if (subSensorID.equals("05")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqhw + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }
            if (subSensorID.equals("06")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.pzzz + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }
            if (subSensorID.equals("07")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.jfzz + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }

            if (subSensorID.equals("09")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqrx + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }
            // 20230423 新增測溫裝置
            if (subSensorID.equals("12")) {
                //获取设备最新一条时间
                String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                //获取设备间隔时间
                String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.totalcwzz + "");
                if (cycle!= null) {
                    int temp = Integer.parseInt(cycle);
                    int cycleVo = temp + 2;
                    BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                    int oldTime = timeValue.intValue();
                    Date dt = sdf.parse(newDate);
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(dt);
                    //计算时间差
                    rightNow.add(Calendar.MINUTE, -oldTime);
                    Date sub = rightNow.getTime();
                    String reStr = sdf.format(sub);
                    Integer finalValue = newDateValue.compareTo(reStr);
                    if (finalValue > 0) {
                        count += 1;
                        ids+=sensorId+",";
                    }else{
                        faultIds+=sensorId+",";
                    }
                }
            }

                if (subSensorID.equals("13")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.jjzz + "");
                    if (cycle!= null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                            ids+=sensorId+",";
                        }else{
                            faultIds+=sensorId+",";
                        }
                    }
                }

                if (subSensorID.equals("15")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.byqrzcw + "");
                    if (cycle!= null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                            ids+=sensorId+",";
                        }else{
                            faultIds+=sensorId+",";
                        }
                    }
                }

                if (subSensorID.equals("0D")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zxdcgq + "");
                    if (cycle!= null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                            ids+=sensorId+",";
                        }else{
                            faultIds+=sensorId+",";
                        }
                    }
                }
                if (subSensorID.equals("0E")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zscgq + "");
                    if (cycle!= null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                            ids+=sensorId+",";
                        }else{
                            faultIds+=sensorId+",";
                        }
                    }
                }
                if (subSensorID.equals("0F")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zdcgqup + "");
                    if (cycle!= null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                            ids+=sensorId+",";
                        }else{
                            faultIds+=sensorId+",";
                        }
                    }
                }
                if (subSensorID.equals("1F")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.zdcgqdown + "");
                    if (cycle!= null) {
                        int temp = Integer.parseInt(cycle);
                        int cycleVo = temp + 2;
                        BigDecimal timeValue = DecimalUtil.getVal(cycleVo);
                        int oldTime = timeValue.intValue();
                        Date dt = sdf.parse(newDate);
                        Calendar rightNow = Calendar.getInstance();
                        rightNow.setTime(dt);
                        //计算时间差
                        rightNow.add(Calendar.MINUTE, -oldTime);
                        Date sub = rightNow.getTime();
                        String reStr = sdf.format(sub);
                        Integer finalValue = newDateValue.compareTo(reStr);
                        if (finalValue > 0) {
                            count += 1;
                            ids+=sensorId+",";
                        }else{
                            faultIds+=sensorId+",";
                        }
                    }
                }
                finSumNumber.setSumNormal(count);

        }
        //查询变压器总外挂设备数
        int countSum = transformerNumberMapper.finSumNumber(siteId);
        //查询变压器外挂总异常数
        int countFault = countSum - count;
        finSumNumber.setSumNumber(countSum);
        finSumNumber.setSumNormal(count);
        finSumNumber.setSumFault(countFault);
        finSumNumber.setSiteId(siteId);
        finSumNumber.setType("01");
        String type = "01";finSumNumber.setFaultIds(faultIds);
            finSumNumber.setNormalIds(ids);
            finSumNumber.setFaultIds(faultIds);
        EquipmentSumCache equipmentSumCache = transformerNumberMapper.findCacheType(type,siteId);
        if (equipmentSumCache!=null){
            if (type.equals("01")){
                transformerNumberMapper.updateNumber(finSumNumber);
                flag = true;
                break;
            }
        }
        }
        if (!flag){
            finSumNumber.setId(Uuid.getUUID());
            transformerNumberMapper.addNumber(finSumNumber);
        }

        return finSumNumber;
    }

    @Override
    public TransformerIU findTransformerUI(String transformerId) {
        TransformerIU findIuValue = transformerNumberMapper.findIU(transformerId);
        TransformerIU loadRate = transformerNumberMapper.findLoadRate(transformerId);
        if(findIuValue != null ) {
            loadRate.setHigthCurrent(findIuValue.getHigthCurrent());
            loadRate.setHigthVoltage(findIuValue.getHigthVoltage());
        }
        if (null!=loadRate && "1.0E-4".equals(loadRate.getLoadRate())){
            loadRate.setLoadRate("0.00");
        }
        return loadRate;
    }

    @Override
    public List<TransformerLoadRate> findTransformerLoadRate() {
        List<BusiSiteTree> mainTreeId = transformerNumberMapper.findMainTreeId();
        List<String> dateList = new ArrayList<>();
        List<TransformerLoadRate> results = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 15; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        for (BusiSiteTree busiSiteTree : mainTreeId) {
            String mainEquipmentId = busiSiteTree.getId();
            for(int i = 0; i < dateList.size(); i++){
                TransformerLoadRate transformerLoadRate = new TransformerLoadRate();
                String date = dateList.get(i);
                transformerLoadRate.setLoadTime(date);
                TransformerLoadRate yValue = transformerNumberMapper.findAllLoadRate(mainEquipmentId , date);
                transformerLoadRate.setMainTreeId(mainEquipmentId);
                transformerLoadRate.setName(busiSiteTree.getName());
                if(yValue != null) {
                    String value = yValue.getLoadRate();
                    transformerLoadRate.setLoadRate(value);
                    results.add(transformerLoadRate);
                } else {
                    transformerLoadRate.setLoadRate("0.00");
                    results.add(transformerLoadRate);
                }
            }
        }
        return results;
    }

    @Override
    public EquipmentSumSensorCache findSumSensorNumber() {
        EquipmentSumSensorCache sunNumber = transformerNumberMapper.finSumAllNumber();
        if(!ObjectUtil.isEmpty(sunNumber) && sunNumber.getNumber() != null && sunNumber.getNormalNumber() != null) {
            int number = sunNumber.getNumber();
            int normalNumber = sunNumber.getNormalNumber();
            // 将int型转换为BigDecimal
            BigDecimal numberVo = new BigDecimal(0);
            int value1 = number;
            numberVo = BigDecimal.valueOf((int) value1);

            // 将int型转换为BigDecimal
            BigDecimal normalNumberVo = new BigDecimal(0);
            int value2 = normalNumber;
            normalNumberVo = BigDecimal.valueOf((int) value2);
            //计算百分比
            BigDecimal rate = CalcUtil.calcRate(normalNumberVo, numberVo);
            DecimalFormat df = new DecimalFormat();
            String online = df.format(rate);
            sunNumber.setOnlineRate(online);
        }
        return sunNumber;
    }

    @Override
    public EquipmentSumSensorCache findSumTransformerSensorNumber() {
        EquipmentSumSensorCache equipmentSumSensorCache = transformerNumberMapper.findSumSensorNumber();
        if (!ObjectUtils.isEmpty(equipmentSumSensorCache)) {
            // 计算在线率
            BigDecimal a = BigDecimal.valueOf(equipmentSumSensorCache.getNormalNumber());
            BigDecimal b = BigDecimal.valueOf(equipmentSumSensorCache.getNumber());
            equipmentSumSensorCache.setOnlineRate(new DecimalFormat().format(CalcUtil.calcRate(a, b)));
        } else {
            equipmentSumSensorCache.setNormalNumber(0);
            equipmentSumSensorCache.setNormalNumber(0);
            equipmentSumSensorCache.setFaultNumber(0);
            equipmentSumSensorCache.setOnlineRate("-");
        }
        return equipmentSumSensorCache;
    }

    @Override
    public List<EquipmentTemperatureCacheVo> selectSumTransformerSensorInfo(String flag,String type) {
        List<EquipmentTemperatureCache> list=equipmentTemperatureCacheMapper.selectSumTransformerSensorInfo(type);
        List<EquipmentTemperatureCacheVo> listVo=new ArrayList();
        List<EquipmentTemperatureCacheVo> listVofaultIds=new ArrayList();
        List<EquipmentSumSensorCache> equipmentSumSensorCaches = transformerNumberMapper.findSumSensorInfo(type);
        String normalIds="";
        if(StringUtil.isEmpty(type)){
            for (EquipmentSumSensorCache equipmentSumSensorCache:equipmentSumSensorCaches) {
                normalIds+=equipmentSumSensorCache.getNormalIds();

            }
        }else{
            normalIds=equipmentSumSensorCaches.get(0).getNormalIds();
        }
        for (EquipmentTemperatureCache equipmentTemperatureCache:list) {
            EquipmentTemperatureCacheVo equipmentTemperatureCacheVo=new EquipmentTemperatureCacheVo();
            BeanUtil.copyProperties(equipmentTemperatureCache,equipmentTemperatureCacheVo);

            if(StringUtil.isEmpty(normalIds)){
                equipmentTemperatureCacheVo.setFlag("1");
                listVofaultIds.add(equipmentTemperatureCacheVo);
            }else{
                if(normalIds.contains(equipmentTemperatureCache.getSensorId())){
                    equipmentTemperatureCacheVo.setFlag("0");
                    listVo.add(equipmentTemperatureCacheVo);
                }else{
                    equipmentTemperatureCacheVo.setFlag("1");
                    listVofaultIds.add(equipmentTemperatureCacheVo);

                }
            }
        }
        // type为空则查询全部状态的装置
        if(StringUtil.isEmpty(flag) ){
            listVo.addAll(listVofaultIds);
            return listVo;
       }
        //0-- 正常  1--异常
        if("0".equals(flag)){
            return listVo;
        }else{
            return listVofaultIds;
        }

    }

    @Override
    public Map<String, EquipmentSumSensorCache> getAllDeviceSensorNumber() {
        Map<String, EquipmentSumSensorCache> map = new HashMap<>();
        EquipmentSumSensorCache byq = transformerNumberService.findSumTransformerSensorNumber();
        EquipmentSumSensorCache gis = gisNumberService.findSumGisSensorNumber();
        EquipmentSumSensorCache kgg = cubicleNumberService.findSumCubicleSensorNumber();
        EquipmentSumSensorCache dl = cableNumberService.findSumCableSensorNumber();
        EquipmentSumSensorCache blq = arresterNumberService.findSumArresterSensorNumber();
        EquipmentSumSensorCache fdj = generatorNumberService.findSumGeneratorSensorNumber();
        EquipmentSumSensorCache other = generatorNumberService.findSumOtherSensorNumber();
        map.put("byq", byq);
        map.put("gis", gis);
        map.put("kgg", kgg);
        map.put("dl", dl);
        map.put("blq", blq);
        map.put("fdj", fdj);
        map.put("other", other);
        return map;
    }

    @Override
    public Map<String, EquipmentOnlineRateVo> getMajorDeviceSensorNumber() {
        Map<String, EquipmentOnlineRateVo> map = new HashMap<>();
        /* 主变室*/
        // 油色谱
        EquipmentOnlineRateVo ysp = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> yspList = transformerNumberMapper.findTransformerSensorCache(SiteConstants.TransfromerTreeEnum.YSP_EQM.getValue());
        map.put("ZB_ysp", calculateOnlineRate(ysp, yspList));
        // 铁芯
        EquipmentOnlineRateVo tx = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> txList = transformerNumberMapper.findTransformerSensorCache(SiteConstants.TransfromerTreeEnum.TX_EQM.getValue());
        map.put("ZB_tx", calculateOnlineRate(tx, txList));
        // 夹件
        EquipmentOnlineRateVo jj = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> jjList = transformerNumberMapper.findTransformerSensorCache(SiteConstants.TransfromerTreeEnum.JJ_EQM.getValue());
        map.put("ZB_jj", calculateOnlineRate(jj, jjList));
        /*出线场*/
        // 容性
        EquipmentOnlineRateVo rxBlq = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> rxList = transformerNumberMapper.findArresterSensorCache(SiteConstants.ArresterTreeEnum.RX_EQM.getValue());
        map.put("BLQ_rx", calculateOnlineRate(rxBlq, rxList));
        // 绝缘监测
        EquipmentOnlineRateVo jyBlq = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> jyList = transformerNumberMapper.findArresterSensorCache(SiteConstants.ArresterTreeEnum.JY_EQM.getValue());
        map.put("BLQ_jy", calculateOnlineRate(jyBlq, jyList));
        /*开关室*/
        // sf6
        EquipmentOnlineRateVo sf6Kgs = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> sf6List = transformerNumberMapper.findGisSensorCache(SiteConstants.GisTreeEnum.SF6_EQM.getValue());
        map.put("KGS_sf6", calculateOnlineRate(sf6Kgs, sf6List));
        // 测温装置（GIS红外+开关柜测温）
        EquipmentOnlineRateVo hwGis = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> hwGisList = transformerNumberMapper.findGisSensorCache(SiteConstants.GisTreeEnum.HW_EQM.getValue());
        hwGis =calculateOnlineRate(hwGis, hwGisList);
        EquipmentOnlineRateVo cwg = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> cwgList = transformerNumberMapper.findCwgSensorCache();
        cwg = calculateOnlineRate(cwg, cwgList);

        EquipmentOnlineRateVo cwKgs = new EquipmentOnlineRateVo();
        cwKgs.setNumber(hwGis.getNumber().add(cwg.getNumber()));
        cwKgs.setNormalNumber(hwGis.getNormalNumber().add(cwg.getNormalNumber()));
        cwKgs.setOnlineRate(String.valueOf(CalcUtil.calcOnlineRate(sf6Kgs.getNormalNumber(), sf6Kgs.getNumber())));
        map.put("KGS_cw", cwKgs);
        /*电缆室*/
        // 电缆环流
        EquipmentOnlineRateVo dlhlDLs = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber>dlhlDLsList = transformerNumberMapper.findCableSensorCache(SiteConstants.CableTreeEnum.DLHL_EQM.getValue());
        dlhlDLs = calculateOnlineRate(dlhlDLs, dlhlDLsList);
        map.put("DLS_dlhl", dlhlDLs);

        // 测温装置（红外+测温装置）
        EquipmentOnlineRateVo hwDl = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> hwDlList = transformerNumberMapper.findCableSensorCache(SiteConstants.CableTreeEnum.HW_EQM.getValue());
        hwDl = calculateOnlineRate(hwDl, hwDlList);

        EquipmentOnlineRateVo cwDl = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> cwDlList = transformerNumberMapper.findCableSensorCache(SiteConstants.CableTreeEnum.CW_EQM.getValue());
        cwDl = calculateOnlineRate(cwDl, cwDlList);

        EquipmentOnlineRateVo cwDls = new EquipmentOnlineRateVo();
        cwDls.setNumber(hwDl.getNumber().add(cwDl.getNumber()));
        cwDls.setNormalNumber(hwDl.getNormalNumber().add(cwDl.getNormalNumber()));
        cwDls.setOnlineRate(String.valueOf(CalcUtil.calcOnlineRate(cwDls.getNormalNumber(), cwDls.getNumber())));
        map.put("DLS_cw", cwDls);

        /*发电机室*/
        // 振动
        EquipmentOnlineRateVo zdFdj = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> zdFdjList = transformerNumberMapper.findGeneratorSensorCache(SiteConstants.GeneratorTreeEnum.ZD_EQM.getValue());

        map.put("FDJ_zd", calculateOnlineRate(zdFdj, zdFdjList));
        // 局放
        EquipmentOnlineRateVo jfFdj = new EquipmentOnlineRateVo();
        List<EquipmentSensorNumber> jfFdjList = transformerNumberMapper.findGeneratorSensorCache(SiteConstants.GeneratorTreeEnum.JF_EQM.getValue());
        map.put("FDJ_jf", calculateOnlineRate(jfFdj, jfFdjList));
        return map;
    }

    public EquipmentOnlineRateVo initEquipmentOnlineRateVo(EquipmentOnlineRateVo vo) {
//        vo = new EquipmentOnlineRateVo();
        vo.setNumber(BigDecimal.ZERO);
        vo.setNormalNumber(BigDecimal.ZERO);
        vo.setOnlineRate("0");
        return vo;
    }

    public EquipmentOnlineRateVo calculateOnlineRate(EquipmentOnlineRateVo vo, List<EquipmentSensorNumber> sensorCacheList) {
        if (ObjectUtils.isEmpty(sensorCacheList)) {
            vo = initEquipmentOnlineRateVo(vo);
        } else {
            vo.setNumber(new BigDecimal(sensorCacheList.size()));
            int normalNumber = 0;
            int faultNumber = 0;
            for (EquipmentSensorNumber item : sensorCacheList) {
                // 缓存表的在线率大于0代表该设备在线，否则离线
                if (new BigDecimal(item.getOnlineRate()).compareTo(BigDecimal.ZERO) > 0) {
                    normalNumber += 1;
                } else {
                    faultNumber += 1;
                }
            }
            vo.setNormalNumber(new BigDecimal(normalNumber));
            vo.setFaultNumber(new BigDecimal(faultNumber));
            vo.setOnlineRate(String.valueOf(CalcUtil.calcOnlineRate(vo.getNormalNumber(), vo.getNumber())));
        }
        return vo;
    }
}
