刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
package jnpf.limsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.audit.sdk.annotation.AuditLog;
import jnpf.exception.DataException;
import jnpf.limsEntity.LimsWendingxingXiaohuiShenqingEntity;
import jnpf.limsMapper.LimsWendingxingXiaohuiShenqingMapper;
import jnpf.limsService.LimsWendingxingXiaohuiShenqingService;
import jnpf.limsService.support.LimsWendingxingXiaohuiFlowStatusResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.util.List;
import java.util.Objects;
 
@Service
public class LimsWendingxingXiaohuiShenqingServiceImpl
        implements LimsWendingxingXiaohuiShenqingService {
 
    @Autowired
    private LimsWendingxingXiaohuiShenqingMapper xiaohuiShenqingMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "FLOW_START", label = "稳定性销毁流程发起", bizType = "WENDINGXING",
            bizCodeExpr = "#flowId", bizCodeRequired = false,
            targetTable = "lims_wendingxing_xiaohui_shenqing",
            targetIdExpr = "T(jnpf.limsService.support.LimsAuditTargetIds).wendingxingDestroyByFlow(#flowId, #taskId)",
            diff = true, entityClass = LimsWendingxingXiaohuiShenqingEntity.class)
    public void syncFlowStarted(String flowId, String taskId) throws DataException {
        LimsWendingxingXiaohuiShenqingEntity shenqing = findByFlow(flowId, taskId);
        if (LimsWendingxingXiaohuiFlowStatusResolver.COMPLETED.equals(shenqing.getBizStatus())) {
            return;
        }
        updateBizStatus(shenqing, LimsWendingxingXiaohuiFlowStatusResolver.PROCESSING);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @AuditLog(action = "FLOW_END", label = "稳定性销毁流程结束", bizType = "WENDINGXING",
            bizCodeExpr = "#flowId", bizCodeRequired = false,
            targetTable = "lims_wendingxing_xiaohui_shenqing",
            targetIdExpr = "T(jnpf.limsService.support.LimsAuditTargetIds).wendingxingDestroyByFlow(#flowId, #taskId)",
            diff = true, entityClass = LimsWendingxingXiaohuiShenqingEntity.class)
    public void syncFlowEnded(String flowId, String taskId, Integer flowState) throws DataException {
        if (!LimsWendingxingXiaohuiFlowStatusResolver.isTerminal(flowState)) {
            throw new DataException("流程尚未结束,当前状态:" + flowState);
        }
        LimsWendingxingXiaohuiShenqingEntity shenqing = findByFlow(flowId, taskId);
        updateBizStatus(shenqing, LimsWendingxingXiaohuiFlowStatusResolver.COMPLETED);
    }
 
    private LimsWendingxingXiaohuiShenqingEntity findByFlow(String flowId, String taskId)
            throws DataException {
        String resolvedFlowId = trimToEmpty(flowId);
        String resolvedTaskId = trimToEmpty(taskId);
        if (!StringUtils.hasText(resolvedFlowId)) {
            throw new DataException("流程id(flow_id)不能为空");
        }
        if (!StringUtils.hasText(resolvedTaskId)) {
            throw new DataException("流程任务id(task_id)不能为空");
        }
 
        LambdaQueryWrapper<LimsWendingxingXiaohuiShenqingEntity> query = new LambdaQueryWrapper<>();
        query.eq(LimsWendingxingXiaohuiShenqingEntity::getFFlowId, resolvedFlowId)
                .eq(LimsWendingxingXiaohuiShenqingEntity::getFFlowTaskId, resolvedTaskId)
                .and(w -> w.isNull(LimsWendingxingXiaohuiShenqingEntity::getFDeleteMark)
                        .or()
                        .ne(LimsWendingxingXiaohuiShenqingEntity::getFDeleteMark, 1));
        List<LimsWendingxingXiaohuiShenqingEntity> list = xiaohuiShenqingMapper.selectList(query);
        if (list == null || list.isEmpty()) {
            throw new DataException("稳定性销毁申请不存在:flow_id=" + resolvedFlowId
                    + ", task_id=" + resolvedTaskId);
        }
        if (list.size() > 1) {
            throw new DataException("多个稳定性销毁申请关联同一流程任务:task_id=" + resolvedTaskId);
        }
        return list.get(0);
    }
 
    private void updateBizStatus(LimsWendingxingXiaohuiShenqingEntity shenqing, String targetStatus)
            throws DataException {
        if (Objects.equals(shenqing.getBizStatus(), targetStatus)) {
            return;
        }
        LambdaUpdateWrapper<LimsWendingxingXiaohuiShenqingEntity> update = new LambdaUpdateWrapper<>();
        update.eq(LimsWendingxingXiaohuiShenqingEntity::getId, shenqing.getId())
                .eq(LimsWendingxingXiaohuiShenqingEntity::getFTenantId, shenqing.getFTenantId())
                .and(w -> w.isNull(LimsWendingxingXiaohuiShenqingEntity::getFDeleteMark)
                        .or()
                        .ne(LimsWendingxingXiaohuiShenqingEntity::getFDeleteMark, 1))
                .set(LimsWendingxingXiaohuiShenqingEntity::getBizStatus, targetStatus);
        if (xiaohuiShenqingMapper.update(null, update) != 1) {
            throw new DataException("稳定性销毁申请状态更新失败,请刷新后重试");
        }
    }
 
    private String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
}