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