|
@@ -1,11 +1,12 @@
|
|
|
package com.fjhx.flow.service.flow.impl;
|
|
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
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;
|
|
|
+import com.fjhx.flow.core.FlowThreadLocalUtil;
|
|
|
import com.fjhx.flow.entity.flow.dto.InitiateDto;
|
|
|
import com.fjhx.flow.entity.flow.po.FlowDefinition;
|
|
|
import com.fjhx.flow.entity.flow.po.FlowDefinitionNode;
|
|
@@ -26,7 +27,11 @@ import org.springframework.transaction.PlatformTransactionManager;
|
|
|
import org.springframework.transaction.TransactionDefinition;
|
|
|
import org.springframework.transaction.TransactionStatus;
|
|
|
|
|
|
-import java.util.*;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -58,13 +63,10 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
|
|
|
FlowDelegate flowDelegate;
|
|
|
- try {
|
|
|
- flowDelegate = flowDelegateCls.newInstance();
|
|
|
- flowDelegate.initTemplateMap();
|
|
|
- } catch (InstantiationException e) {
|
|
|
- throw new ServiceException("实例化异常");
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
- throw new ServiceException("非法访问异常");
|
|
|
+
|
|
|
+ flowDelegate = SpringUtil.getBean(flowDelegateCls);
|
|
|
+ if (flowDelegate == null) {
|
|
|
+ throw new ServiceException("没有把流程bean注册到spring中");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -76,6 +78,12 @@ 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()));
|
|
@@ -102,6 +110,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
try {
|
|
|
|
|
|
long flowId = IdWorker.getId();
|
|
|
+ FlowThreadLocalUtil.setFlowId(flowId);
|
|
|
|
|
|
|
|
|
DynamicDataSourceContextHolder.push(DatasourceConstant.MASTER_NAME);
|
|
@@ -111,6 +120,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
|
|
|
Long businessId = flowDelegate.start(flowId, dto.getData());
|
|
|
+ FlowThreadLocalUtil.setBusinessId(businessId);
|
|
|
|
|
|
FlowExampleDetail startExampleDetail = new FlowExampleDetail();
|
|
|
startExampleDetail.setFlowExampleId(flowId);
|
|
@@ -121,10 +131,15 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
|
|
|
if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(nextUserNode.getNodeType()))) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- flowDelegate.end();
|
|
|
+
|
|
|
+ String handlingMethod = nextUserNode.getHandlingMethod();
|
|
|
+ if (StrUtil.isNotBlank(handlingMethod)) {
|
|
|
+ Method method = flowDelegateCls.getMethod(handlingMethod);
|
|
|
+ method.invoke(flowDelegate);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ flowDelegate.end();
|
|
|
+ }
|
|
|
|
|
|
|
|
|
flowExample.setStatus(FlowStatus.HAVE_PASSED.getKey());
|
|
@@ -144,10 +159,11 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
|
|
|
DynamicDataSourceContextHolder.push(DatasourceConstant.SLAVE_NAME);
|
|
|
|
|
|
- flowExample.setTitle(getTitle(flowDelegate.getTemplateMap()));
|
|
|
+ flowExample.setTitle(StrUtil.format(flowDefinition.getTitleTemplate(), templateMap, false));
|
|
|
flowExample.setDefinitionId(flowDefinition.getId());
|
|
|
flowExample.setDefintionNodeId(nextUserNode.getId());
|
|
|
flowExample.setBusinessId(businessId);
|
|
|
+ flowExample.setStartData(dto.getData().toJSONString());
|
|
|
flowExampleService.save(flowExample);
|
|
|
flowExampleDetailService.saveBatch(flowExampleDetailList);
|
|
|
|
|
@@ -182,44 +198,45 @@ public class FlowProcessServiceImpl implements FlowProcessService {
|
|
|
return nextNodeList.get(0);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ FlowDefinitionNode defaultDefinitionNode = null;
|
|
|
+ Map<String, Object> templateData = FlowThreadLocalUtil.getTemplateData();
|
|
|
for (FlowDefinitionNode flowDefinitionNode : nextNodeList) {
|
|
|
- String handlingMethod = flowDefinitionNode.getHandlingMethod();
|
|
|
- if (StrUtil.isBlank(handlingMethod)) {
|
|
|
- throw new ServiceException("未配置分支流转表达式");
|
|
|
+ String jumpCondition = flowDefinitionNode.getJumpCondition();
|
|
|
+ if (StrUtil.isBlank(jumpCondition)) {
|
|
|
+ defaultDefinitionNode = flowDefinitionNode;
|
|
|
+ }
|
|
|
+ if (expressionResult(templateData, jumpCondition)) {
|
|
|
+ return flowDefinitionNode;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- return nextNodeList.get(0);
|
|
|
- }
|
|
|
+ if (defaultDefinitionNode == null) {
|
|
|
+ throw new ServiceException("未识别到分支跳转节点");
|
|
|
+ }
|
|
|
|
|
|
- private String getTitle(Map<String, String> map) {
|
|
|
- return "";
|
|
|
+ return defaultDefinitionNode;
|
|
|
}
|
|
|
|
|
|
|
|
|
* 校验el表达式
|
|
|
- *
|
|
|
- * @param map
|
|
|
- * @param expression
|
|
|
- * @return
|
|
|
*/
|
|
|
public static boolean expressionResult(Map<String, Object> map, String expression) {
|
|
|
Expression exp = AviatorEvaluator.compile(expression);
|
|
|
- final Object execute = exp.execute(map);
|
|
|
+ Object execute = exp.execute(map);
|
|
|
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);
|
|
|
|
|
|
- HashMap<String, Object> map = new HashMap<>();
|
|
|
- map.put("admin", new Date());
|
|
|
- map.put("admin2", DateUtil.offsetDay(new Date(), 1));
|
|
|
- System.out.println(expressionResult(map, "admin < admin2"));
|
|
|
+ System.out.println(expressionResult(map2, "admin1 > 2"));
|
|
|
}
|
|
|
|
|
|
}
|