刘光辉
15 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.limsService;
 
import jnpf.limsMapper.WorkflowTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * enCode → 流程模板 templateId 解析(带进程内缓存)。
 * templateId 是环境相关雪花 id、会随重导模板变化;enCode 稳定,故代码写 enCode、运行时反解 id。
 * 注:缓存为进程内、无失效——若运行期重导/重建流程模板导致 id 变化,需重启服务刷新(属低频运维操作,可接受)。
 */
@Service
public class LimsFlowTemplateResolver {
 
    @Autowired
    private WorkflowTemplateMapper workflowTemplateMapper;
 
    private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
 
    /** 解析 templateId;enCode 为空或查不到返回 null(null 不进缓存,便于模板上架后重试命中)。 */
    public String resolveTemplateId(String enCode) {
        if (enCode == null || enCode.isEmpty()) {
            return null;
        }
        // computeIfAbsent 映射函数返回 null 时不写入 map,天然实现"null 不缓存",且 get-查-put 原子化
        return cache.computeIfAbsent(enCode, k -> {
            String id = workflowTemplateMapper.findTemplateIdByEnCode(k);
            return (id != null && !id.isEmpty()) ? id : null;
        });
    }
}