Lius
2025-03-12 85874be1abe13d027b85a4c7ad0c341c39c4ff31
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
package org.jeecg.modules.mdc.job;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.mdc.dto.MdcEquipmentWaitSectionDto;
import org.jeecg.modules.mdc.entity.MdcDowntime;
import org.jeecg.modules.mdc.entity.MdcEquipmentOvertime;
import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection;
import org.jeecg.modules.mdc.service.IMdcDowntimeService;
import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService;
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.quartz.*;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Author: Lius
 * @CreateTime: 2025-03-12
 * @Description: 扫描停机任务
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class ScanDowntimeJob implements Job {
 
    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改  时间: yyyyMMdd 例: 20230414
     */
    private String parameter;
 
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
 
    @Resource
    private IQuartzJobService quartzJobService;
 
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
 
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
 
    @Resource
    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
 
    @Resource
    private IMdcDowntimeService mdcDowntimeService;
 
    @Override
    public void execute(JobExecutionContext context) 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());
        }
        quartzLog.setParams(this.parameter);
        log.info("定时扫描待机时长超20分钟设备任务 ScanDowntimeJob start!  时间:{}, 参数:{}", DateUtils.getNow(), this.parameter);
        long startTime = System.currentTimeMillis();
        try {
            String date = "";
            if (StringUtils.isNotBlank(this.parameter)) {
                date = DateUtils.format(DateUtils.toDate(this.parameter, DateUtils.STRDATE), DateUtils.STR_DATE);
 
            } else {
                date = DateUtils.format(DateUtils.getNow(), DateUtils.STR_DATE);
            }
            mdcDowntimeService.remove(new LambdaQueryWrapper<MdcDowntime>().eq(MdcDowntime::getTheDate, date).eq(MdcDowntime::getStatus, CommonConstant.DOWNTIME_STATUS_0));
 
            List<MdcEquipmentWaitSectionDto> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.findWaitList(date);
 
            if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) {
                String finalDate = date;
                List<MdcDowntime> downtimeList = mdcEquipmentRunningSections.stream().map(mdcEquipmentWaitSectionDto -> {
                    MdcDowntime downtime = new MdcDowntime();
                    downtime.setEquipmentId(mdcEquipmentWaitSectionDto.getEquipmentId());
                    downtime.setEquipmentName(mdcEquipmentWaitSectionDto.getEquipmentName());
                    downtime.setTheDate(finalDate);
                    downtime.setStartDate(mdcEquipmentWaitSectionDto.getStartTime());
                    downtime.setEndDate(mdcEquipmentWaitSectionDto.getEndTime());
                    return downtime;
                }).collect(Collectors.toList());
                if (!downtimeList.isEmpty()) {
                    mdcDowntimeService.saveBatch(downtimeList);
                }
            }
            quartzLog.setIsSuccess(0);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
            // 发送消息通知
            sysAnnouncementService.jobSendMessage("定时扫描待机时长超20分钟设备任务", quartzLog.getExceptionDetail());
        }
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
        sysQuartzLogService.save(quartzLog);
    }
}