24282 il y a 2 ans
Parent
commit
d977324519

+ 2 - 1
hx-flow/src/main/java/com/fjhx/flow/controller/flow/FlowProcessController.java

@@ -5,6 +5,7 @@ import com.fjhx.flow.entity.flow.dto.InitiateDto;
 import com.fjhx.flow.service.flow.FlowProcessService;
 import com.ruoyi.common.constant.DatasourceConstant;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -22,7 +23,7 @@ public class FlowProcessController {
      * 发起流程
      */
     @PostMapping("/initiate")
-    public void initiate(@RequestBody InitiateDto dto) {
+    public void initiate(@Validated @RequestBody InitiateDto dto) {
         flowProcessService.initiate(dto);
     }
 

+ 10 - 4
hx-flow/src/main/java/com/fjhx/flow/core/FlowDelegate.java

@@ -1,15 +1,21 @@
 package com.fjhx.flow.core;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.ruoyi.common.utils.SecurityUtils;
 
-import java.util.HashMap;
+import java.util.Date;
 import java.util.Map;
 
 public abstract class FlowDelegate {
 
-    public Map<String, Object> initTemplateMap() {
-        HashMap<String, Object> map = new HashMap<>();
-
+    public Map<String, Object> initTemplateMap(JSONObject submitData, Object startData) {
+        Map<String, Object> map = BeanUtil.beanToMap(submitData);
+        map.put("nickName", SecurityUtils.getLoginUser().getUser().getNickName());
+        map.put("date", DateUtil.formatDate(new Date()));
+        map.put("dateTime", DateUtil.formatDateTime(new Date()));
+        map.put("startData", BeanUtil.beanToMap(startData));
         return map;
     }
 

+ 61 - 64
hx-flow/src/main/java/com/fjhx/flow/service/flow/impl/FlowProcessServiceImpl.java

@@ -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"));
-    // }
-
 }

+ 6 - 0
pom.xml

@@ -180,6 +180,12 @@
                 <version>${ruoyi.version}</version>
             </dependency>
 
+            <dependency>
+                <groupId>com.ruoyi</groupId>
+                <artifactId>hx-flow</artifactId>
+                <version>${ruoyi.version}</version>
+            </dependency>
+
         </dependencies>
     </dependencyManagement>
 

+ 5 - 0
ruoyi-admin/pom.xml

@@ -59,6 +59,11 @@
             <artifactId>hx-tenant</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>hx-flow</artifactId>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 1 - 1
ruoyi-framework/src/main/java/com/ruoyi/framework/mybatis/interceptor/SqlLogInterceptor.java

@@ -93,7 +93,7 @@ public class SqlLogInterceptor implements Interceptor {
                             + "\n====================  Sql  End   ====================\n",
                     mappedStatement.getId(), sql, end - start);
         } catch (Exception e) {
-            log.error(String.format("intercept sql error: [%s]", sql), e);
+            log.error(String.format("intercept sql error: [%s]\r\n", sql), e);
         }
 
         return result;