刘光辉
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package jnpf.elnService.impl;
 
import jnpf.elnEntity.runtime.ElnRecordForms;
import jnpf.elnEntity.runtime.ElnRecordVos;
import jnpf.elnService.ElnRecordBatchCreationService;
import jnpf.elnService.ElnRecordCreationService;
import jnpf.exception.DataException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
/**
 * Suspends any outer transaction before delegating each item to the single-record transaction.
 */
@Service
public class ElnRecordBatchCreationServiceImpl implements ElnRecordBatchCreationService {
 
    private static final Logger log = LoggerFactory.getLogger(ElnRecordBatchCreationServiceImpl.class);
    private static final String CREATED = "CREATED";
    private static final String EXISTING = "EXISTING";
    private static final String FAILED = "FAILED";
    private static final String BUSINESS_ERROR = "BUSINESS_ERROR";
    private static final String SYSTEM_ERROR = "SYSTEM_ERROR";
 
    private final ElnRecordCreationService recordCreationService;
 
    public ElnRecordBatchCreationServiceImpl(ElnRecordCreationService recordCreationService) {
        this.recordCreationService = recordCreationService;
    }
 
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public ElnRecordVos.BatchCreated create(ElnRecordForms.BatchCreate command) {
        String operationCode = required(command == null ? null : command.getOperationCode(), "操作码不能为空");
        List<String> testWorkItemIds = testWorkItemIds(command == null ? null : command.getTestWorkItemIds());
        ElnRecordVos.BatchCreated result = new ElnRecordVos.BatchCreated();
        for (String testWorkItemId : testWorkItemIds) {
            result.getItems().add(createOne(testWorkItemId, operationCode));
        }
        return result;
    }
 
    private ElnRecordVos.BatchItem createOne(String testWorkItemId, String operationCode) {
        ElnRecordVos.BatchItem item = new ElnRecordVos.BatchItem();
        item.setTestWorkItemId(testWorkItemId);
        try {
            ElnRecordForms.Create command = new ElnRecordForms.Create();
            command.setTestWorkItemId(testWorkItemId);
            command.setOperationCode(operationCode);
            ElnRecordVos.Created created = recordCreationService.create(command);
            item.setCreated(created);
            item.setStatus(created.isExisting() ? EXISTING : CREATED);
        } catch (DataException exception) {
            item.setStatus(FAILED);
            item.setErrorCode(BUSINESS_ERROR);
            item.setErrorMessage(businessMessage(exception));
        } catch (RuntimeException exception) {
            log.error("ELN 批量创建记录失败,testWorkItemId={}", testWorkItemId, exception);
            item.setStatus(FAILED);
            item.setErrorCode(SYSTEM_ERROR);
            item.setErrorMessage("电子记录创建失败,请稍后重试");
        }
        return item;
    }
 
    private static List<String> testWorkItemIds(List<String> values) {
        if (values == null || values.isEmpty()) {
            throw new DataException("检验项不能为空");
        }
        Set<String> unique = new LinkedHashSet<>();
        for (String value : values) {
            unique.add(required(value, "检验项不能为空"));
        }
        return new ArrayList<>(unique);
    }
 
    private static String required(String value, String message) {
        if (!StringUtils.hasText(value)) {
            throw new DataException(message);
        }
        return value.trim();
    }
 
    private static String businessMessage(DataException exception) {
        return StringUtils.hasText(exception.getMessage()) ? exception.getMessage() : "电子记录创建失败";
    }
}