Commit f778297b7396b1dd6f2d14a3ac4234bf4b8c7ca0

Authored by chenyonghong
1 parent c85971ed

fix -> 发送IM消息 -> 补充消息业务类型从redis获取失败从db查询

fw-hermes-service/src/main/java/cn/fw/hermes/service/biz/MessageBizService.java
@@ -2,6 +2,7 @@ package cn.fw.hermes.service.biz; @@ -2,6 +2,7 @@ package cn.fw.hermes.service.biz;
2 2
3 import cn.fw.common.util.MessageCodeBuilder; 3 import cn.fw.common.util.MessageCodeBuilder;
4 import cn.fw.hermes.common.constant.ActionStatusEnum; 4 import cn.fw.hermes.common.constant.ActionStatusEnum;
  5 +import cn.fw.hermes.common.constant.Constant;
5 import cn.fw.hermes.common.constant.RedisKey; 6 import cn.fw.hermes.common.constant.RedisKey;
6 import cn.fw.hermes.common.constant.RedisKeyCache; 7 import cn.fw.hermes.common.constant.RedisKeyCache;
7 import cn.fw.hermes.common.utils.StringUtils; 8 import cn.fw.hermes.common.utils.StringUtils;
@@ -9,6 +10,7 @@ import cn.fw.hermes.common.utils.UUIDUtils; @@ -9,6 +10,7 @@ import cn.fw.hermes.common.utils.UUIDUtils;
9 import cn.fw.hermes.common.utils.im.RedisKeyUtil; 10 import cn.fw.hermes.common.utils.im.RedisKeyUtil;
10 import cn.fw.hermes.common.utils.im.TXCloudUtils; 11 import cn.fw.hermes.common.utils.im.TXCloudUtils;
11 import cn.fw.hermes.domain.db.Account; 12 import cn.fw.hermes.domain.db.Account;
  13 +import cn.fw.hermes.domain.db.MessageBusinessType;
