From a179f815426337178461ad8a31973ae18af60bfa Mon Sep 17 00:00:00 2001 From: menxipeng Date: Tue, 26 Aug 2025 22:08:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=8B=E5=8A=A8=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E9=80=9A=E7=9F=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/NotificationRecordsMapper.java | 2 + .../ruoyi/system/mapper/ShopUserMapper.java | 3 +- .../impl/NotificationsServiceImpl.java | 136 +++++++++++------- .../system/NotificationRecordsMapper.xml | 4 + .../mapper/system/ShopUserMapper.xml | 7 + 5 files changed, 99 insertions(+), 53 deletions(-) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/NotificationRecordsMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/NotificationRecordsMapper.java index 548d5b2..97c3530 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/NotificationRecordsMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/NotificationRecordsMapper.java @@ -67,4 +67,6 @@ public interface NotificationRecordsMapper * @return 结果 */ public int batchInsertNotificationRecords(List list); + + int updateStatusNotificationRecords(List records); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShopUserMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShopUserMapper.java index 697c134..31980ad 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShopUserMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ShopUserMapper.java @@ -86,4 +86,5 @@ public interface ShopUserMapper { */ public List selectUserMusicTags(String userId); -} \ No newline at end of file + List selectPhoneShopUserByUserIds(List userIds); +} \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/NotificationsServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/NotificationsServiceImpl.java index 9224997..cd9d6c3 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/NotificationsServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/NotificationsServiceImpl.java @@ -3,9 +3,12 @@ package com.ruoyi.system.service.impl; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.NotificationRecords; import com.ruoyi.common.core.domain.entity.Notifications; +import com.ruoyi.common.core.domain.entity.PushMsgInfo; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.system.config.UmengConfig; import com.ruoyi.system.mapper.NotificationRecordsMapper; import com.ruoyi.system.mapper.NotificationsMapper; +import com.ruoyi.system.mapper.ShopUserMapper; import com.ruoyi.system.service.INotificationsService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; /** * 【请填写功能名称】Service业务层处理 @@ -22,6 +26,7 @@ import java.util.List; * @author ruoyi * @date 2025-08-23 */ +@Transactional @Service public class NotificationsServiceImpl implements INotificationsService { @@ -33,6 +38,12 @@ public class NotificationsServiceImpl implements INotificationsService @Autowired private NotificationRecordsMapper notificationRecordsMapper; + @Autowired + private UmengConfig umengConfig; + + @Autowired + private ShopUserMapper shopUserMapper; + /** * 查询【请填写功能名称】 * @@ -127,67 +138,74 @@ public class NotificationsServiceImpl implements INotificationsService NotificationRecords query = new NotificationRecords(); query.setNotificationId(id); List records = notificationRecordsMapper.selectNotificationRecordsList(query); - + + List userIds = records.stream().map(NotificationRecords::getUserId).collect(Collectors.toList()); + if (records.isEmpty()) { return AjaxResult.error("该通知没有绑定任何用户,请先绑定用户"); } - + List phones = shopUserMapper.selectPhoneShopUserByUserIds(userIds); // 当前时间 Date now = DateUtils.getNowDate(); int successCount = 0; int failCount = 0; - + + + sendNotificationToThirdParty(notification, phones); + query.setStatus("sent"); + notificationRecordsMapper.updateNotificationRecords(query); + // 遍历所有记录,进行发布 - for (NotificationRecords record : records) - { - // 如果记录已经发送过,则不再创建新记录,只更新状态 - boolean sendResult = sendNotificationToThirdParty(notification, record); - if ("sent".equals(record.getStatus())) - { - // 调用第三方接口发送通知 - - if (sendResult) - { - successCount++; - } - else - { - failCount++; - // 更新失败原因 - record.setFailureReason("重新发送失败"); - notificationRecordsMapper.updateNotificationRecords(record); - } - } - else - { - // 调用第三方接口发送通知 - - if (sendResult) - { - // 更新记录状态为已发送 - record.setStatus("sent"); - record.setSentTime(now); - record.setUpdateTime(now); - notificationRecordsMapper.updateNotificationRecords(record); - successCount++; - } - else - { - // 更新记录状态为发送失败 - record.setStatus("failed"); - record.setFailureReason("发送失败"); - record.setUpdateTime(now); - notificationRecordsMapper.updateNotificationRecords(record); - failCount++; - } - } - } +// for (NotificationRecords record : records) +// { +// // 如果记录已经发送过,则不再创建新记录,只更新状态 +// boolean sendResult = sendNotificationToThirdParty(notification, record); +// if ("sent".equals(record.getStatus())) +// { +// // 调用第三方接口发送通知 +// +// if (sendResult) +// { +// successCount++; +// } +// else +// { +// failCount++; +// // 更新失败原因 +// record.setFailureReason("重新发送失败"); +// notificationRecordsMapper.updateNotificationRecords(record); +// } +// } +// else +// { +// // 调用第三方接口发送通知 +// +// if (sendResult) +// { +// // 更新记录状态为已发送 +// record.setStatus("sent"); +// record.setSentTime(now); +// record.setUpdateTime(now); +// notificationRecordsMapper.updateNotificationRecords(record); +// successCount++; +// } +// else +// { +// // 更新记录状态为发送失败 +// record.setStatus("failed"); +// record.setFailureReason("发送失败"); +// record.setUpdateTime(now); +// notificationRecordsMapper.updateNotificationRecords(record); +// failCount++; +// } +// } +// } // 更新通知状态为已发布 // notification.setStatus("published"); // notification.setPublishTime(now); - notificationsMapper.updateNotifications(notification); + // notificationsMapper.updateNotifications(notification); return AjaxResult.success("通知发布完成,成功:" + successCount + ",失败:" + failCount); } @@ -199,15 +217,29 @@ public class NotificationsServiceImpl implements INotificationsService * @param record 通知记录 * @return 发送结果 */ - private boolean sendNotificationToThirdParty(Notifications notification, NotificationRecords record) + private boolean sendNotificationToThirdParty(Notifications notification, List phones) { try { // TODO: 实现调用第三方接口发送通知的逻辑 // 这里是模拟实现,实际项目中需要根据具体的第三方接口进行调用 - log.info("发送通知:通知ID={}, 用户ID={}, 标题={}, 内容={}", - notification.getId(), record.getUserId(), notification.getTitle(), notification.getContent()); - +// log.info("发送通知:通知ID={}, 用户ID={}, 标题={}, 内容={}", +// notification.getId(), record.getUserId(), notification.getTitle(), notification.getContent()); + // + // 按照499个手机号一组进行分批处理 + for (int i = 0; i < phones.size(); i += 499) { + int endIndex = Math.min(i + 499, phones.size()); + List phoneBatch = phones.subList(i, endIndex); + String phoneS = String.join(",", phoneBatch); + PushMsgInfo pushMsgInfo = new PushMsgInfo(); + pushMsgInfo.setAlias(phoneS); + pushMsgInfo.setTitle(notification.getTitle()); + pushMsgInfo.setText(notification.getContent()); + umengConfig.sendAndroidCustomizedcast(pushMsgInfo); + umengConfig.sendIOSCustomizedcast(pushMsgInfo); + log.info("发送通知批次 {}/{}, 本批次包含 {} 个手机号", + (i / 499) + 1, (phones.size() + 498) / 499, phoneBatch.size()); + } // 模拟发送成功 return true; } diff --git a/ruoyi-system/src/main/resources/mapper/system/NotificationRecordsMapper.xml b/ruoyi-system/src/main/resources/mapper/system/NotificationRecordsMapper.xml index 55afb2a..f29f3ae 100644 --- a/ruoyi-system/src/main/resources/mapper/system/NotificationRecordsMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/NotificationRecordsMapper.xml @@ -110,4 +110,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ) + + + + \ No newline at end of file diff --git a/ruoyi-system/src/main/resources/mapper/system/ShopUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/ShopUserMapper.xml index 4528c4f..3b56e6b 100644 --- a/ruoyi-system/src/main/resources/mapper/system/ShopUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/ShopUserMapper.xml @@ -219,4 +219,11 @@ WHERE su.user_id = #{userId} AND mi.label IS NOT NULL AND mi.label != '' GROUP BY mi.label + + \ No newline at end of file