刘光辉
5 小时以前 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
package jnpf.dmsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.dmsEntity.DmsWenjianBianhaoGuizeEntity;
import jnpf.dmsMapper.DmsWenjianBianhaoGuizeMapper;
import jnpf.dmsService.DmsWenjianBianhaoGuizeTongguoService;
import jnpf.exception.DataException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
 
@Service
@RequiredArgsConstructor
public class DmsWenjianBianhaoGuizeTongguoServiceImpl
        implements DmsWenjianBianhaoGuizeTongguoService {
 
    private static final String BIZ_ENABLED_DISABLED = "disabled";
 
    private final DmsWenjianBianhaoGuizeMapper guizeMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void tongguo(String flowId, String taskId) throws DataException {
        String resolvedFlowId = requireValue(flowId, "流程ID(flow_id)");
        String resolvedTaskId = requireValue(taskId, "流程任务ID(task_id)");
        List<DmsWenjianBianhaoGuizeEntity> guizes = findGuizes(resolvedFlowId, resolvedTaskId);
        Set<String> guizeIds = new LinkedHashSet<>();
        Set<String> liushuihaos = new LinkedHashSet<>();
        for (DmsWenjianBianhaoGuizeEntity guize : guizes) {
            guizeIds.add(guize.getId());
            String liushuihao = trimToEmpty(guize.getGuizeLiushuihao());
            if (!liushuihao.isEmpty()) {
                liushuihaos.add(liushuihao);
            }
        }
        for (String liushuihao : liushuihaos) {
            disableOtherRules(liushuihao, guizeIds);
        }
    }
 
    private List<DmsWenjianBianhaoGuizeEntity> findGuizes(String flowId, String taskId) throws DataException {
        LambdaQueryWrapper<DmsWenjianBianhaoGuizeEntity> query = new LambdaQueryWrapper<>();
        query.eq(DmsWenjianBianhaoGuizeEntity::getFlowId, flowId)
                .eq(DmsWenjianBianhaoGuizeEntity::getFlowTaskId, taskId)
                .isNull(DmsWenjianBianhaoGuizeEntity::getDeleteMark);
        List<DmsWenjianBianhaoGuizeEntity> guizes = guizeMapper.selectList(query);
        if (guizes.isEmpty()) {
            throw new DataException("未找到文件编号规则:flow_id=" + flowId + ", task_id=" + taskId);
        }
        return guizes;
    }
 
    private void disableOtherRules(String liushuihao, Set<String> currentGuizeIds) {
        LambdaUpdateWrapper<DmsWenjianBianhaoGuizeEntity> update = new LambdaUpdateWrapper<>();
        update.eq(DmsWenjianBianhaoGuizeEntity::getGuizeLiushuihao, liushuihao)
                .notIn(DmsWenjianBianhaoGuizeEntity::getId, currentGuizeIds)
                .and(wrapper -> wrapper.isNull(DmsWenjianBianhaoGuizeEntity::getBizEnabled)
                        .or()
                        .ne(DmsWenjianBianhaoGuizeEntity::getBizEnabled, BIZ_ENABLED_DISABLED))
                .set(DmsWenjianBianhaoGuizeEntity::getBizEnabled, BIZ_ENABLED_DISABLED);
        guizeMapper.update(null, update);
    }
 
    private String requireValue(String value, String fieldName) throws DataException {
        String resolved = trimToEmpty(value);
        if (resolved.isEmpty()) {
            throw new DataException(fieldName + "不能为空");
        }
        return resolved;
    }
 
    private String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
}