|
@@ -0,0 +1,240 @@
|
|
|
+package com.dragon.tj.portal.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.dragon.tj.portal.common.base.R;
|
|
|
+import com.dragon.tj.portal.common.convert.notice.NoticeInfoVOConvert;
|
|
|
+import com.dragon.tj.portal.common.convert.notice.NoticeReqConvert;
|
|
|
+import com.dragon.tj.portal.common.dto.notice.NoticeInfoReq;
|
|
|
+import com.dragon.tj.portal.common.dto.notice.NoticeInfoSend;
|
|
|
+import com.dragon.tj.portal.common.enums.message.MessageInfoErrorEnums;
|
|
|
+import com.dragon.tj.portal.common.enums.message.MsgRecordTypeEnum;
|
|
|
+import com.dragon.tj.portal.common.enums.message.ReadStatusEnum;
|
|
|
+import com.dragon.tj.portal.common.enums.message.ScopeEnums;
|
|
|
+import com.dragon.tj.portal.common.vo.message.MessagePageParam;
|
|
|
+import com.dragon.tj.portal.common.vo.notice.NoticeInfoParam;
|
|
|
+import com.dragon.tj.portal.common.vo.notice.NoticeInfoVO;
|
|
|
+import com.dragon.tj.portal.component.exception.message.MessageInfoException;
|
|
|
+import com.dragon.tj.portal.component.message.MessageProducer;
|
|
|
+import com.dragon.tj.portal.entity.MessageInfo;
|
|
|
+import com.dragon.tj.portal.entity.MsgRecord;
|
|
|
+import com.dragon.tj.portal.entity.NoticeInfo;
|
|
|
+import com.dragon.tj.portal.entity.NoticeInfoScope;
|
|
|
+import com.dragon.tj.portal.mapper.NoticeInfoMapper;
|
|
|
+import com.dragon.tj.portal.service.InstallInfoService;
|
|
|
+import com.dragon.tj.portal.service.MsgRecordService;
|
|
|
+import com.dragon.tj.portal.service.NoticeInfoScopeService;
|
|
|
+import com.dragon.tj.portal.service.NoticeInfoService;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.google.common.collect.Sets;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 消息表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author huey
|
|
|
+ * @since 2023-07-11
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class NoticeInfoServiceImpl extends ServiceImpl<NoticeInfoMapper, NoticeInfo> implements NoticeInfoService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MessageProducer producer;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MsgRecordService msgRecordService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private InstallInfoService installInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private NoticeInfoScopeService noticeInfoScopeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private NoticeReqConvert noticeReqConvert;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private NoticeInfoVOConvert noticeInfoVOConvert;
|
|
|
+
|
|
|
+ @Value("${notice-secret:true}")
|
|
|
+ private boolean noticeSecret;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R send(NoticeInfoReq noticeInfoReq) {
|
|
|
+ if (this.validate(noticeInfoReq)) {
|
|
|
+ return R.ok(this.transferIds(noticeInfoReq));
|
|
|
+ }
|
|
|
+ //封装消息
|
|
|
+ //保存及推送mq
|
|
|
+ return R.failed(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public NoticeInfoVO detail(Long id) {
|
|
|
+ NoticeInfo noticeInfo = this.getById(id);
|
|
|
+ return noticeInfoVOConvert.toVo(noticeInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean update(NoticeInfoParam noticeInfoParam) {
|
|
|
+ NoticeInfo messageInfo = noticeInfoVOConvert.paramTo(noticeInfoParam);
|
|
|
+ return this.updateById(messageInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean read(Long id) {
|
|
|
+ NoticeInfo noticeInfo = this.getById(id);
|
|
|
+ Integer readStatus = noticeInfo.getReadStatus();
|
|
|
+ if (ReadStatusEnum.YES.value().equals(readStatus)) {
|
|
|
+ throw new MessageInfoException(MessageInfoErrorEnums.Code.MESSAGE_READ_ALREADY);
|
|
|
+ }
|
|
|
+ noticeInfo.setReadStatus(ReadStatusEnum.YES.value());
|
|
|
+ return this.updateById(noticeInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean reads(List<Long> ids) {
|
|
|
+ List<NoticeInfo> list = Lists.newArrayList();
|
|
|
+ ids.forEach(e -> {
|
|
|
+ MessageInfo messageInfo = new MessageInfo();
|
|
|
+ messageInfo.setReadStatus(ReadStatusEnum.YES.value());
|
|
|
+ messageInfo.setId(e);
|
|
|
+
|
|
|
+ });
|
|
|
+ return this.updateBatchById(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage queryPage(Page page, MessagePageParam req) {
|
|
|
+ IPage<MessageInfo> queryPage = baseMapper.queryPage(page, req);
|
|
|
+ return queryPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean transferIds(NoticeInfoReq noticeInfoReq) {
|
|
|
+ // 120101510000 120000450200
|
|
|
+ //120222197001010001 120222197001010002
|
|
|
+ boolean isSend = false;
|
|
|
+ Integer messageType = noticeInfoReq.getMessageType();
|
|
|
+ String scopeId = noticeInfoReq.getScopeId();
|
|
|
+ Set<String> messageClientIds = this.getMessageClientIds(messageType, scopeId);
|
|
|
+ log.info("transferIds-scopeId-{}-{}", scopeId, messageClientIds);
|
|
|
+ if (CollUtil.isNotEmpty(messageClientIds)) {
|
|
|
+ NoticeInfoSend noticeInfoSend = this.doSaveNoticeInfo(noticeInfoReq, messageClientIds);
|
|
|
+ isSend = producer.send("sseTopic-a1", JSON.toJSONString(noticeInfoSend));
|
|
|
+ if (isSend) {
|
|
|
+ MsgRecord msgRecord = new MsgRecord();
|
|
|
+ msgRecord.setCreateBy("admin");
|
|
|
+ msgRecord.setCreateTime(LocalDateTime.now());
|
|
|
+ msgRecord.setReqContent(JSON.toJSONString(noticeInfoSend));
|
|
|
+ msgRecord.setType(MsgRecordTypeEnum.MESSAGE.value());
|
|
|
+ msgRecordService.save(msgRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return isSend;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @author huey China.
|
|
|
+ * @Description :
|
|
|
+ * @Date Created in 2023/7/12 16:09
|
|
|
+ */
|
|
|
+ private Set<String> getMessageClientIds(Integer messageType, String scopeId) {
|
|
|
+ //登录人标识
|
|
|
+ Set<String> messageClientIds = Sets.newHashSet();
|
|
|
+ if (StringUtils.isEmpty(scopeId)) {
|
|
|
+ throw new MessageInfoException(MessageInfoErrorEnums.Code.NOTICE_LIMIT_EMPTY);
|
|
|
+ }
|
|
|
+ //查询有当前应用的人
|
|
|
+ Set<String> allUserIds = installInfoService.findUsersByAppId(scopeId);
|
|
|
+ if (CollUtil.isNotEmpty(allUserIds)) {
|
|
|
+ messageClientIds.addAll(allUserIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageClientIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean validate(NoticeInfoReq noticeInfoReq) {
|
|
|
+
|
|
|
+ String id = noticeInfoReq.getId();
|
|
|
+ Long t = noticeInfoReq.getT();
|
|
|
+ String scopeId = noticeInfoReq.getScopeId();
|
|
|
+ log.info("noticeInfoReq-validate-start-{}", noticeInfoReq.getId());
|
|
|
+ if (noticeSecret) {
|
|
|
+ String correctHash = DigestUtils.md5Hex(id + t);
|
|
|
+ if (!correctHash.equalsIgnoreCase(noticeInfoReq.getK())) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("noticeInfoReq-validate-end-{}-{}", scopeId, id);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public NoticeInfoSend doSaveNoticeInfo(NoticeInfoReq noticeInfoReq, Set<String> messageClientIds) {
|
|
|
+ log.info("doSaveNoticeInfo-start-{}-{}", noticeInfoReq.getId(), messageClientIds);
|
|
|
+ Integer messageType = noticeInfoReq.getMessageType();
|
|
|
+ ScopeEnums scopeEnums = ScopeEnums.ofMessageType(messageType);
|
|
|
+ List<NoticeInfoScope> insertList = Lists.newArrayList();
|
|
|
+ NoticeInfoSend noticeInfoSend = new NoticeInfoSend();
|
|
|
+ noticeInfoSend.setNoticeInfoReq(noticeInfoReq);
|
|
|
+ noticeInfoSend.setClientIds(messageClientIds);
|
|
|
+
|
|
|
+ NoticeInfo noticeInfo = noticeReqConvert.reqToInfo(noticeInfoReq);
|
|
|
+ noticeInfo.setCreateBy(noticeInfoReq.getId());
|
|
|
+ // 存储
|
|
|
+ boolean isSaveBatchSuccess = false;
|
|
|
+ try {
|
|
|
+ isSaveBatchSuccess = this.save(noticeInfo);
|
|
|
+ messageClientIds.forEach(e -> {
|
|
|
+ NoticeInfoScope noticeInfoScope = new NoticeInfoScope();
|
|
|
+ noticeInfoScope.setNoticeInfoId(noticeInfo.getId());
|
|
|
+ noticeInfoScope.setScopeId(noticeInfoReq.getScopeId());
|
|
|
+ noticeInfoScope.setMemberId(e);
|
|
|
+ noticeInfoScope.setScopeType(scopeEnums.value());
|
|
|
+ //messageInfoScope.setScopeLevel("");
|
|
|
+ insertList.add(noticeInfoScope);
|
|
|
+ });
|
|
|
+ isSaveBatchSuccess = noticeInfoScopeService.saveBatch(insertList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("消息中心 saveBatch 存储失败 {}");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量存储失败,则逐个存储
|
|
|
+ if (!isSaveBatchSuccess) {
|
|
|
+ try {
|
|
|
+ this.save(noticeInfo);
|
|
|
+ insertList.forEach(e -> {
|
|
|
+ noticeInfoScopeService.save(e);
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("消息中心 存储失败 {}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("doSaveNoticeInfo-end-{}-{}", noticeInfoReq.getId(), messageClientIds);
|
|
|
+ return noticeInfoSend;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String correctHash = DigestUtils.md5Hex("1" + "333");
|
|
|
+ System.out.println(correctHash);
|
|
|
+ }
|
|
|
+}
|