Skip to content

Commit

Permalink
优化子流程新增尾节点测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed Sep 6, 2024
1 parent 28fb853 commit 5bcb797
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public interface QueryService {
*/
FlwHisInstance getHistInstance(Long instanceId);

/**
* 判断流程实例下是否存在活跃子流程实例
*
* @param instanceId 流程实例ID
* @return true 存在 false 不存在
*/
boolean existActiveSubProcess(Long instanceId);

/**
* 根据任务ID获取任务对象
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ public boolean executeNodeModel(FlowLongContext flowLongContext, String nodeKey)
* @return true 执行成功 false 执行失败
*/
public boolean endInstance(NodeModel endNode) {
if (engine.queryService().existActiveSubProcess(flwInstance.getId())) {
/*
* 存在执行中的子流程,不允许结束
*/
return true;
}

/*
* 执行完成任务
*/
List<FlwTask> flwTasks = engine.queryService().getTasksByInstanceId(flwInstance.getId());
for (FlwTask flwTask : flwTasks) {
Assert.illegal(flwTask.major(), "There are unfinished major tasks");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ public void restartProcessInstance(Long id, String currentNodeKey, Execution exe
FlwProcess process = processService().getProcessById(id);
NodeModel nodeModel = process.model().getNode(currentNodeKey);
if (null != nodeModel) {
nodeModel.nextNode().ifPresent(childNode -> childNode.execute(flowLongContext, execution));
Optional<NodeModel> nodeModelOptional = nodeModel.nextNode();
if (nodeModelOptional.isPresent()) {
// 执行子节点
nodeModelOptional.get().execute(flowLongContext, execution);
} else {
// 不存在任何子节点结束流程
execution.endInstance(nodeModel);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface FlwInstanceDao {

boolean updateById(FlwInstance instance);

Long selectCountByParentInstanceId(Long parentInstanceId);

FlwInstance selectById(Long id);

Optional<List<FlwInstance>> selectListByParentInstanceId(Long parentInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class FlwInstance extends FlowEntity {
protected String instanceNo;
/**
* 业务KEY(用于关联业务逻辑实现预留)
*
* <p>
* 子流程情况,该字段用于存放父流程所在节点KEY
* </p>
*/
protected String businessKey;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public FlwHisInstance getHistInstance(Long instanceId) {
return hisInstanceDao.selectById(instanceId);
}

@Override
public boolean existActiveSubProcess(Long instanceId) {
return instanceDao.selectCountByParentInstanceId(instanceId) > 0;
}

@Override
public FlwHisTask getHistTask(Long taskId) {
return hisTaskDao.selectById(taskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ public boolean endInstance(Execution execution, Long instanceId, NodeModel endNo
* 实例为子流程,重启动父流程任务
*/
if (null != flwInstance.getParentInstanceId()) {
// 结束调用外部流程任务
taskService.endCallProcessTask(flwInstance.getProcessId(), flwInstance.getId());

// 重启父流程实例
FlwInstance parentFlwInstance = instanceDao.selectById(flwInstance.getParentInstanceId());
execution.setFlwInstance(parentFlwInstance);
execution.restartProcessInstance(parentFlwInstance.getProcessId(), parentFlwInstance.getCurrentNodeKey());

// 结束调用外部流程任务
taskService.endCallProcessTask(flwInstance.getProcessId(), flwInstance.getId());
String currentNodeKey = flwInstance.getBusinessKey();
if (null == currentNodeKey) {
// 子流程节点为空,则取父流程当前节点
currentNodeKey = parentFlwInstance.getCurrentNodeKey();
}
execution.restartProcessInstance(parentFlwInstance.getProcessId(), currentNodeKey);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ public List<FlwTask> createTask(NodeModel nodeModel, Execution execution) {
// 启动子流程,任务归档历史
execution.getEngine().startProcessInstance(flwProcess, flowCreator, null, () -> {
FlwInstance flwInstance = new FlwInstance();
flwInstance.setBusinessKey(nodeModel.getNodeKey());
flwInstance.setParentInstanceId(flwTask.getInstanceId());
return flwInstance;
}).ifPresent(instance -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public boolean updateById(FlwInstance instance) {
return instanceMapper.updateById(instance) > 0;
}

@Override
public Long selectCountByParentInstanceId(Long parentInstanceId) {
return instanceMapper.selectCount(Wrappers.<FlwInstance>lambdaQuery().eq(FlwInstance::getParentInstanceId, parentInstanceId));
}

@Override
public FlwInstance selectById(Long id) {
return instanceMapper.selectById(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023-2025 Licensed under the AGPL License
*/
package test.mysql;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* 测试子流程
*
* @author xdg
*/
public class TestSubProcessEnd extends MysqlTest {

@BeforeEach
public void before() {
processId = this.deployByResource("test/subProcessEnd.json", testCreator);

// 部署子流程
this.deployByResource("test/workHandover.json", testCreator);
}

@Test
public void test() {
// 发起,执行条件路由
flowLongEngine.startInstanceById(processId, testCreator, "这里是关联业务KEY").ifPresent(instance -> {

// 人事审批
this.executeActiveTasks(instance.getId(), testCreator);

// 找到子流程并执行【接收工作任务】完成启动父流程执行结束
flowLongEngine.queryService().getHisTasksByInstanceId(instance.getId()).ifPresent(flwHisTasks -> flwHisTasks.forEach(flwHisTask -> {
if (null != flwHisTask.getCallInstanceId()) {
this.executeActiveTasks(flwHisTask.getCallInstanceId(), test3Creator);
}
}));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"key": "subProcessEnd",
"name": "请假审批",
"nodeConfig": {
"nodeName": "发起人",
"nodeKey": "k001",
"type": 0,
"nodeAssigneeList": [],
"childNode": {
"nodeName": "人事审批",
"nodeKey": "k002",
"type": 1,
"setType": 1,
"nodeAssigneeList": [
{
"tenantId": "1000",
"id": "test001",
"name": "魏磊"
}
],
"examineLevel": 1,
"directorLevel": 1,
"selectMode": 1,
"termAuto": false,
"term": 0,
"termMode": 1,
"examineMode": 1,
"directorMode": 0,
"childNode": {
"nodeName": "工作交接",
"nodeKey": "k007",
"type": 5,
"callProcess": "workHandover"
}
}
}
}

0 comments on commit 5bcb797

Please sign in to comment.