如果归集任务下面的子项都取消了,归集任务可以直接关闭
This commit is contained in:
1 parent
62f43056e2
commit
c94f0f14d2
7 files changed
+74
-12
No files matched your search
@@ -1,5 +1,7 @@
|
||||
## 平台简介
|
||||
|
||||
归集任务全部有效明细取消后,取消事务自动将主任务标记为 `status=4(已关闭)`,保留审核状态和历史记录。部分取消不会关闭任务;已关闭任务不能再次执行或审核,且不再占用待执行任务名额。执行开始及结束采用条件更新,防止旧执行快照覆盖关闭状态。无需修改数据库表结构。
|
||||
|
||||
归集明细支持管理端及用户端取消:`POST /system/usdtTaskItem/{id}/cancel`、`POST /system/usdtTaskItem/user/{id}/cancel`,分别复用明细 edit/user-edit 权限,用户端校验任务及明细归属。仅待执行明细可取消,取消保留审计记录且不可恢复。明细状态为 `0=待执行、1=成功、2=失败、3=已取消、4=执行中`;执行前以条件更新抢占明细,取消成功后不会广播。无需变更表结构。
|
||||
|
||||
用户套餐绑定与默认套餐下单:旧库先执行 [绑定表增量SQL](sql/rent_user_package.sql),三个NILE示例套餐见 [套餐初始化脚本](sql/rent_seed_three_packages.sql),管理及API说明见 [用户套餐绑定](docs/rental/user-package-bindings.md)。升级后未绑定用户不能创建新租赁订单。
|
||||
|
||||
+30
@@ -26,12 +26,14 @@ public class AggregationItemCancellationTest {
|
||||
when(tasks.selectUsdtAggregationTaskByIdForUpdate(10L)).thenReturn(new UsdtAggregationTask());
|
||||
when(items.cancelPendingItem(1L)).thenReturn(1);
|
||||
service.cancelItem(1L, false);
|
||||
verify(tasks).closeIfAllItemsCancelled(10L);
|
||||
when(items.cancelPendingItem(1L)).thenReturn(0);
|
||||
try {
|
||||
service.cancelItem(1L, false);
|
||||
fail("执行中、成功、失败和已取消状态必须拒绝取消");
|
||||
} catch (CustomException expected) {
|
||||
assertTrue(expected.getMessage().contains("仅待执行"));
|
||||
verify(tasks, times(1)).closeIfAllItemsCancelled(10L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +87,34 @@ public class AggregationItemCancellationTest {
|
||||
assertEquals(Boolean.TRUE, execute.invoke(service, snapshot, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closedTaskCannotBeExecutedOrApproved() {
|
||||
com.ruoyi.system.service.IUsdtAggregationTaskService taskService =
|
||||
mock(com.ruoyi.system.service.IUsdtAggregationTaskService.class);
|
||||
UsdtAggregationTaskMapper tasks = mock(UsdtAggregationTaskMapper.class);
|
||||
com.ruoyi.system.domain.UsdtAggregationTaskPlus task = new com.ruoyi.system.domain.UsdtAggregationTaskPlus();
|
||||
task.setStatus(4);
|
||||
when(taskService.getById(10L)).thenReturn(task);
|
||||
UsdtAggregationWorkflowService workflow = new UsdtAggregationWorkflowService(
|
||||
null, null, taskService, null, null, tasks, null, null, null, null, null);
|
||||
try {
|
||||
workflow.executeTask(10L, null, false);
|
||||
fail("已关闭任务不可执行");
|
||||
} catch (CustomException expected) {
|
||||
assertTrue(expected.getMessage().contains("已关闭"));
|
||||
verify(tasks, never()).startExecutableTask(10L);
|
||||
}
|
||||
UsdtAggregationTask closed = new UsdtAggregationTask();
|
||||
closed.setStatus(4);
|
||||
when(tasks.selectUsdtAggregationTaskByIdForUpdate(10L)).thenReturn(closed);
|
||||
try {
|
||||
workflow.approve(10L, true);
|
||||
fail("已关闭任务不可重新审核");
|
||||
} catch (CustomException expected) {
|
||||
verify(tasks, never()).updateUsdtAggregationTask(closed);
|
||||
}
|
||||
}
|
||||
|
||||
private UsdtAggregationWorkflowService service(UsdtAggregationTaskMapper tasks,
|
||||
UsdtAggregationTaskItemMapper items) {
|
||||
return new UsdtAggregationWorkflowService(null, null, null, null, null,
|
||||
|
||||
@@ -41,8 +41,8 @@ public class UsdtAggregationTask extends BaseEntity
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long userId;
|
||||
|
||||
/** 状态 */
|
||||
@Excel(name = "状态")
|
||||
/** 主任务状态:0 待执行,1 执行中,2 成功,3 存在失败,4 全部明细取消后关闭。 */
|
||||
@Excel(name = "状态", readConverterExp = "0=待执行,1=执行中,2=成功,3=存在失败,4=已关闭")
|
||||
private Integer status;
|
||||
|
||||
/** 创建时间 */
|
||||
|
||||
@@ -47,8 +47,8 @@ public class UsdtAggregationTaskPlus implements Serializable
|
||||
/** 备注 */
|
||||
@Excel(name = "备注")
|
||||
private String remark;
|
||||
/** 状态 */
|
||||
@Excel(name = "状态")
|
||||
/** 主任务状态:0 待执行,1 执行中,2 成功,3 存在失败,4 全部明细取消后关闭。 */
|
||||
@Excel(name = "状态", readConverterExp = "0=待执行,1=执行中,2=成功,3=存在失败,4=已关闭")
|
||||
private Integer status;
|
||||
/** 创建时间 */
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
|
||||
@@ -15,6 +15,14 @@ import com.ruoyi.system.domain.UsdtAggregationTaskPlus;
|
||||
*/
|
||||
public interface UsdtAggregationTaskMapper extends BaseMapper<UsdtAggregationTaskPlus>
|
||||
{
|
||||
/** 所有有效子项均取消时关闭主任务;不得将空任务或部分取消误判为关闭。 */
|
||||
int closeIfAllItemsCancelled(@Param("id") Long id);
|
||||
|
||||
/** 条件抢占主任务,避免取消事务关闭后被旧执行快照重新启动。 */
|
||||
int startExecutableTask(@Param("id") Long id);
|
||||
|
||||
/** 结束执行时保留取消事务已经写入的关闭状态。 */
|
||||
int finishExecutableTask(@Param("id") Long id, @Param("status") int status, @Param("remark") String remark);
|
||||
/**
|
||||
* 查询归集任务
|
||||
*
|
||||
|
||||
+16
-8
@@ -120,15 +120,19 @@ public class UsdtAggregationWorkflowService {
|
||||
return task.getId();
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void approve(Long taskId, boolean approved) {
|
||||
UsdtAggregationTaskPlus task = requireTask(taskId, false);
|
||||
UsdtAggregationTask task = taskMapper.selectUsdtAggregationTaskByIdForUpdate(taskId);
|
||||
if (task == null) {
|
||||
throw new CustomException("归集任务不存在或已删除");
|
||||
}
|
||||
if (task.getStatus() != null && task.getStatus() != 0) {
|
||||
throw new CustomException("任务已开始执行,不能重复审核");
|
||||
throw new CustomException("任务已开始执行或已关闭,不能重复审核");
|
||||
}
|
||||
task.setVerifyStatus(approved ? 1 : 2);
|
||||
task.setUpdatedAt(DateUtils.getNowDate());
|
||||
task.setRemark(approved ? "审核通过" : "审核拒绝");
|
||||
taskService.updateById(task);
|
||||
taskMapper.updateUsdtAggregationTask(task);
|
||||
}
|
||||
|
||||
/** 仅允许删除审核拒绝且尚未执行的任务;主任务与明细同事务软删除,保留审计记录。 */
|
||||
@@ -175,10 +179,15 @@ public class UsdtAggregationWorkflowService {
|
||||
if (itemMapper.cancelPendingItem(itemId) != 1) {
|
||||
throw new CustomException("仅待执行的归集明细可以取消,请刷新列表");
|
||||
}
|
||||
// 与子项取消处于同一事务;主任务行锁使并发取消串行,最后一个取消负责关闭任务。
|
||||
taskMapper.closeIfAllItemsCancelled(item.getParentId());
|
||||
}
|
||||
|
||||
public void executeTask(Long taskId, String password, boolean userMode) {
|
||||
UsdtAggregationTaskPlus task = requireTask(taskId, userMode);
|
||||
if (Integer.valueOf(4).equals(task.getStatus())) {
|
||||
throw new CustomException("归集任务已关闭,不能执行");
|
||||
}
|
||||
if (!Integer.valueOf(1).equals(task.getVerifyStatus())) {
|
||||
throw new CustomException("归集任务尚未审核通过");
|
||||
}
|
||||
@@ -190,10 +199,9 @@ public class UsdtAggregationWorkflowService {
|
||||
.eq(UsdtAggregationTaskItemPlus::getParentId, taskId)
|
||||
.in(UsdtAggregationTaskItemPlus::getStatus, 0L, 2L));
|
||||
boolean failed = walletSigningService.executeUnlocked(password, session -> {
|
||||
task.setStatus(1);
|
||||
task.setExeTime(DateUtils.getNowDate());
|
||||
task.setUpdatedAt(DateUtils.getNowDate());
|
||||
taskService.updateById(task);
|
||||
if (taskMapper.startExecutableTask(taskId) != 1) {
|
||||
throw new CustomException("任务状态已变化,可能已关闭或正在执行,请刷新后重试");
|
||||
}
|
||||
|
||||
boolean hasFailure = false;
|
||||
for (UsdtAggregationTaskItemPlus item : items) {
|
||||
@@ -295,7 +303,7 @@ public class UsdtAggregationWorkflowService {
|
||||
task.setStatus(failed ? 3 : 2);
|
||||
task.setUpdatedAt(DateUtils.getNowDate());
|
||||
task.setRemark(failed ? "执行完成,存在失败明细" : "归集执行完成(已取消明细不参与执行)");
|
||||
taskService.updateById(task);
|
||||
taskMapper.finishExecutableTask(task.getId(), task.getStatus(), task.getRemark());
|
||||
}
|
||||
|
||||
private AppsPlus resolveApp(AggregationTaskCreateRequest request, boolean userMode) {
|
||||
|
||||
@@ -3,6 +3,20 @@
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ruoyi.system.mapper.UsdtAggregationTaskMapper">
|
||||
<update id="closeIfAllItemsCancelled">
|
||||
update usdt_aggregation_task set status = 4, remark = '全部归集明细已取消,任务已关闭', updated_at = current_timestamp
|
||||
where id = #{id} and deleted_at is null and status in (0, 1, 3)
|
||||
and exists (select 1 from usdt_aggregation_task_item where parent_id = #{id} and deleted_at is null)
|
||||
and not exists (select 1 from usdt_aggregation_task_item where parent_id = #{id} and deleted_at is null and (status is null or status != 3))
|
||||
</update>
|
||||
<update id="startExecutableTask">
|
||||
update usdt_aggregation_task set status = 1, exe_time = current_timestamp, updated_at = current_timestamp
|
||||
where id = #{id} and deleted_at is null and verify_status = 1 and status in (0, 3)
|
||||
</update>
|
||||
<update id="finishExecutableTask">
|
||||
update usdt_aggregation_task set status = #{status}, remark = #{remark}, updated_at = current_timestamp
|
||||
where id = #{id} and deleted_at is null and status != 4
|
||||
</update>
|
||||
|
||||
<resultMap type="UsdtAggregationTask" id="UsdtAggregationTaskResult">
|
||||
<result property="id" column="id" />
|
||||
|
||||
Reference in new issue
Block a user