浏览代码

消息获取接口修改为传输接收人uu、enuu、消费app

wangyc 7 年之前
父节点
当前提交
3990256c86

+ 4 - 3
src/main/java/com/uas/ps/message/api/MessageController.java

@@ -29,13 +29,14 @@ public class MessageController {
 
     /**
      * 获取消息
-     * @param receiver 收信人
+     * @param receiverUu 收信人uu
+     * @param receiverEnuu 收信人企业uu
      * @param consumerApp 接收app
      * @return
      */
     @RequestMapping(method = RequestMethod.GET, produces = "application/json")
-    public List<Message> getMessages(@RequestParam String receiver, String consumerApp) {
-        return messageService.getMessages(receiver, consumerApp);
+    public List<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp) {
+        return messageService.getMessages(receiverUu, receiverEnuu, consumerApp);
     }
 
     /**

+ 5 - 4
src/main/java/com/uas/ps/message/dao/MessageDao.java

@@ -5,6 +5,7 @@ import java.util.List;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
 /**
@@ -22,8 +23,8 @@ public interface MessageDao extends JpaRepository<Message, Long>, JpaSpecificati
      * @param consumerAppId 接收应用id
      * @return
      */
-    @Query(nativeQuery = true, value = "select * from messages e where e.ms_receiveruu = :receiverUu and e.ms_receiverenuu = :receiverEnuu and e.id in (select ms_id from message$consumeapp a where a.app_id = :consumerAppId)")
-    List<Message> findByReceiverUuAndReceiverEnuuAndConsumerAppId(Long receiverUu, Long receiverEnuu,
+    @Query(nativeQuery = true, value = "select * from messages e where e.ms_receiveruu = :receiverUu and e.ms_receiverenuu = :receiverEnuu and e.ms_id in (select ms_id from message$consumeapp a where a.app_id = :consumerAppId)")
+    List<Message> findByReceiverUuAndReceiverEnuuAndConsumerAppId(@Param("receiverUu") Long receiverUu, @Param("receiverEnuu") Long receiverEnuu, @Param("consumerAppId")
         Long consumerAppId);
 
     /**
@@ -33,6 +34,6 @@ public interface MessageDao extends JpaRepository<Message, Long>, JpaSpecificati
      * @param isSent 发送状态
      * @return
      */
-    @Query(nativeQuery = true, value = "select * from messages e where e.ms_receiveruu = :receiverUu and e.ms_receiverenuu = :receiverEnuu and e.id in (select ms_id from message$consumeapp a where a.app_id = :consumerAppId)")
-    List<Message> findByConsumerAppIdAndIsReadAndIsSent(Long consumerAppId, Short isRead, Short isSent);
+    @Query(nativeQuery = true, value = "select * from messages e where e.ms_isread = :isRead and e.ms_issent = :isSent and e.ms_id in (select ms_id from message$consumeapp a where a.app_id = :consumerAppId)")
+    List<Message> findByConsumerAppIdAndIsReadAndIsSent(@Param("consumerAppId") Long consumerAppId, @Param("isRead") Short isRead, @Param("isSent") Short isSent);
 }

+ 1 - 1
src/main/java/com/uas/ps/message/domain/Message.java

@@ -146,7 +146,7 @@ public class Message implements Serializable {
      */
     @ManyToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, fetch = FetchType.EAGER)
     @JoinTable(name = "message$consumeapp", joinColumns = @JoinColumn(name = "ms_id", referencedColumnName = "ms_id"), inverseJoinColumns = @JoinColumn(name = "app_id", referencedColumnName = "app_id"))
-    @OrderBy("uu")
+    @OrderBy("id")
     @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
     private Set<App> consumerApp;
 

+ 3 - 2
src/main/java/com/uas/ps/message/service/MessageService.java

@@ -13,11 +13,12 @@ public interface MessageService {
 
     /**
      * 获取消息
-     * @param receiver 收信人
+     * @param receiverUu 收信人uu
+     * @param receiverEnuu 收信人企业uu
      * @param consumerApp 接收app
      * @return
      */
-    List<Message> getMessages(String receiver, String consumerApp);
+    List<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp);
 
     /**
      * 保存消息

+ 31 - 24
src/main/java/com/uas/ps/message/service/impl/MessageServiceImpl.java

@@ -1,8 +1,8 @@
 package com.uas.ps.message.service.impl;
 
+
 import com.alibaba.fastjson.JSONObject;
 import com.uas.message.mail.domain.MailLog;
-import com.uas.message.mail.service.MailService;
 import com.uas.ps.message.dao.AppDao;
 import com.uas.ps.message.dao.MessageDao;
 import com.uas.ps.message.domain.App;
@@ -83,18 +83,18 @@ public class MessageServiceImpl implements MessageService {
     }
 
     @Override
-    public List<Message> getMessages(String receiver, String consumerApp) {
+    public List<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp) {
         List<Message> messages = new ArrayList<>();
-        User user = FastjsonUtils.fromJson(receiver, User.class);
+        if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
+            throw new ParameterMissingException("接收人信息为空");
+        }
         App consumerAppExists = appDao.findByName(consumerApp);
         if (consumerApp == null) {
             throw new IllegalOperatorException("接收应用不存在");
         }
 
-        if (user.getUserUU() != null && user.getEnterprise() != null && user.getEnterprise().getUu() != null) {
-            messages = messageDao.findByReceiverUuAndReceiverEnuuAndConsumerAppId(user.getUserUU(),
-                user.getEnterprise().getUu(), consumerAppExists.getId());
-        }
+        messages = messageDao.findByReceiverUuAndReceiverEnuuAndConsumerAppId(Long.valueOf(receiverUu),
+            Long.valueOf(receiverEnuu), consumerAppExists.getId());
         return messages;
     }
 
@@ -113,7 +113,8 @@ public class MessageServiceImpl implements MessageService {
         }
 
         // 获取改应用应接收的未读、未发送消息
-        List<Message> messages = messageDao.findByConsumerAppIdAndIsReadAndIsSent(consumerAppExists.getId(), Constant.NO, Constant.NO);
+        List<Message> messages = messageDao.findByConsumerAppIdAndIsReadAndIsSent(consumerAppExists.getId(),
+            Constant.NO, Constant.NO);
 
         Map<String, Object> resultMap = sendMessageByAPI(messages, consumerApp);
         return resultMap;
@@ -164,7 +165,7 @@ public class MessageServiceImpl implements MessageService {
             } else {
                 App app = appDao.findByName(String.valueOf(produceApp));
                 if (app == null) {
-                    throw new ParameterMissingException("发送应用不存在");
+                    throw new IllegalOperatorException("发送应用不存在");
                 }
                 message.setProducerApp(app.getId());
             }
@@ -195,7 +196,7 @@ public class MessageServiceImpl implements MessageService {
                         for (String consumer : consumers) {
                             App app = appDao.findByName(consumer);
                             if (app == null) {
-                                throw new ParameterMissingException("接收应用不存在");
+                                throw new IllegalOperatorException("接收应用不存在");
                             }
                             consumerApps.add(app);
                         }
@@ -239,7 +240,7 @@ public class MessageServiceImpl implements MessageService {
      * 推送消息(调用邮件、短信、IM接口)
      * @param messages 消息
      * @param consumerApp 消费app
-     * @return
+     * @return resultMap
      */
     private Map<String, Object> sendMessageByAPI(List<Message> messages, String consumerApp) {
         Map<String, Object> resultMap = new HashMap<>();
@@ -260,7 +261,9 @@ public class MessageServiceImpl implements MessageService {
                 if (message.getSmsType().contains(SMSType.IM)) {
                     sendIM(message, consumerApp);
                 }
+                message.setIsSent(Constant.YES);
             }
+            messageDao.save(messages);
         }
         return resultMap;
     }
