Browse Source

增加读取未读消息数量和读取的功能

yujia 7 years ago
parent
commit
8fda36107a

+ 25 - 0
src/main/java/com/uas/platform/b2c/common/message/model/MessageModel.java

@@ -103,6 +103,12 @@ public class MessageModel {
      */
     private String consumerApp;
 
+
+    private String sendEnterpriseName;
+
+
+    private String sendUserName;
+
     public Long getId() {
         return id;
     }
@@ -245,4 +251,23 @@ public class MessageModel {
     public void setSourceId(Long sourceId) {
         this.sourceId = sourceId;
     }
+
+    public String getSendEnterpriseName() {
+
+        return sendEnterpriseName;
+    }
+
+    public MessageModel setSendEnterpriseName(String sendEnterpriseName) {
+        this.sendEnterpriseName = sendEnterpriseName;
+        return this;
+    }
+
+    public String getSendUserName() {
+        return sendUserName;
+    }
+
+    public MessageModel setSendUserName(String sendUserName) {
+        this.sendUserName = sendUserName;
+        return this;
+    }
 }

+ 61 - 0
src/main/java/com/uas/platform/b2c/common/psmessage/controller/MessageController.java

@@ -0,0 +1,61 @@
+package com.uas.platform.b2c.common.psmessage.controller;
+
+import com.uas.platform.b2c.common.message.model.MessageModel;
+import com.uas.platform.b2c.common.psmessage.service.MessageService;
+import com.uas.platform.b2c.trade.support.ResultMap;
+import com.uas.platform.core.model.PageParams;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Map;
+
+/**
+ * 获取ps-message保存的站内信的接口
+ *
+ * @author yuj 2018-07-11 17:01
+ */
+@RestController
+@RequestMapping("/webMessage")
+public class MessageController {
+
+    private final MessageService messageService;
+
+    @Autowired
+    public MessageController(MessageService messageService) {
+        this.messageService = messageService;
+    }
+
+    /**
+     * 根据分页信息和过滤类型分页获取站内信
+     * @param params
+     * @param type
+     * @return
+     */
+    @RequestMapping(value = "/page", method = RequestMethod.GET)
+    public Page<MessageModel> findByPageAndParams(PageParams params, String type) {
+        return messageService.findByPageAndParams(params, type);
+    }
+
+
+    /**
+     * 获取未读消息数量
+     * @return
+     */
+    @RequestMapping(value = "/count/unread", method = RequestMethod.GET)
+    public Map<String, Object> getUnReadMessageCount() {
+        return messageService.getUnReadMessageCount();
+    }
+
+    /**
+     * 阅读消息
+     * @param messageId 消息接收者信息
+     * @return
+     */
+    @RequestMapping(value = "/read", method = RequestMethod.POST)
+    public ResultMap read(Long messageId) {
+        return messageService.readMessage(messageId);
+    }
+}

+ 32 - 0
src/main/java/com/uas/platform/b2c/common/psmessage/service/MessageService.java

@@ -1,7 +1,12 @@
 package com.uas.platform.b2c.common.psmessage.service;
 
 import com.uas.platform.b2c.common.message.model.MessageModel;
+import com.uas.platform.b2c.trade.support.ResultMap;
+import com.uas.platform.core.model.PageParams;
+import org.springframework.data.domain.Page;
+
 import java.util.List;
+import java.util.Map;
 
 /**
  * 公共消息服务接口
@@ -19,6 +24,18 @@ public interface MessageService {
     String sendMessage(List<MessageModel> messageModel);
 
 
+
+    /**
+     * 根据分页信息和过滤类型分页获取站内信
+     * @param params
+     * @param type
+     * @return
+     */
+    Page<MessageModel> findByPageAndParams(PageParams params, String type);
+
+
+
+
     /**
      * 根据所给字段生成消息
      * @param content 消息内容
@@ -31,4 +48,19 @@ public interface MessageService {
      * @return
      */
     MessageModel initMessage(String content, String type, Long receiverUu, Long receiverEnuu, String consumerType,String consumerApp, String smsType, Long... args);
+
+
+    /**
+     * 获取未读消息数量
+     * @return
+     */
+    Map<String, Object> getUnReadMessageCount();
+
+    /**
+     * 短信
+     *
+     * @param messageId 消息的接口
+     * @return ModelMap
+     */
+    ResultMap readMessage(Long messageId);
 }

+ 183 - 4
src/main/java/com/uas/platform/b2c/common/psmessage/service/impl/MessageServiceImpl.java

@@ -1,20 +1,36 @@
 package com.uas.platform.b2c.common.psmessage.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
