package jnpf.elnService.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import jnpf.elnEntity.runtime.ElnRecordForms; import jnpf.elnEntity.runtime.ElnRecordVos; import jnpf.elnEntity.runtime.ElnSharedFillOperationEntity; import jnpf.elnEntity.runtime.ElnSharedFillTargetOperationEntity; import jnpf.elnMapper.ElnSharedFillOperationMapper; import jnpf.elnMapper.ElnSharedFillTargetOperationMapper; import jnpf.exception.DataException; import jnpf.util.JsonUtil; import jnpf.util.RandomUtil; import org.springframework.stereotype.Service; import java.util.Objects; @Service public class ElnRecordSharedFillOperationService implements ElnRecordSharedFillServiceImpl.OperationCoordinator { private final ElnSharedFillOperationMapper operationMapper; private final ElnSharedFillTargetOperationMapper targetMapper; public ElnRecordSharedFillOperationService( ElnSharedFillOperationMapper operationMapper, ElnSharedFillTargetOperationMapper targetMapper) { this.operationMapper = operationMapper; this.targetMapper = targetMapper; } @Override public ElnRecordSharedFillServiceImpl.OperationStart begin( String tenantId, String userId, String operationCode, String requestDigest) { ElnSharedFillOperationEntity candidate = new ElnSharedFillOperationEntity(); candidate.setId(RandomUtil.uuId()); candidate.setOperationCode(operationCode); candidate.setRequestDigest(requestDigest); candidate.setOperatorUserId(userId); candidate.setFTenantId(tenantId); boolean inserted = operationMapper.insertIgnore(candidate) == 1; ElnSharedFillOperationEntity existing = findOperation(tenantId, operationCode); if (existing == null) throw new DataException("批量填写操作状态创建失败"); if (!Objects.equals(requestDigest, existing.getRequestDigest()) || !Objects.equals(userId, existing.getOperatorUserId())) { throw new DataException("批量填写操作码已用于其他请求"); } if ("COMPLETED".equals(existing.getOperationStatus())) { return ElnRecordSharedFillServiceImpl.OperationStart.replay( parseResult(existing.getResultJson())); } if (!"OPEN".equals(existing.getOperationStatus())) { throw new DataException("批量填写操作状态无效"); } if (inserted || !hasCompletedTarget(tenantId, operationCode)) { return ElnRecordSharedFillServiceImpl.OperationStart.fresh(); } return ElnRecordSharedFillServiceImpl.OperationStart.resuming(); } @Override public ElnRecordVos.SharedFillTargetResult reserveTarget( String tenantId, String userId, String operationCode, ElnRecordForms.SharedFillTarget target) { ElnSharedFillTargetOperationEntity candidate = new ElnSharedFillTargetOperationEntity(); candidate.setId(RandomUtil.uuId()); candidate.setOperationCode(operationCode); candidate.setJianyanXiangId(target.getTestWorkItemId()); candidate.setDianziJiluId(target.getRecordId()); candidate.setFTenantId(tenantId); candidate.setFCreatorUserId(userId); if (targetMapper.insertIgnore(candidate) == 1) return null; ElnSharedFillTargetOperationEntity existing = findTarget( tenantId, operationCode, target.getTestWorkItemId()); if (existing == null) throw new DataException("批量填写目标操作状态创建失败"); if (!Objects.equals(target.getRecordId(), existing.getDianziJiluId())) { throw new DataException("批量填写操作码目标记录不一致"); } if ("COMPLETED".equals(existing.getOperationStatus())) { return parseTargetResult(existing.getResultJson()); } throw new DataException("批量填写目标正在处理中,请稍后重试"); } @Override public void completeTarget( String tenantId, String userId, String operationCode, ElnRecordForms.SharedFillTarget target, ElnRecordVos.SharedFillTargetResult result) { if (targetMapper.complete(tenantId, operationCode, target.getTestWorkItemId(), target.getRecordId(), JsonUtil.getObjectToString(result), userId) != 1) { throw new DataException("批量填写目标操作状态提交失败"); } } @Override public ElnRecordVos.SharedFilled complete( String tenantId, String userId, String operationCode, String requestDigest, ElnRecordVos.SharedFilled result) { String resultJson = JsonUtil.getObjectToString(result); if (operationMapper.complete(tenantId, operationCode, requestDigest, resultJson, userId) == 1) { return result; } ElnSharedFillOperationEntity existing = findOperation(tenantId, operationCode); if (existing != null && "COMPLETED".equals(existing.getOperationStatus()) && Objects.equals(requestDigest, existing.getRequestDigest())) { return parseResult(existing.getResultJson()); } throw new DataException("批量填写操作状态提交失败"); } private ElnSharedFillOperationEntity findOperation( String tenantId, String operationCode) { return operationMapper.selectOne( Wrappers.lambdaQuery() .eq(ElnSharedFillOperationEntity::getFTenantId, tenantId) .eq(ElnSharedFillOperationEntity::getOperationCode, operationCode) .and(q -> q.isNull(ElnSharedFillOperationEntity::getFDeleteMark) .or().eq(ElnSharedFillOperationEntity::getFDeleteMark, 0)) .last("limit 1")); } private ElnSharedFillTargetOperationEntity findTarget( String tenantId, String operationCode, String testWorkItemId) { return targetMapper.selectOne( Wrappers.lambdaQuery() .eq(ElnSharedFillTargetOperationEntity::getFTenantId, tenantId) .eq(ElnSharedFillTargetOperationEntity::getOperationCode, operationCode) .eq(ElnSharedFillTargetOperationEntity::getJianyanXiangId, testWorkItemId) .and(q -> q.isNull(ElnSharedFillTargetOperationEntity::getFDeleteMark) .or().eq(ElnSharedFillTargetOperationEntity::getFDeleteMark, 0)) .last("limit 1")); } private boolean hasCompletedTarget(String tenantId, String operationCode) { return targetMapper.selectCount( Wrappers.lambdaQuery() .eq(ElnSharedFillTargetOperationEntity::getFTenantId, tenantId) .eq(ElnSharedFillTargetOperationEntity::getOperationCode, operationCode) .eq(ElnSharedFillTargetOperationEntity::getOperationStatus, "COMPLETED") .and(q -> q.isNull(ElnSharedFillTargetOperationEntity::getFDeleteMark) .or().eq(ElnSharedFillTargetOperationEntity::getFDeleteMark, 0))) > 0; } private static ElnRecordVos.SharedFilled parseResult(String json) { if (json == null) throw new DataException("批量填写操作结果缺失"); return JsonUtil.getJsonToBean(json, ElnRecordVos.SharedFilled.class); } private static ElnRecordVos.SharedFillTargetResult parseTargetResult(String json) { if (json == null) throw new DataException("批量填写目标操作结果缺失"); return JsonUtil.getJsonToBean(json, ElnRecordVos.SharedFillTargetResult.class); } }