Browse Source

# 消息APP/WEB多人即时推送接口

zhongwei
haungxing 1 year ago
parent
commit
77717ce336
  1. 66
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/BusinessMessageDTO.java
  2. 10
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/MessageClientFallback.java
  3. 49
      hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java
  4. 6
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/IMessagePushRecordService.java
  5. 42
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MessagePushRecordServiceImpl.java
  6. 40
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/PushMessageServiceImpl.java
  7. 1
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/WebsocketServiceImpl.java

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

@ -0,0 +1,66 @@
package com.hnac.hzims.message.dto;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.support.QueryField;
import org.springblade.core.mp.support.SqlCondition;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* @ClassName BusinessMessageDTO
* @description:
* @author: hx
* @create: 2023-06-15 19:10
* @Version 4.0
**/
@Data
@ApiModel("事务消息")
@EqualsAndHashCode
public class BusinessMessageDTO implements Serializable {
@ApiModelProperty(value = "机构ID",required = true)
@NotNull(message = "机构ID不能为空")
private Long deptId;
@ApiModelProperty("机构名称")
private String deptName;
@ApiModelProperty(value = "业务关键字",required = true)
@NotNull(message = "业务关键字不能为空")
private String businessKey;
@ApiModelProperty(value = "业务分类。系统通知:system,事务消息:business,日常提醒:dailyRemind,巡检消息:inspect",required = true)
@NotNull(message = "业务分类不能为空")
private String businessClassify;
@ApiModelProperty("业务任务ID")
@QueryField(condition = SqlCondition.EQUAL)
private Long taskId;
@NotNull
@ApiModelProperty(value = "内容")
private String content;
@NotNull
@ApiModelProperty(value = "主题")
private String subject;
@NotNull
@ApiModelProperty(value = "推送用户")
private String userIds;
@ApiModelProperty(value = "创建用户")
private Long createUser;
@ApiModelProperty(value = "租户ID")
private String tenantId;
}

10
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/MessageClientFallback.java

@ -1,9 +1,6 @@
package com.hnac.hzims.message.fegin;
import com.hnac.hzims.message.dto.MessagePushRecordDto;
import com.hnac.hzims.message.dto.PlanMsgRecordDto;
import com.hnac.hzims.message.dto.PushDto;
import com.hnac.hzims.message.dto.SmsPushDto;
import com.hnac.hzims.message.dto.*;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
import com.hnac.hzims.message.entity.config.MessageTemplateEntity;
import org.springblade.core.tool.api.R;
@ -34,4 +31,9 @@ public class MessageClientFallback implements IMessageClient {
public R<PlanMsgRecordDto> planSendMessage(PlanMsgRecordDto request) {
return R.fail("调用失败!");
}
@Override
public R<Boolean> sendAppAndWsMsgByUsers(BusinessMessageDTO request) {
return R.fail("调用失败!");
}
}

49
hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java

@ -2,28 +2,26 @@ package com.hnac.hzims.message.fegin;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.dto.MessagePushRecordDto;
import com.hnac.hzims.message.dto.PlanMsgRecordDto;
import com.hnac.hzims.message.dto.PushDto;
import com.hnac.hzims.message.dto.SmsPushDto;
import com.hnac.hzims.message.dto.*;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
import com.hnac.hzims.message.entity.config.MessageTemplateEntity;
import com.hnac.hzims.message.service.IMessagePushRecordService;
import com.hnac.hzims.message.service.IMessageService;
import com.hnac.hzims.message.service.IMessageTemplateService;
import com.hnac.hzims.message.service.impl.PushMessageServiceImpl;
import com.hnac.hzims.message.service.impl.SmsMessageServiceImpl;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.*;
import org.springblade.system.user.cache.UserCache;
import org.springblade.system.user.entity.User;
import org.springframework.util.Assert;
@ -33,7 +31,9 @@ import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author hx
@ -115,4 +115,37 @@ public class MessageClient extends BladeController implements IMessageClient{
return R.success("推送成功!");
}
@Override
@PostMapping(SEND_APP_AND_WS_MSG)
public R<Boolean> sendAppAndWsMsgByUsers(@RequestBody BusinessMessageDTO request) {
// 保存消息记录
List<MessagePushRecordEntity> pushRecords = 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.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());
return record;
});
}).collect(Collectors.toList());
boolean saveResult = recordService.saveBatch(pushRecords);
if(saveResult) {
List<MessagePushRecordEntity> appRecords = pushRecords.stream().filter(record -> MessageConstants.APP_PUSH.equals(record.getType())).collect(Collectors.toList());
// 推送消息 - app
recordService.sendAppMsgByUsers(request,appRecords);
// 推送消息 - web
recordService.sendWsMsgByUsers(CollectionUtils.subtract(pushRecords,appRecords).stream().collect(Collectors.toList()));
return R.success("推送成功");
}
else {
return R.fail("保存消息失败");
}
}
}

6
hzims-service/message/src/main/java/com/hnac/hzims/message/service/IMessagePushRecordService.java

