3 天以前 b852254d90e7d1955db31db154193ec8ee84d8b3
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
package cn.iocoder.yudao.module.mes.job.wm;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.stocktaking.plan.MesWmStockTakingPlanDO;
import cn.iocoder.yudao.module.mes.service.wm.stocktaking.plan.MesWmStockTakingPlanService;
import cn.iocoder.yudao.module.mes.service.wm.stocktaking.task.MesWmStockTakingTaskService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronExpression;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * MES 定期盘点自动生成 Job
 *
 * <p>扫描启用且开启定期自动生成的盘点方案,按各方案配置的生成周期 cron 判断是否到期,
 * 到期则按方案自动创建盘点任务,避免人工逐期建单。后台调度 cron 建议配置为每分钟执行一次。
 *
 * @author 超级管理员
 */
@Component
@Slf4j
public class MesWmStockTakingPlanAutoJob implements JobHandler {
 
    @Resource
    private MesWmStockTakingPlanService stockTakingPlanService;
    @Resource
    private MesWmStockTakingTaskService stockTakingTaskService;
 
    @Override
    @TenantJob
    public String execute(String param) {
        // 1. 获取启用且开启定期自动生成的方案
        List<MesWmStockTakingPlanDO> plans = stockTakingPlanService.getEnableAutoGeneratePlanList();
        if (CollUtil.isEmpty(plans)) {
            return "定期盘点:无启用的自动生成方案,跳过";
        }
 
        // 2. 逐个方案判断周期是否到期,到期则自动生成任务
        Date now = new Date();
        int successCount = 0;
        List<String> errors = new ArrayList<>();
        for (MesWmStockTakingPlanDO plan : plans) {
            try {
                if (shouldGenerate(plan, now)) {
                    Long taskId = stockTakingTaskService.createStockTakingTaskFromPlan(plan.getId());
                    successCount++;
                    log.info("[execute] 方案({}) 自动生成盘点任务({}) 成功", plan.getId(), taskId);
                }
            } catch (Exception e) {
                log.error("[execute] 方案({}) 自动生成盘点任务失败", plan.getId(), e);
                errors.add(String.format("方案[%s]:%s", plan.getName(), e.toString()));
            }
        }
 
        // 3. 汇总执行结果
        String msg = String.format("定期盘点:扫描 %s 个方案,本次生成 %s 个任务", plans.size(), successCount);
        if (CollUtil.isNotEmpty(errors)) {
            msg += ",失败明细:" + String.join(";", errors);
        }
        return msg;
    }
 
    /**
     * 判断方案当前周期是否到期
     *
     * <p>从未生成过则仅记录基线时间并从下一周期开始触发(刚启用不立即生成);
     * 否则按「上次生成时间 + cron」计算下一次触发时间,落在当前时间之前即为到期。
     *
     * @param plan 盘点方案
     * @param now  当前时间
     * @return 是否到期
     */
    private boolean shouldGenerate(MesWmStockTakingPlanDO plan, Date now) {
        CronExpression cron;
        try {
            cron = new CronExpression(plan.getGenerateCron());
        } catch (Exception e) {
            log.warn("[shouldGenerate] 方案({}) cron 表达式({}) 不合法,跳过",
                    plan.getId(), plan.getGenerateCron());
            return false;
        }
        LocalDateTime lastTime = plan.getLastGenerateTime();
        if (lastTime == null) {
            // 首次扫描:仅记录基线时间,不立即生成
            stockTakingPlanService.updateStockTakingPlanLastGenerateTime(plan.getId(), LocalDateTime.now());
            return false;
        }
        Date last = Date.from(lastTime.atZone(ZoneId.systemDefault()).toInstant());
        Date next = cron.getNextValidTimeAfter(last);
        return next != null && !next.after(now);
    }
 
}