cuijian
2025-06-16 ec1bf4658e36a17f971a54007920a44c5378b7dc
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
 
package org.jeecg.modules.mdc.job;
 
import cn.hutool.core.date.DatePattern;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.mdc.entity.MdcEquipmentPunch;
import org.jeecg.modules.mdc.service.IMdcEquipmentPunchRateService;
import org.jeecg.modules.mdc.service.IMdcEquipmentPunchService;
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.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
 
/**
 * @Description: 每日凌晨定时计算前一天的设备打卡率,并入库
 * @Author: Lius
 * @CreateTime: 2025-06-12
 */
@Slf4j
public class DailyPunchRateJob implements Job {
 
    private String parameter; // 可选参数:指定日期(如 "2025-06-11")
 
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
 
    @Resource
    private IQuartzJobService quartzJobService;
 
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
 
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
 
    @Resource
    private IMdcEquipmentPunchService mdcEquipmentPunchService;
 
 
    @Resource
    private IMdcEquipmentPunchRateService mdcEquipmentPunchRateService;
 
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SysQuartzLog quartzLog = new SysQuartzLog();
        quartzLog.setCreateTime(new Date());
 
        List<QuartzJob> jobList = this.quartzJobService.findByJobClassName(this.getClass().getName());
        if (jobList != null && !jobList.isEmpty()) {
            quartzLog.setJobId(jobList.get(0).getId());
        }
 
        log.info("【开始执行每日设备打卡率统计任务】");
 
        long startTime = System.currentTimeMillis();
 
        try {
            String yesterdayStr;
 
 
 
            if (parameter != null && !parameter.isEmpty()) {
                yesterdayStr = parameter; // 支持手动传参
            } else {
                LocalDate yesterday = LocalDate.now().minusDays(1);
                 yesterdayStr = yesterday.format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATE_PATTERN)); // 格式化为 "yyyy-MM-dd"
            }
            log.info("✅ 成功完成每日设备打卡率统计数据", yesterdayStr);
            // Step 1:获取昨日打卡数据
            List<MdcEquipmentPunch> punchRecords = mdcEquipmentPunchService.getYesterdayRecords(yesterdayStr);
            log.info("✅ 成功完成每日设备打卡率统计数据", punchRecords);
            if (punchRecords == null || punchRecords.isEmpty()) {
                log.warn("⚠️ 没有找到昨日设备打卡数据");
                quartzLog.setIsSuccess(0);
                return;
            }
 
            // Step 2:保存到打卡率表
            mdcEquipmentPunchRateService.savePunchRates(yesterdayStr, punchRecords);
 
            quartzLog.setIsSuccess(0);
            log.info("✅ 成功完成每日设备打卡率统计,共处理 {} 条记录,日期:{}", punchRecords.size(), yesterdayStr);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(e.getMessage());
            log.error("❌ 设备打卡率统计任务执行失败", e);
            sysAnnouncementService.jobSendMessage("设备打卡率统计任务", e.getMessage());
        }
 
        // 记录执行时间
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime((int)(endTime - startTime));
        sysQuartzLogService.save(quartzLog);
    }
 
}