Lius
2024-01-27 3519b2bf0f97d5017f97447b6eafc2b4bd9487c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.jeecg.modules.mdc.job;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.MdcEfficiencyReportService;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.util.ThrowableUtil;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.entity.SysQuartzLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ISysQuartzLogService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.service.ISysDictService;
import org.quartz.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
 
/**
 * @author Lius
 * @Description: 利用率正常值,设备状态长期无变化报警
 * @date 2024/1/17 14:10
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class RunningEquipmentStatusJob implements Job {
 
    @Resource
    private IQuartzJobService quartzJobService;
 
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
 
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
 
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
 
    @Resource
    private ISysDictService sysDictService;
 
    @Resource
    private MdcEfficiencyReportService mdcEfficiencyReportService;
 
    @Resource
    private ISysBaseAPI sysBaseApi;
 
    @Resource
    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
 
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SysQuartzLog quartzLog = new SysQuartzLog();
        quartzLog.setCreateTime(new Date());
        List<QuartzJob> byJobClassName = this.quartzJobService.findByJobClassName(this.getClass().getName());
        if (byJobClassName != null && !byJobClassName.isEmpty()) {
            quartzLog.setJobId(byJobClassName.get(0).getId());
        }
        log.info("设备状态长期无变化报警任务 RunningEquipmentStatusJob start!  时间:" + DateUtils.getNow());
        long startTime = System.currentTimeMillis();
        try {
            List<MdcEquipment> equipmentList = mdcEquipmentService.list(new LambdaQueryWrapper<>());
            //List<MdcEquipment> equipmentList = mdcEquipmentService.list(new LambdaQueryWrapper<MdcEquipment>().eq(MdcEquipment::getEquipmentId, "2140198"));
            // 获取利用率判定天数
            List<DictModel> dictModelList1 = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_EQUIPMENT_RATE_JUDGE);
            Integer equipmentRateJudge = 5;
            if (dictModelList1 != null && !dictModelList1.isEmpty()) {
                equipmentRateJudge = Integer.valueOf(dictModelList1.get(0).getValue());
            }
            // 获取利用率正常值
            List<DictModel> dictModelList2 = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_EQUIPMENT_RATE_NORMAL);
            Integer equipmentRateNormal = 20;
            if (dictModelList2 != null && !dictModelList2.isEmpty()) {
                equipmentRateNormal = Integer.valueOf(dictModelList2.get(0).getValue());
            }
            // 获取设备状态天数
            List<DictModel> dictModelList3 = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_EQUIPMENT_SPEED_JUDGE);
            Integer equipmentStatusJudge = 5;
            if (dictModelList3 != null && !dictModelList3.isEmpty()) {
                equipmentStatusJudge = Integer.valueOf(dictModelList3.get(0).getValue());
            }
            for (MdcEquipment mdcEquipment : equipmentList) {
                // 判断利用率
                String date = DateUtils.format(DateUtils.toDate(LocalDate.now().plusDays(-equipmentRateJudge).toString(), DateUtils.STR_DATE), DateUtils.STRDATE);
                List<BigDecimal> efficiencyRateList = mdcEfficiencyReportService.getEfficiencyRate(mdcEquipment.getEquipmentId(), date);
                if (efficiencyRateList != null && !efficiencyRateList.isEmpty()) {
                    boolean flag = true;
                    for (BigDecimal processLong : efficiencyRateList) {
                        BigDecimal efficiencyRate = processLong.divide(new BigDecimal("86400"), 4, RoundingMode.HALF_UP);
                        if (efficiencyRate.compareTo(new BigDecimal(equipmentRateNormal)) > -1) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        // 上报
                        MessageDTO messageDTO = new MessageDTO();
                        messageDTO.setTitle("设备利用率报警!");
                        messageDTO.setCategory("预警消息");
                        messageDTO.setFromUser("admin");
                        messageDTO.setToUser("admin");
                        messageDTO.setContent("设备编号为 [" + mdcEquipment.getEquipmentId() + "] 的设备利用率低于正常值报警!");
                        sysBaseApi.sendSysAnnouncement(messageDTO);
                    }
                }
                // 判断设备状态
                Date date1 = DateUtils.toDate(LocalDate.now().plusDays(-equipmentStatusJudge).toString(), DateUtils.STR_DATE);
                List<Integer> sectionList = mdcEquipmentRunningSectionService.getDataList(mdcEquipment.getEquipmentId(), date1);
                if (sectionList != null && !sectionList.isEmpty() && sectionList.size() > 1) {
                    boolean flag = true;
                    Integer integer = sectionList.get(0);
                    for (Integer integer1 : sectionList) {
                        if (!integer.equals(integer1)) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        // 上报
                        MessageDTO messageDTO = new MessageDTO();
                        messageDTO.setTitle("设备状态长期无变化报警!");
                        messageDTO.setCategory("预警消息");
                        messageDTO.setFromUser("admin");
                        messageDTO.setToUser("admin");
                        messageDTO.setContent("设备编号为 [" + mdcEquipment.getEquipmentId() + "] 的设备状态长期无变化报警!");
                        sysBaseApi.sendSysAnnouncement(messageDTO);
                    }
                }
            }
            quartzLog.setIsSuccess(0);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
            // 发送消息通知
            sysAnnouncementService.jobSendMessage("设备状态长期无变化报警任务失败!", quartzLog.getExceptionDetail());
        }
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
        sysQuartzLogService.save(quartzLog);
    }
}