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
package cn.iocoder.yudao.module.mes.service.collect;
 
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.task.MesProTaskDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.task.MesProTaskMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
 
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * MES 数采-生产任务关联解析
 *
 * @author 超级管理员
 */
@Service
public class MesCollectTaskResolver {
 
    private static final long CACHE_MILLIS = 30_000L;
 
    @Resource
    private MesProTaskMapper taskMapper;
 
    private final ConcurrentHashMap<Long, CachedTask> cache = new ConcurrentHashMap<>();
 
    /**
     * 查询工作站上正在运行的生产任务(生产中/部分完成/已停工)
     */
    public MesProTaskDO resolve(Long workstationId) {
        if (workstationId == null) {
            return null;
        }
        long now = System.currentTimeMillis();
        CachedTask cached = cache.get(workstationId);
        if (cached != null && now - cached.time < CACHE_MILLIS) {
            return cached.task;
        }
        MesProTaskDO task = taskMapper.selectRunningTaskByWorkstationId(workstationId);
        cache.put(workstationId, new CachedTask(task, now));
        return task;
    }
 
    private record CachedTask(MesProTaskDO task, long time) {
    }
 
}