|
|
@@ -22,9 +22,12 @@ import work.baiyun.chronicdiseaseapp.service.MessageService;
|
|
|
import work.baiyun.chronicdiseaseapp.service.PushService;
|
|
|
import work.baiyun.chronicdiseaseapp.service.UserBindingService;
|
|
|
import work.baiyun.chronicdiseaseapp.util.SecurityUtils;
|
|
|
+import work.baiyun.chronicdiseaseapp.mapper.PatientReminderMapper;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.po.PatientReminder;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class MessageServiceImpl implements MessageService {
|
|
|
@@ -43,12 +46,17 @@ public class MessageServiceImpl implements MessageService {
|
|
|
@Autowired
|
|
|
private PushService pushService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PatientReminderMapper patientReminderMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public String sendMessage(SendMessageRequest request) {
|
|
|
Long currentUserId = SecurityUtils.getCurrentUserId();
|
|
|
// 校验权限:医生
|
|
|
// TODO: 添加权限校验
|
|
|
|
|
|
+ StringBuilder pushDetails = new StringBuilder();
|
|
|
+
|
|
|
// 首先处理直接接收者
|
|
|
for (Long receiverId : request.getReceiverIds()) {
|
|
|
MessagePO message = new MessagePO();
|
|
|
@@ -61,9 +69,37 @@ public class MessageServiceImpl implements MessageService {
|
|
|
message.setStatus((byte) 0); // 未读
|
|
|
messageMapper.insert(message);
|
|
|
|
|
|
- // 推送通知
|
|
|
+ // 推送通知(先检查患者提醒设置)
|
|
|
if (message.getNotifySubscribe() == 1) {
|
|
|
- pushService.sendSubscribeMessage(receiverId, message);
|
|
|
+ boolean canSend = true;
|
|
|
+ try {
|
|
|
+ PatientReminder pr = patientReminderMapper.selectByPatientUserId(receiverId);
|
|
|
+ if (pr != null) {
|
|
|
+ // 如果总开关被关闭,禁止推送;如果订阅授权不可用,也禁止
|
|
|
+ if (pr.getNotificationEnabled() == null || pr.getNotificationEnabled() != 1) {
|
|
|
+ canSend = false;
|
|
|
+ }
|
|
|
+ if (pr.getSubscriptionAvailable() == null || pr.getSubscriptionAvailable() != 1) {
|
|
|
+ canSend = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("检查患者提醒设置失败,不允许继续尝试推送,receiverId={},err={} ", receiverId, e.getMessage());
|
|
|
+ canSend = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (canSend) {
|
|
|
+ Map<String, Object> pushResult = pushService.sendSubscribeMessage(receiverId, message);
|
|
|
+ UserInfo user = userInfoMapper.selectById(receiverId);
|
|
|
+ String nickname = user != null ? user.getNickname() : "未知用户";
|
|
|
+ if (pushResult.get("errcode").equals(0)) {
|
|
|
+ pushDetails.append("已成功推送给患者").append(nickname).append("\n");
|
|
|
+ } else {
|
|
|
+ pushDetails.append("未能推送给患者").append(nickname).append("(无权限)\n");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.info("推送已被患者设置禁止,跳过订阅消息发送,receiverId={}, messageId={}", receiverId, message.getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
logger.info("[MessageOperation] userId={}, action=send, messageId={}", currentUserId, message.getId());
|
|
|
@@ -82,9 +118,36 @@ public class MessageServiceImpl implements MessageService {
|
|
|
familyMessage.setStatus((byte) 0); // 未读
|
|
|
messageMapper.insert(familyMessage);
|
|
|
|
|
|
- // 推送通知给家属
|
|
|
+ // 推送通知给家属(先检查家属的提醒设置)
|
|
|
if (familyMessage.getNotifySubscribe() == 1) {
|
|
|
- pushService.sendSubscribeMessage(familyId, familyMessage);
|
|
|
+ boolean canSendFamily = true;
|
|
|
+ try {
|
|
|
+ PatientReminder prFamily = patientReminderMapper.selectByPatientUserId(familyId);
|
|
|
+ if (prFamily != null) {
|
|
|
+ if (prFamily.getNotificationEnabled() == null || prFamily.getNotificationEnabled() != 1) {
|
|
|
+ canSendFamily = false;
|
|
|
+ }
|
|
|
+ if (prFamily.getSubscriptionAvailable() == null || prFamily.getSubscriptionAvailable() != 1) {
|
|
|
+ canSendFamily = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("检查家属提醒设置失败,允许继续尝试推送,familyId={},err={}", familyId, e.getMessage());
|
|
|
+ canSendFamily = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (canSendFamily) {
|
|
|
+ Map<String, Object> pushResultFamily = pushService.sendSubscribeMessage(familyId, familyMessage);
|
|
|
+ UserInfo familyUser = userInfoMapper.selectById(familyId);
|
|
|
+ String familyNickname = familyUser != null ? familyUser.getNickname() : "未知用户";
|
|
|
+ if (pushResultFamily.get("errcode").equals(0)) {
|
|
|
+ pushDetails.append("已成功推送给患者家属").append(familyNickname).append("\n");
|
|
|
+ } else {
|
|
|
+ pushDetails.append("未能推送给患者家属").append(familyNickname).append("(无权限)\n");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.info("推送已被家属设置禁止,跳过订阅消息发送,familyId={}, messageId={}", familyId, familyMessage.getId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
logger.info("[MessageOperation] userId={}, action=send_to_family, patientId={}, familyId={}, messageId={}",
|
|
|
@@ -93,8 +156,11 @@ public class MessageServiceImpl implements MessageService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 返回最后一个消息ID
|
|
|
- return "消息发送成功"; // 或返回ID列表
|
|
|
+ String result = "消息发送成功";
|
|
|
+ if (pushDetails.length() > 0) {
|
|
|
+ result += "\n" + pushDetails.toString().trim();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -130,8 +196,27 @@ public class MessageServiceImpl implements MessageService {
|
|
|
message.setStatus((byte) 0);
|
|
|
messageMapper.insert(message);
|
|
|
|
|
|
- // 推送
|
|
|
- pushService.sendSubscribeMessage(request.getPatientId(), message);
|
|
|
+ // 推送(先检查患者提醒设置)
|
|
|
+ try {
|
|
|
+ boolean canSendAnomaly = true;
|
|
|
+ PatientReminder pr = patientReminderMapper.selectByPatientUserId(request.getPatientId());
|
|
|
+ if (pr != null) {
|
|
|
+ if (pr.getNotificationEnabled() == null || pr.getNotificationEnabled() != 1) {
|
|
|
+ canSendAnomaly = false;
|
|
|
+ }
|
|
|
+ if (pr.getSubscriptionAvailable() == null || pr.getSubscriptionAvailable() != 1) {
|
|
|
+ canSendAnomaly = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (canSendAnomaly) {
|
|
|
+ pushService.sendSubscribeMessage(request.getPatientId(), message);
|
|
|
+ } else {
|
|
|
+ logger.info("异常通知订阅消息已被患者设置禁止,跳过推送,patientId={}, messageId={}", request.getPatientId(), message.getId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.warn("检查患者提醒设置失败,异常通知仍尝试推送,patientId={},err={}", request.getPatientId(), e.getMessage());
|
|
|
+ pushService.sendSubscribeMessage(request.getPatientId(), message);
|
|
|
+ }
|
|
|
|
|
|
logger.info("[MessageOperation] action=system_anomaly_send, messageId={}", message.getId());
|
|
|
return message.getId().toString();
|