package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.UploadCycle;
import com.huatek.busi.common.util.DecimalUtil;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.ArresterNumberMapper;
import com.huatek.busi.mapper.GisNumberMapper;
import com.huatek.busi.mapper.SiteUploadCycleMapper;
import com.huatek.busi.mapper.TransformerNumberMapper;
import com.huatek.busi.service.ArresterNumberService;
import com.huatek.busi.service.GisNumberService;
import com.huatek.busi.util.CalcUtil;
import com.huatek.busi.util.DateUtil;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Service(interfaceClass = ArresterNumberService.class)
@Transactional(rollbackFor = Exception.class)
public class ArresterNumberServiceImpl implements ArresterNumberService {

    @Autowired
    private TransformerNumberMapper transformerNumberMapper;

    @Autowired
    ArresterNumberMapper arresterNumberMapper;

    @Autowired
    private SiteUploadCycleMapper siteUploadCycleMapper;

    @Autowired
    private ArresterNumberService arresterNumberService;

    @Override
    public int findArresterNumber() throws Exception {
        //查询避雷器所有设备
        List<EquipmentSensorNumber> findEquipment = arresterNumberMapper.findEquipment();
        for (EquipmentSensorNumber equipmentMainId: findEquipment) {
            String mainEquipmentId = equipmentMainId.getMainEquipmentId();
            //通过Id查询出缓存表中设备
            List<EquipmentSensorNumber> findNumber = transformerNumberMapper.findTransformerNumber(mainEquipmentId);
            //查询所有站点Id
            List<SiteData> findSite = transformerNumberMapper.findSiteData() ;
            //循环所有站点
            for (SiteData siteData:findSite) {
                String siteId = siteData.getId();

                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("03")) {
                        boolean flag = false;
                        //获取设备最新一条时间
                        String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                        //获取设备间隔时间
                        String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqtg + "");
                        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.MINUTE, -oldTime);
                            Date sub = rightNow.getTime();
                            String reStr = sdf.format(sub);
                            Integer finalValue = newDateValue.compareTo(reStr);
                            if (finalValue > 0) {
                                count += 1;
                            }
                            //查询记录计算出来值的表
                            List<EquipmentSensorCache> numberList= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(equipmentTemperatureCache);
                            }
                        }

                    }
                    if (subSensorID.equals("04")) {
                        boolean flag = false;
                        int count = 0;
                        //获取设备最新一条时间
                        String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                        //获取设备间隔时间
                        String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqqx + "");
                        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= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(equipmentTemperatureCache);
                            }
                        }
                    }
                    if (subSensorID.equals("05")) {
                        boolean flag = false;
                        int count = 0;
                        //获取设备最新一条时间
                        String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                        //获取设备间隔时间
                        String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqhw + "");
                        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= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(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 + 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= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(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= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(equipmentTemperatureCache);
                            }
                        }
                    }
                    if (subSensorID.equals("09")) {
                        boolean flag = false;
                        int count = 0;
                        //获取设备最新一条时间
                        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;
                            }
                            //查询记录计算出来值的表
                            List<EquipmentSensorCache> numberList= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(equipmentTemperatureCache);
                            }
                        }
                    }
                    if (subSensorID.equals("10")) {
                        boolean flag = false;
                        int count = 0;
                        //获取设备最新一条时间
                        String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                        //获取设备间隔时间
                        String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqzz + "");
                        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= arresterNumberMapper.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)){
                                    arresterNumberMapper.updateArresterSensor(equipmentTemperatureCache);
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag){
                                equipmentTemperatureCache.setId(Uuid.getUUID());
                                arresterNumberMapper.addArresterSensor(equipmentTemperatureCache);
                            }
                        }
                    }
                }
            }

        }
        return 0;
    }

    @Override
    public List<ArresterStatus> findArresterStatus() {
        List<ArresterStatus> findStatus = arresterNumberMapper.findArresterStatus();
        for (ArresterStatus arresterStatus: findStatus) {
            String findArresterId = arresterStatus.getMainEquipmentId();
            TAndTransformerStatus findTransformerStatus = arresterNumberMapper.findTransformerStatus(findArresterId);
            if(findTransformerStatus != null){
                arresterStatus.setStype(findTransformerStatus.getStype());
            }
            String mainEquipmentId = arresterStatus.getMainEquipmentId();
            List<EquipmentSensorCache> list = arresterNumberService.findArresterSensorNumber(mainEquipmentId);
            for (EquipmentSensorCache sensor: list) {
                if (sensor!=null){
                    if (sensor.getFaultNumber()!=BigDecimal.ZERO){
                        arresterStatus.setPrior("0");
                    }
                    if (sensor.getSensorName()!=null&&sensor.getFaultNumber().equals(BigDecimal.ZERO)){
                        arresterStatus.setPrior("1");
                    }
                }

            }
        }
        return findStatus;
    }

    public static  final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public EquipmentSumNumber findSumArresterNumber() throws Exception{

        //查询所有站点Id
        List<SiteData> findSite = transformerNumberMapper.findSiteData() ;

        EquipmentSumNumber findSumNumber = new EquipmentSumNumber();
        //查詢所有裝置
        List<EquipmentSensor> equipmentList = arresterNumberMapper.findEquipmentVoList();
        boolean flag= false;
        int count = 0;
        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("03")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqtg + "");
                    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.blqqx + "");
                    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.blqhw + "");
                    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("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+",";
                        }
                    }
                }
                if (subSensorID.equals("10")) {
                    //获取设备最新一条时间
                    String newDateValue = equipmentTemperatureCache.getTimeStamp() + "";
                    //获取设备间隔时间
                    String cycle = siteUploadCycleMapper.getCycle(siteId, UploadCycle.ConfigField.blqzz + "");
                    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+",";
                        }
                    }
                }

                //计算避雷器外挂在线数
                findSumNumber.setSumNormal(count);
            }

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

    }


    @Override
    public ArresterHwChaobiaoLv findHwChaobiao(String siteId, String mainEquipmentId, String rtpTime) {
        ArresterHwChaobiaoLv arresterHwChaobiaoLv = arresterNumberMapper.findHwChaobiao(siteId,mainEquipmentId,rtpTime);
        return arresterHwChaobiaoLv;
    }

    @Override
    public List<EquipmentSensorCache> findArresterSensorNumber(String mainEquipmentId) {
        List<EquipmentSensorCache> online= arresterNumberMapper.findArresterSensor(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 EquipmentSumSensorCache findSumArresterSensorNumber() {
        EquipmentSumSensorCache equipmentSumSensorCache = arresterNumberMapper.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;
    }
}
