24282 2 years ago
parent
commit
0f33d1f254

File diff suppressed because it is too large
+ 0 - 0
hx-flow/src/main/java/com/fjhx/flow/a-json/FlowInfoApi.json


+ 1 - 1
hx-flow/src/main/java/com/fjhx/flow/entity/flow/dto/FlowResult.java

@@ -13,7 +13,7 @@ public class FlowResult {
     /**
      * 1成功 0失败
      */
-    private Integer status;
+    private Boolean success;
 
     /**
      * 处理用户

+ 170 - 118
hx-flow/src/main/java/com/fjhx/flow/service/flow/impl/FlowProcessServiceImpl.java

@@ -28,12 +28,13 @@ import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.system.service.ISysDeptService;
 import com.ruoyi.system.service.ISysUserService;
+import io.seata.spring.annotation.GlobalTransactional;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
@@ -61,6 +62,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     @Autowired
     private ISysUserService sysUserService;
 
+    @GlobalTransactional
     @Override
     public FlowResult initiate(InitiateDto dto) {
 
@@ -71,7 +73,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         FlowDelegate flowDelegate = getFlowDelegate(flowDelegateCls);
 
         // 查找可用流程
-        FlowDefinition flowDefinition = flowDefinitionService.getOne(q -> q.eq(FlowDefinition::getFlowKey, dto.getFlowKey())
+        FlowDefinition flowDefinition = flowDefinitionService.getOne(q -> q
+                .eq(FlowDefinition::getFlowKey, dto.getFlowKey())
                 .eq(FlowDefinition::getCurrentVersion, StatusConstant.YES));
 
         if (flowDefinition == null) {
@@ -79,8 +82,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         }
 
         // 流程节点列表
-        List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(
-                q -> q.eq(FlowDefinitionNode::getFlowDefinitionId, flowDefinition.getId()));
+        List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(q -> q
+                .eq(FlowDefinitionNode::getFlowDefinitionId, flowDefinition.getId()));
 
         // 父级节点map
         Map<Long, List<FlowDefinitionNode>> parentNodeMap = flowDefinitionNodeList.stream()
@@ -108,19 +111,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 寻找下一节点
         FlowDefinitionNode nextUserNode = getNextUserNode(startNode, parentNodeMap);
 
-        // 处理用户
-        Long userId;
-
-        if (dto.getHandleUserId() == null) {
-            FlowResult handleUser = getHandleUser(nextUserNode);
-            if (handleUser.getStatus() == 0) {
-                return handleUser;
-            }
-            userId = handleUser.getUserId();
-        } else {
-            userId = dto.getHandleUserId();
-        }
-
         // 执行开始流程方法
         Long businessId;
         try {
@@ -130,6 +120,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
             throw new ServiceException("开始节点方法异常");
         }
 
+        List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
+
         // 流程实例
         FlowExample flowExample = new FlowExample();
         flowExample.setTitle(StrUtil.format(flowDefinition.getTitleTemplate(), templateMap, true));
@@ -137,14 +129,9 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         flowExample.setDefinitionId(flowDefinition.getId());
         flowExample.setDefinitionNodeId(nextUserNode.getId());
         flowExample.setBusinessId(businessId);
-        if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(nextUserNode.getNodeType()))) {
-            flowExample.setStatus(FlowStatusEnum.HAVE_PASSED.getKey());
-        } else {
-            flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
-        }
         flowExample.setStartData(dto.getData().toJSONString());
-        flowExample.setHandleUserId(userId);
 
+        // 开始节点明细
         FlowExampleDetail startExampleDetail = new FlowExampleDetail();
         startExampleDetail.setFlowExampleId(flowId);
         startExampleDetail.setFlowDefinitionNodeId(startNode.getId());
@@ -152,21 +139,59 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         startExampleDetail.setHandleType(HandleTypeEnum.NEXT.getKey());
         startExampleDetail.setHandleRemark(dto.getRemark());
         startExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
+        flowExampleDetailList.add(startExampleDetail);
 
-        flowExampleService.save(flowExample);
-        flowExampleDetailService.save(startExampleDetail);
+        if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(nextUserNode.getNodeType()))) {
 
-        // 如果下个节点是结束节点
-        if (NodeTypeEnum.END.getKey().equals(nextUserNode.getNodeType())) {
+            // 赋值业务id到租户线程
             FlowThreadLocalUtil.setBusinessId(businessId);
-            endFlow(flowId, dto, nextUserNode, flowDelegateCls, flowDelegate);
+
+            // 执行结束方法
+            invokeEndMethod(nextUserNode, flowDelegateCls, flowDelegate);
+
+            // 结束流程明细
+            FlowExampleDetail endExampleDetail = new FlowExampleDetail();
+            endExampleDetail.setFlowExampleId(flowId);
+            endExampleDetail.setFlowDefinitionNodeId(nextUserNode.getId());
+            endExampleDetail.setFlowDefinitionNodeType(nextUserNode.getNodeType());
+            endExampleDetail.setHandleType(HandleTypeEnum.OVER.getKey());
+            endExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
+            flowExampleDetailList.add(endExampleDetail);
+
+            // 流程已通过
+            flowExample.setStatus(FlowStatusEnum.HAVE_PASSED.getKey());
+
+        } else {
+
+            // 流程进行中
+            flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
+
+            // 赋值下一个节点处理用户
+            if (dto.getHandleUserId() == null) {
+                FlowResult handleUser = getHandleUser(nextUserNode);
+                // 如果下一节点处理用户有多人,放回用户列表让用户选择
+                if (handleUser.getSuccess()) {
+                    // 如果下一节点处理用户只有1人,赋值用户id
+                    flowExample.setHandleUserId(handleUser.getUserId());
+                } else {
+                    return handleUser;
+                }
+            } else {
+                // 传入用户id
+                flowExample.setHandleUserId(dto.getHandleUserId());
+            }
+
         }
 
+        flowExampleService.save(flowExample);
+        flowExampleDetailService.saveBatch(flowExampleDetailList);
+
         FlowResult flowResult = new FlowResult();
-        flowResult.setStatus(1);
+        flowResult.setSuccess(true);
         return flowResult;
     }
 
+    @GlobalTransactional
     @Override
     public FlowResult jump(JumpDto dto) {
         Long flowId = dto.getFlowId();
@@ -192,8 +217,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         FlowDelegate flowDelegate = getFlowDelegate(flowDelegateCls);
 
         // 流程节点列表
-        List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(
-                q -> q.eq(FlowDefinitionNode::getFlowDefinitionId, flowExample.getDefinitionId()));
+        List<FlowDefinitionNode> flowDefinitionNodeList = flowDefinitionNodeService.list(q -> q
+                .eq(FlowDefinitionNode::getFlowDefinitionId, flowExample.getDefinitionId()));
 
         // 获取当前审批节点
         Long definitionNodeId = flowExample.getDefinitionNodeId();
@@ -208,8 +233,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 查找跳转类型
         HandleTypeEnum handleTypeEnum = HandleTypeEnum.getEnum(dto.getHandleType());
 
-        FlowDefinitionNode jumpUserNode = null;
-
         // 发起流程参数
         JSONObject startDataJson = JSONObject.parseObject(flowExample.getStartData());
         Map<String, Object> templateMap = flowDelegate.initTemplateMap(dto.getData(), startDataJson);
@@ -220,114 +243,164 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         FlowThreadLocalUtil.setBusinessId(flowExample.getBusinessId());
         FlowThreadLocalUtil.setHandleTypeEnum(handleTypeEnum);
 
-        FlowStatusEnum flowStatusEnum;
+        // 执行当前节点方法
+        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;
         switch (handleTypeEnum) {
             case NEXT:
+
                 // 父级节点map
                 Map<Long, List<FlowDefinitionNode>> parentNodeMap = flowDefinitionNodeList.stream()
                         .collect(Collectors.groupingBy(FlowDefinitionNode::getParentId));
+
                 // 寻找下一节点
                 jumpUserNode = getNextUserNode(currentNode, parentNodeMap);
+
                 // 赋值流程所在id
                 flowExample.setDefinitionNodeId(jumpUserNode.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);
+
                     // 流程已通过
-                    flowStatusEnum = FlowStatusEnum.HAVE_PASSED;
+                    flowExample.setStatus(FlowStatusEnum.HAVE_PASSED.getKey());
+
                 } else {
+
+                    // 赋值下一个节点处理用户
+                    if (dto.getHandleUserId() == null) {
+                        FlowResult handleUser = getHandleUser(jumpUserNode);
+                        // 如果下一节点处理用户有多人,放回用户列表让用户选择
+                        if (handleUser.getSuccess()) {
+                            // 如果下一节点处理用户只有1人,赋值用户id
+                            flowExample.setHandleUserId(handleUser.getUserId());
+                        } else {
+                            return handleUser;
+                        }
+                    } else {
+                        // 传入用户id
+                        flowExample.setHandleUserId(dto.getHandleUserId());
+                    }
+
                     // 流程进行中
-                    flowStatusEnum = FlowStatusEnum.IN_PROGRESS;
+                    flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
                 }
                 break;
 
             case PREVIOUS:
+
                 // 节点id对象map
                 Map<Long, FlowDefinitionNode> nodeMap = flowDefinitionNodeList.stream()
                         .collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
+
                 // 查找上一个节点
                 jumpUserNode = getLastOneUserNode(currentNode, nodeMap);
+
                 // 赋值流程所在id
                 flowExample.setDefinitionNodeId(jumpUserNode.getId());
+
                 // 如果流程回退到开始节点
                 if (NodeTypeEnum.START.equals(NodeTypeEnum.getEnum(jumpUserNode.getNodeType()))) {
+
                     // 流程未发起
-                    flowStatusEnum = FlowStatusEnum.UNINITIATED;
+                    flowExample.setStatus(FlowStatusEnum.UNINITIATED.getKey());
+
+                    // 赋值处理人为发起人
+                    flowExample.setHandleUserId(flowExample.getCreateUser());
+
                 } else {
+
                     // 流程进行中
-                    flowStatusEnum = FlowStatusEnum.IN_PROGRESS;
+                    flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
+
+                    // 赋值下一个节点处理用户
+                    if (dto.getHandleUserId() == null) {
+                        FlowResult handleUser = getHandleUser(jumpUserNode);
+                        // 如果下一节点处理用户有多人,放回用户列表让用户选择
+                        if (handleUser.getSuccess()) {
+                            // 如果下一节点处理用户只有1人,赋值用户id
+                            flowExample.setHandleUserId(handleUser.getUserId());
+                        } else {
+                            return handleUser;
+                        }
+                    } else {
+                        // 传入用户id
+                        flowExample.setHandleUserId(dto.getHandleUserId());
+                    }
+
                 }
                 break;
 
             case OVER:
-                flowStatusEnum = FlowStatusEnum.REJECTED;
+                flowExample.setStatus(FlowStatusEnum.REJECTED.getKey());
                 break;
 
             default:
                 throw new ServiceException("未知节点处理类型");
-        }
-
-        // 处理用户
-        Long userId = null;
-        if (jumpUserNode != null) {
-            if (dto.getHandleUserId() == null) {
-                FlowResult handleUser = getHandleUser(jumpUserNode);
-                if (handleUser.getStatus() == 0) {
-                    return handleUser;
-                }
-                userId = handleUser.getUserId();
-            } else {
-                userId = dto.getHandleUserId();
-            }
-        }
 
-        // 执行当前节点方法
-        String handlingMethod = currentNode.getHandlingMethod();
-        if (StrUtil.isNotBlank(handlingMethod)) {
-            try {
-                invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
-            } catch (Exception e) {
-                log.error("跳转节点方法异常", e);
-                throw new ServiceException("跳转节点方法异常");
-            }
         }
 
-        flowExample.setStatus(flowStatusEnum.getKey());
-        flowExample.setHandleUserId(userId);
-
-        // 节点处理
-        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));
-
         flowExampleService.updateById(flowExample);
-        flowExampleDetailService.save(nodeExampleDetail);
-
-        // 执行结束方法
-        if (jumpUserNode != null && NodeTypeEnum.END.getKey().equals(jumpUserNode.getNodeType())) {
-            endFlow(flowId, dto, jumpUserNode, flowDelegateCls, flowDelegate);
-        }
+        flowExampleDetailService.saveBatch(flowExampleDetailList);
 
         FlowResult flowResult = new FlowResult();
-        flowResult.setStatus(1);
+        flowResult.setSuccess(true);
         return flowResult;
     }
 
     /**
-     * 结束流程
+     * 执行节点方法
+     *
+     * @param flowDelegateCls 流程代理class
+     * @param flowDelegate    流程代理对象
+     * @param handlingMethod  流程执行方法名
+     */
+    private void invokeMethod(Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate, String handlingMethod) throws Exception {
+        Method method = flowDelegateCls.getMethod(handlingMethod.trim());
+        method.invoke(flowDelegate);
+    }
+
+    /**
+     * 执行结束节点方法
      *
-     * @param flowId          流程id
-     * @param data            提交参数
      * @param endNode         结束节点
      * @param flowDelegateCls java委托对象class
      * @param flowDelegate    java委托对象
      */
-    private void endFlow(Long flowId, Object data, FlowDefinitionNode endNode,
-                         Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate) {
+    private void invokeEndMethod(FlowDefinitionNode endNode, Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate) {
         try {
             // 执行结束方法
             String handlingMethod = endNode.getHandlingMethod();
@@ -339,35 +412,9 @@ public class FlowProcessServiceImpl implements FlowProcessService {
                 flowDelegate.end();
             }
         } catch (Exception e) {
-            flowExampleService.update(q ->
-                    q.eq(BaseIdPo::getId, flowId).set(FlowExample::getStatus, FlowStatusEnum.END_ERROR.getKey()));
-
             log.error("结束节点方法异常", e);
             throw new ServiceException("结束节点方法异常");
         }
-
-        // 结束流程
-        FlowExampleDetail endExampleDetail = new FlowExampleDetail();
-        endExampleDetail.setFlowExampleId(flowId);
-        endExampleDetail.setFlowDefinitionNodeId(endNode.getId());
-        endExampleDetail.setFlowDefinitionNodeType(endNode.getNodeType());
-        endExampleDetail.setHandleType(HandleTypeEnum.OVER.getKey());
-        endExampleDetail.setSubmitData(JSONObject.toJSONString(data));
-        flowExampleDetailService.save(endExampleDetail);
-
-    }
-
-    /**
-     * 执行节点方法
-     *
-     * @param flowDelegateCls 流程代理class
-     * @param flowDelegate    流程代理对象
-     * @param handlingMethod  流程执行方法名
-     */
-    private void invokeMethod(Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate, String handlingMethod)
-            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
-        Method method = flowDelegateCls.getMethod(handlingMethod.trim());
-        method.invoke(flowDelegate);
     }
 
     /**
@@ -454,6 +501,12 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         return Boolean.parseBoolean(String.valueOf(execute));
     }
 
+    /**
+     * 获取处理用户
+     *
+     * @param node 处理节点
+     * @return 处理用户
+     */
     private FlowResult getHandleUser(FlowDefinitionNode node) {
         FlowResult flowResult = new FlowResult();
 
@@ -464,7 +517,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
 
         switch (handleObjectTypeEnum) {
             case USER:
-                flowResult.setStatus(1);
+                flowResult.setSuccess(true);
                 flowResult.setUserId(handleObjectId);
                 return flowResult;
             case DETP_LEADER:
@@ -486,7 +539,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
                     }
                     flowResult.setUserId(directorId);
                 }
-                flowResult.setStatus(1);
+                flowResult.setSuccess(true);
                 return flowResult;
             case POST:
                 List<SysUser> userList = sysUserService.getListByPostId(handleObjectId);
@@ -494,12 +547,11 @@ public class FlowProcessServiceImpl implements FlowProcessService {
                     throw new ServiceException("岗位无用户");
                 }
                 if (userList.size() == 1) {
-                    flowResult.setStatus(1);
+                    flowResult.setSuccess(true);
                     flowResult.setUserId(userList.get(0).getUserId());
                     return flowResult;
                 }
-
-                flowResult.setStatus(0);
+                flowResult.setSuccess(false);
                 flowResult.setUserList(userList);
                 return flowResult;
         }

