刘光辉
118 分钟以前 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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<ElnSharedFillOperationEntity> 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.<ElnSharedFillOperationEntity>lambdaQuery()
                .eq(ElnSharedFillOperationEntity::getFTenantId, tenantId)
                .eq(ElnSharedFillOperationEntity::getOperationCode, operationCode)
                .and(q -> q.isNull(ElnSharedFillOperationEntity::getFDeleteMark)
                        .or().eq(ElnSharedFillOperationEntity::getFDeleteMark, 0)));
    }
 
    private List<ElnSharedFillOperationEntity> pendingCandidates(
            String tenantId, String userId) {
        return operations.selectList(
                Wrappers.<ElnSharedFillOperationEntity>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)));
    }
}