feat: 钉钉通知模板增加任务类型明细、数量和积分信息

This commit is contained in:
Your Name 2026-01-07 09:48:08 +08:00
parent 1b6ed7538b
commit b6457bba01
2 changed files with 103 additions and 58 deletions

View File

@ -247,9 +247,7 @@ public class DingTalkController {
content.append("测试时间:").append(java.time.LocalDateTime.now().format(
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append(" \n");
content.append(" \n");
content.append("-------------------------------- \n");
content.append("访问系统:qd.yydd.ai \n");
content.append("--------------------------------");
content.append("点击下方按钮查看详情");
break;
}
@ -388,9 +386,7 @@ public class DingTalkController {
content.append("测试时间:").append(java.time.LocalDateTime.now().format(
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append(" \n");
content.append(" \n");
content.append("-------------------------------- \n");
content.append("访问系统:qd.yydd.ai \n");
content.append("--------------------------------");
content.append("点击下方按钮查看详情");
break;
}

View File

@ -30,6 +30,7 @@ import java.util.stream.Collectors;
public class TaskApprovalService {
private final TaskMapper taskMapper;
private final TaskItemMapper taskItemMapper;
private final TaskApprovalLogMapper approvalLogMapper;
private final DepartmentMapper departmentMapper;
private final UserMapper userMapper;
@ -350,76 +351,102 @@ public class TaskApprovalService {
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(task.getTitle()).append(" \n");
content.append("**任务编号**:").append(task.getTaskNo()).append(" \n");
// 数量信息
if (task.getQuantity() != null && task.getQuantity() > 0) {
content.append("任务数量:").append(task.getQuantity()).append(" 个 \n");
// 任务详情
if (task.getBrandName() != null && !task.getBrandName().isEmpty()) {
content.append("**品牌**:").append(task.getBrandName()).append(" \n");
}
// 任务需求类型
if (task.getTaskTypeIds() != null && !task.getTaskTypeIds().isEmpty()) {
StringBuilder typeNames = new StringBuilder();
String[] typeIds = task.getTaskTypeIds().split(",");
for (String typeIdStr : typeIds) {
try {
Long typeId = Long.parseLong(typeIdStr.trim());
DemandType demandType = configService.getDemandTypeById(typeId);
if (demandType != null) {
if (typeNames.length() > 0) {
typeNames.append("、");
}
typeNames.append(demandType.getName());
}
} catch (NumberFormatException e) {
log.warn("解析任务类型ID失败:{}", typeIdStr);
}
}
if (typeNames.length() > 0) {
content.append("需求类型:").append(typeNames.toString()).append(" \n");
}
if (task.getPlatformName() != null && !task.getPlatformName().isEmpty()) {
content.append("**平台**:").append(task.getPlatformName()).append(" \n");
}
if (task.getSpuInfo() != null && !task.getSpuInfo().isEmpty()) {
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");
// 任务详情
if (task.getBrandName() != null && !task.getBrandName().isEmpty()) {
content.append("品牌:").append(task.getBrandName()).append(" \n");
}
if (task.getCategoryName() != null && !task.getCategoryName().isEmpty()) {
content.append("类目:").append(task.getCategoryName()).append(" \n");
}
if (task.getPlatformName() != null && !task.getPlatformName().isEmpty()) {
content.append("平台:").append(task.getPlatformName()).append(" \n");
}
if (task.getSpuInfo() != null && !task.getSpuInfo().isEmpty()) {
content.append("SPU:").append(task.getSpuInfo()).append(" \n");
}
if (task.getTaskSubType() != null && !task.getTaskSubType().isEmpty()) {
content.append("子类型:").append(task.getTaskSubType()).append(" \n");
}
// 查询子任务获取任务类型明细
List<TaskItem> taskItems = taskItemMapper.selectList(
new LambdaQueryWrapper<TaskItem>()
.eq(TaskItem::getTaskId, task.getId())
.orderByAsc(TaskItem::getGroupNo)
);
// 时间信息
if (task.getEstimatedHours() != null) {
content.append("预估工时:").append(task.getEstimatedHours().intValue()).append(" 小时(接单后开始计时) \n");
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()) {
content.append("发起人:").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");
content.append("**发起部门**:").append(task.getPublisherDeptName()).append(" \n");
}
content.append(" \n");
content.append("-------------------------------- \n");
content.append("前往抢单:qd.yydd.ai \n");
content.append("--------------------------------");
content.append("点击下方按钮查看详情");
log.info("准备调用钉钉机器人发送消息,title={}", title);
boolean success = dingTalkService.sendRobotMessage(title, content.toString());
@ -795,4 +822,26 @@ public class TaskApprovalService {
this.log.error("记录审核日志失败", e);
}
}
/**
* 格式化预估工时显示
* 将小时数转换为 "X天X小时" 格式
*/
private String formatEstimatedHours(java.math.BigDecimal hours) {
if (hours == null) {
return "未设置";
}
int totalHours = hours.intValue();
int days = totalHours / 24;
int remainingHours = totalHours % 24;
StringBuilder sb = new StringBuilder();
if (days > 0) {
sb.append(days).append("天");
}
if (remainingHours > 0 || days == 0) {
sb.append(remainingHours).append("小时");
}
return sb.toString();
}
}