package org.jeecg.modules.job;

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.BuVibration;
import org.jeecg.modules.energy.service.IBuVibrationService;
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 BatteryVoltageJob implements Job {
    @Autowired
    private IBuDeviceService buDeviceService;
    @Autowired
    private IBuSensorService buSensorService;
    @Autowired
    private IBuStatusService buStatusService;
    @Autowired
    private IBuVibrationService buVibrationService;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuRiskItemService buRiskItemService;
    @Autowired
    private IBuRiskItemValueService buRiskItemValueService;
    @Autowired
    private MqttProductPush mqttProductPush;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            //获取正在运行的传感器id：振动表获取状态为1的
            List<BuVibration> buVibrations = buVibrationService.getVibrationByStatus();
            if(buVibrations.size()<=0){
                return;
            }
            for (BuVibration buVibration : buVibrations){
                BuSensor buSensor = buSensorService.getById(buVibration.getSensorId());
                BuDevice buDevice = buDeviceService.getById(buSensor.getDeviceId());
                batteryVoltage(buDevice,buVibration);

            }
        }catch (Exception ex){
            System.out.println("异常信息：" + ex);
        }
    }

    //电池欠压检测
    public void batteryVoltage(BuDevice buDevice, BuVibration buVibration){
        BuRiskItem buRiskItem = buRiskItemService.getById("566706131");
        BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(buDevice.getId(),buRiskItem.getId());
        if (!Objects.equals(buRiskItemValue.getEnabled(), "0")){
            return;
        }
        if (buVibration.getVoltage() < buRiskItemValue.getMultiple()){
            //创建新的风险信息
            BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
            buDeviceOnline.setRemind(buRiskItem.getRiskExplain());
            //数据推送，并且绑定消息id
            mqttProductPush.dataPushBattery(buDevice,buDeviceOnline.getRemind());
        }
    }
}
