Procházet zdrojové kódy

新增获取未读消息数量功能

wangyc před 7 roky
rodič
revize
4fd5c0ca64

+ 13 - 1
src/main/java/com/uas/ps/message/api/MessageController.java

@@ -26,7 +26,7 @@ public class MessageController {
     private final MessageService messageService;
 
     @Autowired
-    private MessageController(MessageService messageService) {
+    public MessageController(MessageService messageService) {
         this.messageService = messageService;
     }
 
@@ -47,6 +47,18 @@ public class MessageController {
         return messageService.getMessages(receiverUu, receiverEnuu, consumerApp, isRead, keyword, pageParams);
     }
 
+    /**
+     * 获取未读消息数量
+     * @param receiverUu 收信人uu
+     * @param receiverEnuu 收信人企业uu
+     * @param consumerApp 接收app
+     * @return
+     */
+    @RequestMapping(value = "/count/unread", method = RequestMethod.GET, produces = "application/json")
+    public Map<String, Object> getUnReadMessageCount(String receiverUu, String receiverEnuu, String consumerApp) {
+        return messageService.getUnReadMessageCount(receiverUu, receiverEnuu, consumerApp);
+    }
+
     /**
      * 保存消息
      * @param messages 消息

+ 9 - 0
src/main/java/com/uas/ps/message/service/MessageService.java

@@ -26,6 +26,15 @@ public interface MessageService {
      */
     Page<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp, String isRead, String keyword, PageParams pageParams);
 
+    /**
+     * 获取未读消息数量
+     * @param receiverUu 收信人uu
+     * @param receiverEnuu 收信人企业uu
+     * @param consumerApp 接收app
+     * @return
+     */
+    Map<String, Object> getUnReadMessageCount(String receiverUu, String receiverEnuu, String consumerApp);
+
     /**
      * 保存消息
      * @param messages 消息

+ 29 - 5
src/main/java/com/uas/ps/message/service/impl/MessageServiceImpl.java

@@ -1,8 +1,6 @@
 package com.uas.ps.message.service.impl;
 
 
-import static antlr.build.ANTLR.root;
-
 import com.alibaba.fastjson.JSONObject;
 import com.uas.account.entity.User;
 import com.uas.account.entity.UserView;
@@ -96,15 +94,19 @@ public class MessageServiceImpl implements MessageService {
     }
 
     @Override
-    public Page<Message> getMessages(String receiverUu, final String receiverEnuu, String consumerApp, String isRead,
+    public Page<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp, String isRead,
         String keyword, PageParams pageParams) {
         if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
             throw new ParameterMissingException("接收人信息为空");
         }
-        final App consumerAppExists = appDao.findByName(consumerApp);
+        if (StringUtils.isEmpty(consumerApp)) {
+            throw new ParameterMissingException("接收应用信息为空");
+        }
+        App consumerAppExists = appDao.findByName(consumerApp);
         if (consumerApp == null) {
             throw new IllegalOperatorException("接收应用不存在");
         }
+        final Long consumerAppId = consumerAppExists.getId();
 
         // 消息接收人过滤
         SimpleExpression receiverUuExp = PredicateUtils.eq("receiverUu", Long.valueOf(receiverUu), true);
@@ -136,7 +138,7 @@ public class MessageServiceImpl implements MessageService {
                     criteriaBuilder));
                 // 消费应用过滤
                 Predicate consumerAppPredicate = criteriaBuilder.equal(root.join("consumerApp").get("id"),
-                    consumerAppExists.getId());
+                    consumerAppId);
                 Predicate all = criteriaBuilder.and(messagePredicate, consumerAppPredicate);
                 criteriaQuery.where(all);
                 return null;
@@ -144,6 +146,28 @@ public class MessageServiceImpl implements MessageService {
         }, pageInfo);
     }
 
+    @Override
+    public Map<String, Object> getUnReadMessageCount(String receiverUu, String receiverEnuu, String consumerApp) {
+        if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
+            throw new ParameterMissingException("接收人信息为空");
+        }
+        if (StringUtils.isEmpty(consumerApp)) {
+            throw new ParameterMissingException("接收应用信息为空");
+        }
+        App consumerAppExists = appDao.findByName(consumerApp);
+        if (consumerApp == null) {
+            throw new IllegalOperatorException("接收应用不存在");
+        }
+
+        List<Message> messages = messageDao.findByReceiverUuAndReceiverEnuuAndConsumerAppId(Long.valueOf(receiverUu), Long.valueOf(receiverEnuu),
+            consumerAppExists.getId());
+
+        Map<String, Object> resultMap = new HashMap<>();
+        resultMap.put("success", "success");
+        resultMap.put("count", CollectionUtils.isEmpty(messages) ? 0 : messages.size());
+        return resultMap;
+    }
+
     @Override
     public List<Message> saveMessages(String messages) {
         List<JSONObject> jsonObjects = FastjsonUtils.fromJsonArray(messages, JSONObject.class);