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 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 testWorkItemIds(List values) { if (values == null || values.isEmpty()) { throw new DataException("检验项不能为空"); } Set 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() : "电子记录创建失败"; } }