feat: 添加批量更新低时效任务截止时间接口

- 在 AdminTaskService 添加 batchUpdateLowHoursDeadlines 方法
- 批量更新时效低于1小时的任务,使用最低1小时计算截止时间
- 同时处理主任务和子任务
This commit is contained in:
Your Name 2026-01-16 17:32:39 +08:00
parent 3d5026b512
commit f685f99688
2 changed files with 108 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* 管理员任务调配Controller
@ -124,6 +125,18 @@ public class AdminTaskController {
return Result.success(task);
}
@Operation(summary = "批量更新低时效任务的截止时间", description = "将时效低于1小时的任务截止时间更新为按1小时计算")
@PostMapping("/batch-update-low-hours-deadlines")
public Result<Map<String, Integer>> batchUpdateLowHoursDeadlines(
@RequestHeader("Authorization") String authHeader
) {
String token = jwtUtil.extractTokenFromHeader(authHeader);
Long adminId = jwtUtil.getUserIdFromToken(token);
// 这里可以添加管理员权限校验
Map<String, Integer> result = adminTaskService.batchUpdateLowHoursDeadlines();
return Result.success(result);
}
// DTO类
public static class TransferRequest {
private Long newDesignerId;

View File

@ -12,9 +12,15 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.designhub.entity.TaskItem;
import com.designhub.mapper.TaskItemMapper;
import com.designhub.util.WorkHoursCalculator;
/**
* 管理员任务调配Service
@ -25,10 +31,12 @@ import java.util.List;
public class AdminTaskService {
private final TaskMapper taskMapper;
private final TaskItemMapper taskItemMapper;
private final NotificationService notificationService;
private final UserService userService;
private final DepartmentService departmentService;
private final RoleService roleService;
private final WorkHoursCalculator workHoursCalculator;
/**
* 管理员分配任务
@ -421,5 +429,92 @@ public class AdminTaskService {
return false;
}
}
/**
* 批量更新低时效任务的截止时间(应用最低1小时时效)
* @return 更新结果统计
*/
@Transactional(rollbackFor = Exception.class)
public Map<String, Integer> batchUpdateLowHoursDeadlines() {
Map<String, Integer> result = new HashMap<>();
int taskUpdated = 0;
int itemUpdated = 0;
BigDecimal minHours = BigDecimal.ONE; // 最低1小时
// 1. 更新主任务的截止时间
LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<Task>()
.in(Task::getStatus, "pending", "in_progress")
.eq(Task::getDeleted, 0)
.lt(Task::getEstimatedHours, minHours)
.gt(Task::getEstimatedHours, BigDecimal.ZERO)
.isNotNull(Task::getStartTime); // 只更新已接单的任务
List<Task> lowHoursTasks = taskMapper.selectList(taskWrapper);
log.info("找到 {} 个低时效主任务需要更新", lowHoursTasks.size());
for (Task task : lowHoursTasks) {
try {
// 使用接单时间计算新的截止时间(应用最低1小时)
LocalDateTime newDeadline = workHoursCalculator.calculateDeadline(
task.getStartTime(),
minHours
);
if (newDeadline != null && !newDeadline.equals(task.getDeadline())) {
log.info("更新任务截止时间: taskNo={}, 原时效={}小时, 原截止={}, 新截止={}",
task.getTaskNo(), task.getEstimatedHours(), task.getDeadline(), newDeadline);
task.setDeadline(newDeadline);
task.setUpdateTime(LocalDateTime.now());
taskMapper.updateById(task);
taskUpdated++;
}
} catch (Exception e) {
log.error("更新任务截止时间失败: taskId={}, error={}", task.getId(), e.getMessage());
}
}
// 2. 更新子任务(TaskItem)的截止时间
LambdaQueryWrapper<TaskItem> itemWrapper = new LambdaQueryWrapper<TaskItem>()
.in(TaskItem::getStatus, "pending", "in_progress")
.lt(TaskItem::getEstimatedHours, minHours)
.gt(TaskItem::getEstimatedHours, BigDecimal.ZERO)
.isNotNull(TaskItem::getReceiverId); // 只更新已接单的子任务
List<TaskItem> lowHoursItems = taskItemMapper.selectList(itemWrapper);
log.info("找到 {} 个低时效子任务需要更新", lowHoursItems.size());
for (TaskItem item : lowHoursItems) {
try {
// 子任务使用 update_time 作为接单时间(如果没有专门的接单时间字段)
LocalDateTime startTime = item.getUpdateTime();
if (startTime == null) {
startTime = item.getCreateTime();
}
LocalDateTime newDeadline = workHoursCalculator.calculateDeadline(
startTime,
minHours
);
if (newDeadline != null && !newDeadline.equals(item.getDeadline())) {
log.info("更新子任务截止时间: itemNo={}, 原时效={}小时, 原截止={}, 新截止={}",
item.getItemNo(), item.getEstimatedHours(), item.getDeadline(), newDeadline);
item.setDeadline(newDeadline);
item.setUpdateTime(LocalDateTime.now());
taskItemMapper.updateById(item);
itemUpdated++;
}
} catch (Exception e) {
log.error("更新子任务截止时间失败: itemId={}, error={}", item.getId(), e.getMessage());
}
}
result.put("tasksFound", lowHoursTasks.size());
result.put("tasksUpdated", taskUpdated);
result.put("itemsFound", lowHoursItems.size());
result.put("itemsUpdated", itemUpdated);
log.info("批量更新低时效任务截止时间完成: {}", result);
return result;
}
}