ny
23 小时以前 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package jnpf.flowable.util;
 
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.ImmutableList;
import jnpf.constant.MsgCode;
import jnpf.exception.WorkFlowException;
import jnpf.flowable.entity.TaskEntity;
import jnpf.flowable.enums.DivideRuleEnum;
import jnpf.flowable.enums.NodeEnum;
import jnpf.flowable.model.flowable.FlowAbleUrl;
import jnpf.flowable.model.flowable.OutgoingFlowsFo;
import jnpf.flowable.model.task.FlowMethod;
import jnpf.flowable.model.templatenode.nodejson.NodeModel;
import jnpf.flowable.model.templatenode.nodejson.ProperCond;
import jnpf.permission.entity.UserEntity;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
/**
 * 类的描述
 *
 * @author JNPF@YinMai Info. Co., Ltd
 * @version 5.0.x
 * @since 2024/4/19 15:14
 */
@Component
public class ConditionUtil {
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private FlowAbleUrl flowAbleUrl;
 
    // 处理选择分支的条件
    public Map<String, Boolean> getForBranch(FlowMethod flowMethod, List<String> branchList) throws WorkFlowException {
        Map<String, Boolean> resMap = new HashMap<>();
 
        String deploymentId = flowMethod.getDeploymentId();
        String nodeCode = flowMethod.getNodeCode();
        Map<String, NodeModel> nodes = flowMethod.getNodes();
        NodeModel global = nodes.get(NodeEnum.global.getType());
        List<String> connectList = global.getConnectList();
        List<String> typeList = ImmutableList.of(NodeEnum.trigger.getType());
 
        OutgoingFlowsFo flowsFo = new OutgoingFlowsFo();
        flowsFo.setDeploymentId(deploymentId);
        flowsFo.setTaskKey(nodeCode);
        List<String> outgoingFlows = flowAbleUrl.getOutgoingFlows(flowsFo);
 
        for (String flow : outgoingFlows) {
            resMap.put(flow, false);
            if (!connectList.contains(flow)) {
                // 不存在connectList中,说明是隐藏的线,默认给true
                resMap.put(flow, true);
                continue;
            }
            List<String> nodeKey = flowAbleUrl.getTaskKeyAfterFlow(deploymentId, flow);
            if (CollectionUtil.isNotEmpty(nodeKey)) {
                NodeModel nodeModel = nodes.get(nodeKey.get(0));
                if (null != nodeModel && typeList.contains(nodeModel.getType())) {
                    resMap.put(flow, true);
                } else if (branchList.contains(nodeKey.get(0))) {
                    resMap.put(flow, true);
                }
            }
        }
 
        return resMap;
    }
 
    /**
     * 处理条件
     */
    public Map<String, Boolean> handleCondition(FlowMethod flowMethod) throws WorkFlowException {
        String deploymentId = flowMethod.getDeploymentId();
        String nodeCode = flowMethod.getNodeCode();
        // 获取节点的出线
        OutgoingFlowsFo flowsFo = new OutgoingFlowsFo();
        flowsFo.setDeploymentId(deploymentId);
        flowsFo.setTaskKey(nodeCode);
        List<String> outgoingFlows = flowAbleUrl.getOutgoingFlows(flowsFo);
 
        Map<String, Boolean> resMap = new HashMap<>();
        flowMethod.setResMap(resMap);
        flowMethod.setOutgoingFlows(outgoingFlows);
        // 判断条件
        getConditionResult(flowMethod);
        return resMap;
    }
 
