Ver Fonte

feat(notice): notice-base

huey há 1 ano atrás
pai
commit
b34d19b890

+ 17 - 3
src/main/java/com/dragon/tj/portal/common/constants/BusinessConstants.java

@@ -11,13 +11,27 @@ public interface BusinessConstants {
     /**
      * 1 通知公告
      */
-    public static Integer DICT_ITEM_ID_1 = 1;
+    public static final Integer DICT_ITEM_ID_1 = 1;
     /**
      * 2 信息交流
      */
-    public static Integer DICT_ITEM_ID_2 = 2;
+    public static final Integer DICT_ITEM_ID_2 = 2;
     /**
      * 3 标准规范
      */
-    public static Integer DICT_ITEM_ID_3 = 3;
+    public static final Integer DICT_ITEM_ID_3 = 3;
+
+
+    /**
+     * 4 任务类
+     */
+    public static final Integer DICT_ITEM_ID_4 = 4;
+    /**
+     * 5 通知类
+     */
+    public static final Integer DICT_ITEM_ID_5 = 5;
+    /**
+     * 6 超期提醒类
+     */
+    public static final Integer DICT_ITEM_ID_6 = 6;
 }

+ 25 - 0
src/main/java/com/dragon/tj/portal/common/convert/notice/NoticeReqConvert.java

@@ -0,0 +1,25 @@
+package com.dragon.tj.portal.common.convert.notice;
+
+
+import com.dragon.tj.portal.common.dto.message.MessageInfoItem;
+import com.dragon.tj.portal.common.dto.message.MessageInfoReq;
+import com.dragon.tj.portal.common.dto.notice.NoticeInfoReq;
+import com.dragon.tj.portal.entity.NoticeInfo;
+import org.mapstruct.Mapper;
+import org.mapstruct.MappingConstants;
+
+@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
+public interface NoticeReqConvert {
+
+    /**
+     * User转换UserDTO
+     *
+     * @return {@link } Mappings是对字段名不一致的字段进行映射
+     * <p>
+     * Mapping可以处理变量名不一致映射关系以及指定日期格式等等
+     */
+    MessageInfoItem reqToItem(MessageInfoReq messageInfoReq);
+
+    NoticeInfo reqToInfo(NoticeInfoReq noticeInfoReq);
+
+}

+ 68 - 0
src/main/java/com/dragon/tj/portal/common/dto/notice/NoticeInfoItem.java

@@ -0,0 +1,68 @@
+package com.dragon.tj.portal.common.dto.notice;
+
+
+import lombok.Getter;
+import lombok.Setter;
+import org.hibernate.validator.constraints.Range;
+
+import java.util.Set;
+
+/**
+ * @author huey China.
+ * @Description :
+ * @Date Created in 2023/6/15 15:59
+ */
+@Getter
+@Setter
+public class NoticeInfoItem {
+
+
+    @Range(min = 1, max = 3, message = "请填入1~3,1 通知公告 2信息交流 3 标准规范 ")
+    private Integer messageType;
+
+    /**
+     * 范围ids
+     */
+    private Set<String> scopeIds;
+
+    /**
+     * 阅读状态 默认0未读 1已读
+     */
+    private Integer readStatus;
+
+    /**
+     * 信息标题
+     */
+    private String title;
+
+    /**
+     * 信息内容
+     */
+    private String content;
+
+    /**
+     * 附件地址
+     */
+    private String attachmentPath;
+
+
+    /**
+     * 版本号
+     */
+    private Integer version;
+
+    /**
+     *
+     *--------------非req参数
+     */
+    /**
+     * TODO appId
+     */
+    private String appId = "appId";
+
+    /**
+     * 客户端id
+     */
+    private String clientId;
+
+}

+ 56 - 0
src/main/java/com/dragon/tj/portal/common/dto/notice/NoticeInfoReq.java

@@ -0,0 +1,56 @@
+package com.dragon.tj.portal.common.dto.notice;
+
+
+import lombok.Getter;
+import lombok.Setter;
+import org.hibernate.validator.constraints.Range;
+
+import javax.validation.constraints.NotNull;
+import java.util.Set;
+
+/**
+ * @author huey China.
+ * @Description :
+ * @Date Created in 2023/6/15 15:59
+ */
+@Getter
+@Setter
+public class NoticeInfoReq {
+
+
+    /**
+    *范围id 为appCode
+    */
+    private String scopeId = "appId";
+
+    @Range(min = 4, max = 6, message = "请填入4~6,4 任务类 5 通知类 6 超期提醒类")
+    private Integer messageType;
+
+    /**
+     * 信息标题
+     */
+    private String title;
+
+    /**
+     * 信息内容
+     */
+    private String content;
+
+    /**
+     * 附件地址
+     */
+    private String attachmentPath;
+
+    @NotNull(message = "毫秒时间戳 不能为空")
+    private Long t;
+
+    private String id;
+
+    /**
+     * md5 (登录人标识+t)
+     */
+//    @NotBlank(message = "调用者身份验证标识 不能为空")
+    private String k;
+
+
+}

