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