package org.jeecg.modules.job;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import org.jeecg.config.mqtt.MqttMessageEvent;
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.service.IBuAirQualityService;
import org.jeecg.modules.sensor.service.IBuSensorService;
import org.jeecg.modules.sensor.service.IBuStatusService;
import org.jeecg.modules.toolBorrowingRecord.mapper.ToolBorrowingRecordMapper;
import org.jeecg.modules.under.entity.Under;
import org.jeecg.modules.under.service.IUnderService;
import org.jeecg.modules.workOrder.mapper.WorkOrderMapper;
import org.jeecg.modules.workOrder.mapper.WorkRecordMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;

import java.lang.reflect.Field;
import java.util.Date;


/**
 * 地坑架车机报警提示:
 *
 * @author: chen
 * @date: 2025/2/8
 */
public class DrivingMachineJob implements Job {
    @Autowired
    private IUnderService underService;
    @Autowired
    private MqttProductPush mqttProductPush;


    @Autowired
    private IBuDeviceService buDeviceService;
    @Autowired
    private IBuSensorService buSensorService;
    @Autowired
    private IBuStatusService buStatusService;
    @Autowired
    private IBuAirQualityService buAirQualityService;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuRiskItemService buRiskItemService;
    @Autowired
    private IBuRiskItemValueService buRiskItemValueService;


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Class<Under> orderDtoClass = Under.class;
        Field[] declaredFields = orderDtoClass.getDeclaredFields();
        Under under = underService.list().get(0);

        for (Field declaredField : declaredFields) {
            Field field = null;
            try {
                field = under.getClass().getDeclaredField(declaredField.getName());
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            }
            field.setAccessible(true);
            Object declaredFieldValue = null;
            try {
                declaredFieldValue = field.get(under);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            if(declaredFieldValue!=null && declaredFieldValue.equals("true")){
                boolean annotation = declaredField.isAnnotationPresent(ApiModelProperty.class);


                BuRiskItem buRiskItem = buRiskItemService.getById("566706132");
                BuRiskItemValue buRiskItemValue = buRiskItemValueService.getDataByTwoId(under.getId(),buRiskItem.getId());

                BuDeviceOnline buDeviceOnline = new BuDeviceOnline();
                buDeviceOnline.setDeviceId(under.getId());
                buDeviceOnline.setDeviceName("地坑架车机");
                buDeviceOnline.setDeviceCode("468996012");
                buDeviceOnline.setTime(new Date());
                buDeviceOnline.setRemindType(9);
                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(under));

                BuDevice buDevice = new BuDevice();
                //数据推送，并且绑定消息id
                String annId = mqttProductPush.dataPush(buDevice,buDeviceOnline.getRemind());//获取故障信息
                buDeviceOnline.setId(annId);
                //推送到mqtt
                mqttProductPush.JobPush(buDeviceOnline);
                //更改风险项
                buDeviceOnlineService.save(buDeviceOnline);
                if (buRiskItemValue.getStatus() == 1){
                    buRiskItemValue.setStatus(0);
                    buRiskItemValueService.updateById(buRiskItemValue);
                }

                if (annotation) {
                    String value = declaredField.getAnnotation(ApiModelProperty.class).value();
                    mqttProductPush.dataPushJCJ(value+"报警");
                }
            }
        }
    }
}
