fix: 统一任务通知模版格式,指派任务通知与新任务发布通知保持一致

This commit is contained in:
Your Name 2026-01-08 15:56:40 +08:00
parent eb53c182ad
commit 7b92476bc5

View File

@ -269,41 +269,115 @@ public class TaskApprovalService {
String receiverName = receiver != null ? receiver.getRealName() : "未知";
String receiverPhone = receiver != null ? receiver.getPhone() : null;
// 1. 发送个人钉钉通知给设计师
StringBuilder personalContent = new StringBuilder();
personalContent.append("### 任务已自动分配给您,无需抢单 \n\n");
personalContent.append("**任务名称**:").append(task.getTitle()).append(" \n");
personalContent.append("**任务编号**:").append(task.getTaskNo()).append(" \n");
personalContent.append("**任务积分**:").append(task.getScore()).append(" 分 \n");
// 1. 构建统一格式的通知内容(与"新任务发布"模版保持一致)
StringBuilder content = new StringBuilder();
if (task.getEstimatedHours() != null) {
int hours = task.getEstimatedHours().intValue();
int days = hours / 8;
int remainingHours = hours % 8;
String timeStr = days > 0 ? (days + "天" + (remainingHours > 0 ? remainingHours + "小时" : "")) : (hours + "小时");
personalContent.append("**时效要求**:").append(timeStr).append(" \n");
}
// 基本信息
content.append("**任务名称**:").append(task.getTitle()).append(" \n");
content.append("**任务编号**:").append(task.getTaskNo()).append(" \n");
// 任务详情
if (task.getBrandName() != null && !task.getBrandName().isEmpty()) {
personalContent.append("**品牌**:").append(task.getBrandName()).append(" \n");
content.append("**品牌**:").append(task.getBrandName()).append(" \n");
}
if (task.getPlatformName() != null && !task.getPlatformName().isEmpty()) {
personalContent.append("**平台**:").append(task.getPlatformName()).append(" \n");
content.append("**平台**:").append(task.getPlatformName()).append(" \n");
}
if (task.getSpuInfo() != null && !task.getSpuInfo().isEmpty()) {
personalContent.append("**SPU**:").append(task.getSpuInfo()).append(" \n");
content.append("**SPU**:").append(task.getSpuInfo()).append(" \n");
}
if (task.getTaskSubType() != null && !task.getTaskSubType().isEmpty()) {
content.append("**任务类型**:").append(task.getTaskSubType()).append(" \n");
}
content.append(" \n");
// 查询子任务获取任务类型明细
List<TaskItem> taskItems = taskItemMapper.selectList(
new LambdaQueryWrapper<TaskItem>()
.eq(TaskItem::getTaskId, task.getId())
.orderByAsc(TaskItem::getGroupNo)
);
if (!taskItems.isEmpty()) {
content.append("**任务明细**: \n");
for (TaskItem item : taskItems) {
// 通过任务类型ID获取接单部门名称
String deptName = "未指定";
if (item.getTaskTypeId() != null) {
try {
DemandType demandType = configService.getDemandTypeById(item.getTaskTypeId());
if (demandType != null && demandType.getAllowedDeptIds() != null && !demandType.getAllowedDeptIds().isEmpty()) {
String[] deptIds = demandType.getAllowedDeptIds().split(",");
Long firstDeptId = Long.parseLong(deptIds[0].trim());
Department dept = departmentMapper.selectById(firstDeptId);
if (dept != null) {
deptName = dept.getDeptName();
}
}
} catch (Exception e) {
log.warn("获取部门名称失败: {}", e.getMessage());
}
}
// 格式化工时显示
String hoursDisplay = formatEstimatedHours(item.getEstimatedHours());
content.append(" \n");
content.append("▸ **").append(deptName).append("** | ")
.append(item.getTotalScore()).append("分 | ")
.append(hoursDisplay).append(" \n");
// 解析并显示任务类型明细
if (item.getTypeDetails() != null && !item.getTypeDetails().isEmpty()) {
try {
@SuppressWarnings("unchecked")
List<java.util.Map<String, Object>> typeDetails =
objectMapper.readValue(item.getTypeDetails(), List.class);
for (java.util.Map<String, Object> detail : typeDetails) {
String typeName = String.valueOf(detail.get("typeName"));
Object qtyObj = detail.get("quantity");
Object scoreObj = detail.get("totalScore");
int qty = qtyObj instanceof Number ? ((Number) qtyObj).intValue() : 1;
String score = scoreObj != null ? String.valueOf(scoreObj) : "0";
content.append("  └ ").append(typeName)
.append(" ×").append(qty)
.append(" = ").append(score).append("分 \n");
}
} catch (Exception e) {
log.warn("解析任务类型明细失败: {}", e.getMessage());
}
}
}
} else {
// 没有子任务时显示总积分和工时
content.append("**任务积分**:").append(task.getScore()).append(" 分 \n");
if (task.getEstimatedHours() != null) {
content.append("**预估工时**:").append(formatEstimatedHours(task.getEstimatedHours())).append(" \n");
}
}
content.append(" \n");
// 发起人信息
if (task.getPublisherName() != null && !task.getPublisherName().isEmpty()) {
personalContent.append(" \n**发起人**:").append(task.getPublisherName()).append(" \n");
content.append("**发起人**:").append(task.getPublisherName()).append(" \n");
}
if (task.getPublisherDeptName() != null && !task.getPublisherDeptName().isEmpty()) {
content.append("**发起部门**:").append(task.getPublisherDeptName()).append(" \n");
}
personalContent.append(" \n> 💡 任务已开始计时,请前往【我的任务】查看详情");
// 指派信息
content.append("**指派给**:").append(receiverName).append(" \n");
content.append(" \n");
content.append("点击下方按钮查看详情");
// 2. 发送个人钉钉通知给设计师
Notification notification = new Notification();
notification.setReceiverId(finalReceiverId);
notification.setTitle("任务已指派给您(已自动开始)");
notification.setContent(personalContent.toString());
notification.setTitle("任务已指派");
notification.setContent(content.toString());
notification.setNotificationType("TASK_ASSIGNED");
notification.setRelatedType("task");
notification.setRelatedId(task.getId());
@ -311,36 +385,12 @@ public class TaskApprovalService {
notificationService.sendNotification(notification);
log.info("任务指派个人通知已发送给设计师: userId={}", finalReceiverId);
// 2. 只有非预先指定的任务才发送群通知(预先指定的任务只发送个人通知)
// 3. 只有非预先指定的任务才发送群通知(预先指定的任务只发送个人通知)
if (!isPreAssigned) {
// 发送群通知并@指定的设计师
String title = "任务已指派";
StringBuilder content = new StringBuilder();
content.append("-------------------------------- \n");
content.append(" 【 任 务 已 指 派 】 \n");
content.append("-------------------------------- \n\n");
content.append("任务名称:").append(task.getTitle()).append(" \n");
content.append("任务编号:").append(task.getTaskNo()).append(" \n");
content.append("任务积分:").append(task.getScore()).append(" 分 \n");
content.append("指派给:").append(receiverName).append(" \n");
if (task.getEstimatedHours() != null) {
content.append("预估工时:").append(task.getEstimatedHours().intValue()).append(" 小时 \n");
}
if (task.getPublisherName() != null && !task.getPublisherName().isEmpty()) {
content.append("发起人:").append(task.getPublisherName()).append(" \n");
}
content.append(" \n");
content.append("-------------------------------- \n");
content.append("请尽快处理! \n");
content.append("--------------------------------");
// 发送群通知并@指定的设计师,使用相同的内容模版
java.util.List<String> atMobiles = receiverPhone != null ?
java.util.Arrays.asList(receiverPhone) : null;
dingTalkService.sendRobotMessage(title, content.toString(), atMobiles);
dingTalkService.sendRobotMessage("任务已指派", content.toString(), atMobiles);
log.info("任务指派群通知已发送,@手机号: {}", receiverPhone);
} else {
log.info("任务【{}】为预先指定任务,只发送个人通知,不发送群通知", task.getTitle());