package org.jeecg.modules.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.constant.CommonSendStatus;
import org.jeecg.common.constant.WebsocketConst;
import org.jeecg.modules.device.entity.BuDevice;
import org.jeecg.modules.device.entity.BuDeviceOnline;
import org.jeecg.modules.device.entity.BuRiskItem;
import org.jeecg.modules.device.entity.BuRiskItemValue;
import org.jeecg.modules.device.service.IBuDeviceOnlineService;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.device.service.IBuRiskItemService;
import org.jeecg.modules.device.service.IBuRiskItemValueService;
import org.jeecg.modules.energy.entity.BuElectric;
import org.jeecg.modules.energy.service.IBuElectricService;
import org.jeecg.modules.message.websocket.WebSocket;
import org.jeecg.modules.sensor.entity.BuSensor;
import org.jeecg.modules.sensor.service.IBuSensorService;
import org.jeecg.modules.sensor.service.IBuStatusService;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.graphbuilder.org.apache.harmony.awt.gl.Crossing.isZero;

public class ElectricDetectionJob implements Job {

    @Autowired
    private IBuDeviceService buDeviceService;
    @Autowired
    private IBuSensorService buSensorService;
    @Autowired
    private IBuStatusService buStatusService;
    @Autowired
    private IBuElectricService buElectricService;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuRiskItemService buRiskItemService;
    @Autowired
    private IBuRiskItemValueService buRiskItemValueService;
    @Autowired
    private MqttProductPush mqttProductPush;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(JobExecutionContext jobExecutionContext) {

        //获取正在运行的传感器id：电能表获取状态为2的
        List<BuElectric> buElectrics = buElectricService.getElectricByStatus();
        if(buElectrics.size()<=0){
            return;
        }
        for (BuElectric buElectric : buElectrics){
            BuSensor buSensor = buSensorService.getById(buElectric.getSensorId());
            BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
            overvoltageU(buDevice,buElectric);
            overvoltageV(buDevice,buElectric);
            overvoltageW(buDevice,buElectric);
            undervoltageU(buDevice,buElectric);
            undervoltageV(buDevice,buElectric);
            undervoltageW(buDevice,buElectric);
            overloadU(buDevice,buElectric);
            overloadV(buDevice,buElectric);
            overloadW(buDevice,buElectric);
            voltagePhaseLossU(buDevice,buElectric);
            voltagePhaseLossV(buDevice,buElectric);
            voltagePhaseLossW(buDevice,buElectric);
            electricPhaseLossU(buDevice,buElectric);
            electricPhaseLossV(buDevice,buElectric);
            electricPhaseLossW(buDevice,buElectric);
            voltageImbalanceU(buDevice,buElectric);
            voltageImbalanceV(buDevice,buElectric);
            voltageImbalanceW(buDevice,buElectric);
            if(!isZero(buElectric.getUdl()) && !isZero(buElectric.getVdl()) && !isZero(buElectric.getWdl())){
                electricImbalanceU(buDevice,buElectric);
                electricImbalanceV(buDevice,buElectric);
                electricImbalanceW(buDevice,buElectric);
            }
//            System.out.println("结束时间:" + new Date());
        }
    }

