diff --git a/backend/src/main/java/com/designhub/service/TaskApprovalService.java b/backend/src/main/java/com/designhub/service/TaskApprovalService.java index 1cf0b54..fbcc292 100644 --- a/backend/src/main/java/com/designhub/service/TaskApprovalService.java +++ b/backend/src/main/java/com/designhub/service/TaskApprovalService.java @@ -59,106 +59,47 @@ public class TaskApprovalService { logApproval(taskId, approverId, 1, "modify", dto.getComment()); } - // 3. 根据发布类型决定是否需要接单部门负责人审核 + // 3. 所有任务都需要接单部门负责人审核(无论是否预先指定了设计师) String releaseType = task.getReleaseType(); - boolean needReceivingApproval = "default".equals(releaseType); - if (needReceivingApproval) { - // default模式:需要接单部门负责人审核 - Long receivingLeaderId = determineReceivingLeader(task); - if (receivingLeaderId == null) { - throw new BusinessException(ResultCode.PARAM_ERROR, "无法确定接单部门负责人,请在系统设置中为接单部门配置负责人"); - } - - // 更新任务状态为待接单部门审核 - task.setApprovalStatus("pending_receiving_approval"); - task.setDemandApprovalTime(LocalDateTime.now()); - task.setCurrentApproverId(receivingLeaderId); - task.setCurrentApproverRole("receiving_leader"); - taskMapper.updateById(task); - - // 记录审核日志 - logApproval(taskId, approverId, 1, "approve", dto.getComment()); - - // 发送通知给接单部门负责人 - User receivingLeader = userMapper.selectById(receivingLeaderId); - if (receivingLeader != null) { - notificationService.sendNotification( - receivingLeaderId, - "新任务待审核", - String.format("任务【%s】需要您审核并分配设计师", task.getTitle()), - "task_approval", - taskId - ); - } - - log.info("需求部门负责人审核通过,任务ID:{},审核人:{},等待接单部门负责人审核", taskId, approverId); - } else { - // recruitment或assigned模式:不需要接单部门负责人审核,直接发布 - task.setDemandApprovalTime(LocalDateTime.now()); - - // 判断是否为流水线任务 - if (task.getIsPipeline() != null && task.getIsPipeline() == 1) { - // 流水线任务:拆分子任务 - splitIntoPipelineSubTasks(task); - // splitIntoPipelineSubTasks方法会根据releaseType设置任务状态 - } else { - // 普通任务处理 - if ("assigned".equals(releaseType) && task.getReceiverId() != null) { - // assigned模式:如果指定了接收人,直接分配给该设计师 - User receiver = userMapper.selectById(task.getReceiverId()); - if (receiver != null && receiver.getDeptId() != null) { - task.setReceiverDeptId(receiver.getDeptId()); - task.setStatus("in_progress"); - task.setStartTime(LocalDateTime.now()); - task.setActualStartTime(LocalDateTime.now()); - - // 重新计算截止时间 - if (task.getEstimatedHours() != null) { - int hours = task.getEstimatedHours().intValue(); - if (hours > 0) { - task.setDeadline(LocalDateTime.now().plusHours(hours)); - } - } - - // 发送通知给被指定的设计师 - notificationService.sendNotification( - task.getReceiverId(), - "新任务分配", - String.format("任务【%s】已分配给您", task.getTitle()), - "task_assigned", - taskId - ); - - log.info("assigned模式任务已直接分配给设计师:taskId={}, receiverId={}", taskId, task.getReceiverId()); - } else { - throw new BusinessException(ResultCode.PARAM_ERROR, "指定的接收人不存在或部门信息不完整"); - } - } else { - // recruitment模式:发布到任务大厅 - task.setStatus("pending"); - } - } - - // 更新审核状态为已通过 - task.setApprovalStatus("approved"); - task.setCurrentApproverId(null); - task.setCurrentApproverRole(null); - taskMapper.updateById(task); - - // 扣除部门积分 - try { - teamScorePoolService.deductScore(task.getPublisherDeptId(), task.getScore()); - } catch (Exception e) { - log.error("扣除部门积分失败", e); - throw new BusinessException(ResultCode.ERROR, "扣除部门积分失败:" + e.getMessage()); - } - - // 记录审核日志 - logApproval(taskId, approverId, 1, "approve", dto.getComment()); - - log.info("需求部门负责人审核通过,任务ID:{},审核人:{},发布类型:{},已直接发布", taskId, approverId, releaseType); + // 确定接单部门负责人 + Long receivingLeaderId = determineReceivingLeader(task); + if (receivingLeaderId == null) { + throw new BusinessException(ResultCode.PARAM_ERROR, "无法确定接单部门负责人,请在系统设置中为接单部门配置负责人"); } + + // 更新任务状态为待接单部门审核 + task.setApprovalStatus("pending_receiving_approval"); + task.setDemandApprovalTime(LocalDateTime.now()); + task.setCurrentApproverId(receivingLeaderId); + task.setCurrentApproverRole("receiving_leader"); + taskMapper.updateById(task); + + // 记录审核日志 + logApproval(taskId, approverId, 1, "approve", dto.getComment()); + + // 发送通知给接单部门负责人 + User receivingLeader = userMapper.selectById(receivingLeaderId); + if (receivingLeader != null) { + // 根据是否预先指定了设计师,调整通知内容 + String notificationContent; + if ("assigned".equals(releaseType) && task.getReceiverId() != null) { + User preAssignedDesigner = userMapper.selectById(task.getReceiverId()); + String designerName = preAssignedDesigner != null ? preAssignedDesigner.getRealName() : "未知"; + notificationContent = String.format("任务【%s】需要您审核确认(已预先指定设计师:%s)", task.getTitle(), designerName); + } else { + notificationContent = String.format("任务【%s】需要您审核并分配设计师", task.getTitle()); + } + notificationService.sendNotification( + receivingLeaderId, + "新任务待审核", + notificationContent, + "task_approval", + taskId + ); + } + + log.info("需求部门负责人审核通过,任务ID:{},审核人:{},发布类型:{},等待接单部门负责人审核", taskId, approverId, releaseType); } /** @@ -223,7 +164,7 @@ public class TaskApprovalService { * @param taskId 任务ID * @param approverId 审核人ID * @param dto 审核数据(包含备注和任务修改) - * @param assignedReceiverId 指定的接收人ID(可选) + * @param assignedReceiverId 指定的接收人ID(可选,如果任务已预先指定设计师则忽略此参数) */ @Transactional(rollbackFor = Exception.class) public void approveReceivingLevel(Long taskId, Long approverId, TaskApprovalDTO dto, Long assignedReceiverId) { @@ -236,8 +177,19 @@ public class TaskApprovalService { logApproval(taskId, approverId, 2, "modify", dto.getComment()); } - // 如果指定了接收人,验证接收人是否存在并设置 - if (assignedReceiverId != null) { + // 检查任务是否已预先指定了设计师 + boolean isPreAssigned = "assigned".equals(task.getReleaseType()) && task.getReceiverId() != null; + Long finalReceiverId = null; + + if (isPreAssigned) { + // 任务已预先指定设计师,不允许修改 + if (assignedReceiverId != null && !assignedReceiverId.equals(task.getReceiverId())) { + log.warn("任务【{}】已预先指定设计师,忽略接单部门负责人的指定操作", task.getTitle()); + } + finalReceiverId = task.getReceiverId(); + log.info("任务【{}】使用预先指定的设计师: {}", task.getTitle(), finalReceiverId); + } else if (assignedReceiverId != null) { + // 接单部门负责人新指定设计师 com.designhub.entity.User receiver = userMapper.selectById(assignedReceiverId); if (receiver == null) { throw new BusinessException(ResultCode.PARAM_ERROR, "指定的接收人不存在"); @@ -245,6 +197,7 @@ public class TaskApprovalService { task.setReceiverId(assignedReceiverId); task.setReceiverDeptId(receiver.getDeptId()); task.setReleaseType("assigned"); // 设置为指定模式 + finalReceiverId = assignedReceiverId; log.info("任务【{}】已指定给设计师【{}】", task.getTitle(), receiver.getRealName()); } @@ -253,19 +206,27 @@ public class TaskApprovalService { // 流水线任务:拆分子任务 splitIntoPipelineSubTasks(task); } else { - // 普通任务:根据是否指定接收人设置状态 - if (assignedReceiverId != null) { + // 普通任务:根据是否有接收人设置状态 + if (finalReceiverId != null) { + // 如果接收人部门ID还没设置,需要设置 + if (task.getReceiverDeptId() == null) { + com.designhub.entity.User receiver = userMapper.selectById(finalReceiverId); + if (receiver != null) { + task.setReceiverDeptId(receiver.getDeptId()); + } + } + task.setStatus("in_progress"); // 指定接单直接进入进行中状态 LocalDateTime now = LocalDateTime.now(); task.setStartTime(now); task.setActualStartTime(now); - // 重新计算deadline:从指定接单时间开始计算 + // 重新计算deadline:从接单时间开始计算 if (task.getEstimatedHours() != null) { int hours = task.getEstimatedHours().intValue(); if (hours > 0) { task.setDeadline(now.plusHours(hours)); - log.info("任务deadline已重新计算:从指定接单时间 {} 开始,预估 {} 小时,截止时间 {}", + log.info("任务deadline已重新计算:从接单时间 {} 开始,预估 {} 小时,截止时间 {}", now, hours, task.getDeadline()); } } @@ -291,19 +252,20 @@ public class TaskApprovalService { // 记录审核日志 String logComment = dto.getComment(); - if (assignedReceiverId != null) { - com.designhub.entity.User receiver = userMapper.selectById(assignedReceiverId); + if (finalReceiverId != null) { + com.designhub.entity.User receiver = userMapper.selectById(finalReceiverId); + String assignNote = isPreAssigned ? "(预先指定)" : ""; logComment = (logComment != null ? logComment + ";" : "") + - "已指定给设计师:" + (receiver != null ? receiver.getRealName() : assignedReceiverId); + "已指定给设计师" + assignNote + ":" + (receiver != null ? receiver.getRealName() : finalReceiverId); } logApproval(taskId, approverId, 2, "approve", logComment); // 发送钉钉通知 - if (assignedReceiverId != null) { + if (finalReceiverId != null) { // 指定了设计师,发送个人通知和群通知 log.info("任务【{}】已指定给设计师,准备发送钉钉通知", task.getTitle()); try { - com.designhub.entity.User receiver = userMapper.selectById(assignedReceiverId); + com.designhub.entity.User receiver = userMapper.selectById(finalReceiverId); String receiverName = receiver != null ? receiver.getRealName() : "未知"; String receiverPhone = receiver != null ? receiver.getPhone() : null; @@ -336,7 +298,7 @@ public class TaskApprovalService { } Notification notification = new Notification(); - notification.setReceiverId(assignedReceiverId); + notification.setReceiverId(finalReceiverId); notification.setTitle("您有新任务指派"); notification.setContent(personalContent.toString()); notification.setNotificationType("TASK_ASSIGNED"); @@ -344,7 +306,7 @@ public class TaskApprovalService { notification.setRelatedId(task.getId()); notification.setPushDingtalk(1); // 推送到钉钉 notificationService.sendNotification(notification); - log.info("任务指派个人通知已发送给设计师: userId={}", assignedReceiverId); + log.info("任务指派个人通知已发送给设计师: userId={}", finalReceiverId); // 2. 发送群通知并@指定的设计师 String title = "任务已指派";