feat: 删除任务时回退已完成任务的积分

This commit is contained in:
Your Name 2026-01-15 18:25:06 +08:00
parent 0acf4c650e
commit 33b6ddd0d7
2 changed files with 73 additions and 2 deletions

View File

@ -102,6 +102,35 @@ public class DeptMonthlyScoreService {
deptMonthlyScoreMapper.updateById(record);
}
/**
* 扣除部门获得积分(删除已完成任务时调用,用于回退积分)
*/
@Transactional(rollbackFor = Exception.class)
public void deductEarnedScore(Long deptId, BigDecimal score) {
DeptMonthlyScore record = getOrCreateCurrentMonth(deptId);
// 扣除获得积分(不能小于0)
BigDecimal newEarnedScore = record.getEarnedScore().subtract(score);
if (newEarnedScore.compareTo(BigDecimal.ZERO) < 0) {
newEarnedScore = BigDecimal.ZERO;
}
record.setEarnedScore(newEarnedScore);
// 减少完成任务数(不能小于0)
int newCompletedCount = record.getCompletedTaskCount() - 1;
if (newCompletedCount < 0) {
newCompletedCount = 0;
}
record.setCompletedTaskCount(newCompletedCount);
// 重新计算净积分
record.setNetScore(record.getEarnedScore().subtract(record.getDeductedScore()));
record.setUpdateTime(LocalDateTime.now());
deptMonthlyScoreMapper.updateById(record);
log.info("已扣除部门月度积分: deptId={}, score={}, newEarnedScore={}", deptId, score, newEarnedScore);
}
/**
* 获取部门当月积分
*/

View File

@ -3356,14 +3356,56 @@ public class TaskService {
Long taskId = task.getId();
// 1. 删除所有子任务
// 1. 如果任务已完成,需要回退积分
if ("completed".equals(task.getStatus())) {
// 回退主任务积分
if (task.getReceiverDeptId() != null && task.getScoreReward() != null
&& task.getScoreReward().compareTo(BigDecimal.ZERO) > 0) {
try {
// 从团队积分池扣除已奖励的积分
teamScorePoolService.deductScore(task.getReceiverDeptId(), task.getScoreReward().intValue());
log.info("已回退主任务积分: deptId={}, score={}", task.getReceiverDeptId(), task.getScoreReward());
// 回退部门月度积分统计
deptMonthlyScoreService.deductEarnedScore(task.getReceiverDeptId(), task.getScoreReward());
log.info("已回退部门月度积分: deptId={}, score={}", task.getReceiverDeptId(), task.getScoreReward());
} catch (Exception e) {
log.warn("回退主任务积分失败: {}", e.getMessage());
}
}
}
// 2. 查询所有子任务,回退已完成子任务的积分
List<TaskItem> subTasks = taskItemMapper.selectList(
new LambdaQueryWrapper<TaskItem>()
.eq(TaskItem::getTaskId, taskId)
);
for (TaskItem item : subTasks) {
if ("completed".equals(item.getStatus()) && item.getReceiverDeptId() != null
&& item.getTotalScore() != null && item.getTotalScore().compareTo(BigDecimal.ZERO) > 0) {
try {
// 从团队积分池扣除已奖励的积分
teamScorePoolService.deductScore(item.getReceiverDeptId(), item.getTotalScore().intValue());
log.info("已回退子任务积分: itemNo={}, deptId={}, score={}",
item.getItemNo(), item.getReceiverDeptId(), item.getTotalScore());
// 回退部门月度积分统计
deptMonthlyScoreService.deductEarnedScore(item.getReceiverDeptId(), item.getTotalScore());
} catch (Exception e) {
log.warn("回退子任务积分失败: itemNo={}, error={}", item.getItemNo(), e.getMessage());
}
}
}
// 3. 删除所有子任务
int deletedItems = taskItemMapper.delete(
new LambdaQueryWrapper<TaskItem>()
.eq(TaskItem::getTaskId, taskId)
);
log.info("已删除 {} 个子任务", deletedItems);
// 2. 删除主任务
// 4. 删除主任务
taskMapper.deleteById(taskId);
log.info("已删除主任务: taskNo={}, taskId={}", taskNo, taskId);
}