Browse Source

# 消息推送即时推送接口API

# 消息由放在站点下改为机构下
zhongwei
haungxing 2 years ago
parent
commit
3beb5e507e
  1. 8
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/MessageConstants.java
  2. 25
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/MessagePushRecordDto.java
  3. 20
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/MessagePushRecordEntity.java
  4. 1
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/config/MessageTemplateEntity.java
  5. 3
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/IMessageClient.java
  6. 3
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/MessageClientFallback.java
  7. 17
      hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java
  8. 2
      hzims-service/message/src/main/java/com/hnac/hzims/message/schedule/MessagePushSchedule.java
  9. 19
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MessagePushRecordServiceImpl.java
  10. 17
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/WebsocketServiceImpl.java
  11. 71
      hzims-service/message/src/main/resources/db/1.0.0.sql
  12. 4
      hzims-service/message/src/main/resources/db/1.0.1.sql

8
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/MessageConstants.java

@ -73,4 +73,12 @@ public class MessageConstants {
public final static Integer CONFIRM = 3;
/**====================================推送状态====================================**/
/**====================================推送类型====================================**/
/**即时推送**/
public final static String IMMEDIATELY = "0";
/**计划推送**/
public final static String PLAN = "1";
/**====================================推送类型====================================**/
}

25
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/MessagePushRecordDto.java

@ -0,0 +1,25 @@
package com.hnac.hzims.message.dto;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
/**
* @ClassName MessagePushRecordDto
* @description:
* @author: hx
* @create: 2023-04-12 11:11
* @Version 4.0
**/
@Data
public class MessagePushRecordDto extends MessagePushRecordEntity implements Serializable {
@ApiModelProperty("多种消息类型推送使用字段")
@NotNull
private List<String> types;
}

20
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/MessagePushRecordEntity.java

