刘光辉
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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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.<ElnSharedFillOperationEntity>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.<ElnSharedFillTargetOperationEntity>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.<ElnSharedFillTargetOperationEntity>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);
    }
}