package jnpf.elnService.impl; import jnpf.elnEntity.runtime.ElnRecordEntity; import jnpf.elnService.ElnTestRequestIdentityLookup; import jnpf.elnService.ElnRecordSourceCandidateStore; import jnpf.elnService.ElnRecordSourceResolver; import jnpf.exception.DataException; import org.springframework.util.StringUtils; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Arrays; @Service public class ElnRecordSourceResolverImpl implements ElnRecordSourceResolver { private static final List ALLOWED_SOURCE_STATUSES = Arrays.asList("pending_audit", "audited"); private final ElnRecordSourceCandidateStore candidateStore; private final ElnTestRequestIdentityLookup requestIdentityLookup; public ElnRecordSourceResolverImpl(ElnRecordSourceCandidateStore candidateStore, ElnTestRequestIdentityLookup requestIdentityLookup) { this.candidateStore = candidateStore; this.requestIdentityLookup = requestIdentityLookup; } @Override public ElnRecordEntity resolve(String tenantId, String testRequestId, String sourceTemplateCode) { String actualTenantId = required(tenantId, "SOURCE_RECORD_QUERY_INVALID: tenantId"); String actualRequestId = required(testRequestId, "SOURCE_RECORD_QUERY_INVALID: testRequestId"); String actualTemplateCode = required(sourceTemplateCode, "SOURCE_RECORD_QUERY_INVALID: sourceTemplateCode"); List candidates = candidateStore.list(actualTenantId, actualTemplateCode); if (candidates == null || candidates.isEmpty()) { throw notFound(actualRequestId, actualTemplateCode); } List tenantCandidates = new ArrayList<>(); List testWorkItemIds = new ArrayList<>(); for (ElnRecordEntity candidate : candidates) { if (candidate == null || !actualTenantId.equals(candidate.getFTenantId()) || !actualTemplateCode.equals(candidate.getMubanBianhao()) || !StringUtils.hasText(candidate.getJianyanXiangId())) { continue; } tenantCandidates.add(candidate); testWorkItemIds.add(candidate.getJianyanXiangId()); } if (tenantCandidates.isEmpty()) { throw notFound(actualRequestId, actualTemplateCode); } Map identities = requestIdentityLookup.resolve( actualTenantId, testWorkItemIds); if (identities == null) identities = Collections.emptyMap(); List matches = new ArrayList<>(); List invalidStateMatches = new ArrayList<>(); for (ElnRecordEntity candidate : tenantCandidates) { ElnTestRequestIdentityLookup.Identity identity = identities.get(candidate.getJianyanXiangId()); if (identity == null || !actualRequestId.equals(identity.getTestRequestId())) continue; if (ALLOWED_SOURCE_STATUSES.contains(identity.getTaskStatus())) matches.add(candidate); else invalidStateMatches.add(candidate); } if (matches.isEmpty() && !invalidStateMatches.isEmpty()) { throw new DataException("SOURCE_RECORD_STATE_NOT_ALLOWED: 源检验项尚未提交或状态无效"); } if (matches.isEmpty()) { throw notFound(actualRequestId, actualTemplateCode); } if (matches.size() != 1) { throw new DataException("SOURCE_RECORD_AMBIGUOUS: 请验单下命中多条源电子记录"); } return matches.get(0); } private static DataException notFound(String testRequestId, String templateCode) { return new DataException("SOURCE_RECORD_NOT_FOUND: 未找到请验单、模板下匹配的源电子记录" + " (" + testRequestId + ", " + templateCode + ")"); } private static String required(String value, String message) { if (!StringUtils.hasText(value)) throw new DataException(message); return value.trim(); } }