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.BuAirWater;
import org.jeecg.modules.energy.entity.BuDipAngle;
import org.jeecg.modules.energy.service.IBuAirWaterService;
import org.jeecg.modules.energy.service.IBuDipAngleService;
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.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class DipAngleJob implements Job{

    @Autowired
    private IBuDeviceService buDeviceService;
    @Autowired
    private IBuSensorService buSensorService;
    @Autowired
    private IBuStatusService buStatusService;
    @Autowired
    private IBuDipAngleService buDipAngleService;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuRiskItemService buRiskItemService;
    @Autowired
    private IBuRiskItemValueService buRiskItemValueService;
    @Autowired
    private MqttProductPush mqttProductPush;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            //获取正在运行的传感器id：获取状态为1(开启)的倾角传感器
            List<BuDipAngle> buDipAngleList = buDipAngleService.getDipAngleByStatus();
            if(buDipAngleList.size()<=0){
                return;
            }
            for (BuDipAngle buDipAngle : buDipAngleList){
                BuSensor buSensor = buSensorService.getById(buDipAngle.getSensorId());
                BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
                buDipAngleHigh(buDevice,buDipAngle);
            }
        }catch (Exception ex){
            System.out.println("异常信息：" + ex);
        }
    }
    //倾角报警推送
    public void buDipAngleHigh(BuDevice buDevice, BuDipAngle buDipAngle){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706104");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buDipAngle.getXAngle() > buRiskItemValue.getMultiple()){
            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(8);
                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(buDipAngle));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
        if (buDipAngle.getYAngle() > buRiskItemValue.getValueOne()){
            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(8);
                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(buDipAngle));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
        if (buDipAngle.getZAngle() > buRiskItemValue.getValueTwo()){
            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(8);
                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(buDipAngle));
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());
                buDeviceOnline.setId(annId);
                buDeviceOnlineService.save(buDeviceOnline);
                //更改风险项
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }
            }
        }
    }

}
