feat: 实现钉钉个人通知功能

- NotificationService添加DingTalkService和UserMapper依赖
- 实现asyncPushToDingtalk方法,支持发送个人工作通知
- 自动检查用户是否绑定钉钉ID
- 构建Markdown格式的通知内容
- 异步发送,不影响主流程性能
This commit is contained in:
Your Name 2026-01-03 10:29:55 +08:00
parent e0a9402efd
commit 1fd4d245a1

View File

@ -3,13 +3,18 @@ package com.designhub.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.designhub.entity.Notification;
import com.designhub.entity.User;
import com.designhub.mapper.NotificationMapper;
import com.designhub.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
/**
@ -21,6 +26,10 @@ import java.util.List;
public class NotificationService {
private final NotificationMapper notificationMapper;
private final UserMapper userMapper;
@Autowired(required = false)
private DingTalkService dingTalkService;
/**
* 将优先级字符串转换为整数
@ -152,11 +161,84 @@ public class NotificationService {
}
/**
* 异步推送到钉钉(预留接口)
* 异步推送到钉钉
*/
@Async
private void asyncPushToDingtalk(Notification notification) {
// TODO: 实现钉钉推送逻辑
log.info("钉钉推送功能待实现: notificationId={}", notification.getId());
try {
// 检查钉钉服务是否可用
if (dingTalkService == null) {
log.warn("钉钉服务未启用,跳过推送");
return;
}
// 检查是否启用了钉钉通知功能
if (!dingTalkService.isFeatureEnabled("notification")) {
log.info("钉钉通知功能未启用,跳过推送: notificationId={}", notification.getId());
return;
}
// 获取接收者信息
User receiver = userMapper.selectById(notification.getReceiverId());
if (receiver == null) {
log.warn("接收者不存在,跳过钉钉推送: receiverId={}", notification.getReceiverId());
return;
}
// 检查用户是否绑定了钉钉ID
if (receiver.getDingUserId() == null || receiver.getDingUserId().isEmpty()) {
log.info("用户未绑定钉钉ID,跳过推送: userId={}, username={}",
receiver.getId(), receiver.getUsername());
return;
}
// 构建钉钉消息内容
String title = notification.getTitle();
String content = buildDingTalkContent(notification);
// 发送工作通知(个人消息)
boolean success = dingTalkService.sendWorkNotification(
Collections.singletonList(receiver.getDingUserId()),
title,
content
);
if (success) {
log.info("钉钉个人通知发送成功: notificationId={}, receiverId={}, dingUserId={}",
notification.getId(), receiver.getId(), receiver.getDingUserId());
} else {
log.warn("钉钉个人通知发送失败: notificationId={}, receiverId={}",
notification.getId(), receiver.getId());
}
} catch (Exception e) {
log.error("推送钉钉通知异常: notificationId={}", notification.getId(), e);
}
}
/**
* 构建钉钉消息内容(Markdown格式)
*/
private String buildDingTalkContent(Notification notification) {
StringBuilder content = new StringBuilder();
content.append("### ").append(notification.getTitle()).append("\n\n");
content.append(notification.getContent()).append("\n\n");
// 根据通知类型添加不同的提示
if ("task".equals(notification.getNotificationType())) {
content.append("> 📋 任务相关通知\n\n");
} else if ("review".equals(notification.getNotificationType())) {
content.append("> ⭐ 评审相关通知\n\n");
} else if ("score".equals(notification.getNotificationType())) {
content.append("> 💰 积分相关通知\n\n");
} else if ("system".equals(notification.getNotificationType())) {
content.append("> 🔔 系统通知\n\n");
}
content.append("---\n\n");
content.append("请登录系统查看详情");
return content.toString();
}
// ==================== 便捷方法 ====================