Browse Source

# 消息中心即时推送逻辑修改

zhongwei
haungxing 9 months ago
parent
commit
7b666a529a
  1. 3
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/BusinessMessageDTO.java
  2. 69
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MessagePushRecordServiceImpl.java
  3. 20
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java

3
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/BusinessMessageDTO.java

@ -66,4 +66,7 @@ public class BusinessMessageDTO implements Serializable {
@ApiModelProperty(value = "租户ID") @ApiModelProperty(value = "租户ID")
private String tenantId; private String tenantId;
@ApiModelProperty(value = "APP推送是否紧急")
private boolean isEmergent = false;
} }

69
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MessagePushRecordServiceImpl.java

@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.hnac.hzims.common.constant.CommonConstant;
import com.hnac.hzims.common.utils.CacheUtil; import com.hnac.hzims.common.utils.CacheUtil;
import com.hnac.hzims.message.MessageConstants; import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.config.MessageFactory; import com.hnac.hzims.message.config.MessageFactory;
@ -24,6 +25,8 @@ import com.hnac.hzims.message.service.IMessagePushRecordService;
import com.hnac.hzims.message.service.IMessageService; import com.hnac.hzims.message.service.IMessageService;
import com.hnac.hzims.message.vo.UnreadMessageVO; import com.hnac.hzims.message.vo.UnreadMessageVO;
import com.hnac.hzims.message.vo.msgpushrecord.*; import com.hnac.hzims.message.vo.msgpushrecord.*;
import com.hnac.hzinfo.core.push.model.PushResponse;
import com.xxl.job.core.log.XxlJobLogger;
import io.minio.messages.Item; import io.minio.messages.Item;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -324,11 +327,32 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
/** /**
* 推送APP即时消息 * 推送APP即时消息
* @param record 消息记录 * @param record 消息记录
* @param isEmergent 是否紧急 若紧急则直接推送 若不紧急则存入redis进行消费
* @return 推送结果 * @return 推送结果
*/ */
private Boolean sendAppImmediatelyMsg(MessagePushRecordEntity record) { private Boolean sendAppImmediatelyMsg(MessagePushRecordEntity record, boolean isEmergent) {
if(this.saveOrUpdate(record)) { if(this.saveOrUpdate(record)) {
PushMessageServiceImpl pushService = SpringUtil.getBean(PushMessageServiceImpl.class); PushMessageServiceImpl pushService = SpringUtil.getBean(PushMessageServiceImpl.class);
if(isEmergent) {
ArrayList<String> pushers = Lists.newArrayList(record.getPusher().toString());
R<PushResponse> androidPush = pushService.sendAndroidMsg(record.getSubject(), record.getContent(), pushers, CommonConstant.TENANT_ID);
R<PushResponse> iosPush = pushService.sendIOSMsg(record.getSubject(), record.getContent(), pushers, CommonConstant.TENANT_ID);
String faultResult = "";
if(!androidPush.isSuccess()) {
record.setStatus(MessageConstants.PUSH_FAILED);
faultResult += "ANDROID消息推送失败,推送结果为:"+androidPush.getMsg()+";";
}
if(!iosPush.isSuccess()) {
record.setStatus(MessageConstants.PUSH_FAILED);
faultResult += "IOS消息推送失败,推送结果为:"+androidPush.getMsg()+";";
}
else {
record.setStatus(MessageConstants.PUSH_SUCCESS);
}
record.setFaultResult(faultResult);
this.updateById(record);
}
else {
if(pushService.send(record)) { if(pushService.send(record)) {
record.setStatus(MessageConstants.PUSH_SUCCESS); record.setStatus(MessageConstants.PUSH_SUCCESS);
record.setPushTime(LocalDateTime.now()); record.setPushTime(LocalDateTime.now());
@ -336,6 +360,7 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
else { else {
record.setStatus(MessageConstants.PUSH_FAILED); record.setStatus(MessageConstants.PUSH_FAILED);
} }
}
this.updateById(record); this.updateById(record);
} }
return true; return true;
@ -364,7 +389,7 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
}); });
switch(record.getType()) { switch(record.getType()) {
case MessageConstants.APP_PUSH: case MessageConstants.APP_PUSH:
return this.sendAppImmediatelyMsg(record); return this.sendAppImmediatelyMsg(record,false);
case MessageConstants.WX_PUSH: case MessageConstants.WX_PUSH:
break; break;
case MessageConstants.WS_PUSH: case MessageConstants.WS_PUSH:
@ -380,29 +405,14 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
@Override @Override
public Boolean sendAppAndWsMsgByUsers(BusinessMessageDTO request) { public Boolean sendAppAndWsMsgByUsers(BusinessMessageDTO request) {
// 保存消息记录 // 保存消息记录
List<MessagePushRecordEntity> pushRecords = Func.toLongList(request.getUserIds()).stream().flatMap(userId -> { List<MessagePushRecordEntity> pushRecords = this.convert(request);
long messageId = IdWorker.getId();
return Lists.newArrayList(MessageConstants.APP_PUSH, MessageConstants.WS_PUSH).stream().map(messageType -> {
MessagePushRecordEntity record = BeanUtil.copy(request, MessagePushRecordEntity.class);
record.setDeptName(Func.isNotEmpty(record.getDeptName()) ? record.getDeptName() : this.getDeptNameById(record.getDeptId()));
record.setMessageId(messageId);
record.setPusher(userId.toString());
record.setPusherName(Optional.ofNullable(UserCache.getUser(userId)).map(User::getName).orElse(null));
record.setPushType(MessageConstants.IMMEDIATELY);
record.setAccount(userId.toString());
record.setPlanTime(LocalDateTime.now());
record.setType(messageType);
record.setCreateDept(record.getDeptId());
record.setStatus(MessageConstants.NOT_PUSH);
return record;
});
}).collect(Collectors.toList());
if(this.saveBatch(pushRecords)) { if(this.saveBatch(pushRecords)) {
// 推送消息 // 推送消息
WebsocketServiceImpl wsMessageService = SpringUtil.getBean(WebsocketServiceImpl.class); WebsocketServiceImpl wsMessageService = SpringUtil.getBean(WebsocketServiceImpl.class);
pushRecords.forEach(record -> { pushRecords.forEach(record -> {
if(MessageConstants.APP_PUSH.equals(record.getType())) { if(MessageConstants.APP_PUSH.equals(record.getType())) {
// APP消息存入redis中进行消费 // APP消息存入redis中进行消费
this.sendAppImmediatelyMsg(record,request.isEmergent());
redisTemplate.opsForList().leftPush(appPushRedisKey.concat(":").concat(record.getPusher()), record); redisTemplate.opsForList().leftPush(appPushRedisKey.concat(":").concat(record.getPusher()), record);
} else if (MessageConstants.WS_PUSH.equals(record.getType())) { } else if (MessageConstants.WS_PUSH.equals(record.getType())) {
// WEB消息调用接口直接进行推送 // WEB消息调用接口直接进行推送
@ -415,13 +425,32 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
} }
this.updateById(record); this.updateById(record);
} }
}); });
return true; return true;
} }
return false; return false;
} }
private List<MessagePushRecordEntity> convert(BusinessMessageDTO request) {
return Func.toLongList(request.getUserIds()).stream().flatMap(userId -> {
long messageId = IdWorker.getId();
return Lists.newArrayList(MessageConstants.APP_PUSH, MessageConstants.WS_PUSH).stream().map(messageType -> {
MessagePushRecordEntity record = BeanUtil.copy(request, MessagePushRecordEntity.class);
record.setDeptName(Func.isNotEmpty(record.getDeptName()) ? record.getDeptName() : this.getDeptNameById(record.getDeptId()));
record.setMessageId(messageId);
record.setPusher(userId.toString());
record.setPusherName(Optional.ofNullable(UserCache.getUser(userId)).map(User::getName).orElse(null));
record.setPushType(MessageConstants.IMMEDIATELY);
record.setAccount(userId.toString());
record.setPlanTime(LocalDateTime.now());
record.setType(messageType);
record.setCreateDept(record.getDeptId());
record.setStatus(MessageConstants.NOT_PUSH);
return record;
});
}).collect(Collectors.toList());
}
/** /**
* 根据机构ID获取机构名称 * 根据机构ID获取机构名称
* @param deptId 机构ID * @param deptId 机构ID

20
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java

@ -102,25 +102,7 @@ public class PushMessageServiceImpl implements IMessageService {
return Func.isNotEmpty(pushResult); return Func.isNotEmpty(pushResult);
} }
/**
* 发送APP消息(多人)
* @param subject 消息主题
* @param content 消息内容
* @param userIds 用户ID列表
* @param tenantId 租户ID
* @return
*/
// public boolean sendByUsers(String subject, String content, List<String> userIds,String tenantId) throws Exception {
// // 安卓推送
// FutureTask<Boolean> androidPush = new FutureTask<>(() -> this.sendAndroidMsg(subject,content,userIds,tenantId));
// appMessagePushExecutor.submit(new Thread(androidPush,"安卓推送"));
//
// // IOS推送
// FutureTask<Boolean> iosPush = new FutureTask<>(() -> this.sendIOSMsg(subject,content,userIds,tenantId));
// appMessagePushExecutor.submit(new Thread(iosPush,"ios推送"));
//
// return androidPush.get() && iosPush.get();
// }
/** /**
* 发送APP安卓消息 * 发送APP安卓消息

Loading…
Cancel
Save