yzc hace 2 años
padre
commit
0c5632ca2f

+ 36 - 10
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/KwmMessageUserService.java

@@ -25,25 +25,36 @@ public class KwmMessageUserService {
     private final KwmMessageUserMapper kwmMessageUserMapper;
 
     /**
+     * @param userId
+     * @return java.lang.Long
      * @desc: 获取用户未删除消息ids
      * @author: yzc
      * @date: 2023-06-09 14:24
-     * @param userId
-     * @return java.lang.Long
      */
     public List<Long> getMsgIdsByUserId(Long userId) {
+        List<KwmMessageUser> list = getByUserId(userId);
+        return list.stream().map(KwmMessageUser::getMsgId).toList();
+    }
+
+    /**
+     * @desc: 根据用户id获取
+     * @author: yzc
+     * @date: 2023-06-16 11:18
+     * @Param userId:
+     * @return: java.util.List<com.sckw.message.model.KwmMessageUser>
+     */
+    public List<KwmMessageUser> getByUserId(Long userId) {
         LambdaQueryWrapper<KwmMessageUser> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(KwmMessageUser::getUserId, userId).eq(KwmMessageUser::getDelFlag, 0);
-        List<KwmMessageUser> list = CollectionUtils.emptyIfNull(kwmMessageUserMapper.selectList(wrapper));
-        return list.stream().map(KwmMessageUser::getMsgId).toList();
+        return CollectionUtils.emptyIfNull(kwmMessageUserMapper.selectList(wrapper));
     }
 
     /**
+     * @param userId, msgIds
+     * @return void
      * @desc: 更新为已读根据用户id及消息ids
      * @author: yzc
      * @date: 2023-06-09 14:30
-     * @param userId, msgIds
-     * @return void
      */
     @Transactional(rollbackFor = Exception.class)
     public void readByUserAndMsgIds(Long userId, List<Long> msgIds) {
@@ -54,11 +65,11 @@ public class KwmMessageUserService {
     }
 
     /**
+     * @param userId, msgIds
+     * @return void
      * @desc: 根据用户删除消息
      * @author: yzc
      * @date: 2023-06-09 15:07
-     * @param userId, msgIds
-     * @return void
      */
     @Transactional(rollbackFor = Exception.class)
     public void delByUserAndMsgIds(long userId, List<Long> msgIds) {
@@ -70,14 +81,29 @@ public class KwmMessageUserService {
 
 
     /**
+     * @param messageUsers
+     * @return void
      * @desc: 批量插入
      * @author: yzc
      * @date: 2023-06-09 15:58
-     * @param messageUsers
-     * @return void
      */
     @Transactional(rollbackFor = Exception.class)
     public void batchSave(List<KwmMessageUser> messageUsers) {
         messageUsers.forEach(kwmMessageUserMapper::insert);
     }
+
+    /**
+     * @desc: 通过消息id和userId获取
+     * @author: yzc
+     * @date: 2023-06-16 11:09
+     * @Param msgId:
+     * @Param userId:
+     * @return: com.sckw.message.model.KwmMessageUser
+     */
+    public KwmMessageUser getByMsgIdAndUserId(Long msgId, long userId) {
+        LambdaQueryWrapper<KwmMessageUser> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(KwmMessageUser::getUserId, userId).eq(KwmMessageUser::getMsgId, msgId)
+                .eq(KwmMessageUser::getDelFlag, 0).last("LIMIT 1");
+        return kwmMessageUserMapper.selectOne(wrapper);
+    }
 }

+ 27 - 16
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/MessageService.java

@@ -6,6 +6,7 @@ import com.sckw.core.utils.CollectionUtils;
 import com.sckw.core.utils.StringUtils;
 import com.sckw.message.model.FindMessagePageParam;
 import com.sckw.message.model.KwmMessage;
+import com.sckw.message.model.KwmMessageUser;
 import com.sckw.message.model.vo.req.DeleteMessagesReqVO;
 import com.sckw.message.model.vo.req.FindMessagesReqVO;
 import com.sckw.message.model.vo.req.ReadMessagesReqVO;
@@ -42,11 +43,21 @@ public class MessageService {
      */
     public List<KwmMessage> selectMessages(FindMessagesReqVO params) {
         //TODO 当前线程获取用户id
-        List<Long> msgIds = kwmMessageUserService.getMsgIdsByUserId(1L);
-        if (CollectionUtils.isEmpty(msgIds)) {
+        List<KwmMessageUser> messageUsers = kwmMessageUserService.getByUserId(1L);
+        Map<Long, KwmMessageUser> map = messageUsers.stream().collect(Collectors.toMap(KwmMessageUser::getMsgId, e -> e, (k1, k2) -> k1));
+        if (CollectionUtils.isEmpty(map)) {
             return Collections.emptyList();
         }
-        return kwmMessageService.getList(msgIds, params.getCategory(), params.getType(), null);
+        List<Long> msgIds = map.keySet().stream().toList();
+        List<KwmMessage> list = kwmMessageService.getList(msgIds, params.getCategory(), params.getType(), null);
+        list.forEach(e->{
+            KwmMessageUser messageUser = map.get(e.getId());
+            e.setStatus(messageUser.getStatus());
+            e.setCreateTime(messageUser.getCreateTime());
+            e.setUpdateTime(messageUser.getUpdateTime());
+            e.setUpdateBy(messageUser.getUpdateBy());
+        });
+        return list;
     }
 
     /**
@@ -67,7 +78,6 @@ public class MessageService {
         if (CollectionUtils.isNotEmpty(ids)) {
             List<Long> list = ids.stream().filter(msgIds::contains).toList();
             kwmMessageUserService.readByUserAndMsgIds(1L, list);
-            kwmMessageService.readByMsgIds(list);
         } else {
             List<KwmMessage> messageList = kwmMessageService.getList(msgIds, reqVO.getCategory(), reqVO.getType(), 0);
             if (CollectionUtils.isEmpty(messageList)) {
@@ -75,7 +85,6 @@ public class MessageService {
             }
             List<Long> updateMsgIds = messageList.stream().map(KwmMessage::getId).toList();
             kwmMessageUserService.readByUserAndMsgIds(1L, updateMsgIds);
-            kwmMessageService.readByMsgIds(updateMsgIds);
         }
     }
 
@@ -97,7 +106,6 @@ public class MessageService {
         if (CollectionUtils.isNotEmpty(ids)) {
             List<Long> list = ids.stream().filter(msgIds::contains).toList();
             kwmMessageUserService.delByUserAndMsgIds(1L, list);
-            kwmMessageService.delByMsgIds(list);
         } else {
             List<KwmMessage> messageList = kwmMessageService.getList(msgIds, reqVO.getCategory(), reqVO.getType(), null);
             if (CollectionUtils.isEmpty(messageList)) {
@@ -105,7 +113,6 @@ public class MessageService {
             }
             List<Long> delMsgIds = messageList.stream().map(KwmMessage::getId).toList();
             kwmMessageUserService.delByUserAndMsgIds(1L, delMsgIds);
-            kwmMessageService.delByMsgIds(delMsgIds);
         }
     }
 
@@ -131,19 +138,26 @@ public class MessageService {
      */
     public List<MessagesStatisticsResVO> statistics(String category) {
         //TODO 当前线程获取用户id
-        List<Long> msgIds = kwmMessageUserService.getMsgIdsByUserId(1L);
-        if (CollectionUtils.isEmpty(msgIds)) {
+        List<KwmMessageUser> messageUserList = kwmMessageUserService.getByUserId(1L);
+        Map<Long, KwmMessageUser> messageUserMap = messageUserList.stream().collect(Collectors.toMap(KwmMessageUser::getMsgId, e -> e, (k1, k2) -> k1));
+        if (CollectionUtils.isEmpty(messageUserMap)) {
             return Collections.emptyList();
         }
+        List<Long> msgIds = messageUserMap.keySet().stream().toList();
         List<KwmMessage> messages = kwmMessageService.statistics(msgIds, category);
         if (CollectionUtils.isEmpty(messages)) {
             return Collections.emptyList();
         }
+        messages.forEach(e->{
+            KwmMessageUser kwmMessageUser = messageUserMap.get(e.getId());
+            e.setStatus(kwmMessageUser.getStatus());
+            e.setCreateTime(kwmMessageUser.getCreateTime());
+        });
         Map<String, List<KwmMessage>> collect;
         if (StringUtils.isNotBlank(category)) {
-            collect = messages.stream().sorted(Comparator.comparingInt(KwmMessage::getStatus).thenComparing(KwmMessage::getCreateTime,Comparator.reverseOrder())).collect(Collectors.groupingBy(KwmMessage::getType));
+            collect = messages.stream().sorted(Comparator.comparingInt(KwmMessage::getStatus).thenComparing(KwmMessage::getCreateTime, Comparator.reverseOrder())).collect(Collectors.groupingBy(KwmMessage::getType));
         } else {
-            collect = messages.stream().sorted(Comparator.comparingInt(KwmMessage::getStatus).thenComparing(KwmMessage::getCreateTime,Comparator.reverseOrder())).collect(Collectors.groupingBy(KwmMessage::getCategory));
+            collect = messages.stream().sorted(Comparator.comparingInt(KwmMessage::getStatus).thenComparing(KwmMessage::getCreateTime, Comparator.reverseOrder())).collect(Collectors.groupingBy(KwmMessage::getCategory));
         }
         List<MessagesStatisticsResVO> list = new ArrayList<>(collect.size());
         collect.values().forEach(e -> {
@@ -167,11 +181,8 @@ public class MessageService {
     public KwmMessage detail(Long id) {
         //TODO 当前线程获取用户id
         KwmMessage message = kwmMessageService.getById(id);
-        if (Global.UN_READ.equals(message.getStatus())) {
-            List<Long> ids = Collections.singletonList(id);
-            kwmMessageService.readByMsgIds(ids);
-            kwmMessageUserService.readByUserAndMsgIds(1L, ids);
-        }
+        List<Long> ids = Collections.singletonList(id);
+        kwmMessageUserService.readByUserAndMsgIds(1L, ids);
         return message;
     }
 }

+ 1 - 1
sckw-modules/sckw-message/src/main/resources/mapper/KwmMessageMapper.xml

@@ -5,7 +5,7 @@
   <select id="findPage" resultType="com.sckw.message.model.KwmMessage" >
     select
       m.id, m.category, m.type, m.title, m.content, m.url, m.params, m.client_type clientType, m.remark,
-      m.status, m.create_by createBy, m.create_time createTime, m.update_by updateBy, m.update_time updateTime
+      mu.status, m.create_by createBy, m.create_time createTime, m.update_by updateBy, m.update_time updateTime
     from kwm_message m
     left join kwm_message_user mu
     on m.id = mu.msg_id