@ -1,11 +1,14 @@
package com.hnac.hzims.message.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hnac.hzims.message.dto.BusinessMessageDTO;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
import com.hnac.hzims.message.vo.UnreadMessageVO;
import com.hnac.hzims.message.vo.msgpushrecord.*;
import org.springblade.core.mp.base.BaseService;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
@ -84,4 +87,7 @@ public interface IMessagePushRecordService extends BaseService<MessagePushRecord
**/
List<UnreadMessageVO> getPersonalAppUnreadMessage();
Boolean sendAppMsgByUsers(BusinessMessageDTO request,List<MessagePushRecordEntity> records);
Boolean sendWsMsgByUsers(List<MessagePushRecordEntity> request);
}

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

@ -14,6 +14,7 @@ import com.google.common.collect.Lists;
import com.hnac.hzims.common.utils.CacheUtil;
import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.config.MessageFactory;
import com.hnac.hzims.message.dto.BusinessMessageDTO;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
import com.hnac.hzims.message.enums.PushStatEnum;
import com.hnac.hzims.message.mapper.MessagePushRecordMapper;
@ -30,6 +31,7 @@ import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.system.cache.DictCache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -41,10 +43,7 @@ import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -321,6 +320,41 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
}).collect(Collectors.toList());
}
@Override
public Boolean sendAppMsgByUsers(BusinessMessageDTO request,List<MessagePushRecordEntity> records) {
PushMessageServiceImpl service = SpringUtil.getBean(PushMessageServiceImpl.class);
try {
boolean sendFlag = service.sendByUsers(request.getSubject(), request.getContent(), Arrays.asList(request.getUserIds().split(",")), request.getTenantId());
if(sendFlag) {
return this.update(Wrappers.<MessagePushRecordEntity>lambdaUpdate()
.set(MessagePushRecordEntity::getPushTime, LocalDateTime.now())
.set(MessagePushRecordEntity::getStatus, MessageConstants.PUSH_SUCCESS)
.in(MessagePushRecordEntity::getId, records.stream().map(MessagePushRecordEntity::getId).collect(Collectors.toList()))
);
}
return false;
}
catch(Exception e) {
throw new ServiceException(e.getMessage());
}
}
@Override
public Boolean sendWsMsgByUsers(List<MessagePushRecordEntity> request) {
WebsocketServiceImpl service = SpringUtil.getBean(WebsocketServiceImpl.class);
request.forEach(record -> {
boolean sendFlag = service.send(record);
if(sendFlag) {
this.update(Wrappers.<MessagePushRecordEntity>lambdaUpdate()
.set(MessagePushRecordEntity::getPushTime, LocalDateTime.now())
.set(MessagePushRecordEntity::getStatus, MessageConstants.PUSH_SUCCESS)
.in(MessagePushRecordEntity::getId, record.getId())
);
}
});
return true;
}
/**
* 状态 ===> 状态名称

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

@ -29,6 +29,8 @@ import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicReference;
/**
@ -117,4 +119,42 @@ public class PushMessageServiceImpl implements IMessageService {
});
return true;
}
/**
* 发送APP消息(多人)
* @param subject 消息主题
* @param content 消息内容
* @param userIds 用户ID列表
* @param tenantId 推送成功标识
* @return
*/
public boolean sendByUsers(String subject, String content, List<String> userIds,String tenantId) throws Exception {
// 安卓推送
FutureTask<Boolean> androidPush = new FutureTask<>(() -> {
PushInfoVO pushInfoVO = new PushInfoVO();
PushPlatform pushPlatform = PushPlatform.newBuilder().addPlatformType(PlatformType.Android).build();
PushAudience pushAudience = PushAudience.newBuilder().addPushAudienceType(PushAudienceType.TAG, userIds).build();
PushInfo pushInfo = new PushInfo("ops-push-android", subject, content,"", null, pushPlatform, pushAudience);
pushInfoVO.setPushInfo(pushInfo);
pushInfoVO.setTenantId(tenantId);
R<PushResponse> pushResult = pushClient.tenantPush(pushInfoVO);
return pushResult.isSuccess();
});
new Thread(androidPush,"安卓推送").start();
// IOS推送
FutureTask<Boolean> iosPush = new FutureTask<>(() -> {
PushInfoVO pushInfoVO = new PushInfoVO();
PushPlatform pushPlatform = PushPlatform.newBuilder().addPlatformType(PlatformType.IOS).build();
PushAudience pushAudience = PushAudience.newBuilder().addPushAudienceType(PushAudienceType.TAG, userIds).build();
PushInfo pushInfo = new PushInfo("ops-push-ios", subject, content,"", null, pushPlatform, pushAudience);
pushInfoVO.setPushInfo(pushInfo);
pushInfoVO.setTenantId(tenantId);
R<PushResponse> pushResult = pushClient.tenantPush(pushInfoVO);
return pushResult.isSuccess();
});
new Thread(iosPush,"ios推送").start();
return androidPush.get() && iosPush.get();
}
}

1
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/WebsocketServiceImpl.java

@ -23,6 +23,7 @@ import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**

Loading…
Cancel
Save