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.BuAirWater;
import org.jeecg.modules.energy.service.IBuAirWaterService;
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.util.Date;
import java.util.List;
import java.util.Objects;

@Component
public class WaterDetectionJob {

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

    @XxlJob("WaterDetectionJob")
    public void executeWaterDetectionJob() throws Exception{
        try {
            //获取正在运行的传感器id：空气质量表获取状态为1的
            List<BuAirWater> buAirWaters = buAirWaterService.getAirWaterByStatus();
            if(buAirWaters.size()<=0){
                return ;
            }
            for (BuAirWater buAirWater : buAirWaters){
                BuSensor buSensor = buSensorService.getById(buAirWater.getSensorId());
                BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
                waterHigh(buDevice,buAirWater);
            }
        }catch (Exception ex){
            System.out.println("异常信息：" + ex);
            throw new Exception("水浸监控任务执行失败："+ex.getMessage());
        }
    }
    //地坑积水报警
    public void waterHigh(BuDevice buDevice, BuAirWater buAirWater){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706130");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buAirWater.getData1() > buRiskItemValue.getMultiple() ||buAirWater.getData1()==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(6);
                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(buAirWater));
                //数据推送，并且绑定消息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);
                }
            }
        }
    }
}
