|
@@ -2,7 +2,6 @@ package com.fjhx.flow.service.flow.impl;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
-import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.fjhx.flow.core.FlowBean;
|
|
|
import com.fjhx.flow.core.FlowDelegate;
|
|
@@ -18,9 +17,9 @@ import com.fjhx.flow.enums.NodeTypeEnum;
|
|
|
import com.fjhx.flow.service.flow.*;
|
|
|
import com.googlecode.aviator.AviatorEvaluator;
|
|
|
import com.googlecode.aviator.Expression;
|
|
|
-import com.ruoyi.common.constant.DatasourceConstant;
|
|
|
import com.ruoyi.common.constant.StatusConstant;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
@@ -33,6 +32,7 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
@@ -77,12 +77,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
throw new ServiceException("没有找到可用流程");
|
|
|
}
|
|
|
|
|
|
- // 初始化模板参数
|
|
|
- Map<String, Object> templateMap = flowDelegate.initTemplateMap();
|
|
|
- FlowThreadLocalUtil.setTemplateData(templateMap);
|
|
|
- FlowThreadLocalUtil.setStartData(dto.getData());
|
|
|
- FlowThreadLocalUtil.setCurrentData(dto.getData());
|
|
|
-
|
|
|
// 流程节点列表
|
|
|
List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService
|
|
|
.list(q -> q.eq(FlowDefinitionNode::getFlowDefinitionId, flowDefinition.getId()));
|
|
@@ -100,22 +94,30 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
throw new ServiceException("没有找到开始节点");
|
|
|
}
|
|
|
|
|
|
+ // 生成流程id
|
|
|
+ long flowId = IdWorker.getId();
|
|
|
+
|
|
|
+ // 初始化模板参数
|
|
|
+ Map<String, Object> templateMap = flowDelegate.initTemplateMap(dto.getData(), dto.getData());
|
|
|
+
|
|
|
+ FlowThreadLocalUtil.setTemplateData(templateMap);
|
|
|
+ FlowThreadLocalUtil.setStartData(dto.getData());
|
|
|
+ FlowThreadLocalUtil.setCurrentData(dto.getData());
|
|
|
+ FlowThreadLocalUtil.setFlowId(flowId);
|
|
|
+
|
|
|
// 寻找下一节点
|
|
|
FlowDefinitionNode nextUserNode = getNextUserNode(startNode, parentNodeMap);
|
|
|
|
|
|
+ // 流程实例
|
|
|
+ FlowExample flowExample = new FlowExample();
|
|
|
+
|
|
|
+ // 流程实例明细列表
|
|
|
+ List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
|
|
|
+
|
|
|
// 开启一个事务
|
|
|
TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
|
|
|
|
|
|
try {
|
|
|
- // 生成流程id
|
|
|
- long flowId = IdWorker.getId();
|
|
|
- FlowThreadLocalUtil.setFlowId(flowId);
|
|
|
-
|
|
|
- // 切换到主库执行流程开始节点逻辑
|
|
|
- // DynamicDataSourceContextHolder.push(DatasourceConstant.MASTER_NAME);
|
|
|
-
|
|
|
- FlowExample flowExample = new FlowExample();
|
|
|
- List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
|
|
|
|
|
|
// 执行开始流程方法
|
|
|
Long businessId = flowDelegate.start(flowId, dto.getData());
|
|
@@ -133,8 +135,12 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
// 走非默认方法
|
|
|
String handlingMethod = nextUserNode.getHandlingMethod();
|
|
|
if (StrUtil.isNotBlank(handlingMethod)) {
|
|
|
- Method method = flowDelegateCls.getMethod(handlingMethod);
|
|
|
- method.invoke(flowDelegate);
|
|
|
+ try {
|
|
|
+ Method method = flowDelegateCls.getMethod(handlingMethod);
|
|
|
+ method.invoke(flowDelegate);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("未找到指定处理方法:" + handlingMethod);
|
|
|
+ }
|
|
|
} else {
|
|
|
// 执行流程结束方法
|
|
|
flowDelegate.end();
|
|
@@ -145,20 +151,17 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
// 结束流程
|
|
|
FlowExampleDetail endExampleDetail = new FlowExampleDetail();
|
|
|
- startExampleDetail.setFlowExampleId(flowId);
|
|
|
- startExampleDetail.setFlowDefinitionNodeId(nextUserNode.getId());
|
|
|
- startExampleDetail.setFlowDefinitionNodeType(nextUserNode.getNodeType());
|
|
|
- startExampleDetail.setHandleType(HandleType.OVER.getKey());
|
|
|
+ endExampleDetail.setFlowExampleId(flowId);
|
|
|
+ endExampleDetail.setFlowDefinitionNodeId(nextUserNode.getId());
|
|
|
+ endExampleDetail.setFlowDefinitionNodeType(nextUserNode.getNodeType());
|
|
|
+ endExampleDetail.setHandleType(HandleType.OVER.getKey());
|
|
|
flowExampleDetailList.add(endExampleDetail);
|
|
|
} else {
|
|
|
// 流程进行中
|
|
|
flowExample.setStatus(FlowStatus.IN_PROGRESS.getKey());
|
|
|
}
|
|
|
|
|
|
- // 切换到从库执行流程信息保存
|
|
|
- DynamicDataSourceContextHolder.push(DatasourceConstant.BASE);
|
|
|
-
|
|
|
- flowExample.setTitle(StrUtil.format(flowDefinition.getTitleTemplate(), templateMap, false));
|
|
|
+ flowExample.setTitle(StrUtil.format(flowDefinition.getTitleTemplate(), templateMap, true));
|
|
|
flowExample.setDefinitionId(flowDefinition.getId());
|
|
|
flowExample.setDefintionNodeId(nextUserNode.getId());
|
|
|
flowExample.setBusinessId(businessId);
|
|
@@ -171,6 +174,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
} catch (Exception e) {
|
|
|
// 回滚事务
|
|
|
platformTransactionManager.rollback(transaction);
|
|
|
+ log.error("业务错误", e);
|
|
|
+ throw new ServiceException("业务代码错误");
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -183,38 +188,42 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
* @return 用户节点
|
|
|
*/
|
|
|
private FlowDefinitionNode getNextUserNode(FlowDefinitionNode currentNode, Map<Long, List<FlowDefinitionNode>> parentNodeMap) {
|
|
|
-
|
|
|
// 查找下个节点
|
|
|
List<FlowDefinitionNode> nextNodeList = parentNodeMap.get(currentNode.getId());
|
|
|
-
|
|
|
- // 如果不为分支,返回此节点
|
|
|
- if (!NodeTypeEnum.BRANCH.equals(NodeTypeEnum.getEnum(currentNode.getNodeType()))) {
|
|
|
-
|
|
|
- if (nextNodeList.size() != 1) {
|
|
|
- throw new ServiceException("流程定义错误");
|
|
|
+ FlowDefinitionNode nextNode = null;
|
|
|
+ // 如果为分支
|
|
|
+ if (NodeTypeEnum.BRANCH.equals(NodeTypeEnum.getEnum(currentNode.getNodeType()))) {
|
|
|
+ // 循环分支
|
|
|
+ for (FlowDefinitionNode flowDefinitionNode : nextNodeList) {
|
|
|
+ // 获取跳转条件
|
|
|
+ String jumpCondition = flowDefinitionNode.getJumpCondition();
|
|
|
+ // 跳转条件为空表示默认跳转
|
|
|
+ if (StrUtil.isBlank(jumpCondition)) {
|
|
|
+ nextNode = flowDefinitionNode;
|
|
|
+ }
|
|
|
+ // 符合条件
|
|
|
+ if (expressionResult(FlowThreadLocalUtil.getTemplateData(), jumpCondition)) {
|
|
|
+ nextNode = flowDefinitionNode;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- return nextNodeList.get(0);
|
|
|
- }
|
|
|
-
|
|
|
- // 找到分支跳转节点
|
|
|
- FlowDefinitionNode defaultDefinitionNode = null;
|
|
|
- Map<String, Object> templateData = FlowThreadLocalUtil.getTemplateData();
|
|
|
- for (FlowDefinitionNode flowDefinitionNode : nextNodeList) {
|
|
|
- String jumpCondition = flowDefinitionNode.getJumpCondition();
|
|
|
- if (StrUtil.isBlank(jumpCondition)) {
|
|
|
- defaultDefinitionNode = flowDefinitionNode;
|
|
|
+ if (nextNode == null) {
|
|
|
+ throw new ServiceException("未识别到分支跳转节点");
|
|
|
}
|
|
|
- if (expressionResult(templateData, jumpCondition)) {
|
|
|
- return flowDefinitionNode;
|
|
|
+ }
|
|
|
+ // 如果不为分支
|
|
|
+ else {
|
|
|
+ if (nextNodeList.size() != 1) {
|
|
|
+ throw new ServiceException("流程定义错误");
|
|
|
}
|
|
|
+ // 查找下一个节点方法
|
|
|
+ nextNode = nextNodeList.get(0);
|
|
|
}
|
|
|
-
|
|
|
- if (defaultDefinitionNode == null) {
|
|
|
- throw new ServiceException("未识别到分支跳转节点");
|
|
|
+ // 如果下一节点为分支,递归找下一节点
|
|
|
+ if (NodeTypeEnum.BRANCH.equals(NodeTypeEnum.getEnum(nextNode.getNodeType()))) {
|
|
|
+ return getNextUserNode(nextNode, parentNodeMap);
|
|
|
}
|
|
|
-
|
|
|
- return defaultDefinitionNode;
|
|
|
+ return nextNode;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -226,16 +235,4 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
return Boolean.parseBoolean(String.valueOf(execute));
|
|
|
}
|
|
|
|
|
|
- // public static void main(String[] args) {
|
|
|
- // // HashMap<Object, Object> map1 = new HashMap<>();
|
|
|
- // // map1.put("test2", "2");
|
|
|
- // //
|
|
|
- // HashMap<String, Object> map2 = new HashMap<>();
|
|
|
- //
|
|
|
- // map2.put("admin3", "dsaasdsad");
|
|
|
- // map2.put("admin1", 2);
|
|
|
- //
|
|
|
- // System.out.println(expressionResult(map2, "admin1 > 2"));
|
|
|
- // }
|
|
|
-
|
|
|
}
|