12 import cn.fw.hermes.domain.db.SysMsg; 14 import cn.fw.hermes.domain.db.SysMsg;
13 import cn.fw.hermes.domain.dto.AdminConfigDto; 15 import cn.fw.hermes.domain.dto.AdminConfigDto;
14 import cn.fw.hermes.domain.dto.MsgBodyDto; 16 import cn.fw.hermes.domain.dto.MsgBodyDto;
@@ -78,7 +80,7 @@ public class MessageBizService { @@ -78,7 +80,7 @@ public class MessageBizService {
78 msgSubject.setCondition(condition); 80 msgSubject.setCondition(condition);
79 msgSubject.setMsgBodyDto(msgBodyDto); 81 msgSubject.setMsgBodyDto(msgBodyDto);
80 redisTemplate.opsForList() 82 redisTemplate.opsForList()
81 - .leftPush(RedisKey.ASYN_MESSAGE_TARGET.getCode(), JSON.toJSONString(msgSubject)); 83 + .leftPush(RedisKey.ASYN_MESSAGE_TARGET.getCode(), JSON.toJSONString(msgSubject));
82 return messageId; 84 return messageId;
83 } 85 }
84 86
@@ -113,23 +115,23 @@ public class MessageBizService { @@ -113,23 +115,23 @@ public class MessageBizService {
113 if (msgPara.isUseOfflineMsg()) { 115 if (msgPara.isUseOfflineMsg()) {
114 OfflinePushInfo pushInfo = msgPara.getOfflinePushInfo(); 116 OfflinePushInfo pushInfo = msgPara.getOfflinePushInfo();
115 info = TXCloudUtils.getOfflinePushInfo(pushInfo.getTitle(), pushInfo.getDesc(), 117 info = TXCloudUtils.getOfflinePushInfo(pushInfo.getTitle(), pushInfo.getDesc(),
116 - JSON.toJSONString(pushInfo.getExt())); 118 + JSON.toJSONString(pushInfo.getExt()));
117 } 119 }
118 //获取管理员签名 120 //获取管理员签名
119 String adminKey = RedisKeyUtil.getAdminkey(adminConfigDto.getAdminId()); 121 String adminKey = RedisKeyUtil.getAdminkey(adminConfigDto.getAdminId());
120 String adminSig = accountBizService.getSig(adminConfigDto.getAdminId(), 360 * 24 * 10, adminKey) 122 String adminSig = accountBizService.getSig(adminConfigDto.getAdminId(), 360 * 24 * 10, adminKey)
121 - .getSig(); 123 + .getSig();
122 //调用IM发送消息接口发送消息 124 //调用IM发送消息接口发送消息
123 JSONObject jsonObject = TXCloudUtils 125 JSONObject jsonObject = TXCloudUtils
124 - .openimSendMsg(msgBodyDto.getFromAccount(), msgBodyDto.getToAccount(),  
125 - msgBodyDto.getMsgBody(), adminSig,  
126 - adminConfigDto.getAdminId(), adminConfigDto.getAppId(), msgPara.getMsgLifeTime(),  
127 - msgPara.getMsgTypeEnum(), info); 126 + .openimSendMsg(msgBodyDto.getFromAccount(), msgBodyDto.getToAccount(),
  127 + msgBodyDto.getMsgBody(), adminSig,
  128 + adminConfigDto.getAdminId(), adminConfigDto.getAppId(), msgPara.getMsgLifeTime(),
  129 + msgPara.getMsgTypeEnum(), info);
128 //是否发送成功 130 //是否发送成功
129 BV.isTrue(jsonObject.get("ActionStatus").equals(ActionStatusEnum.OK.getCode()) && 0 == jsonObject.getInteger("ErrorCode"), 131 BV.isTrue(jsonObject.get("ActionStatus").equals(ActionStatusEnum.OK.getCode()) && 0 == jsonObject.getInteger("ErrorCode"),
130 - MessageCodeBuilder  
131 - .messageCode(jsonObject.getInteger("ErrorCode"), "调用IM消息发送接口失败"),  
132 - "调用IM消息发送接口失败"); 132 + MessageCodeBuilder
  133 + .messageCode(jsonObject.getInteger("ErrorCode"), "调用IM消息发送接口失败"),
  134 + "调用IM消息发送接口失败");
133 SysMsg sysMsg = msgBodyDto.getSysMsg(); 135 SysMsg sysMsg = msgBodyDto.getSysMsg();
134 sysMsg.setMessageId(msgPara.getMessageId()); 136 sysMsg.setMessageId(msgPara.getMessageId());
135 sysMsg.setMsgStatusDesc(ActionStatusEnum.OK.getMsg()); 137 sysMsg.setMsgStatusDesc(ActionStatusEnum.OK.getMsg());
@@ -155,7 +157,7 @@ public class MessageBizService { @@ -155,7 +157,7 @@ public class MessageBizService {
155 } catch (Exception ex) { 157 } catch (Exception ex) {
156 //放入redis队列 158 //放入redis队列
157 redisTemplate.opsForList() 159 redisTemplate.opsForList()
158 - .leftPush(RedisKey.SAVE_MESSAGE_FAILED.getCode(), JSON.toJSONString(sysMsg)); 160 + .leftPush(RedisKey.SAVE_MESSAGE_FAILED.getCode(), JSON.toJSONString(sysMsg));
159 } 161 }
160 return result; 162 return result;
161 } 163 }
@@ -172,18 +174,18 @@ public class MessageBizService { @@ -172,18 +174,18 @@ public class MessageBizService {
172 BV.isTrue(msgBodyDto.getMsgBody() != null, "消息内容不合法"); 174 BV.isTrue(msgBodyDto.getMsgBody() != null, "消息内容不合法");
173 //如果消息是自定义消息,则检查对应的消息业务类型是否存在 175 //如果消息是自定义消息,则检查对应的消息业务类型是否存在
174 if (msgBodyDto.getMsgTypeEnum().getCode().equals(MsgTypeEnum.CUSTOM.getCode())) { 176 if (msgBodyDto.getMsgTypeEnum().getCode().equals(MsgTypeEnum.CUSTOM.getCode())) {
175 - BV.notNull(msgPara.getCustomContent().getExt(),"扩展字段参数不能为空"); 177 + BV.notNull(msgPara.getCustomContent().getExt(), "扩展字段参数不能为空");
176 this.checkExt(msgPara); 178 this.checkExt(msgPara);
177 } 179 }
178 //获取发送方用户识别码 180 //获取发送方用户识别码
179 String fromAccount = (String) redisTemplate.opsForHash() 181 String fromAccount = (String) redisTemplate.opsForHash()
180 - .get(RedisKeyCache.SYS_KEY, msgPara.getBusinessType() == null ?  
181 - BusinessType.SYS_NOTICE.getCode().toString()  
182 - : msgPara.getBusinessType().getCode().toString()); 182 + .get(RedisKeyCache.SYS_KEY, msgPara.getBusinessType() == null ?
  183 + BusinessType.SYS_NOTICE.getCode().toString()
  184 + : msgPara.getBusinessType().getCode().toString());
183 BV.isTrue(!StringUtils.isEmpty(fromAccount), "发送方用户不存在"); 185 BV.isTrue(!StringUtils.isEmpty(fromAccount), "发送方用户不存在");
184 //获取接收方用户识别码 186 //获取接收方用户识别码
185 Optional<Account> optional = accountService.queryAccountByUserId(msgPara.getUserId(), 187 Optional<Account> optional = accountService.queryAccountByUserId(msgPara.getUserId(),
186 - msgPara.getIsStaff() ? UserTypeEnum.B.getValue() : UserTypeEnum.C.getValue()); 188 + msgPara.getIsStaff() ? UserTypeEnum.B.getValue() : UserTypeEnum.C.getValue());
187 String toAccount; 189 String toAccount;
188 //如果消息接收人未注册,将该用户注册 190 //如果消息接收人未注册,将该用户注册
189 if (!optional.isPresent()) { 191 if (!optional.isPresent()) {
@@ -211,7 +213,12 @@ public class MessageBizService { @@ -211,7 +213,12 @@ public class MessageBizService {
211 Map<String, Object> extMap = msgPara.getCustomContent().getExt(); 213 Map<String, Object> extMap = msgPara.getCustomContent().getExt();
212 BV.notNull(extMap.get("type"), "消息业务类型不能为空"); 214 BV.notNull(extMap.get("type"), "消息业务类型不能为空");
213 String type = extMap.get("type").toString(); 215 String type = extMap.get("type").toString();
214 - BV.isTrue(messageBusinessTypeBizService.getTypeFromRedis(type), () -> "消息业务类型不正确"); 216 + Boolean flag = messageBusinessTypeBizService.getTypeFromRedis(type);
  217 + if (!flag) {
  218 + Optional<MessageBusinessType> optional = messageBusinessTypeBizService.getMessageBusinessType(type);
  219 + BV.isTrue(optional.isPresent(), () -> "消息业务类型不正确");
  220 + redisTemplate.opsForHash().put(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), optional.get().getType(),"1");
  221 + }
215 } 222 }
216 223
217 } 224 }
fw-hermes-service/src/main/java/cn/fw/hermes/service/biz/MessageBusinessTypeBizService.java
@@ -7,8 +7,10 @@ import cn.fw.hermes.service.data.MessageBusinessTypeService; @@ -7,8 +7,10 @@ import cn.fw.hermes.service.data.MessageBusinessTypeService;
7 7
8 import java.util.List; 8 import java.util.List;
9 import java.util.Map; 9 import java.util.Map;
  10 +import java.util.Optional;
10 import java.util.stream.Collectors; 11 import java.util.stream.Collectors;
11 12
  13 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
12 import lombok.RequiredArgsConstructor; 14 import lombok.RequiredArgsConstructor;
13 import lombok.extern.slf4j.Slf4j; 15 import lombok.extern.slf4j.Slf4j;
14 import org.springframework.data.redis.core.StringRedisTemplate; 16 import org.springframework.data.redis.core.StringRedisTemplate;
@@ -62,9 +64,9 @@ public class MessageBusinessTypeBizService { @@ -62,9 +64,9 @@ public class MessageBusinessTypeBizService {
62 */ 64 */
63 public Boolean cacheAllTypeToRedis() { 65 public Boolean cacheAllTypeToRedis() {
64 List<MessageBusinessType> typeList = messageBusinessTypeService 66 List<MessageBusinessType> typeList = messageBusinessTypeService
65 - .queryAllMessageType(); 67 + .queryAllMessageType();
66 Map<String, String> map = typeList.stream() 68 Map<String, String> map = typeList.stream()
67 - .collect(Collectors.toMap(MessageBusinessType::getType, (r) -> "1")); 69 + .collect(Collectors.toMap(MessageBusinessType::getType, (r) -> "1"));
68 redisTemplate.opsForHash().putAll(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), map); 70 redisTemplate.opsForHash().putAll(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), map);
69 log.info("缓存消息业务类型到redis操作完成"); 71 log.info("缓存消息业务类型到redis操作完成");
70 return true; 72 return true;
@@ -100,9 +102,20 @@ public class MessageBusinessTypeBizService { @@ -100,9 +102,20 @@ public class MessageBusinessTypeBizService {
100 */ 102 */
101 public Boolean getTypeFromRedis(String key) { 103 public Boolean getTypeFromRedis(String key) {
102 String messageType = (String) redisTemplate.opsForHash() 104 String messageType = (String) redisTemplate.opsForHash()
103 - .get(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), key); 105 + .get(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), key);
104 return !StringUtils.isEmpty(messageType); 106 return !StringUtils.isEmpty(messageType);
105 } 107 }
106 108
  109 + /**
  110 + * 通过type查询业务类型
  111 + *
  112 + * @param type 类型
  113 + * @return 业务类型
  114 + */
  115 + public Optional<MessageBusinessType> getMessageBusinessType(String type) {
  116 + return Optional.ofNullable(messageBusinessTypeService.getOne(
  117 + Wrappers.<MessageBusinessType>lambdaQuery().eq(MessageBusinessType::getType, type)));
  118 + }
  119 +
107 120
108 } 121 }
109 \ No newline at end of file 122 \ No newline at end of file
fw-hermes-service/src/main/java/cn/fw/hermes/service/mq/MessageProducer.java
@@ -20,7 +20,7 @@ public class MessageProducer { @@ -20,7 +20,7 @@ public class MessageProducer {
20 final String msg = "消息发送mq"; 20 final String msg = "消息发送mq";
21 try { 21 try {
22 log.info("{} : body [{}]", msg, messageResult); 22 log.info("{} : body [{}]", msg, messageResult);
23 - rocketMQTemplate.syncSend(MessageResult.TOPIC, messageResult); 23 + rocketMQTemplate.syncSend(MessageResult.TOPIC + ":*", messageResult);
24 } catch (Exception e) { 24 } catch (Exception e) {
25 log.error("{} 消息结果发送失败 messageId[{}]", msg, messageResult.getMessageId()); 25 log.error("{} 消息结果发送失败 messageId[{}]", msg, messageResult.getMessageId());
26 } 26 }