4 changed files with 286 additions and 66 deletions
@ -0,0 +1,93 @@ |
|||||
|
package com.example.web.service; |
||||
|
|
||||
|
import com.example.web.entity.CustomerData; |
||||
|
import com.example.web.entity.Login; |
||||
|
import com.example.web.dto.NotificationDTO; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
import java.time.ZoneOffset; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* 客户数据变化监控服务 |
||||
|
* 用于监控数据库中客户数据的变化,当管理员在外部系统修改客户数据时,通知对应的业务员 |
||||
|
*/ |
||||
|
@Service |
||||
|
public class CustomerChangeMonitorService { |
||||
|
|
||||
|
@Autowired |
||||
|
private CustomerService customerService; |
||||
|
|
||||
|
@Autowired |
||||
|
private NotificationService notificationService; |
||||
|
|
||||
|
// 存储上次检查时的客户数据状态
|
||||
|
private Map<String, String> lastCustomerStates = new HashMap<>(); |
||||
|
|
||||
|
/** |
||||
|
* 定时检查客户数据变化 |
||||
|
* 每5秒检查一次 |
||||
|
*/ |
||||
|
@Scheduled(fixedRate = 5000) |
||||
|
public void monitorCustomerChanges() { |
||||
|
try { |
||||
|
// 获取所有客户数据
|
||||
|
List<CustomerData> allCustomers = customerService.getAllCustomers(); |
||||
|
|
||||
|
// 检查每个客户的数据变化
|
||||
|
for (CustomerData customer : allCustomers) { |
||||
|
// 构建客户状态字符串,包含关键字段
|
||||
|
String currentState = buildCustomerState(customer); |
||||
|
String customerId = customer.getId(); |
||||
|
String lastState = lastCustomerStates.get(customerId); |
||||
|
|
||||
|
// 如果是新客户或者客户数据发生变化
|
||||
|
if (lastState == null || !lastState.equals(currentState)) { |
||||
|
// 检查是否是分配给业务员的情况
|
||||
|
if (customer.getUserName() != null && !customer.getUserName().isEmpty() && |
||||
|
"banold".equals(customer.getNotice())) { |
||||
|
|
||||
|
// 发送客户分配通知
|
||||
|
NotificationDTO notification = new NotificationDTO(); |
||||
|
notification.setType("CUSTOMER_ASSIGNMENT"); |
||||
|
notification.setTitle("客户分配通知"); |
||||
|
notification.setMessage("您已被分配了一个新客户: " + customer.getNickName()); |
||||
|
notification.setCustomerId(customerId); |
||||
|
notification.setTimestamp(LocalDateTime.now(ZoneOffset.UTC)); |
||||
|
|
||||
|
// 广播通知
|
||||
|
notificationService.broadcastNotification(notification); |
||||
|
|
||||
|
// 发送特定客户通知
|
||||
|
notificationService.sendCustomerUpdateNotification(customerId, "客户已分配给您"); |
||||
|
} |
||||
|
|
||||
|
// 更新客户状态
|
||||
|
lastCustomerStates.put(customerId, currentState); |
||||
|
} |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 构建客户状态字符串,用于比较客户数据是否变化 |
||||
|
*/ |
||||
|
private String buildCustomerState(CustomerData customer) { |
||||
|
return String.format("%s|%s|%s|%s|%s|%s|%s|%s", |
||||
|
customer.getId(), |
||||
|
customer.getNickName(), |
||||
|
customer.getUserName(), |
||||
|
customer.getManagercompany(), |
||||
|
customer.getManagerdepartment(), |
||||
|
customer.getOrganization(), |
||||
|
customer.getNotice(), |
||||
|
customer.getUpdated_at()); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue