24282 2 年之前
父節點
當前提交
6e710cd073

+ 0 - 6
hx-flow/src/main/java/com/fjhx/flow/core/FlowDelegate.java

@@ -37,10 +37,4 @@ public abstract class FlowDelegate {
      */
     public abstract void end();
 
-    /**
-     * 驳回结束流程
-     */
-    public void rejected() {
-    }
-
 }

+ 1 - 1
hx-flow/src/main/java/com/fjhx/flow/entity/flow/po/FlowExample.java

@@ -46,7 +46,7 @@ public class FlowExample extends BasePo {
     private Long businessId;
 
     /**
-     * 状态 0未发起 1进行中 2已通过 3已驳回
+     * 状态 0未发起 1进行中 2已通过 3已驳回 9结束流程异常
      */
     private Integer status;
 

+ 1 - 0
hx-flow/src/main/java/com/fjhx/flow/enums/FlowStatusEnum.java

@@ -15,6 +15,7 @@ public enum FlowStatusEnum {
     IN_PROGRESS(1, "进行中"),
     HAVE_PASSED(2, "已通过"),
     REJECTED(3, "已驳回"),
+    END_ERROR(9, "结束流程异常"),
     ;
 
     private final Integer key;

+ 87 - 126
hx-flow/src/main/java/com/fjhx/flow/service/flow/impl/FlowProcessServiceImpl.java

@@ -25,12 +25,9 @@ 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;
-import org.springframework.transaction.TransactionDefinition;
-import org.springframework.transaction.TransactionStatus;
 
+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;
@@ -52,12 +49,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
     @Autowired
     private FlowExampleDetailService flowExampleDetailService;
 
-    @Autowired
-    private PlatformTransactionManager platformTransactionManager;
-
-    @Autowired
-    private TransactionDefinition transactionDefinition;
-
     @Override
     public void initiate(InitiateDto dto) {
 
@@ -98,7 +89,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
 
         // 初始化模板参数
         Map<String, Object> templateMap = flowDelegate.initTemplateMap(dto.getData(), dto.getData());
-
         FlowThreadLocalUtil.setTemplateData(templateMap);
         FlowThreadLocalUtil.setStartData(dto.getData());
         FlowThreadLocalUtil.setCurrentData(dto.getData());
@@ -107,66 +97,45 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 寻找下一节点
         FlowDefinitionNode nextUserNode = getNextUserNode(startNode, parentNodeMap);
 
-        // 流程实例
-        FlowExample flowExample = new FlowExample();
-
-        // 流程实例明细列表
-        List<FlowExampleDetail> flowExampleDetailList = new ArrayList<>();
-
         // 执行开始流程方法
-        Long businessId = flowDelegate.start(flowId, dto.getData());
-        FlowThreadLocalUtil.setBusinessId(businessId);
-
-        FlowExampleDetail startExampleDetail = new FlowExampleDetail();
-        startExampleDetail.setFlowExampleId(flowId);
-        startExampleDetail.setFlowDefinitionNodeId(startNode.getId());
-        startExampleDetail.setFlowDefinitionNodeType(startNode.getNodeType());
-        startExampleDetail.setHandleType(HandleTypeEnum.NEXT.getKey());
-        startExampleDetail.setHandleRemark(dto.getRemark());
-        flowExampleDetailList.add(startExampleDetail);
-
-        // 如果下个节点是结束节点
-        if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(nextUserNode.getNodeType()))) {
-
-            // 添加明细
-            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.setStatus(FlowStatusEnum.HAVE_PASSED.getKey());
-        } else {
-            // 流程进行中
-            flowExample.setStatus(FlowStatusEnum.IN_PROGRESS.getKey());
+        Long businessId;
+        try {
+            businessId = flowDelegate.start(flowId, dto.getData());
+        } catch (Exception e) {
+            log.error("开始节点方法异常", e);
+            throw new ServiceException("开始节点方法异常");
         }
 
+        // 流程实例
+        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);
+        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.setVersion(1);
 
-        // 开启一个事务
-        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
+        FlowExampleDetail startExampleDetail = new FlowExampleDetail();
+        startExampleDetail.setFlowExampleId(flowId);
+        startExampleDetail.setFlowDefinitionNodeId(startNode.getId());
+        startExampleDetail.setFlowDefinitionNodeType(startNode.getNodeType());
+        startExampleDetail.setHandleType(HandleTypeEnum.NEXT.getKey());
+        startExampleDetail.setHandleRemark(dto.getRemark());
+        startExampleDetail.setSubmitData(JSONObject.toJSONString(dto));
 
-        try {
-            flowExampleService.save(flowExample);
-            flowExampleDetailService.saveBatch(flowExampleDetailList);
+        flowExampleService.save(flowExample);
+        flowExampleDetailService.save(startExampleDetail);
 
-            // 提交事务
-            platformTransactionManager.commit(transaction);
-        } catch (Exception e) {
-            // 回滚事务
-            platformTransactionManager.rollback(transaction);
-            log.error("保存流程异常", e);
-            throw new ServiceException("保存流程异常");
+        // 如果下个节点是结束节点
+        if (NodeTypeEnum.END.getKey().equals(nextUserNode.getNodeType())) {
+            FlowThreadLocalUtil.setBusinessId(businessId);
+            endFlow(flowId, dto, nextUserNode, flowDelegateCls, flowDelegate);
         }
 
     }
@@ -202,7 +171,8 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 获取当前审批节点
         Long definitionNodeId = flowExample.getDefinitionNodeId();
         FlowDefinitionNode currentNode = flowDefinitionNodeList.stream()
-                .filter(item -> item.getId().equals(definitionNodeId)).findFirst().orElse(null);
+                .filter(item -> item.getId().equals(definitionNodeId))
+                .findFirst().orElse(null);
 
         if (currentNode == null) {
             throw new ServiceException("没有找到当前审批节点");
@@ -211,19 +181,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
         // 查找跳转类型
         HandleTypeEnum handleTypeEnum = HandleTypeEnum.getEnum(dto.getHandleType());
 
-        // 流程实例明细列表
-        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(dto.getData().toJSONString());
-        flowExampleDetailList.add(nodeExampleDetail);
-
         FlowDefinitionNode jumpUserNode = null;
 
         // 发起流程参数
@@ -251,14 +208,6 @@ public class FlowProcessServiceImpl implements FlowProcessService {
                 if (NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(jumpUserNode.getNodeType()))) {
                     // 流程已通过
                     flowStatusEnum = FlowStatusEnum.HAVE_PASSED;
-
-                    // 结束流程
-                    FlowExampleDetail endExampleDetail = new FlowExampleDetail();
-                    endExampleDetail.setFlowExampleId(flowId);
-                    endExampleDetail.setFlowDefinitionNodeId(jumpUserNode.getId());
-                    endExampleDetail.setFlowDefinitionNodeType(jumpUserNode.getNodeType());
-                    endExampleDetail.setHandleType(HandleTypeEnum.OVER.getKey());
-                    flowExampleDetailList.add(endExampleDetail);
                 } else {
                     // 流程进行中
                     flowStatusEnum = FlowStatusEnum.IN_PROGRESS;
@@ -275,7 +224,7 @@ public class FlowProcessServiceImpl implements FlowProcessService {
                 flowExample.setDefinitionNodeId(jumpUserNode.getId());
                 // 如果流程回退到开始节点
                 if (NodeTypeEnum.START.equals(NodeTypeEnum.getEnum(jumpUserNode.getNodeType()))) {
-                    // 流程进行中
+                    // 流程未发起
                     flowStatusEnum = FlowStatusEnum.UNINITIATED;
                 } else {
                     // 流程进行中
@@ -291,60 +240,76 @@ public class FlowProcessServiceImpl implements FlowProcessService {
                 throw new ServiceException("未知节点处理类型");
         }
 
-        flowExample.setStatus(flowStatusEnum.getKey());
-
         // 执行当前节点方法
         String handlingMethod = currentNode.getHandlingMethod();
         if (StrUtil.isNotBlank(handlingMethod)) {
-            invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
+            try {
+                invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
+            } catch (Exception e) {
+                log.error("跳转节点方法异常", e);
+                throw new ServiceException("跳转节点方法异常");
+            }
         }
 
-        // 执行结束方法
-        if (jumpUserNode != null && NodeTypeEnum.END.equals(NodeTypeEnum.getEnum(jumpUserNode.getNodeType()))) {
-            invokeEndMethod(jumpUserNode, flowDelegateCls, flowDelegate);
-        }
+        flowExample.setStatus(flowStatusEnum.getKey());
 
-        // 驳回通用方法
-        if (handleTypeEnum.equals(HandleTypeEnum.OVER)) {
-            flowDelegate.rejected();
-        }
+        // 节点处理
+        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(dto.getData().toJSONString());
 
-        // 开启一个事务
-        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
+        flowExampleService.updateById(flowExample);
+        flowExampleDetailService.save(nodeExampleDetail);
 
-        try {
-            flowExampleService.updateById(flowExample);
-            flowExampleDetailService.saveBatch(flowExampleDetailList);
-            // 提交事务
-            platformTransactionManager.commit(transaction);
-        } catch (Exception e) {
-            // 提交事务
-            platformTransactionManager.rollback(transaction);
-            log.error("保存流程异常", e);
-            throw new ServiceException("保存流程异常");
+        // 执行结束方法
+        if (jumpUserNode != null && NodeTypeEnum.END.getKey().equals(jumpUserNode.getNodeType())) {
+            endFlow(flowId, dto, jumpUserNode, flowDelegateCls, flowDelegate);
         }
 
     }
 
     /**
-     * 执行结束方法
+     * 结束流程
      *
-     * @param nextUserNode    下个用户节点
-     * @param flowDelegateCls 流程代理class
-     * @param flowDelegate    流程代理对象
+     * @param flowId          流程id
+     * @param data            提交参数
+     * @param endNode         结束节点
+     * @param flowDelegateCls java委托对象class
+     * @param flowDelegate    java委托对象
      */
-    private void invokeEndMethod(FlowDefinitionNode nextUserNode, Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate) {
+    private void endFlow(Long flowId, Object data, FlowDefinitionNode endNode,
+                         Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate) {
+        try {
+            // 执行结束方法
+            String handlingMethod = endNode.getHandlingMethod();
+            if (StrUtil.isNotBlank(handlingMethod)) {
+                // 执行指定方法
+                invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
+            } else {
+                // 执行默认方法
+                flowDelegate.end();
+            }
+        } catch (Exception e) {
+            flowExampleService.update(q ->
+                    q.eq(BaseIdPo::getId, flowId).set(FlowExample::getStatus, FlowStatusEnum.END_ERROR.getKey()));
 
-        // 走非默认方法
-        String handlingMethod = nextUserNode.getHandlingMethod();
-        if (StrUtil.isNotBlank(handlingMethod)) {
-            // 执行节点方法
-            invokeMethod(flowDelegateCls, flowDelegate, handlingMethod);
-        } else {
-            // 执行流程结束方法
-            flowDelegate.end();
+            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);
+
     }
 
     /**
@@ -354,14 +319,10 @@ public class FlowProcessServiceImpl implements FlowProcessService {
      * @param flowDelegate    流程代理对象
      * @param handlingMethod  流程执行方法名
      */
-    private void invokeMethod(Class<? extends FlowDelegate> flowDelegateCls, FlowDelegate flowDelegate, String handlingMethod) {
-        try {
-            Method method = flowDelegateCls.getMethod(handlingMethod.trim());
-            method.invoke(flowDelegate);
-        } catch (Exception e) {
-            log.error("自定义处理方法异常:{}", handlingMethod, e);
-            throw new ServiceException("自定义处理方法异常:" + 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);
     }
 
     /**

+ 2 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -221,7 +221,8 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
             updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
         }
         int result = deptMapper.updateDept(dept);
-        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
+        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus())
+                && StringUtils.isNotEmpty(dept.getAncestors())
                 && !StringUtils.equals("0", dept.getAncestors())) {
             // 如果该部门是启用状态,则启用该部门的所有上级部门
             updateParentDeptStatusNormal(dept);

+ 2 - 3
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java

@@ -402,9 +402,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
      * @return String
      */
     public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
-        List<SysMenu> returnList = new ArrayList<SysMenu>();
-        for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); ) {
-            SysMenu t = (SysMenu) iterator.next();
+        List<SysMenu> returnList = new ArrayList<>();
+        for (SysMenu t : list) {
             // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
             if (t.getParentId() == parentId) {
                 recursionFn(list, t);

+ 4 - 1
ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml

@@ -39,7 +39,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 			   d.del_flag,
 			   d.create_by,
 			   d.create_time,
-			   d.tenant_id
+			   d.tenant_id,
+			   d.type
 		from sys_dept d
 	</sql>
     
@@ -128,6 +129,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <if test="phone != null and phone != ''">phone,</if>
         <if test="email != null and email != ''">email,</if>
         <if test="status != null">status,</if>
+        <if test="tenantId != null and tenantId != ''">tenant_id,</if>
         <if test="createBy != null and createBy != ''">create_by,</if>
         create_time
         )values(
@@ -142,6 +144,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <if test="phone != null and phone != ''">#{phone},</if>
         <if test="email != null and email != ''">#{email},</if>
         <if test="status != null">#{status},</if>
+        <if test="tenantId != null and tenantId != ''">#{tenantId},</if>
         <if test="createBy != null and createBy != ''">#{createBy},</if>
         sysdate()
         )