刘光辉
4 小时以前 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
package jnpf.elnService.impl;
 
import jnpf.base.ActionResult;
import jnpf.base.ActionResultCode;
import jnpf.elnService.ElnTestRequestIdentityLookup;
import jnpf.exception.DataException;
import jnpf.lims.api.TestWorkItemSnapshotApi;
import jnpf.lims.api.model.TestWorkItemRequestIdentityActorRequest;
import jnpf.lims.api.model.TestWorkItemRequestIdentityBatch;
import jnpf.lims.api.model.TestWorkItemRequestIdentity;
import jnpf.lims.api.model.TestWorkItemRequestIdentityRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class LimsTestRequestIdentityLookupAdapter
        implements ElnTestRequestIdentityLookup {
 
    private final TestWorkItemSnapshotApi api;
 
    @Autowired
    public LimsTestRequestIdentityLookupAdapter(TestWorkItemSnapshotApi api) {
        this.api = api;
    }
 
    @Override
    public Map<String, ElnTestRequestIdentityLookup.Identity> resolve(
            String tenantId, List<String> testWorkItemIds) {
        if (!StringUtils.hasText(tenantId)) {
            throw new DataException("LIMS 请验单身份查询租户不能为空");
        }
        TestWorkItemRequestIdentityRequest request = new TestWorkItemRequestIdentityRequest();
        request.setTestWorkItemIds(testWorkItemIds);
        TestWorkItemRequestIdentityActorRequest actor =
                new TestWorkItemRequestIdentityActorRequest();
        actor.setTenantId(tenantId.trim());
        actor.setRequest(request);
        TestWorkItemRequestIdentityBatch batch = result(api.requestIdentitiesAsActor(actor));
        Map<String, ElnTestRequestIdentityLookup.Identity> identities =
                new LinkedHashMap<>();
        if (batch.getItems() == null) return identities;
        for (TestWorkItemRequestIdentity item : batch.getItems()) {
            if (item == null || !StringUtils.hasText(item.getTestWorkItemId())) {
                continue;
            }
            if ("TEST_REQUEST_INVALID".equals(item.getOutcomeCode())) {
                throw new DataException("SOURCE_RECORD_IDENTITY_INVALID: 检验项缺少请验单身份 ("
                        + item.getTestWorkItemId().trim() + ")");
            }
            if (!"SUCCESS".equals(item.getOutcomeCode())
                    || !StringUtils.hasText(item.getTestRequestId())) continue;
            identities.put(item.getTestWorkItemId().trim(),
                    new ElnTestRequestIdentityLookup.Identity(
                            item.getTestRequestId().trim(), item.getTaskStatus()));
        }
        return identities;
    }
 
    private static TestWorkItemRequestIdentityBatch result(
            ActionResult<TestWorkItemRequestIdentityBatch> result) {
        if (result == null
                || !ActionResultCode.SUCCESS.getCode().equals(result.getCode())
                || result.getData() == null) {
            throw new DataException(result == null || !StringUtils.hasText(result.getMsg())
                    ? "LIMS 请验单身份读取失败,请稍后重试" : result.getMsg());
        }
        return result.getData();
    }
}