+ 22 - 0
src/main/java/com/dragon/tj/portal/common/dto/notice/NoticeInfoSend.java

@@ -0,0 +1,22 @@
+package com.dragon.tj.portal.common.dto.notice;
+
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Set;
+
+/**
+ * @author huey China.
+ * @Description :
+ * @Date Created in 2023/6/15 15:59
+ */
+@Getter
+@Setter
+public class NoticeInfoSend {
+
+    private NoticeInfoReq noticeInfoReq;
+
+    private Set<String> clientIds;
+
+}

+ 2 - 0
src/main/java/com/dragon/tj/portal/common/enums/message/MessageInfoErrorEnums.java

@@ -29,6 +29,8 @@ public interface MessageInfoErrorEnums extends FrameResultError {
         MESSAGE_DEPT_EMPTY(201002, "当前信息类型部门名称不能为空!"),
         MESSAGE_READ_ALREADY(201002, "当前信息已读"),
 
+        NOTICE_LIMIT_EMPTY(201002, "当前消息范围不能为空!"),
+
 
         ;
 

+ 7 - 2
src/main/java/com/dragon/tj/portal/common/enums/message/ScopeEnums.java

@@ -10,7 +10,9 @@ import com.dragon.tj.portal.common.constants.BusinessConstants;
 public enum ScopeEnums {
 
     SCOPE_DEPT(1, "部门"),
-    SCOPE_MEMBER(2, "人员");
+    SCOPE_MEMBER(2, "人员"),
+
+    SCOPE_APP(3, "应用");
 
     private final Integer value;
     private final String name;
@@ -26,9 +28,12 @@ public enum ScopeEnums {
 
     public static ScopeEnums ofMessageType(Integer messageType) {
         if (messageType.equals(BusinessConstants.DICT_ITEM_ID_2)) {
+            return SCOPE_DEPT;
+        } else if (messageType.equals(BusinessConstants.DICT_ITEM_ID_1)
+                || messageType.equals(BusinessConstants.DICT_ITEM_ID_3)) {
             return SCOPE_MEMBER;
         } else {
-            return SCOPE_MEMBER;
+            return SCOPE_APP;
         }
     }
 }

+ 4 - 3
src/main/java/com/dragon/tj/portal/controller/MessageCenterController.java

@@ -6,6 +6,7 @@ import com.dragon.tj.portal.auth.model.LoginUser;
 import com.dragon.tj.portal.auth.util.SecurityUtils;
 import com.dragon.tj.portal.common.base.R;
 import com.dragon.tj.portal.common.dto.message.MessageInfoReq;
+import com.dragon.tj.portal.common.dto.notice.NoticeInfoReq;
 import com.dragon.tj.portal.component.message.MessageProducer;
 import com.dragon.tj.portal.service.MessageInfoService;
 import com.dragon.tj.portal.service.NoticeInfoService;
@@ -57,9 +58,9 @@ public class MessageCenterController {
      * @Date Created in 2023/7/11 16:12
      */
     @PostMapping("/send")
-    public R send(@Validated @RequestBody MessageInfoReq messageInfoReq) {
-        log.info("api/message/publish-req-is {}", JSON.toJSONString(messageInfoReq));
-        return noticeInfoService.send(messageInfoReq);
+    public R send(@Validated @RequestBody NoticeInfoReq noticeInfoReq) {
+        log.info("api/message/send-req-is {}", JSON.toJSONString(noticeInfoReq));
+        return noticeInfoService.send(noticeInfoReq);
     }
 
     //topic 颁发 appcode 及

+ 0 - 21
src/main/java/com/dragon/tj/portal/controller/NoticeInfoScopeController.java

@@ -1,21 +0,0 @@
-package com.dragon.tj.portal.controller;
-
-
-import org.springframework.web.bind.annotation.RequestMapping;
-
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * <p>
- * 消息通知范围表 前端控制器
- * </p>
- *
- * @author huey
- * @since 2023-07-11
- */
-@RestController
-@RequestMapping("/noticeInfoScope")
-public class NoticeInfoScopeController {
-
-}
-

+ 2 - 0
src/main/java/com/dragon/tj/portal/entity/NoticeInfoScope.java

@@ -30,6 +30,8 @@ public class NoticeInfoScope implements Serializable {
     @TableId(value = "id", type = IdType.AUTO)
     private Long id;
 
+    private Long noticeInfoId;
+
     /**
      * 人员id
      */

+ 17 - 0
src/main/java/com/dragon/tj/portal/service/InstallInfoService.java

@@ -0,0 +1,17 @@
+package com.dragon.tj.portal.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.dragon.tj.portal.entity.InstallInfo;
+
+import java.util.Set;
+
+public interface InstallInfoService extends IService<InstallInfo> {
+
+    /**
+    * @author huey China.
+    * @Description : 获取appid下所有有效用户id标识
+    * @Date Created in 2023/7/12 16:21
+     * @return
+    */
+    Set<String> findUsersByAppId(String appId);
+}

+ 2 - 2
src/main/java/com/dragon/tj/portal/service/NoticeInfoService.java

@@ -1,7 +1,7 @@
 package com.dragon.tj.portal.service;
 
 import com.dragon.tj.portal.common.base.R;
-import com.dragon.tj.portal.common.dto.message.MessageInfoReq;
+import com.dragon.tj.portal.common.dto.notice.NoticeInfoReq;
 import com.dragon.tj.portal.entity.NoticeInfo;
 import com.baomidou.mybatisplus.extension.service.IService;
 
@@ -15,5 +15,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface NoticeInfoService extends IService<NoticeInfo> {
 
-    R send(MessageInfoReq messageInfoReq);
+    R send(NoticeInfoReq messageInfoReq);
 }

+ 38 - 0
src/main/java/com/dragon/tj/portal/service/impl/InstallInfoServiceImpl.java

@@ -0,0 +1,38 @@
+package com.dragon.tj.portal.service.impl;
+
+
+import cn.hutool.core.collection.CollUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.dragon.tj.portal.entity.InstallInfo;
+import com.dragon.tj.portal.mapper.app.InstallInfoMapper;
+import com.dragon.tj.portal.service.InstallInfoService;
+import com.google.common.collect.Sets;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+@Service
+public class InstallInfoServiceImpl extends ServiceImpl<InstallInfoMapper, InstallInfo> implements InstallInfoService {
+
+
+    @Override
+    public Set<String> findUsersByAppId(String appId) {
+        LambdaQueryWrapper<InstallInfo> wrapper = Wrappers.lambdaQuery();
+        wrapper.eq(InstallInfo::getAppId, appId);
+        List<InstallInfo> list = this.list(wrapper);
+        if (CollUtil.isNotEmpty(list)) {
+            Set<String> idCardSet = list.stream().filter(e -> StringUtils.isNotEmpty(e.getUserIdcard())).map(InstallInfo::getUserIdcard).collect(Collectors.toSet());
+            if (CollUtil.isNotEmpty(idCardSet)) {
+                return idCardSet;
+            }
+        }
+        return Sets.newHashSet();
+    }
+}
+
+

+ 152 - 5
src/main/java/com/dragon/tj/portal/service/impl/NoticeInfoServiceImpl.java

@@ -1,12 +1,38 @@
 package com.dragon.tj.portal.service.impl;
 
+import cn.hutool.core.collection.CollUtil;
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.dragon.tj.portal.common.base.R;
-import com.dragon.tj.portal.common.dto.message.MessageInfoReq;
+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.ScopeEnums;
+import com.dragon.tj.portal.component.exception.message.MessageInfoException;
+import com.dragon.tj.portal.component.message.KafkaInitialConfiguration;
+import com.dragon.tj.portal.component.message.MessageProducer;
+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.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.Set;
 
 /**
  * <p>
@@ -16,14 +42,135 @@ import org.springframework.stereotype.Service;
  * @author huey
  * @since 2023-07-11
  */
+@Slf4j
 @Service
 public class NoticeInfoServiceImpl extends ServiceImpl<NoticeInfoMapper, NoticeInfo> implements NoticeInfoService {
 
-    //        producer.send(KafkaInitialConfiguration.sseTopic + "-a1", JSON.toJSONString(messageInfoSend));
+    @Autowired
+    private MessageProducer producer;
+
+    @Autowired
+    private MsgRecordService msgRecordService;
+
+    @Autowired
+    private InstallInfoService installInfoService;
+
+    @Autowired
+    private NoticeInfoScopeService noticeInfoScopeService;
+
+    @Autowired
+    private NoticeReqConvert noticeReqConvert;
+
 
     @Override
-    public R send(MessageInfoReq messageInfoReq) {
-        //鉴权加密钥
-        return null;
+    public R send(NoticeInfoReq noticeInfoReq) {
+        this.validate(noticeInfoReq);
+        //判断scopeId及获得对应的人
+        boolean b = transferIds(noticeInfoReq);
+        //封装消息
+        //保存及推送mq
+        return R.ok(b);
+    }
+
+    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);
+
+        if (CollUtil.isNotEmpty(messageClientIds)) {
+            NoticeInfoSend noticeInfoSend = this.doSaveNoticeInfo(noticeInfoReq, messageClientIds);
+            isSend = producer.send(KafkaInitialConfiguration.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 void validate(NoticeInfoReq noticeInfoReq) {
+        String id = noticeInfoReq.getId();
+        Long t = noticeInfoReq.getT();
+        String scopeId = noticeInfoReq.getScopeId();
+        log.info("noticeInfoReq-validate-start-{}", noticeInfoReq.getId());
+        String correctHash = DigestUtils.md5Hex(id + t);
+        if (!correctHash.equalsIgnoreCase(noticeInfoReq.getK())) {
+            return;
+        }
+        log.info("noticeInfoReq-validate-end-{}-{}", scopeId, id);
+
+    }
+
+    @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;
     }
 }