刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
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<String> 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<ElnRecordEntity> candidates = candidateStore.list(actualTenantId, actualTemplateCode);
        if (candidates == null || candidates.isEmpty()) {
            throw notFound(actualRequestId, actualTemplateCode);
        }
        List<ElnRecordEntity> tenantCandidates = new ArrayList<>();
        List<String> 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<String, ElnTestRequestIdentityLookup.Identity> identities =
                requestIdentityLookup.resolve(
                actualTenantId, testWorkItemIds);
        if (identities == null) identities = Collections.emptyMap();
        List<ElnRecordEntity> matches = new ArrayList<>();
        List<ElnRecordEntity> 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();
    }
}