package com.example.web.task; import com.example.web.entity.Contacts; import com.example.web.entity.Enterprise; import com.example.web.entity.Users; import com.example.web.mapper.CustomerMapper; import com.example.web.mapper.WechatCustomerMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.util.List; /** * @Description: 客户状态流转定时任务 */ @Component public class CustomerStatusTask { @Autowired private CustomerMapper customerMapper; @Autowired private WechatCustomerMapper wechatCustomerMapper; /** * 每分钟执行一次客户状态流转处理 */ @Scheduled(cron = "0 * */5 * * ?") public void processCustomerStatus() { System.out.println("DEBUG: 开始执行客户状态流转定时任务,当前时间: " + LocalDateTime.now()); // 获取当前时间 LocalDateTime now = LocalDateTime.now(); // 计算3天前和6天前的时间 LocalDateTime threeDaysAgo = now.minusDays(3); LocalDateTime sixDaysAgo = now.minusDays(6); // 处理primary数据源的客户 processPrimaryCustomers(threeDaysAgo, sixDaysAgo, now); // 处理wechat数据源的客户 processWechatCustomers(threeDaysAgo, sixDaysAgo, now); System.out.println("DEBUG: 客户状态流转定时任务执行完成"); } /** * 处理primary数据源的客户状态流转 * @param threeDaysAgo 3天前的时间 * @param sixDaysAgo 6天前的时间 * @param now 当前时间 */ private void processPrimaryCustomers(LocalDateTime threeDaysAgo, LocalDateTime sixDaysAgo, LocalDateTime now) { try { System.out.println("DEBUG: 开始处理primary数据源的客户状态流转"); // 获取所有联系人信息 List contactsList = customerMapper.getAllContacts(); for (Contacts contacts : contactsList) { // 获取企业信息 Enterprise enterprise = customerMapper.getEnterpriseByContactId(contacts.getId()); if (enterprise == null) { continue; } String currentLevel = enterprise.getLevel(); LocalDateTime followupAt = contacts.getFollowup_at(); // 第一次筛选:查找 followup_at ≤ 3天前的客户,当前状态为"未分级" → 变更为"组织公海池客户" if ("unclassified".equals(currentLevel) && followupAt != null && followupAt.isBefore(threeDaysAgo)) { enterprise.setLevel("organization-sea-pools"); customerMapper.updateEnterprise(enterprise); System.out.println("DEBUG: primary数据源客户ID: " + contacts.getId() + " 从 未分级 变更为 组织公海池客户"); } // 第二次筛选:查找 followup_at ≤ 6天前的客户,当前状态为"组织公海池客户" → 变更为"部门公海池" else if ("organization-sea-pools".equals(currentLevel) && followupAt != null && followupAt.isBefore(sixDaysAgo)) { enterprise.setLevel("department-sea-pools"); customerMapper.updateEnterprise(enterprise); System.out.println("DEBUG: primary数据源客户ID: " + contacts.getId() + " 从 组织公海池客户 变更为 部门公海池"); } } System.out.println("DEBUG: primary数据源的客户状态流转处理完成"); } catch (Exception e) { System.out.println("DEBUG: 处理primary数据源的客户状态流转失败,异常信息: " + e.getMessage()); e.printStackTrace(); } } /** * 处理wechat数据源的客户状态流转 * @param threeDaysAgo 3天前的时间 * @param sixDaysAgo 6天前的时间 * @param now 当前时间 */ private void processWechatCustomers(LocalDateTime threeDaysAgo, LocalDateTime sixDaysAgo, LocalDateTime now) { try { System.out.println("DEBUG: 开始处理wechat数据源的客户状态流转"); // 获取所有用户信息 List usersList = wechatCustomerMapper.getAllUsers(); for (Users user : usersList) { // 跳过type为manager的用户 if ("manager".equals(user.getType())) { continue; } String currentLevel = user.getLevel(); LocalDateTime followupAt = user.getFollowup_at(); // 第一次筛选:查找 followup_at ≤ 3天前的客户,当前状态为"未分级" → 变更为"组织公海池客户" if ("unclassified".equals(currentLevel) && followupAt != null && followupAt.isBefore(threeDaysAgo)) { user.setLevel("organization-sea-pools"); user.setUpdated_at(now); wechatCustomerMapper.updateUsers(user); System.out.println("DEBUG: wechat数据源客户ID: " + user.getUserId() + " 从 未分级 变更为 组织公海池客户"); } // 第二次筛选:查找 followup_at ≤ 6天前的客户,当前状态为"组织公海池客户" → 变更为"部门公海池" else if ("organization-sea-pools".equals(currentLevel) && followupAt != null && followupAt.isBefore(sixDaysAgo)) { user.setLevel("department-sea-pools"); user.setUpdated_at(now); wechatCustomerMapper.updateUsers(user); System.out.println("DEBUG: wechat数据源客户ID: " + user.getUserId() + " 从 组织公海池客户 变更为 部门公海池"); } } System.out.println("DEBUG: wechat数据源的客户状态流转处理完成"); } catch (Exception e) { System.out.println("DEBUG: 处理wechat数据源的客户状态流转失败,异常信息: " + e.getMessage()); e.printStackTrace(); } } }