    //U项过压检测——传入udy值
    public void overvoltageU(BuDevice buDevice, BuElectric buElectric){
        //搜索风险项id
        //根据udy和methodName确定唯一的风险项id，或可写死使用固定的id=566706124
        BuRiskItem buRiskItem = buRiskItemService.getById("566706124");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getUdy() > buRiskItemValue.getMultiple() * 1.1){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);//电能表风险
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //V项过压检测——传入vdy值
    public void overvoltageV(BuDevice buDevice, BuElectric buElectric){
        //根据vdy和methodName确定唯一的风险项id，或可写死使用固定的id=566706122
        BuRiskItem buRiskItem = buRiskItemService.getById("566706122");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getVdy() > buRiskItemValue.getMultiple() * 1.1){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //W项过压检测——传入wdy值
    public void overvoltageW(BuDevice buDevice, BuElectric buElectric){
        //根据wdy和methodName确定唯一的风险项id，或可写死使用固定的id=566706120
        BuRiskItem buRiskItem = buRiskItemService.getById("566706120");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getWdy() > buRiskItemValue.getMultiple() * 1.1){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //U项欠压检测——传入udy值
    public void undervoltageU(BuDevice buDevice, BuElectric buElectric){
        //根据udy和methodName确定唯一的风险项id，或可写死使用固定的id=566706123
        BuRiskItem buRiskItem = buRiskItemService.getById("566706123");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getUdy() < buRiskItemValue.getMultiple() * 0.9){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //V项欠压检测——传入vdy值
    public void undervoltageV(BuDevice buDevice, BuElectric buElectric){
        //根据vdy和methodName确定唯一的风险项id，或可写死使用固定的id=566706121
        BuRiskItem buRiskItem = buRiskItemService.getById("566706121");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getVdy() < buRiskItemValue.getMultiple() * 0.9){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //W项欠压检测——传入wdy值
    public void undervoltageW(BuDevice buDevice, BuElectric buElectric){
        //根据wdy和methodName确定唯一的风险项id，或可写死使用固定的id=566706119
        BuRiskItem buRiskItem = buRiskItemService.getById("566706119");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getWdy() < buRiskItemValue.getMultiple() * 0.9){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //U项过载检测——传入udl值
    public void overloadU(BuDevice buDevice, BuElectric buElectric){
        //根据udl和methodName确定唯一的风险项id，或可写死使用固定的id=566706127
        BuRiskItem buRiskItem = buRiskItemService.getById("566706127");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getUdl() > buRiskItemValue.getMultiple() * 1.2){//暂时判断电压超过运行电流的1.2倍，1.2可化为数据库变量
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //V项过载检测——传入vdl值
    public void overloadV(BuDevice buDevice, BuElectric buElectric){
        //根据vdl和methodName确定唯一的风险项id，或可写死使用固定的id=566706126
        BuRiskItem buRiskItem = buRiskItemService.getById("566706126");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getVdl() > buRiskItemValue.getMultiple() * 1.2){//暂时判断电压超过运行电流的1.2倍，1.2可化为数据库变量
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //W项过载检测——传入wdl值
    public void overloadW(BuDevice buDevice, BuElectric buElectric){
        //根据wdl和methodName确定唯一的风险项id，或可写死使用固定的id=566706125
        BuRiskItem buRiskItem = buRiskItemService.getById("566706125");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getWdl() > buRiskItemValue.getMultiple() * 1.2){//暂时判断电压超过运行电流的1.2倍，1.2可化为数据库变量
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //U项电压缺相检测——传入udy值
    public void voltagePhaseLossU(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706118");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getUdy() < buRiskItemValue.getMultiple()){//2处于运行状态的设备，电压小于2即判断为缺相
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //V项电压缺相检测——传入vdy值
    public void voltagePhaseLossV(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706117");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getVdy() < buRiskItemValue.getMultiple()){//2处于运行状态的设备，电压小于2即判断为缺相
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //W项电压缺相检测——传入wdy值
    public void voltagePhaseLossW(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706116");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getWdy() < buRiskItemValue.getMultiple()){//2处于运行状态的设备，电压小于2即判断为缺相
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //U项电流缺相检测——传入udl值
    public void electricPhaseLossU(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706115");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getUdl().equals(buRiskItemValue.getMultiple()) ){//0处于运行状态的设备，电流=0即判断为缺相
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //V项电流缺相检测——传入vdl值
    public void electricPhaseLossV(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706114");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getVdl().equals(buRiskItemValue.getMultiple())){//0处于运行状态的设备，电流=0即判断为缺相
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //W项电流缺相检测——传入wdl值
    public void electricPhaseLossW(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706113");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buElectric.getWdl().equals(buRiskItemValue.getMultiple())){//0处于运行状态的设备，电流=0即判断为缺相
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //设备U相电压不平衡检测
    public void voltageImbalanceU(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706109");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        double value = (Math.pow(buElectric.getUdy(),2) + Math.pow(buElectric.getVdy(),2) + Math.pow(buElectric.getWdy(),2)) / 3;
        double result = Math.sqrt(value) / buElectric.getUdy();
        if (result < buRiskItemValue.getValueOne()/100 || result > buRiskItemValue.getValueTwo()/100){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //设备V相电压不平衡检测
    public void voltageImbalanceV(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706108");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        double value = (Math.pow(buElectric.getUdy(),2) + Math.pow(buElectric.getVdy(),2) + Math.pow(buElectric.getWdy(),2)) / 3;
        double result = Math.sqrt(value) / buElectric.getVdy();
        if (result < buRiskItemValue.getValueOne()/100 || result > buRiskItemValue.getValueTwo()/100){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //设备W相电压不平衡检测
    public void voltageImbalanceW(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706107");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        double value = (Math.pow(buElectric.getUdy(),2) + Math.pow(buElectric.getVdy(),2) + Math.pow(buElectric.getWdy(),2)) / 3;
        double result = Math.sqrt(value) / buElectric.getWdy();
        if (result < buRiskItemValue.getValueOne()/100 || result > buRiskItemValue.getValueTwo()/100){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //设备U相电流不平衡检测
    public void electricImbalanceU(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706112");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        double value = (Math.pow(buElectric.getUdl(),2) + Math.pow(buElectric.getVdl(),2) + Math.pow(buElectric.getWdl(),2)) / 3;
        double result = Math.sqrt(value) / buElectric.getUdl();
        if (result < buRiskItemValue.getValueOne()/100 || result > buRiskItemValue.getValueTwo()/100){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //设备V相电流不平衡检测
    public void electricImbalanceV(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706111");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        double value = (Math.pow(buElectric.getUdl(),2) + Math.pow(buElectric.getVdl(),2) + Math.pow(buElectric.getWdl(),2)) / 3;
        double result = Math.sqrt(value) / buElectric.getVdl();
        if (result < buRiskItemValue.getValueOne()/100 || result > buRiskItemValue.getValueTwo()/100){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

    //设备W相电流不平衡检测
    public void electricImbalanceW(BuDevice buDevice, BuElectric buElectric){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706110");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        double value = (Math.pow(buElectric.getUdl(),2) + Math.pow(buElectric.getVdl(),2) + Math.pow(buElectric.getWdl(),2)) / 3;
        double result = Math.sqrt(value) / buElectric.getWdl();
        if (result < buRiskItemValue.getValueOne()/100 || result > buRiskItemValue.getValueTwo()/100){
            List<BuDeviceOnline> oldData = buDeviceOnlineService.getLastTimeData(buDevice.getId(),buRiskItem.getRiskName());
            if (oldData.size() == 0){
                //创建新的风险信息
                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(buDevice.getId());
                buDeviceOnline.setDeviceName(buDevice.getDeviceName());
                buDeviceOnline.setDeviceCode(buDevice.getDeviceCode());
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(1);
                buDeviceOnline.setTestItem(buRiskItem.getRiskName());
                buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
                buDeviceOnline.setStatus(0);
                buDeviceOnline.setRiskExplain(buRiskItem.getRiskExplain());
                buDeviceOnline.setRiskHarm(buRiskItem.getRiskHarm());
                buDeviceOnline.setSuggest(buRiskItem.getDealAdvice());
                buDeviceOnline.setRiskData(JSON.toJSONString(buElectric));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

}