+import com.uas.platform.b2c.common.account.model.Enterprise;
 import com.uas.platform.b2c.common.account.model.User;
+import com.uas.platform.b2c.common.account.service.EnterpriseService;
+import com.uas.platform.b2c.common.account.service.UserService;
 import com.uas.platform.b2c.common.message.model.MessageModel;
+import com.uas.platform.b2c.common.message.type.ConsumerApp;
 import com.uas.platform.b2c.common.message.type.ProducerApp;
 import com.uas.platform.b2c.common.psmessage.service.MessageService;
+import com.uas.platform.b2c.common.psmessage.util.JsonObjectUtil;
 import com.uas.platform.b2c.core.config.SysConf;
 import com.uas.platform.b2c.core.support.SystemSession;
 import com.uas.platform.b2c.core.utils.FastjsonUtils;
+import com.uas.platform.b2c.trade.support.CodeType;
+import com.uas.platform.b2c.trade.support.ResultMap;
 import com.uas.platform.b2c.trade.util.BoundedExecutor;
+import com.uas.platform.core.model.PageInfo;
+import com.uas.platform.core.model.PageParams;
 import com.uas.platform.core.util.HttpUtil;
+import org.apache.commons.collections.map.HashedMap;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
@@ -26,12 +42,19 @@ import java.util.concurrent.Executors;
 @Service
 public class MessageServiceImpl implements MessageService {
 
-    @Autowired
-    private SysConf sysConf;
+    private final SysConf sysConf;
 
     private final BoundedExecutor executor;
 
-    public MessageServiceImpl() {
+    private final EnterpriseService enterpriseService;
+
+    private final UserService userService;
+
+    @Autowired
+    public MessageServiceImpl(SysConf sysConf, EnterpriseService enterpriseService, UserService userService) {
+        this.sysConf = sysConf;
+        this.enterpriseService = enterpriseService;
+        this.userService = userService;
         ExecutorService executorService = Executors.newCachedThreadPool();
         executor = new BoundedExecutor(executorService, 1600);
     }
@@ -42,7 +65,8 @@ public class MessageServiceImpl implements MessageService {
             @Override
             public void run() {
                 try {
-                    HttpUtil.doPost(sysConf.getMessageServiceUrl() + "messages", FastjsonUtils.toJson(models));
+                    String result = HttpUtil.doPost(sysConf.getMessageServiceUrl() + "messages", FastjsonUtils.toJson(models));
+                    System.out.println(result);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
@@ -57,6 +81,76 @@ public class MessageServiceImpl implements MessageService {
         return "true";
     }
 
+
+    /**
+     * 根据分页信息和过滤类型分页获取站内信
+     *
+     * @param params
+     * @param type
+     * @return
+     */
+    @Override
+    public Page<MessageModel> findByPageAndParams(PageParams params, String type) {
+        if (params == null) {
+            params = new PageParams();
+            params.setPage(1);
+            params.setCount(10);
+        } else {
+            int page = params.getPage();
+            if (page < 1) {
+                params.setPage(1);
+            }
+            int count = params.getCount();
+            if (count < 1) {
+                params.setCount(10);
+            }
+        }
+        Map<String, Object> map = new HashedMap();
+        map.put("consumerApp", ConsumerApp.MALL);
+        map.put("isRead", 0);
+        map.put("page", params.getPage());
+        map.put("count", params.getCount());
+        map.put("sorting", "{\"createTime\":\"DESC\"}");
+        User user = SystemSession.getUser();
+        if (user != null) {
+            map.put("receiverUu", user.getUserUU());
+            Enterprise enterprise = user.getEnterprise();
+            if (enterprise != null) {
+                map.put("receiverEnuu", enterprise.getUu());
+            }
+        }
+        try {
+            HttpUtil.Response response = HttpUtil.sendGetRequest(sysConf.getMessageServiceUrl() + "messages", map);
+            int statusCode = response.getStatusCode();
+            if (statusCode == HttpStatus.OK.value()) {
+                String text = response.getResponseText();
+                if (!StringUtils.isEmpty(text)) {
+                    JSONObject jsonObject = FastjsonUtils.fromJson(text, JSONObject.class);
+                    Page<MessageModel> messageModels = JsonObjectUtil.convertToMessageModelPage(jsonObject);
+                    for (MessageModel messageModel : messageModels) {
+                            if (messageModel.getSenderEnuu() != null) {
+                                Enterprise enterpriseInfo = enterpriseService.getEnterpriseInfo(messageModel.getSenderEnuu());
+                                if (enterpriseInfo != null) {
+                                    messageModel.setSendEnterpriseName(enterpriseInfo.getEnName());
+                                }
+                            }
+                        if (messageModel.getSenderUu() != null) {
+                            User userByUserUU = userService.findUserByUserUU(messageModel.getSenderUu());
+                            if (userByUserUU != null) {
+                                messageModel.setSendUserName(userByUserUU.getUserName());
+                            }
+                        }
+                    }
+                    return messageModels;
+                }
+            }
+            return new PageImpl<MessageModel>(Collections.<MessageModel>emptyList(), new PageInfo(params), 0);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new PageImpl<MessageModel>(Collections.<MessageModel>emptyList(), new PageInfo(params), 0);
+        }
+    }
+
     /**
      * 根据所给字段生成消息
      *
@@ -96,4 +190,89 @@ public class MessageServiceImpl implements MessageService {
             return model;
         }
     }
+
+    /**
+     * 获取未读消息数量
+     * @return
+     */
+    @Override
+    public Map<String, Object> getUnReadMessageCount() {
+        Map<String , Object> resultMap = new HashedMap();
+        resultMap.put("success", "success");
+        resultMap.put("count", "0");
+        User user = SystemSession.getUser();
+        Map<String, Object> map = new HashedMap();
+        if (user != null) {
+            map.put("receiverUu", user.getUserUU());
+            Enterprise enterprise = user.getEnterprise();
+            if (enterprise != null) {
+                map.put("receiverEnuu", enterprise.getUu());
+            }
+        }
+        map.put("consumerApp", ConsumerApp.MALL);
+        try {
+            HttpUtil.Response response = HttpUtil.sendGetRequest(sysConf.getMessageServiceUrl() + "messages/count/unread", map);
+            int statusCode = response.getStatusCode();
+            if (statusCode == HttpStatus.OK.value()) {
+                String responseText = response.getResponseText();
+                if (!StringUtils.isEmpty(responseText)) {
+                    resultMap = FastjsonUtils.fromJson(responseText, HashedMap.class);
+                    return resultMap;
+                } else {
+                    return resultMap;
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return resultMap;
+        }
+        return resultMap;
+        }
+
+    /**
+     * 短信
+     *
+     * @param messageId 消息的接口
+     * @return ModelMap
+     */
+    @Override
+    public ResultMap readMessage(Long messageId) {
+        Map<String, Object> resultMap = new HashedMap();
+        resultMap.put("success", "success");
+        resultMap.put("data", "");
+        Map<String, Object> map = new HashedMap();
+        if (messageId ==  null) {
+            return  new ResultMap(CodeType.NO_INFO, "未阅读任何信息");
+        } else {
+            map.put("messageId", messageId);
+        }
+        User user = SystemSession.getUser();
+        if (user != null) {
+            map.put("receiverUu", user.getUserUU());
+            Enterprise enterprise = user.getEnterprise();
+            if (enterprise != null) {
+                map.put("receiverEnuu", enterprise.getUu());
+            }
+        }
+        map.put("consumerApp", ConsumerApp.MALL);
+        String response = null;
+        try {
+            response = HttpUtil.doPost(sysConf.getMessageServiceUrl() + "messages/read", FastjsonUtils.toJson(map));
+            if (!StringUtils.isEmpty(response)) {
+                HashedMap hashedMap = FastjsonUtils.fromJson(response, HashedMap.class);
+                if (hashedMap != null) {
+                    Object success = hashedMap.get("success");
+                    if ("success".equals(success)) {
+                        return ResultMap.success(null);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ResultMap(CodeType.ERROR_STATE, "删除失败");
+        }
+        return new ResultMap(CodeType.ERROR_STATE, "删除失败");
+    }
+
+
 }

+ 71 - 0
src/main/java/com/uas/platform/b2c/common/psmessage/util/JsonObjectUtil.java

@@ -0,0 +1,71 @@
+package com.uas.platform.b2c.common.psmessage.util;
+
+import com.alibaba.fastjson.JSONObject;
+import com.uas.platform.b2c.common.message.model.MessageModel;
+import com.uas.platform.b2c.core.utils.FastjsonUtils;
+import com.uas.platform.core.model.PageInfo;
+import com.uas.platform.core.model.PageParams;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.util.StringUtils;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * json 转换成 page
+ *
+ * @author yuj 2018-07-11 21:23
+ */
+public class JsonObjectUtil {
+
+    /**
+     * jsonobject 转换成page
+     * @return
+     */
+    public static Page<Object> convertToPage(JSONObject jsonObject, Class cls) {
+//        String content  = String.valueOf(jsonObject.get("content"));
+//        if (!StringUtils.isEmpty(content)) {
+//            try {
+//                List<? extends Class> array = FastjsonUtils.fromJsonArray(content, cls.getClass());
+//                Object number = jsonObject.get("number");
+//                PageImpl<T> page = new PageImpl<T>(array);
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//                return new PageImpl<?>(Collections.<T>emptyList(), new PageInfo(new PageParams(1, 10, null, null)), 0);
+//            }
+//        }
+//        return new PageImpl<T>(Collections.<T>emptyList(), new PageInfo(new PageParams(1, 10, null, null)), 0);
+        //TODO
+        return null;
+    }
+
+
+    /**
+     * jsonobject 转换成page
+     * @return
+     */
+    public static Page<MessageModel> convertToMessageModelPage(JSONObject jsonObject) {
+        String content  = String.valueOf(jsonObject.get("content"));
+        if (!StringUtils.isEmpty(content)) {
+            try {
+                List<MessageModel> messageModels = FastjsonUtils.fromJsonArray(content, MessageModel.class);
+                PageParams pageParams = new PageParams();
+                Object number = jsonObject.get("number");
+                number = number  == null ? 1 : number;
+                pageParams.setPage((Integer) number);
+                Object size = jsonObject.get("size");
+                size = size == null ? 10 : size;
+                pageParams.setCount((Integer)size );
+                PageInfo info = new PageInfo(pageParams);
+                Object totalElements = jsonObject.get("totalElements");
+                totalElements = totalElements == null ? messageModels.size() : totalElements;
+                return new PageImpl<MessageModel>(messageModels, info, Long.valueOf(totalElements.toString()));
+            } catch (Exception e) {
+                e.printStackTrace();
+                return new PageImpl<MessageModel>(Collections.<MessageModel>emptyList(), new PageInfo(new PageParams(1, 10, null, null)), 0);
+            }
+        }
+        return new PageImpl<MessageModel>(Collections.<MessageModel>emptyList(), new PageInfo(new PageParams(1, 10, null, null)), 0);
+    }
+}

+ 6 - 3
src/main/java/com/uas/platform/b2c/trade/order/StringConstant/StringFormat.java

@@ -7,9 +7,12 @@ package com.uas.platform.b2c.trade.order.StringConstant;
  */
 public class StringFormat {
 
-    public static final String CONFIRMPAID = "订单/admin#/trade/order/%s  买家已付款成功,请确认是否收款";
+    /**
+     * 订单/admin#/trade/order/%s  买家已付款成功,请确认是否收款
+     */
+    public static final String CONFIRMPAID = "订单 %s  买家已付款成功,请确认是否收款";
 
-    public static final String APPLYPAID = "订单/admin#/trade/order/%s 买家已确认收货,请尽快申请付款";
+    public static final String APPLYPAID = "订单 %s 买家已确认收货,请尽快申请付款";
 
-    public static final String FINANCIAL_SETTLEMENT = "订单/admin#/trade/order/%s 买家已确认收货,请尽快申请付款";
+    public static final String FINANCIAL_SETTLEMENT = "订单 %s 买家已确认收货,请尽快申请付款";
 }

+ 1 - 2
src/main/java/com/uas/platform/b2c/trade/order/service/impl/OrderServiceImpl.java

@@ -21,7 +21,6 @@ import com.uas.platform.b2c.core.constant.SplitChar;
 import com.uas.platform.b2c.core.support.SystemSession;
 import com.uas.platform.b2c.core.support.log.TradeBufferedLogger;
 import com.uas.platform.b2c.core.utils.DoubleArith;
-import com.uas.platform.b2c.core.utils.EncryptionFilter;
 import com.uas.platform.b2c.core.utils.FastjsonUtils;
 import com.uas.platform.b2c.core.utils.NumberUtil;
 import com.uas.platform.b2c.fa.payment.dao.BankTransferDao;
@@ -3482,7 +3481,7 @@ public class OrderServiceImpl implements OrderService {
                     continue;
                 }
                 if (useruu != null) {
-                    model = messageService.initMessage(String.format(StringFormat.CONFIRMPAID, EncryptionFilter.encode(order.getOrderid())), com.uas.platform.b2c.trade.order.type.MessageType.CONFIRM_PAID.getType(), useruu, sysConf.getEnUU(), ConsumerType.SINGLE, ConsumerApp.MALL, SMSType.DONT_SEND);
+                    model = messageService.initMessage(String.format(StringFormat.CONFIRMPAID, order.getOrderid()), com.uas.platform.b2c.trade.order.type.MessageType.CONFIRM_PAID.getType(), useruu, sysConf.getEnUU(), ConsumerType.SINGLE, ConsumerApp.MALL, SMSType.DONT_SEND);
                     if (model != null) {
                         list.add(model);
                     }