package org.jeecg.modules.jobXxl;

import com.alibaba.fastjson.JSON;
import com.xxl.job.core.handler.annotation.XxlJob;
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.BuAirNoise;
import org.jeecg.modules.energy.service.IBuAirNoiseService;
import org.jeecg.modules.job.MqttProductPush;
import org.jeecg.modules.sensor.entity.BuSensor;
import org.jeecg.modules.sensor.service.IBuSensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component
public class NoiseDetectionJob {

    @Autowired
    private IBuDeviceService buDeviceService;
    @Autowired
    private IBuSensorService buSensorService;
    @Autowired
    private IBuAirNoiseService buAirNoiseService;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuRiskItemService buRiskItemService;
    @Autowired
    private IBuRiskItemValueService buRiskItemValueService;
    @Autowired
    private MqttProductPush mqttProductPush;


    @XxlJob("NoiseDetectionJob")
    public void executeNoiseDetectionJob() throws Exception {
        try {
            //获取正在运行的传感器id：空气质量表获取状态为1的
            List<BuAirNoise> buAirNoises = buAirNoiseService.getAirNoiseByStatus();
            if(buAirNoises.size()<=0){
                return ;
            }
            for (BuAirNoise buAirNoise : buAirNoises){
                BuSensor buSensor = buSensorService.getById(buAirNoise.getSensorId());
                BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
                noiseHigh(buDevice,buAirNoise);
            }
        }catch (Exception ex){
            System.out.println("异常信息：" + ex);
            throw new Exception("噪声表实时监测失败："+ex.getMessage());
        }
    }

    //噪声监测
    public void noiseHigh(BuDevice buDevice, BuAirNoise buAirNoise){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706128");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        LocalTime currentTime  = LocalTime.now();
        if (currentTime.isBefore(LocalTime.of(6, 0)) || currentTime.isAfter(LocalTime.of(18, 0))) {
            System.out.println("夜间");//55db
            if (buAirNoise.getData() > buRiskItemValue.getValueFour()){
                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(4);
                    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(buAirNoise));
                    //数据推送，并且绑定消息id
                    String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                    buDeviceOnline.setId(annId);
                    //更改风险项
                    buDeviceOnlineService.save(buDeviceOnline);
                    if (buRiskItemValue.getStatus() == 1){
                        buRiskItemValue.setStatus(0);
                        buRiskItemValueService.updateById(buRiskItemValue);
                    }
                }
            }
        } else {
            System.out.println("白天");//70db
            if (buAirNoise.getData() > buRiskItemValue.getValueThree()){
                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(4);//噪声风险
                    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(buAirNoise));
                    //数据推送，并且绑定消息id
                    String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                    buDeviceOnline.setId(annId);
                    //更改风险项
                    buDeviceOnlineService.save(buDeviceOnline);
                    if (buRiskItemValue.getStatus() == 1){
                        buRiskItemValue.setStatus(0);
                        buRiskItemValueService.updateById(buRiskItemValue);
                    }
                }
            }
        }
    }
}
