|
@@ -1,5 +1,6 @@
|
|
|
package com.fjhx.flow.service.flow.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
@@ -37,7 +38,6 @@ import java.lang.reflect.Method;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@@ -85,10 +85,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(q -> q
|
|
|
.eq(FlowDefinitionNode::getFlowDefinitionId, flowDefinition.getId()));
|
|
|
|
|
|
-
|
|
|
- Map<Long, List<FlowDefinitionNode>> parentNodeMap = flowDefinitionNodeList.stream()
|
|
|
- .collect(Collectors.groupingBy(FlowDefinitionNode::getParentId));
|
|
|
-
|
|
|
|
|
|
FlowDefinitionNode startNode = flowDefinitionNodeList.stream()
|
|
|
.filter(item -> NodeTypeEnum.START.getKey().equals(item.getNodeType()))
|
|
@@ -109,7 +105,32 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
FlowThreadLocalUtil.setFlowId(flowId);
|
|
|
|
|
|
|
|
|
- FlowDefinitionNode nextUserNode = getNextUserNode(startNode, parentNodeMap);
|
|
|
+ FlowDefinitionNode nextUserNode = getNextUserNode(startNode, flowDefinitionNodeList);
|
|
|
+ NodeTypeEnum nextNodeType = NodeTypeEnum.getEnum(nextUserNode.getNodeType());
|
|
|
+
|
|
|
+
|
|
|
+ FlowExample flowExample = new FlowExample();
|
|
|
+
|
|
|
+
|
|
|
+ if (!NodeTypeEnum.END.equals(nextNodeType)) {
|
|
|
+
|
|
|
+ if (dto.getHandleUserId() == null) {
|
|
|
+ FlowResult handleUser = getHandleUser(nextUserNode);
|
|
|
+
|
|
|
+ if (handleUser.getSuccess()) {
|
|
|
+ flowExample.setHandleUserId(handleUser.getUserId());
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+ return handleUser;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ flowExample.setHandleUserId(dto.getHandleUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
Long businessId;
|
|
@@ -122,15 +143,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
|
|
|
|
|
|
-
|
|
|
- FlowExample flowExample = new FlowExample();
|
|
|
- flowExample.setTitle(StrUtil.format(flowDefinition.getTitleTemplate(), templateMap, true));
|
|
|
- flowExample.setFlowKey(dto.getFlowKey());
|
|
|
- flowExample.setDefinitionId(flowDefinition.getId());
|
|
|
- flowExample.setDefinitionNodeId(nextUserNode.getId());
|
|
|
- flowExample.setBusinessId(businessId);
|
|
|
- flowExample.setStartData(dto.getData().toJSONString());
|
|
|
-
|
|
|
|
|
|
FlowExampleDetail startExampleDetail = new FlowExampleDetail();
|
|
|
startExampleDetail.setFlowExampleId(flowId);
|
|
@@ -141,7 +153,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
startExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
|
|
|
flowExampleDetailList.add(startExampleDetail);
|
|
|
|
|
|
- if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(nextUserNode.getNodeType()))) {
|
|
|
+ if (NodeTypeEnum.END.equals(nextNodeType)) {
|
|
|
|
|
|
|
|
|
FlowThreadLocalUtil.setBusinessId(businessId);
|
|
@@ -155,7 +167,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
endExampleDetail.setFlowDefinitionNodeId(nextUserNode.getId());
|
|
|
endExampleDetail.setFlowDefinitionNodeType(nextUserNode.getNodeType());
|
|
|
endExampleDetail.setHandleType(HandleTypeEnum.OVER.getKey());
|
|
|
- endExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
|
|
|
flowExampleDetailList.add(endExampleDetail);
|
|
|
|
|
|
|
|
@@ -166,23 +177,15 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
|
|
|
|
|
|
-
|
|
|
- if (dto.getHandleUserId() == null) {
|
|
|
- FlowResult handleUser = getHandleUser(nextUserNode);
|
|
|
-
|
|
|
- if (handleUser.getSuccess()) {
|
|
|
-
|
|
|
- flowExample.setHandleUserId(handleUser.getUserId());
|
|
|
- } else {
|
|
|
- return handleUser;
|
|
|
- }
|
|
|
- } else {
|
|
|
-
|
|
|
- flowExample.setHandleUserId(dto.getHandleUserId());
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
|
|
|
+ flowExample.setTitle(StrUtil.format(flowDefinition.getTitleTemplate(), templateMap, true));
|
|
|
+ flowExample.setFlowKey(dto.getFlowKey());
|
|
|
+ flowExample.setDefinitionId(flowDefinition.getId());
|
|
|
+ flowExample.setDefinitionNodeId(nextUserNode.getId());
|
|
|
+ flowExample.setBusinessId(businessId);
|
|
|
+ flowExample.setStartData(dto.getData().toJSONString());
|
|
|
+
|
|
|
flowExampleService.save(flowExample);
|
|
|
flowExampleDetailService.saveBatch(flowExampleDetailList);
|
|
|
|
|
@@ -197,17 +200,17 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
Long flowId = dto.getFlowId();
|
|
|
|
|
|
FlowExample flowExample = flowExampleService.getById(flowId);
|
|
|
+
|
|
|
if (flowExample == null) {
|
|
|
throw new ServiceException("没有找到流程");
|
|
|
}
|
|
|
|
|
|
- if (!flowExample.getVersion().equals(dto.getVersion())) {
|
|
|
- throw new ServiceException("流程已被处理");
|
|
|
+ if (flowExample.getStatus() > 1) {
|
|
|
+ throw new ServiceException("流程已结束");
|
|
|
}
|
|
|
|
|
|
- Integer status = flowExample.getStatus();
|
|
|
- if (status > 1) {
|
|
|
- throw new ServiceException("流程已结束");
|
|
|
+ if (ObjectUtil.notEqual(flowExample.getVersion(), dto.getVersion())) {
|
|
|
+ throw new ServiceException("流程已被处理");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -221,9 +224,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
.eq(FlowDefinitionNode::getFlowDefinitionId, flowExample.getDefinitionId()));
|
|
|
|
|
|
|
|
|
- Long definitionNodeId = flowExample.getDefinitionNodeId();
|
|
|
FlowDefinitionNode currentNode = flowDefinitionNodeList.stream()
|
|
|
- .filter(item -> item.getId().equals(definitionNodeId))
|
|
|
+ .filter(item -> item.getId().equals(flowExample.getDefinitionNodeId()))
|
|
|
.findFirst().orElse(null);
|
|
|
|
|
|
if (currentNode == null) {
|
|
@@ -234,83 +236,46 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
HandleTypeEnum handleTypeEnum = HandleTypeEnum.getEnum(dto.getHandleType());
|
|
|
|
|
|
|
|
|
- JSONObject startDataJson = JSONObject.parseObject(flowExample.getStartData());
|
|
|
- Map<String, Object> templateMap = flowDelegate.initTemplateMap(dto.getData(), startDataJson);
|
|
|
+ JSONObject startData = JSONObject.parseObject(flowExample.getStartData());
|
|
|
+ Map<String, Object> templateMap = flowDelegate.initTemplateMap(dto.getData(), startData);
|
|
|
FlowThreadLocalUtil.setTemplateData(templateMap);
|
|
|
- FlowThreadLocalUtil.setStartData(dto.getData());
|
|
|
+ FlowThreadLocalUtil.setStartData(startData);
|
|
|
FlowThreadLocalUtil.setCurrentData(dto.getData());
|
|
|
FlowThreadLocalUtil.setFlowId(flowId);
|
|
|
FlowThreadLocalUtil.setBusinessId(flowExample.getBusinessId());
|
|
|
FlowThreadLocalUtil.setHandleTypeEnum(handleTypeEnum);
|
|
|
|
|
|
-
|
|
|
- String handlingMethod = currentNode.getHandlingMethod();
|
|
|
- if (StrUtil.isNotBlank(handlingMethod)) {
|
|
|
- try {
|
|
|
- invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("跳转节点方法异常", e);
|
|
|
- throw new ServiceException("跳转节点方法异常");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
|
|
|
- FlowExampleDetail nodeExampleDetail = new FlowExampleDetail();
|
|
|
- nodeExampleDetail.setFlowExampleId(flowId);
|
|
|
- nodeExampleDetail.setFlowDefinitionNodeId(currentNode.getId());
|
|
|
- nodeExampleDetail.setFlowDefinitionNodeType(currentNode.getNodeType());
|
|
|
- nodeExampleDetail.setHandleType(dto.getHandleType());
|
|
|
- nodeExampleDetail.setHandleRemark(dto.getRemark());
|
|
|
- nodeExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
|
|
|
- flowExampleDetailList.add(nodeExampleDetail);
|
|
|
-
|
|
|
|
|
|
- FlowDefinitionNode jumpUserNode;
|
|
|
+ FlowDefinitionNode nextUserNode = null;
|
|
|
switch (handleTypeEnum) {
|
|
|
case NEXT:
|
|
|
|
|
|
-
|
|
|
- Map<Long, List<FlowDefinitionNode>> parentNodeMap = flowDefinitionNodeList.stream()
|
|
|
- .collect(Collectors.groupingBy(FlowDefinitionNode::getParentId));
|
|
|
-
|
|
|
|
|
|
- jumpUserNode = getNextUserNode(currentNode, parentNodeMap);
|
|
|
+ nextUserNode = getNextUserNode(currentNode, flowDefinitionNodeList);
|
|
|
|
|
|
|
|
|
- flowExample.setDefinitionNodeId(jumpUserNode.getId());
|
|
|
+ flowExample.setDefinitionNodeId(nextUserNode.getId());
|
|
|
|
|
|
|
|
|
- if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(jumpUserNode.getNodeType()))) {
|
|
|
-
|
|
|
-
|
|
|
- invokeEndMethod(jumpUserNode, flowDelegateCls, flowDelegate);
|
|
|
-
|
|
|
-
|
|
|
- FlowExampleDetail endExampleDetail = new FlowExampleDetail();
|
|
|
- endExampleDetail.setFlowExampleId(flowId);
|
|
|
- endExampleDetail.setFlowDefinitionNodeId(jumpUserNode.getId());
|
|
|
- endExampleDetail.setFlowDefinitionNodeType(jumpUserNode.getNodeType());
|
|
|
- endExampleDetail.setHandleType(HandleTypeEnum.OVER.getKey());
|
|
|
- endExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
|
|
|
- flowExampleDetailList.add(endExampleDetail);
|
|
|
-
|
|
|
+ if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(nextUserNode.getNodeType()))) {
|
|
|
|
|
|
flowExample.setStatus(FlowStatusEnum.HAVE_PASSED.getKey());
|
|
|
-
|
|
|
} else {
|
|
|
|
|
|
-
|
|
|
+
|
|
|
if (dto.getHandleUserId() == null) {
|
|
|
- FlowResult handleUser = getHandleUser(jumpUserNode);
|
|
|
-
|
|
|
+ FlowResult handleUser = getHandleUser(nextUserNode);
|
|
|
+
|
|
|
if (handleUser.getSuccess()) {
|
|
|
-
|
|
|
flowExample.setHandleUserId(handleUser.getUserId());
|
|
|
- } else {
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
return handleUser;
|
|
|
}
|
|
|
- } else {
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
|
|
|
flowExample.setHandleUserId(dto.getHandleUserId());
|
|
|
}
|
|
@@ -322,45 +287,20 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
case PREVIOUS:
|
|
|
|
|
|
-
|
|
|
- Map<Long, FlowDefinitionNode> nodeMap = flowDefinitionNodeList.stream()
|
|
|
- .collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
|
|
|
-
|
|
|
|
|
|
- jumpUserNode = getLastOneUserNode(currentNode, nodeMap);
|
|
|
+ FlowExampleDetail lastOneFlowExampleDetail = getLastOneUserNode(flowId);
|
|
|
|
|
|
|
|
|
- flowExample.setDefinitionNodeId(jumpUserNode.getId());
|
|
|
+ flowExample.setDefinitionNodeId(lastOneFlowExampleDetail.getFlowDefinitionNodeId());
|
|
|
+ flowExample.setHandleUserId(lastOneFlowExampleDetail.getCreateUser());
|
|
|
|
|
|
|
|
|
- if (NodeTypeEnum.START.equals(NodeTypeEnum.getEnum(jumpUserNode.getNodeType()))) {
|
|
|
-
|
|
|
+ if (NodeTypeEnum.START.getKey().equals(lastOneFlowExampleDetail.getFlowDefinitionNodeType())) {
|
|
|
|
|
|
flowExample.setStatus(FlowStatusEnum.UNINITIATED.getKey());
|
|
|
-
|
|
|
-
|
|
|
- flowExample.setHandleUserId(flowExample.getCreateUser());
|
|
|
-
|
|
|
} else {
|
|
|
-
|
|
|
|
|
|
flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
|
|
|
-
|
|
|
-
|
|
|
- if (dto.getHandleUserId() == null) {
|
|
|
- FlowResult handleUser = getHandleUser(jumpUserNode);
|
|
|
-
|
|
|
- if (handleUser.getSuccess()) {
|
|
|
-
|
|
|
- flowExample.setHandleUserId(handleUser.getUserId());
|
|
|
- } else {
|
|
|
- return handleUser;
|
|
|
- }
|
|
|
- } else {
|
|
|
-
|
|
|
- flowExample.setHandleUserId(dto.getHandleUserId());
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
break;
|
|
|
|
|
@@ -373,7 +313,49 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
}
|
|
|
|
|
|
- flowExampleService.updateById(flowExample);
|
|
|
+
|
|
|
+ String handlingMethod = currentNode.getHandlingMethod();
|
|
|
+ if (StrUtil.isNotBlank(handlingMethod)) {
|
|
|
+ try {
|
|
|
+ invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("跳转节点方法异常", e);
|
|
|
+ throw new ServiceException("跳转节点方法异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
|
|
|
+ FlowExampleDetail nodeExampleDetail = new FlowExampleDetail();
|
|
|
+ nodeExampleDetail.setFlowExampleId(flowId);
|
|
|
+ nodeExampleDetail.setFlowDefinitionNodeId(currentNode.getId());
|
|
|
+ nodeExampleDetail.setFlowDefinitionNodeType(currentNode.getNodeType());
|
|
|
+ nodeExampleDetail.setHandleType(dto.getHandleType());
|
|
|
+ nodeExampleDetail.setHandleRemark(dto.getRemark());
|
|
|
+ nodeExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
|
|
|
+ flowExampleDetailList.add(nodeExampleDetail);
|
|
|
+
|
|
|
+
|
|
|
+ if (FlowStatusEnum.HAVE_PASSED.getKey().equals(flowExample.getStatus())) {
|
|
|
+
|
|
|
+
|
|
|
+ invokeEndMethod(nextUserNode, flowDelegateCls, flowDelegate);
|
|
|
+
|
|
|
+
|
|
|
+ FlowExampleDetail endExampleDetail = new FlowExampleDetail();
|
|
|
+ endExampleDetail.setFlowExampleId(flowId);
|
|
|
+ endExampleDetail.setFlowDefinitionNodeId(nextUserNode.getId());
|
|
|
+ endExampleDetail.setFlowDefinitionNodeType(nextUserNode.getNodeType());
|
|
|
+ endExampleDetail.setHandleType(HandleTypeEnum.OVER.getKey());
|
|
|
+ flowExampleDetailList.add(endExampleDetail);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ flowExample.setVersion(dto.getVersion());
|
|
|
+ boolean b = flowExampleService.updateById(flowExample);
|
|
|
+ if (!b) {
|
|
|
+ throw new ServiceException("流程已被处理");
|
|
|
+ }
|
|
|
flowExampleDetailService.saveBatch(flowExampleDetailList);
|
|
|
|
|
|
FlowResult flowResult = new FlowResult();
|
|
@@ -434,11 +416,14 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
* 查找下一个用户执行节点
|
|
|
*
|
|
|
- * @param currentNode 当前节点
|
|
|
- * @param parentNodeMap 父节点map
|
|
|
+ * @param currentNode 当前节点
|
|
|
+ * @param flowDefinitionNodeList 流程节点列表
|
|
|
* @return 用户节点
|
|
|
*/
|
|
|
- private FlowDefinitionNode getNextUserNode(FlowDefinitionNode currentNode, Map<Long, List<FlowDefinitionNode>> parentNodeMap) {
|
|
|
+ private FlowDefinitionNode getNextUserNode(FlowDefinitionNode currentNode, List<FlowDefinitionNode> flowDefinitionNodeList) {
|
|
|
+
|
|
|
+ Map<Long, List<FlowDefinitionNode>> parentNodeMap = flowDefinitionNodeList.stream()
|
|
|
+ .collect(Collectors.groupingBy(FlowDefinitionNode::getParentId));
|
|
|
|
|
|
List<FlowDefinitionNode> nextNodeList = parentNodeMap.get(currentNode.getId());
|
|
|
FlowDefinitionNode nextNode = null;
|
|
@@ -448,15 +433,15 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
for (FlowDefinitionNode flowDefinitionNode : nextNodeList) {
|
|
|
|
|
|
String jumpCondition = flowDefinitionNode.getJumpCondition();
|
|
|
-
|
|
|
- if (StrUtil.isBlank(jumpCondition)) {
|
|
|
- nextNode = flowDefinitionNode;
|
|
|
- }
|
|
|
|
|
|
if (expressionResult(FlowThreadLocalUtil.getTemplateData(), jumpCondition)) {
|
|
|
nextNode = flowDefinitionNode;
|
|
|
break;
|
|
|
}
|
|
|
+
|
|
|
+ if (StrUtil.isBlank(jumpCondition)) {
|
|
|
+ nextNode = flowDefinitionNode;
|
|
|
+ }
|
|
|
}
|
|
|
if (nextNode == null) {
|
|
|
throw new ServiceException("未识别到分支跳转节点");
|
|
@@ -472,7 +457,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
}
|
|
|
|
|
|
if (NodeTypeEnum.BRANCH.equals(NodeTypeEnum.getEnum(nextNode.getNodeType()))) {
|
|
|
- return getNextUserNode(nextNode, parentNodeMap);
|
|
|
+ return getNextUserNode(nextNode, flowDefinitionNodeList);
|
|
|
}
|
|
|
return nextNode;
|
|
|
}
|
|
@@ -480,16 +465,20 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
* 查找上一个节点
|
|
|
*
|
|
|
- * @param currentNode 当前节点
|
|
|
- * @param nodeMap id节点map
|
|
|
+ * @param flowExampleId 流程实例id
|
|
|
* @return 用户节点
|
|
|
*/
|
|
|
- private FlowDefinitionNode getLastOneUserNode(FlowDefinitionNode currentNode, Map<Long, FlowDefinitionNode> nodeMap) {
|
|
|
- FlowDefinitionNode lastOneUserNode = nodeMap.get(currentNode.getParentId());
|
|
|
- if (NodeTypeEnum.BRANCH.equals(NodeTypeEnum.getEnum(lastOneUserNode.getNodeType()))) {
|
|
|
- return getLastOneUserNode(lastOneUserNode, nodeMap);
|
|
|
+ private FlowExampleDetail getLastOneUserNode(Long flowExampleId) {
|
|
|
+ FlowExampleDetail flowExampleDetail = flowExampleDetailService.getOne(q -> q
|
|
|
+ .eq(FlowExampleDetail::getFlowExampleId, flowExampleId)
|
|
|
+ .orderByDesc(BaseIdPo::getId)
|
|
|
+ .last("limit 1,1"));
|
|
|
+
|
|
|
+ if (flowExampleDetail == null) {
|
|
|
+ throw new ServiceException("没有找到回退节点");
|
|
|
}
|
|
|
- return lastOneUserNode;
|
|
|
+
|
|
|
+ return flowExampleDetail;
|
|
|
}
|
|
|
|
|
|
|