package org.jeecg.modules.jobXxl;

import com.alibaba.fastjson.JSON;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.ApiModelProperty;
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.IBuRiskItemService;
import org.jeecg.modules.device.service.IBuRiskItemValueService;
import org.jeecg.modules.job.MqttProductPush;
import org.jeecg.modules.under.entity.Under;
import org.jeecg.modules.under.service.IUnderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class DrivingMachineJob {

    @Autowired
    private IUnderService underService;
    @Autowired
    private MqttProductPush mqttProductPush;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuRiskItemService buRiskItemService;
    @Autowired
    private IBuRiskItemValueService buRiskItemValueService;


    @XxlJob("DrivingMachineJob")
    public void executeDrivingMachineJob() throws Exception{
        Class<Under> orderDtoClass = Under.class;
        Field[] declaredFields = orderDtoClass.getDeclaredFields();
        Under under = underService.list().get(0);

        try {
            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("地坑架车机");
                    buDeviceOnline.setTime(new Date());
                    buDeviceOnline.setRemindType(2);
                    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("设备故障"));

                    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 + "报警");
                    }
                }
            }
        }catch (Exception e){
            System.out.println("异常信息：" + e);
            throw new Exception("驾车机实时监测失败："+e.getMessage());
        }
    }
}
