xuqm 1 rok temu
rodzic
commit
1cae280c4d

+ 12 - 0
src/main/java/cn/org/bjca/trust/java/imserver/bean/group/MsgHistoryForCountData.java

@@ -0,0 +1,12 @@
+package cn.org.bjca.trust.java.imserver.bean.group;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class MsgHistoryForCountData {
+    private String userId;
+    private long timestamp;
+    private int size;
+}

+ 5 - 6
src/main/java/cn/org/bjca/trust/java/imserver/controller/message/v1/MessageV1Controller.java

@@ -2,21 +2,20 @@ package cn.org.bjca.trust.java.imserver.controller.message.v1;
 
 import cn.org.bjca.trust.java.imserver.HttpResult;
 import cn.org.bjca.trust.java.imserver.bean.group.GroupCreateData;
-import cn.org.bjca.trust.java.imserver.common.json.GsonImplHelp;
+import cn.org.bjca.trust.java.imserver.bean.group.MsgHistoryForCountData;
 import cn.org.bjca.trust.java.imserver.request.RequestManager;
 import org.springframework.http.HttpHeaders;
 import org.springframework.web.bind.annotation.*;
 
-import static cn.org.bjca.trust.java.imserver.request.RequestConstant.Group_create_v1;
+import static cn.org.bjca.trust.java.imserver.request.RequestConstant.MsgHistoryForCount;
 
 @RestController
 @RequestMapping("message/v1")
 public class MessageV1Controller {
 
-    @PostMapping("/getHistory")
-    public HttpResult<String> create(@RequestBody GroupCreateData groupCreateData, @RequestHeader HttpHeaders headers) throws Exception {
-        System.out.println(GsonImplHelp.get().toJson(groupCreateData));
-        return RequestManager.getInstance().request(Group_create_v1, groupCreateData, headers);
+    @PostMapping("/getHistoryForCount")
+    public HttpResult<String> getHistoryForCount(@RequestBody MsgHistoryForCountData groupCreateData, @RequestHeader HttpHeaders headers) throws Exception {
+        return RequestManager.getInstance().request(MsgHistoryForCount, groupCreateData, headers);
     }
 
 }

+ 8 - 8
src/main/java/cn/org/bjca/trust/java/imserver/im/ImManager.java

@@ -50,17 +50,17 @@ public class ImManager {
 
         try {
             mqttClient = new MqttClient("tcp://114.115.203.60:18883",
-                    "server" + System.currentTimeMillis(), new MemoryPersistence());
+                    "server1" + System.currentTimeMillis(), new MemoryPersistence());
             mqttClient.setCallback(new MqttCallbackExtended() {
                 @Override
                 public void connectComplete(boolean reconnect, String serverURI) {
                     System.out.println("======>connectComplete:reconnect?" + reconnect + "::" + serverURI);
                     if (reconnect) return;
                     try {
-                        mqttClient.subscribe("server" + PacketType.CONNECT, 2);
-                        mqttClient.subscribe("server" + PacketType.SEND, 2);
-                        mqttClient.subscribe("server" + PacketType.PING, 2);
-                        mqttClient.subscribe("server" + PacketType.REQUEST, 2);
+                        mqttClient.subscribe("server1" + PacketType.CONNECT, 2);
+                        mqttClient.subscribe("server1" + PacketType.SEND, 2);
+                        mqttClient.subscribe("server1" + PacketType.PING, 2);
+                        mqttClient.subscribe("server1" + PacketType.REQUEST, 2);
                     } catch (MqttException ignored) {
                     }
                 }
@@ -74,9 +74,9 @@ public class ImManager {
                 public void messageArrived(String topic, MqttMessage message) throws Exception {
                     String msg = new String(message.getPayload());
                     System.out.println("======>messageArrived:\n" + topic + "::" + msg);
-                    if (topic.equals("server" + PacketType.CONNECT)) {
+                    if (topic.equals("server1" + PacketType.CONNECT)) {
                         sendConnectedMsg(GsonImplHelp.get().toObject(msg, ConnectMessage.class));
-                    } else if (topic.equals("server" + PacketType.SEND)) {
+                    } else if (topic.equals("server1" + PacketType.SEND)) {
                         SZYXMessage szyxMessage = GsonImplHelp.get().toObject(msg, SZYXMessage.class);
                         szyxMessage.setStatus(0);
                         szyxMessage.setFromClientId(szyxMessage.getFromClient().getUserId());
@@ -101,7 +101,7 @@ public class ImManager {
                         sendAckMsg(szyxMessage);
 
                     }// 数据请求
-                    else if (topic.equals("server" + PacketType.REQUEST)) {
+                    else if (topic.equals("server1" + PacketType.REQUEST)) {
                         sendPacketMessage(PacketType.RESULT, RequestManager.getInstance().request(msg));
                     }
 

+ 8 - 1
src/main/java/cn/org/bjca/trust/java/imserver/repository/MessageRepository.java

@@ -1,7 +1,9 @@
 package cn.org.bjca.trust.java.imserver.repository;
 
 import cn.org.bjca.trust.java.imserver.im.msg.msg.SZYXMessage;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
 
 import java.util.List;
@@ -9,5 +11,10 @@ import java.util.List;
 @Repository
 public interface MessageRepository extends JpaRepository<SZYXMessage, Long> {
 
-    List<SZYXMessage> getAllByAppIdAndUserName(String appId, String userName);
+
+    @Query("select msg from SZYXMessage as msg  " +
+            "where msg.appId = ?1 and msg.userName = ?2 and (msg.fromClientId=?3 or msg.groupID=?3 or msg.toClientId=?3)" +
+            "and msg.timestamp<?4 order by msg._id desc ")
+    List<SZYXMessage> getHistoryForCount(String appId, String userName, String userId, long timestamp, Pageable pageable);
+
 }

+ 6 - 0
src/main/java/cn/org/bjca/trust/java/imserver/request/RequestConstant.java

@@ -2,4 +2,10 @@ package cn.org.bjca.trust.java.imserver.request;
 
 public class RequestConstant {
     public static final String Group_create_v1 = "/group/v1/create";
+
+
+    public static final String MsgHistoryForTime = "/msg/v1/getHistoryForTime";
+    public static final String MsgHistoryForCount = "/message/v1/getHistoryForCount";
+    public static final String MsgListForTime = "/message/v1/getListForTime";
+    public static final String MsgListForCount = "/message/v1/getListForCount";
 }

+ 24 - 1
src/main/java/cn/org/bjca/trust/java/imserver/request/RequestManager.java

@@ -2,21 +2,29 @@ package cn.org.bjca.trust.java.imserver.request;
 
 import cn.org.bjca.trust.java.imserver.HttpResult;
 import cn.org.bjca.trust.java.imserver.bean.group.GroupCreateData;
+import cn.org.bjca.trust.java.imserver.bean.group.MsgHistoryForCountData;
 import cn.org.bjca.trust.java.imserver.common.SpringUtilsAuTo;
 import cn.org.bjca.trust.java.imserver.common.TimeHelper;
 import cn.org.bjca.trust.java.imserver.common.json.GsonImplHelp;
 import cn.org.bjca.trust.java.imserver.entitys.GroupEntity;
 import cn.org.bjca.trust.java.imserver.im.msg.RequestMessage;
 import cn.org.bjca.trust.java.imserver.im.msg.ResultMessage;
+import cn.org.bjca.trust.java.imserver.im.msg.msg.SZYXMessage;
 import cn.org.bjca.trust.java.imserver.repository.GroupRepository;
 import cn.org.bjca.trust.java.imserver.repository.MessageRepository;
 import cn.org.bjca.trust.java.imserver.repository.UserRepository;
 import cn.org.bjca.trust.java.imserver.repository.tenant.ApplicationRepository;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
 import org.springframework.http.HttpHeaders;
 
+import java.util.Comparator;
+import java.util.List;
 import java.util.UUID;
 
 import static cn.org.bjca.trust.java.imserver.request.RequestConstant.Group_create_v1;
+import static cn.org.bjca.trust.java.imserver.request.RequestConstant.MsgHistoryForCount;
 
 public class RequestManager {
 
@@ -70,8 +78,12 @@ public class RequestManager {
                     return (HttpResult<T>) this.groupCreate(data instanceof String ?
                             GsonImplHelp.get().toObject((String) data, GroupCreateData.class)
                             : (GroupCreateData) data, appId, userId);
+                case MsgHistoryForCount:
+                    return (HttpResult<T>) this.getHistoryForCount(data instanceof String ?
+                            GsonImplHelp.get().toObject((String) data, MsgHistoryForCountData.class)
+                            : (MsgHistoryForCountData) data, appId, userId);
                 default:
-                    return new HttpResult<>(201, "路径错误", null);
+                    return new HttpResult<>(201, "路径错误" + path, null);
             }
 
         } else return new HttpResult<>(201, "参数错误", null);
@@ -79,6 +91,17 @@ public class RequestManager {
     }
 
 
+    private HttpResult<List<SZYXMessage>> getHistoryForCount(MsgHistoryForCountData data, String appid, String userid) {
+
+        Pageable pageable = PageRequest.of(0, 10, Sort.by("_id").descending());
+
+        List<SZYXMessage> l = messageRepository.getHistoryForCount(appid, userid, data.getUserId(), data.getTimestamp(), pageable);
+        l.sort((o1, o2) -> (int) (o1.get_id() - o2.get_id()));
+
+
+        return new HttpResult<>(200, "查询成功", l);
+    }
+
     private HttpResult<String> groupCreate(GroupCreateData data, String appid, String userid) {
         if (null == data.getUserIdList() || data.getUserIdList().isEmpty() || data.getUserIdList().size() < 2) {
             return new HttpResult<>(201, "群组成员需要大于一", "");