|
1 | 1 | package com.codingapi.springboot.flow.service; |
2 | 2 |
|
| 3 | +import com.codingapi.springboot.flow.bind.BindDataSnapshot; |
| 4 | +import com.codingapi.springboot.flow.content.FlowContent; |
3 | 5 | import com.codingapi.springboot.flow.domain.FlowNode; |
| 6 | +import com.codingapi.springboot.flow.domain.FlowRelation; |
| 7 | +import com.codingapi.springboot.flow.domain.FlowWork; |
| 8 | +import com.codingapi.springboot.flow.domain.Opinion; |
| 9 | +import com.codingapi.springboot.flow.error.ErrorResult; |
| 10 | +import com.codingapi.springboot.flow.error.NodeResult; |
| 11 | +import com.codingapi.springboot.flow.error.OperatorResult; |
4 | 12 | import com.codingapi.springboot.flow.record.FlowRecord; |
| 13 | +import com.codingapi.springboot.flow.repository.FlowOperatorRepository; |
| 14 | +import com.codingapi.springboot.flow.repository.FlowRecordRepository; |
| 15 | +import com.codingapi.springboot.flow.user.IFlowOperator; |
5 | 16 |
|
| 17 | +import java.util.ArrayList; |
6 | 18 | import java.util.List; |
7 | 19 |
|
8 | 20 | class FlowRecordBuilderService { |
9 | 21 |
|
10 | 22 |
|
| 23 | + private final FlowOperatorRepository flowOperatorRepository; |
| 24 | + private final FlowRecordRepository flowRecordRepository; |
| 25 | + |
| 26 | + private final String processId; |
| 27 | + private final long preId; |
| 28 | + |
| 29 | + private final FlowWork flowWork; |
| 30 | + private final Opinion opinion; |
| 31 | + private final IFlowOperator currentOperator; |
| 32 | + private final BindDataSnapshot snapshot; |
| 33 | + private final List<FlowRecord> historyRecords; |
| 34 | + private final IFlowOperator createOperator; |
| 35 | + |
| 36 | + |
| 37 | + public FlowRecordBuilderService(FlowOperatorRepository flowOperatorRepository, |
| 38 | + FlowRecordRepository flowRecordRepository, |
| 39 | + BindDataSnapshot snapshot, |
| 40 | + Opinion opinion, |
| 41 | + IFlowOperator createOperator, |
| 42 | + IFlowOperator currentOperator, |
| 43 | + List<FlowRecord> historyRecords, |
| 44 | + FlowWork flowWork, |
| 45 | + String processId, |
| 46 | + long preId) { |
| 47 | + |
| 48 | + this.createOperator = createOperator; |
| 49 | + this.flowOperatorRepository = flowOperatorRepository; |
| 50 | + |
| 51 | + this.flowRecordRepository = flowRecordRepository; |
| 52 | + this.snapshot = snapshot; |
| 53 | + this.opinion = opinion; |
| 54 | + this.currentOperator = currentOperator; |
| 55 | + this.flowWork = flowWork; |
| 56 | + |
| 57 | + |
| 58 | + this.processId = processId; |
| 59 | + this.preId = preId; |
| 60 | + this.historyRecords = historyRecords; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * 获取下一个节点 |
| 66 | + * |
| 67 | + * @return 下一个节点 |
| 68 | + */ |
| 69 | + private FlowNode matcherNextNode(FlowNode flowNode, boolean back) { |
| 70 | + List<FlowRelation> relations = flowWork.getRelations().stream() |
| 71 | + .filter(relation -> relation.sourceMatcher(flowNode.getCode())) |
| 72 | + .filter(relation -> relation.isBack() == back) |
| 73 | + .sorted((o1, o2) -> (o2.getOrder() - o1.getOrder())) |
| 74 | + .toList(); |
| 75 | + if (relations.isEmpty()) { |
| 76 | + throw new IllegalArgumentException("relation not found"); |
| 77 | + } |
| 78 | + FlowContent flowContent = new FlowContent(flowWork, flowNode, createOperator, currentOperator, snapshot.toBindData(), opinion, historyRecords); |
| 79 | + List<FlowNode> flowNodes = new ArrayList<>(); |
| 80 | + for (FlowRelation flowRelation : relations) { |
| 81 | + FlowNode node = flowRelation.trigger(flowContent); |
| 82 | + if (node != null) { |
| 83 | + flowNodes.add(node); |
| 84 | + } |
| 85 | + } |
| 86 | + if (flowNodes.isEmpty()) { |
| 87 | + throw new IllegalArgumentException("next node not found"); |
| 88 | + } |
| 89 | + return flowNodes.get(0); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 异常匹配 |
| 94 | + * |
| 95 | + * @param currentNode 当前节点 |
| 96 | + * @param currentOperator 当前操作者 |
| 97 | + * @return 流程记录 |
| 98 | + */ |
| 99 | + private List<FlowRecord> errMatcher(FlowNode currentNode, IFlowOperator currentOperator) { |
| 100 | + if (currentNode.hasErrTrigger()) { |
| 101 | + FlowContent flowContent = new FlowContent(flowWork, currentNode, createOperator, currentOperator, snapshot.toBindData(), opinion, historyRecords); |
| 102 | + ErrorResult errorResult = currentNode.errMatcher(flowContent); |
| 103 | + if (errorResult == null) { |
| 104 | + throw new IllegalArgumentException("errMatcher match error."); |
| 105 | + } |
| 106 | + |
| 107 | + // 匹配操作者 |
| 108 | + if (errorResult.isOperator()) { |
| 109 | + List<FlowRecord> recordList = new ArrayList<>(); |
| 110 | + List<Long> operatorIds = ((OperatorResult) errorResult).getOperatorIds(); |
| 111 | + List<? extends IFlowOperator> operators = flowOperatorRepository.findByIds(operatorIds); |
| 112 | + for (IFlowOperator operator : operators) { |
| 113 | + FlowContent content = new FlowContent(flowWork, currentNode, createOperator, operator, snapshot.toBindData(), opinion, historyRecords); |
| 114 | + String recordTitle = currentNode.generateTitle(content); |
| 115 | + FlowRecord record = currentNode.createRecord(flowWork.getId(), processId, preId, recordTitle, createOperator, operator, snapshot); |
| 116 | + recordList.add(record); |
| 117 | + } |
| 118 | + return recordList; |
| 119 | + } |
| 120 | + // 匹配节点 |
| 121 | + if (errorResult.isNode()) { |
| 122 | + String nodeCode = ((NodeResult) errorResult).getNode(); |
| 123 | + FlowNode node = flowWork.getNodeByCode(nodeCode); |
| 124 | + if (node == null) { |
| 125 | + throw new IllegalArgumentException("node not found."); |
| 126 | + } |
| 127 | + List<FlowRecord> recordList = new ArrayList<>(); |
| 128 | + FlowContent content = new FlowContent(flowWork, node, createOperator, currentOperator, snapshot.toBindData(), opinion, historyRecords); |
| 129 | + List<? extends IFlowOperator> matcherOperators = node.loadFlowNodeOperator(content, flowOperatorRepository); |
| 130 | + if (!matcherOperators.isEmpty()) { |
| 131 | + for (IFlowOperator matcherOperator : matcherOperators) { |
| 132 | + String recordTitle = node.generateTitle(content); |
| 133 | + FlowRecord record = node.createRecord(flowWork.getId(), processId, preId, recordTitle, createOperator, matcherOperator, snapshot); |
| 134 | + recordList.add(record); |
| 135 | + } |
| 136 | + } |
| 137 | + return recordList; |
| 138 | + } |
| 139 | + throw new IllegalArgumentException("errMatcher not match."); |
| 140 | + } |
| 141 | + throw new IllegalArgumentException("operator not match."); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /** |
| 146 | + * 创建流程记录 |
| 147 | + * |
| 148 | + * @param currentNode 当前节点 |
| 149 | + * @return 流程记录 |
| 150 | + */ |
| 151 | + public List<FlowRecord> createRecord(FlowNode currentNode, IFlowOperator currentOperator) { |
| 152 | + FlowContent flowContent = new FlowContent(flowWork, currentNode, createOperator, currentOperator, snapshot.toBindData(), opinion, historyRecords); |
| 153 | + long workId = flowWork.getId(); |
| 154 | + List<? extends IFlowOperator> operators = currentNode.loadFlowNodeOperator(flowContent, flowOperatorRepository); |
| 155 | + if (operators.isEmpty()) { |
| 156 | + List<FlowRecord> errorRecordList = this.errMatcher(currentNode, currentOperator); |
| 157 | + if (errorRecordList.isEmpty()) { |
| 158 | + throw new IllegalArgumentException("operator not match."); |
| 159 | + } |
| 160 | + return errorRecordList; |
| 161 | + } else { |
| 162 | + String recordTitle = currentNode.generateTitle(flowContent); |
| 163 | + List<FlowRecord> recordList = new ArrayList<>(); |
| 164 | + for (IFlowOperator operator : operators) { |
| 165 | + FlowRecord record = currentNode.createRecord(workId, processId, preId, recordTitle, createOperator, operator, snapshot); |
| 166 | + recordList.add(record); |
| 167 | + } |
| 168 | + return recordList; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + /** |
| 174 | + * 创建下一个节点 |
| 175 | + */ |
| 176 | + public List<FlowRecord> createNextRecord(FlowNode flowNode) { |
| 177 | + FlowNode nextNode = this.matcherNextNode(flowNode, false); |
| 178 | + return this.createRecord(nextNode, currentOperator); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * 创建自定义的下级别节点 |
| 183 | + */ |
| 184 | + public List<FlowRecord> createCustomBackRecord(FlowNode flowNode, long parentRecordId) { |
| 185 | + FlowNode nextNode = this.matcherNextNode(flowNode, true); |
| 186 | + if (nextNode == null) { |
| 187 | + throw new IllegalArgumentException("next node not found"); |
| 188 | + } |
| 189 | + IFlowOperator flowOperator = currentOperator; |
| 190 | + if (nextNode.isAnyOperatorMatcher()) { |
| 191 | + // 如果是任意人员操作时则需要指定为当时审批人员为当前审批人员 |
| 192 | + FlowRecord preFlowRecord = flowRecordRepository.getFlowRecordById(parentRecordId); |
| 193 | + while (preFlowRecord.isTransfer() || !preFlowRecord.getNodeCode().equals(nextNode.getCode())) { |
| 194 | + preFlowRecord = flowRecordRepository.getFlowRecordById(preFlowRecord.getPreId()); |
| 195 | + } |
| 196 | + flowOperator = flowOperatorRepository.getFlowOperatorById(preFlowRecord.getCurrentOperatorId()); |
| 197 | + } |
| 198 | + return this.createRecord(nextNode, flowOperator); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * 创建默认拒绝时的流程记录 |
| 203 | + */ |
| 204 | + public List<FlowRecord> createDefaultBackRecord(long parentRecordId) { |
| 205 | + IFlowOperator flowOperator; |
| 206 | + // 拒绝时,默认返回上一个节点 |
| 207 | + FlowRecord preRecord = flowRecordRepository.getFlowRecordById(parentRecordId); |
| 208 | + // 去除所有的转办的记录 |
| 209 | + while (preRecord.isTransfer()) { |
| 210 | + // 继续寻找上一个节点 |
| 211 | + preRecord = flowRecordRepository.getFlowRecordById(preRecord.getPreId()); |
| 212 | + } |
| 213 | + // 获取上一个节点的审批者,继续将审批者设置为当前审批者 |
| 214 | + flowOperator = flowOperatorRepository.getFlowOperatorById(preRecord.getCurrentOperatorId()); |
| 215 | + FlowNode nextNode = flowWork.getNodeByCode(preRecord.getNodeCode()); |
| 216 | + if (nextNode == null) { |
| 217 | + throw new IllegalArgumentException("next node not found"); |
| 218 | + } |
| 219 | + return this.createRecord(nextNode, flowOperator); |
| 220 | + } |
| 221 | + |
11 | 222 |
|
12 | 223 | } |
0 commit comments