|
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);
|
}
|
|
}
|