package com.example.web.service; import com.example.web.dto.NotificationDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.ZoneOffset; /** * WebSocket通知服务 */ @Service public class NotificationService { @Autowired private SimpMessagingTemplate messagingTemplate; /** * 广播通知给所有用户 */ public void broadcastNotification(NotificationDTO notification) { messagingTemplate.convertAndSend("/topic/notifications", notification); } /** * 发送通知给特定用户 */ public void sendNotificationToUser(String userId, NotificationDTO notification) { messagingTemplate.convertAndSendToUser(userId, "/queue/notifications", notification); } /** * 发送客户数据更新通知 */ public void sendCustomerUpdateNotification(String customerId, String message) { NotificationDTO notification = new NotificationDTO(); notification.setType("CUSTOMER_UPDATE"); notification.setTitle("客户数据更新"); notification.setMessage(message); notification.setCustomerId(customerId); notification.setTimestamp(LocalDateTime.now(ZoneOffset.UTC)); // 广播给所有用户 messagingTemplate.convertAndSend("/topic/customers/" + customerId, notification); } }