@@ -281,7 +284,8 @@ public class MessageServiceImpl implements MessageService {
         object.put("params", message.getContent());
 
         HttpEntity<String> formEntity = new HttpEntity<String>(object.toJSONString(), headers);
-        ResponseEntity<MailLog> responseEntity = restTemplate.postForEntity(MAIL_CONSOLE_HOST + MAIL_SEND_URL, formEntity, MailLog.class);
+        ResponseEntity<MailLog> responseEntity = restTemplate.postForEntity(MAIL_CONSOLE_HOST + MAIL_SEND_URL,
+            formEntity, MailLog.class);
     }
 
     /**
@@ -290,18 +294,20 @@ public class MessageServiceImpl implements MessageService {
      */
     private void sendSM(Message message) {
         // TODO 账户中心获取用户手机号
-        String templetId = "";
-        User user = FastjsonUtils.fromJson(message.getReceiver(), User.class);
-        if (user.getUserTel() != null) {
-            try {
-                SmsMessage sms = new SmsMessage();
-                List<Object> obj = new ArrayList<Object>();
-                sms.setParams(obj);
-                sms.setReceiver(user.getUserTel());
-                sms.setTemplateId(templetId);
-                com.uas.ps.message.util.HttpUtil.sendPost(messageUrl, FastjsonUtils.toJson(sms));
-            } catch (Exception e) {
-                e.printStackTrace();
+        if (message.getSmTemplate() != null) {
+            String templetId = message.getSmTemplate();
+            User user = FastjsonUtils.fromJson(message.getReceiver(), User.class);
+            if (user.getUserTel() != null) {
+                try {
+                    SmsMessage sms = new SmsMessage();
+                    List<Object> obj = new ArrayList<Object>();
+                    sms.setParams(obj);
+                    sms.setReceiver(user.getUserTel());
+                    sms.setTemplateId(templetId);
+                    com.uas.ps.message.util.HttpUtil.sendPost(messageUrl, FastjsonUtils.toJson(sms));
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
             }
         }
     }
@@ -309,6 +315,7 @@ public class MessageServiceImpl implements MessageService {
     /**
      * 发送IM
      * @param message 消息
+     * @param consumerApp 消费app
      */
     private void sendIM(Message message, String consumerApp) {
         Map<String, Object> params = new HashMap<>();