package jnpf.elnService.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import jnpf.elnEntity.runtime.ElnRecordVos; import jnpf.elnEntity.runtime.ElnSharedFillOperationEntity; import jnpf.elnMapper.ElnSharedFillOperationMapper; import jnpf.elnService.ElnRecordSourceSyncOperationCoordinator; import jnpf.exception.DataException; import jnpf.util.JsonUtil; import jnpf.util.RandomUtil; import org.springframework.stereotype.Service; import java.util.List; import java.util.Objects; @Service public class ElnRecordSourceSyncOperationService implements ElnRecordSourceSyncOperationCoordinator { private final ElnSharedFillOperationMapper operations; public ElnRecordSourceSyncOperationService(ElnSharedFillOperationMapper operations) { this.operations = operations; } @Override public ElnRecordVos.SourceSynchronized 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 = operations.insertIgnore(candidate) == 1; ElnSharedFillOperationEntity existing = find(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()) || ("OPEN".equals(existing.getOperationStatus()) && existing.getResultJson() != null)) { if (existing.getResultJson() == null) { throw new DataException("来源同步操作结果缺失"); } return JsonUtil.getJsonToBean( existing.getResultJson(), ElnRecordVos.SourceSynchronized.class); } if (!inserted) { throw new DataException("来源同步操作正在处理中,请稍后重试"); } return null; } @Override public ElnRecordVos.SourceSynchronized complete( String tenantId, String userId, String operationCode, String requestDigest, ElnRecordVos.SourceSynchronized result) { if (operations.complete(tenantId, operationCode, requestDigest, JsonUtil.getObjectToString(result), userId) != 1) { throw new DataException("来源同步操作状态提交失败"); } return result; } @Override public ElnRecordVos.SourceSynchronized prepare( String tenantId, String userId, String operationCode, String requestDigest, ElnRecordVos.SourceSynchronized result) { if (operations.prepareResult(tenantId, operationCode, requestDigest, JsonUtil.getObjectToString(result), userId) != 1) { throw new DataException("来源同步文档确认状态创建失败"); } return result; } @Override public ElnRecordVos.SourceSynchronized pending( String tenantId, String userId, String recordId) { List candidates = pendingCandidates(tenantId, userId); ElnRecordVos.SourceSynchronized found = null; for (ElnSharedFillOperationEntity candidate : candidates) { ElnRecordVos.SourceSynchronized result = parseResult(candidate); if (!Objects.equals(recordId, result.getRecordId())) continue; if (found != null) throw new DataException("电子记录存在多个待确认来源同步操作"); found = result; } return found; } @Override public boolean hasPendingDocumentConfirmation(String tenantId, String recordId) { for (ElnSharedFillOperationEntity candidate : pendingCandidates(tenantId, null)) { if (Objects.equals(recordId, parseResult(candidate).getRecordId())) return true; } return false; } @Override public boolean confirm(String tenantId, String userId, String operationCode) { if (operations.confirmPrepared(tenantId, operationCode, userId) == 1) return true; ElnSharedFillOperationEntity existing = find(tenantId, operationCode); if (existing != null && Objects.equals(userId, existing.getOperatorUserId()) && "COMPLETED".equals(existing.getOperationStatus()) && existing.getResultJson() != null) return false; throw new DataException("来源同步文档确认状态提交失败"); } private static ElnRecordVos.SourceSynchronized parseResult( ElnSharedFillOperationEntity operation) { if (operation.getResultJson() == null) { throw new DataException("来源同步操作结果缺失"); } return JsonUtil.getJsonToBean( operation.getResultJson(), ElnRecordVos.SourceSynchronized.class); } private ElnSharedFillOperationEntity find(String tenantId, String operationCode) { return operations.selectOne(Wrappers.lambdaQuery() .eq(ElnSharedFillOperationEntity::getFTenantId, tenantId) .eq(ElnSharedFillOperationEntity::getOperationCode, operationCode) .and(q -> q.isNull(ElnSharedFillOperationEntity::getFDeleteMark) .or().eq(ElnSharedFillOperationEntity::getFDeleteMark, 0))); } private List pendingCandidates( String tenantId, String userId) { return operations.selectList( Wrappers.lambdaQuery() .eq(ElnSharedFillOperationEntity::getFTenantId, tenantId) .eq(userId != null, ElnSharedFillOperationEntity::getOperatorUserId, userId) .eq(ElnSharedFillOperationEntity::getOperationStatus, "OPEN") .isNotNull(ElnSharedFillOperationEntity::getResultJson) .and(q -> q.isNull(ElnSharedFillOperationEntity::getFDeleteMark) .or().eq(ElnSharedFillOperationEntity::getFDeleteMark, 0))); } }