+ 2 - 13
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/TestFlow.java

@@ -2,16 +2,12 @@ package com.ruoyi.web.controller.common;
 
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DS;
-import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.fjhx.flow.core.FlowDelegate;
 import com.ruoyi.system.domain.SysConfig;
 import com.ruoyi.system.service.ISysConfigService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.List;
 
 @Slf4j
 @Service
@@ -21,22 +17,14 @@ public class TestFlow extends FlowDelegate {
     private ISysConfigService sysConfigService;
 
     @DS("iot")
-    @Transactional
     @Override
     public Long start(Long flowId, JSONObject data) {
 
-        long id = IdWorker.getId();
-
-        System.err.println(flowId);
-        System.err.println(data);
-        System.err.println(id);
-
-        List<SysConfig> list = sysConfigService.list();
         SysConfig sysConfig = new SysConfig();
         sysConfig.setRemark("备注");
         sysConfigService.save(sysConfig);
 
-        return id;
+        return sysConfig.getConfigId();
     }
 
     @Override
@@ -46,6 +34,7 @@ public class TestFlow extends FlowDelegate {
 
     public void testEnd() {
         log.error("测试结束方法");
+        // int i = 0 / 0;
     }
 
     public void type1() {

+ 17 - 1
ruoyi-admin/src/main/resources/application-dev.yml

@@ -2,8 +2,9 @@
 spring:
     datasource:
         dynamic:
+            seata: true
             strict: true
-            driver-class-name: com.mysql.cj.jdbc.Driver
+            primary: base
             datasource:
                 item:
                     url: jdbc:mysql://36.134.91.96:17330/bytesailing_item?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
@@ -44,3 +45,18 @@ spring:
                 max-active: 8
                 # #连接池最大阻塞等待时间(使用负值表示没有限制)
                 max-wait: -1ms
+
+seata:
+    config:
+        type: file
+    application-id: springboot-seata
+    #  enable-auto-data-source-proxy: false
+    registry:
+        type: file
+    service:
+        group-list:
+            default: 116.205.247.33:8091
+        vgroup-mapping:
+            springboot-seata-group: default
+    # seata 事务组编号 用于TC集群名
+    tx-service-group: springboot-seata-group

+ 7 - 0
ruoyi-common/pom.xml

@@ -73,6 +73,13 @@
             <version>3.6.1</version>
         </dependency>
 
+        <!-- seata-spring-boot-starter -->
+        <dependency>
+            <groupId>io.seata</groupId>
+            <artifactId>seata-spring-boot-starter</artifactId>
+            <version>1.6.1</version>
+        </dependency>
+
         <!-- 阿里JSON解析器 -->
         <dependency>
             <groupId>com.alibaba.fastjson2</groupId>

Some files were not shown because too many files changed in this diff