增加手动发布通知功能

This commit is contained in:
menxipeng
2025-08-26 22:08:37 +08:00
parent ddef5f2146
commit a179f81542
5 changed files with 99 additions and 53 deletions

View File

@@ -67,4 +67,6 @@ public interface NotificationRecordsMapper
* @return 结果 * @return 结果
*/ */
public int batchInsertNotificationRecords(List<NotificationRecords> list); public int batchInsertNotificationRecords(List<NotificationRecords> list);
int updateStatusNotificationRecords(List<NotificationRecords> records);
} }

View File

@@ -86,4 +86,5 @@ public interface ShopUserMapper {
*/ */
public List<String> selectUserMusicTags(String userId); public List<String> selectUserMusicTags(String userId);
} List<String> selectPhoneShopUserByUserIds(List<Long> userIds);
}

View File

@@ -3,9 +3,12 @@ package com.ruoyi.system.service.impl;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.NotificationRecords; import com.ruoyi.common.core.domain.entity.NotificationRecords;
import com.ruoyi.common.core.domain.entity.Notifications; 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.common.utils.DateUtils;
import com.ruoyi.system.config.UmengConfig;
import com.ruoyi.system.mapper.NotificationRecordsMapper; import com.ruoyi.system.mapper.NotificationRecordsMapper;
import com.ruoyi.system.mapper.NotificationsMapper; import com.ruoyi.system.mapper.NotificationsMapper;
import com.ruoyi.system.mapper.ShopUserMapper;
import com.ruoyi.system.service.INotificationsService; import com.ruoyi.system.service.INotificationsService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -15,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* 【请填写功能名称】Service业务层处理 * 【请填写功能名称】Service业务层处理
@@ -22,6 +26,7 @@ import java.util.List;
* @author ruoyi * @author ruoyi
* @date 2025-08-23 * @date 2025-08-23
*/ */
@Transactional
@Service @Service
public class NotificationsServiceImpl implements INotificationsService public class NotificationsServiceImpl implements INotificationsService
{ {
@@ -33,6 +38,12 @@ public class NotificationsServiceImpl implements INotificationsService
@Autowired @Autowired
private NotificationRecordsMapper notificationRecordsMapper; private NotificationRecordsMapper notificationRecordsMapper;
@Autowired
private UmengConfig umengConfig;
@Autowired
private ShopUserMapper shopUserMapper;
/** /**
* 查询【请填写功能名称】 * 查询【请填写功能名称】
* *
@@ -127,67 +138,74 @@ public class NotificationsServiceImpl implements INotificationsService
NotificationRecords query = new NotificationRecords(); NotificationRecords query = new NotificationRecords();
query.setNotificationId(id); query.setNotificationId(id);
List<NotificationRecords> records = notificationRecordsMapper.selectNotificationRecordsList(query); List<NotificationRecords> records = notificationRecordsMapper.selectNotificationRecordsList(query);
List<Long> userIds = records.stream().map(NotificationRecords::getUserId).collect(Collectors.toList());
if (records.isEmpty()) if (records.isEmpty())
{ {
return AjaxResult.error("该通知没有绑定任何用户,请先绑定用户"); return AjaxResult.error("该通知没有绑定任何用户,请先绑定用户");
} }
List<String> phones = shopUserMapper.selectPhoneShopUserByUserIds(userIds);
// 当前时间 // 当前时间
Date now = DateUtils.getNowDate(); Date now = DateUtils.getNowDate();
int successCount = 0; int successCount = 0;
int failCount = 0; int failCount = 0;
sendNotificationToThirdParty(notification, phones);
query.setStatus("sent");
notificationRecordsMapper.updateNotificationRecords(query);
// 遍历所有记录,进行发布 // 遍历所有记录,进行发布
for (NotificationRecords record : records) // for (NotificationRecords record : records)
{ // {
// 如果记录已经发送过,则不再创建新记录,只更新状态 // // 如果记录已经发送过,则不再创建新记录,只更新状态
boolean sendResult = sendNotificationToThirdParty(notification, record); // boolean sendResult = sendNotificationToThirdParty(notification, record);
if ("sent".equals(record.getStatus())) // if ("sent".equals(record.getStatus()))
{ // {
// 调用第三方接口发送通知 // // 调用第三方接口发送通知
//
if (sendResult) // if (sendResult)
{ // {
successCount++; // successCount++;
} // }
else // else
{ // {
failCount++; // failCount++;
// 更新失败原因 // // 更新失败原因
record.setFailureReason("重新发送失败"); // record.setFailureReason("重新发送失败");
notificationRecordsMapper.updateNotificationRecords(record); // notificationRecordsMapper.updateNotificationRecords(record);
} // }
} // }
else // else
{ // {
// 调用第三方接口发送通知 // // 调用第三方接口发送通知
//
if (sendResult) // if (sendResult)
{ // {
// 更新记录状态为已发送 // // 更新记录状态为已发送
record.setStatus("sent"); // record.setStatus("sent");
record.setSentTime(now); // record.setSentTime(now);
record.setUpdateTime(now); // record.setUpdateTime(now);
notificationRecordsMapper.updateNotificationRecords(record); // notificationRecordsMapper.updateNotificationRecords(record);
successCount++; // successCount++;
} // }
else // else
{ // {
// 更新记录状态为发送失败 // // 更新记录状态为发送失败
record.setStatus("failed"); // record.setStatus("failed");
record.setFailureReason("发送失败"); // record.setFailureReason("发送失败");
record.setUpdateTime(now); // record.setUpdateTime(now);
notificationRecordsMapper.updateNotificationRecords(record); // notificationRecordsMapper.updateNotificationRecords(record);
failCount++; // failCount++;
} // }
} // }
} // }
// 更新通知状态为已发布 // 更新通知状态为已发布
// notification.setStatus("published"); // notification.setStatus("published");
// notification.setPublishTime(now); // notification.setPublishTime(now);
notificationsMapper.updateNotifications(notification); // notificationsMapper.updateNotifications(notification);
return AjaxResult.success("通知发布完成,成功:" + successCount + ",失败:" + failCount); return AjaxResult.success("通知发布完成,成功:" + successCount + ",失败:" + failCount);
} }
@@ -199,15 +217,29 @@ public class NotificationsServiceImpl implements INotificationsService
* @param record 通知记录 * @param record 通知记录
* @return 发送结果 * @return 发送结果
*/ */
private boolean sendNotificationToThirdParty(Notifications notification, NotificationRecords record) private boolean sendNotificationToThirdParty(Notifications notification, List<String> phones)
{ {
try try
{ {
// TODO: 实现调用第三方接口发送通知的逻辑 // TODO: 实现调用第三方接口发送通知的逻辑
// 这里是模拟实现,实际项目中需要根据具体的第三方接口进行调用 // 这里是模拟实现,实际项目中需要根据具体的第三方接口进行调用
log.info("发送通知通知ID={}, 用户ID={}, 标题={}, 内容={}", // log.info("发送通知通知ID={}, 用户ID={}, 标题={}, 内容={}",
notification.getId(), record.getUserId(), notification.getTitle(), notification.getContent()); // 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<String> 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; return true;
} }

View File

@@ -110,4 +110,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
) )
</foreach> </foreach>
</insert> </insert>
<update id="updateStatusNotificationRecords">
</update>
</mapper> </mapper>

View File

@@ -219,4 +219,11 @@
WHERE su.user_id = #{userId} AND mi.label IS NOT NULL AND mi.label != '' WHERE su.user_id = #{userId} AND mi.label IS NOT NULL AND mi.label != ''
GROUP BY mi.label GROUP BY mi.label
</select> </select>
<select id="selectPhoneShopUserByUserIds" resultType="java.lang.String">
SELECT phone from shop_user where user_id in
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
</select>
</mapper> </mapper>