    /**
     * 获取条件结果
     */
    public void getConditionResult(FlowMethod flowMethod) {
        List<String> outgoingFlows = flowMethod.getOutgoingFlows();
        Map<String, Boolean> resMap = flowMethod.getResMap();
        Map<String, NodeModel> nodes = flowMethod.getNodes();
        String nodeCode = flowMethod.getNodeCode();
        TaskEntity taskEntity = flowMethod.getTaskEntity();
        if (CollectionUtil.isNotEmpty(outgoingFlows)) {
 
            Set<String> userList = new HashSet<>();
            userList.add(UserProvider.getLoginUserId());
 
            if (taskEntity != null) {
                userList.add(taskEntity.getCreatorUserId());
                if (StringUtil.isNotEmpty(taskEntity.getDelegateUserId())) {
                    userList.add(taskEntity.getDelegateUserId());
                }
            }
 
            List<UserEntity> userName = serviceUtil.getUserName(new ArrayList<>(userList));
            UserEntity createUser = null;
            UserEntity delegate = null;
            if (taskEntity != null) {
                createUser = userName.stream().filter(e -> Objects.equals(e.getId(), taskEntity.getCreatorUserId())).findFirst().orElse(null);
                if (StringUtil.isNotEmpty(taskEntity.getDelegateUserId())) {
                    delegate = userName.stream().filter(e -> Objects.equals(e.getId(), taskEntity.getDelegateUserId())).findFirst().orElse(null);
                }
            }
 
 
            // 设置条件判断 所需参数
            UserEntity userEntity = userName.stream().filter(e -> Objects.equals(e.getId(), UserProvider.getLoginUserId())).findFirst().orElse(null);
            flowMethod.setUserEntity(userEntity);
            if (flowMethod.getUserInfo() == null) {
                flowMethod.setUserInfo(UserProvider.getUser());
            }
            flowMethod.setCreateUser(createUser);
            flowMethod.setDelegate(delegate);
 
            NodeModel currentNodeModel = nodes.get(nodeCode);
            NodeModel global = nodes.get(NodeEnum.global.getType());
            flowMethod.setNodeModel(currentNodeModel);
            if (StringUtil.equals(currentNodeModel.getDivideRule(), DivideRuleEnum.PARALLEL.getType())) {
                // 并行,全都为true
                for (String key : outgoingFlows) {
                    resMap.put(key, true);
                }
            } else {
                List<Boolean> exclusiveList = new ArrayList<>();
                boolean isExclusive = StringUtil.equals(currentNodeModel.getDivideRule(), DivideRuleEnum.EXCLUSIVE.getType());
                List<String> connectList = global.getConnectList();
                for (String key : outgoingFlows) {
                    boolean res = true;
                    if (!connectList.contains(key)) {
                        resMap.put(key, res);
                        continue;
                    }
                    // 获取出线节点 判断条件,没有设置条件的默认true
                    NodeModel nodeModel = nodes.get(key) != null ? nodes.get(key) : new NodeModel();
                    List<ProperCond> conditions = nodeModel.getConditions();
                    if (CollectionUtil.isNotEmpty(conditions)) {
                        flowMethod.setConditions(conditions);
                        flowMethod.setMatchLogic(nodeModel.getMatchLogic());
                        res = FlowJsonUtil.nodeConditionDecide(flowMethod);
                    }
                    if (isExclusive){
                        if (exclusiveList.contains(true)){
                            res = false;
                        }
                        exclusiveList.add(res);
                    }
                    resMap.put(key, res);
                }
            }
        }
    }
 
    // 处理条件
    public boolean hasCondition(FlowMethod flowMethod) {
        TaskEntity taskEntity = flowMethod.getTaskEntity();
        UserEntity createUser = null;
        UserEntity delegate = null;
        if (taskEntity != null) {
            createUser = serviceUtil.getUserInfo(taskEntity.getCreatorUserId());
            delegate = StringUtil.isNotEmpty(taskEntity.getDelegateUserId()) ? serviceUtil.getUserInfo(taskEntity.getDelegateUserId()) : null;
        }
        flowMethod.setCreateUser(createUser);
        flowMethod.setDelegate(delegate);
        UserEntity userEntity = serviceUtil.getUserInfo(UserProvider.getLoginUserId());
        flowMethod.setUserEntity(userEntity);
        flowMethod.setUserInfo(UserProvider.getUser());
        return FlowJsonUtil.nodeConditionDecide(flowMethod);
    }
 
    public void checkCondition(Map<String, Boolean> resMap, Map<String, NodeModel> nodes) throws WorkFlowException {
        if (CollectionUtil.isEmpty(nodes)) {
            return;
        }
        NodeModel global = nodes.get(NodeEnum.global.getType());
        if (null == global) {
            throw new WorkFlowException(MsgCode.WF076.get());
        }
        List<String> connectList = global.getConnectList();
        long count = resMap.values().stream().filter(t -> Objects.equals(t, true)).count();
        Set<String> set = resMap.keySet();
 
        if (count == resMap.size()) {
            return;
        }
        Map<String, Boolean> defaultResMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(connectList)) {
            int c = 0;
            for (String key : set) {
                if (connectList.contains(key)) {
                    if (resMap.get(key)) {
                        c++;
                    }
                    NodeModel nodeModel = nodes.get(key);
                    if (nodeModel != null) {
                        Boolean isDefault = nodeModel.getIsDefault();
                        defaultResMap.put(key, isDefault);
                    }
                }
            }
            if (c == 0) {
                long defaultCount = defaultResMap.values().stream().filter(t -> Objects.equals(t, true)).count();
                if (defaultCount > 0) {
                    resMap.putAll(defaultResMap);
                    c++;
                }
            }
            if (c < 1) {
                throw new WorkFlowException(MsgCode.WF075.get());
            }
        } else {
            if (count < 1) {
                throw new WorkFlowException(MsgCode.WF075.get());
            }
        }
    }
}