@ -14,6 +14,7 @@ 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.time.LocalDate;
import java.time.LocalDateTime;
@ -23,12 +24,21 @@ import java.time.LocalDateTime;
@EqualsAndHashCode
public class MessagePushRecordEntity extends MessageTemplateEntity {
@ApiModelProperty("站点编码")
@ApiModelProperty("机构ID")
@QueryField(condition = SqlCondition.EQUAL)
private String stationCode;
@NotNull
private Long deptId;
@ApiModelProperty("站点名称")
private String stationName;
@ApiModelProperty("机构名称")
private String deptName;
@ApiModelProperty("推送类型,0:即时推送,1:计划推送")
@QueryField(condition = SqlCondition.EQUAL)
@NotNull
private String pushType;
@ApiModelProperty("消息ID")
private Long messageId;
@ApiModelProperty("业务任务ID")
@QueryField(condition = SqlCondition.EQUAL)
@ -44,9 +54,11 @@ public class MessagePushRecordEntity extends MessageTemplateEntity {
@ApiModelProperty(value = "推送人")
@QueryField(condition = SqlCondition.LIKE)
@NotNull
private String pusher;
@ApiModelProperty(value = "推送账号:手机号,邮箱")
@NotNull
private String account;
@ApiModelProperty(value = "推送人名称")

1
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/config/MessageTemplateEntity.java

@ -20,7 +20,6 @@ import java.io.Serializable;
public class MessageTemplateEntity extends TenantEntity implements Serializable {
@ApiModelProperty(value = "消息类型")
@NotNull(message = "消息类型不能为空")
@QueryField(condition = SqlCondition.EQUAL)
private String type;

3
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/IMessageClient.java

@ -1,6 +1,7 @@
package com.hnac.hzims.message.fegin;
import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.dto.MessagePushRecordDto;
import com.hnac.hzims.message.dto.PushDto;
import com.hnac.hzims.message.dto.SmsPushDto;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
@ -29,5 +30,5 @@ public interface IMessageClient {
R sendSmsMessage(SmsPushDto pushDto);
@PostMapping(SEND_MESSAGE)
R<Boolean> sendMessage(@RequestBody MessagePushRecordEntity request);
R<Boolean> sendMessage(@RequestBody MessagePushRecordDto request);
}

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

@ -1,5 +1,6 @@
package com.hnac.hzims.message.fegin;
import com.hnac.hzims.message.dto.MessagePushRecordDto;
import com.hnac.hzims.message.dto.PushDto;
import com.hnac.hzims.message.dto.SmsPushDto;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
@ -18,7 +19,7 @@ public class MessageClientFallback implements IMessageClient {
}
@Override
public R<Boolean> sendMessage(MessagePushRecordEntity request) {
public R<Boolean> sendMessage(MessagePushRecordDto request) {
return R.fail("推送失败");
}
}

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

@ -1,7 +1,9 @@
package com.hnac.hzims.message.fegin;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.dto.MessagePushRecordDto;
import com.hnac.hzims.message.dto.PushDto;
import com.hnac.hzims.message.dto.SmsPushDto;
import com.hnac.hzims.message.entity.MessagePushRecordEntity;
@ -12,11 +14,14 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* @author hx
*/
@ -36,8 +41,16 @@ public class MessageClient extends BladeController implements IMessageClient{
@Override
@PostMapping(SEND_MESSAGE)
public R<Boolean> sendMessage(@RequestBody MessagePushRecordEntity request) {
return R.status(recordService.saveAndSend(request));
public R<Boolean> sendMessage(@RequestBody @Valid MessagePushRecordDto request) {
long messageId = IdWorker.getId();
// 可能存在多种推送类型
request.getTypes().forEach(type -> {
MessagePushRecordEntity entity = BeanUtil.copy(request,MessagePushRecordEntity.class);
entity.setType(type);
entity.setMessageId(messageId);
recordService.saveAndSend(entity);
});
return R.success("推送成功!");
}
}

2
hzims-service/message/src/main/java/com/hnac/hzims/message/schedule/MessagePushSchedule.java

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hnac.hzims.common.utils.DateUtil;
import com.hnac.hzims.message.MessageConstants;
@ -81,6 +82,7 @@ public class MessagePushSchedule {
MessagePushRecordEntity request = records.get(0);
// 完善推送内容
request.setContent(this.getPushContent(type,records));
request.setMessageId(IdWorker.getId());
Boolean flag = service.send(request);
if(!flag) {
return;

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

@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hnac.hzims.common.utils.CacheUtil;
import com.hnac.hzims.message.MessageConstants;
@ -31,6 +32,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
@ -85,13 +87,22 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean saveAndSend(MessagePushRecordEntity request) {
if(Func.isEmpty(request.getPlanTime())) {
request.setPlanTime(LocalDateTime.now());
}
LocalDateTime now = LocalDateTime.now();
if(MessageConstants.IMMEDIATELY.equals(request.getPushType())) {
request.setPlanTime(now);
request.setPushTime(now);
// 立即推送消息
if(this.save(request) && Func.isNotEmpty(request.getId())) {
return this.send(request.getId());
}
return false;
throw new ServiceException("保存消息失败!");
}
else if(MessageConstants.PLAN.equals(request.getPushType())) {
return this.save(request);
}
else {
throw new ServiceException("推送类型只能为即时推送或计划推送");
}
}
/**

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

@ -87,6 +87,23 @@ public class WebsocketServiceImpl implements IMessageService {
@Override
public Boolean send(MessagePushRecordEntity request) {
String userId = request.getAccount();
request.setPushTime(LocalDateTime.now());
WebSocketSession webSocketSession = SessionManager.SESSION_POOL.get(userId);
TextMessage message = new TextMessage(JSON.toJSONString(request));
if(ObjectUtil.isNotEmpty(webSocketSession)){
try {
webSocketSession.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
else {
//存在未上线的用户时 需将消息缓存起来 在用户上线时推送
String key = String.format("%s:%s",MessageConstants.WS_MESSAGE_KEY,userId);
redisTemplate.opsForList().leftPush(key,message);
redisTemplate.expire(key,24, TimeUnit.HOURS);
}
return null;
}
}

71
hzims-service/message/src/main/resources/db/1.0.0.sql

@ -0,0 +1,71 @@
-- 创建消息模板表结构
CREATE TABLE IF NOT EXISTS `hzims_message_template` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '消息类型',
`TYPE` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键ID',
`BUSINESS_KEY` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务关键字',
`BUSINESS_CLASSIFY` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务分类',
`SUBJECT` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主题',
`CONCENTRATED_DURATION` mediumint(9) NULL DEFAULT NULL COMMENT '集中推送时长',
`CONCENTRATED_TIME_UNIT` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '集中推送单位',
`ADVANCE_DURATION` mediumint(9) NULL DEFAULT NULL COMMENT '提前推送时长',
`ADVANCE_TIME_UNIT` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '提前推送单位',
`TENANT_ID` varchar(12) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '租户ID',
`CREATE_DEPT` bigint(20) NULL DEFAULT NULL COMMENT '创建单位',
`CREATE_USER` bigint(20) NOT NULL COMMENT '创建人',
`CREATE_TIME` datetime NOT NULL COMMENT '创建时间',
`UPDATE_USER` bigint(20) NULL DEFAULT NULL COMMENT '修改人',
`UPDATE_TIME` datetime NULL DEFAULT NULL COMMENT '修改时间',
`STATUS` tinyint(4) NULL DEFAULT NULL COMMENT '业务状态',
`IS_DELETED` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否删除 0-正常 1-删除',
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1643920980174729219 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '消息推送模板' ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
-- 创建消息记录表结构
CREATE TABLE IF NOT EXISTS `hzims_message_push_record` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`TASK_ID` bigint(20) NOT NULL COMMENT '消息类型',
`TEMPLATE_ID` bigint(20) NOT NULL COMMENT '消息模板ID',
`PUSHER` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '推送人',
`PUSHER_NAME` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '推送人名称',
`CONTENT` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '内容',
`PLAN_TIME` datetime NULL DEFAULT NULL COMMENT '计划推送时间',
`PUSH_TIME` datetime NULL DEFAULT NULL COMMENT '实际推送时间',
`TYPE` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '消息类型',
`BUSINESS_KEY` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务关键字',
`BUSINESS_CLASSIFY` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '业务分类。系统通知:system,事务消息:business,日常提醒:dailyRemind,巡检消息:inspect',
`SUBJECT` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主题',
`CONCENTRATED_DURATION` mediumint(9) NULL DEFAULT NULL COMMENT '集中推送时长',
`CONCENTRATED_TIME_UNIT` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '集中推送单位',
`ADVANCE_DURATION` mediumint(9) NULL DEFAULT NULL COMMENT '提前推送时长',
`ADVANCE_TIME_UNIT` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '提前推送单位',
`TENANT_ID` varchar(12) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '租户ID',
`CREATE_DEPT` bigint(20) NULL DEFAULT NULL COMMENT '创建单位',
`CREATE_USER` bigint(20) NOT NULL COMMENT '创建人',
`CREATE_TIME` datetime NOT NULL COMMENT '创建时间',
`UPDATE_USER` bigint(20) NULL DEFAULT NULL COMMENT '修改人',
`UPDATE_TIME` datetime NULL DEFAULT NULL COMMENT '修改时间',
`STATUS` tinyint(4) NULL DEFAULT 0 COMMENT '业务状态(0 未推送,1 推送成功,2 推送失败,3 已确认)',
`IS_DELETED` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否删除 0-正常 1-删除',
`RESPOND_TIME` datetime NULL DEFAULT NULL COMMENT '响应时间',
`RESPOND_REMARK` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '响应说明',
`RESOURCE_CODE` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '资源编码',
`ACCOUNT` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '推送账号:手机号,邮箱',
`STATION_CODE` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '站点编码',
`STATION_NAME` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '站点名称',
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1645716467911520258 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '消息推送记录' ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
-- 站点编码字段 -> 机构ID
alter table hzims_message_push_record change `STATION_CODE` `DEPT_ID` bigint(20) DEFAULT NULL comment '机构ID';
-- 站点名称字段 -> 机构名称
alter table hzims_message_push_record change `STATION_NAME` `DEPT_NAME` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL comment '机构ID';
-- 消息推送类型字典
INSERT INTO `bladex`.`blade_dict` (`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_sealed`, `is_deleted`, `dict_type`, `app_id`, `app_code`) VALUES (1645998325178130433, 0, 'pushType', '-1', '推送类型', 6, '', 0, 0, 0, -1, 'message');
INSERT INTO `bladex`.`blade_dict` (`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_sealed`, `is_deleted`, `dict_type`, `app_id`, `app_code`) VALUES (1645998429171703809, 1645998325178130433, 'pushType', '0', '即时推送', 1, '', 0, 0, 0, -1, 'message');
INSERT INTO `bladex`.`blade_dict` (`id`, `parent_id`, `code`, `dict_key`, `dict_value`, `sort`, `remark`, `is_sealed`, `is_deleted`, `dict_type`, `app_id`, `app_code`) VALUES (1645998773519867906, 1645998325178130433, 'pushType', '1', '计划推送', 2, '', 0, 0, 0, -1, 'message');

4
hzims-service/message/src/main/resources/db/1.0.1.sql

@ -0,0 +1,4 @@
-- 添加消息ID
alter table hzims_message_push_record add COLUMN `MESSAGE_ID` bigint(20) DEFAULT NULL comment '消息ID';
-- 添加消息推送类型
alter table hzims_message_push_record add COLUMN `PUSH_TYPE` VARCHAR(2) DEFAULT NULL comment '推送类型,0:即时推送,1:计划推送';
Loading…
Cancel
Save