Browse Source

消息服务提供创建消息的api

nnkwrik 6 years ago
parent
commit
3b4871f01d

+ 49 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/controller/ImServiceController.java

@@ -0,0 +1,49 @@
+package io.github.nnkwrik.imservice.controller;
+
+import io.github.nnkwrik.common.dto.Response;
+import io.github.nnkwrik.imservice.dao.ChatMapper;
+import io.github.nnkwrik.imservice.model.po.Chat;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/08 18:51
+ */
+@RestController
+@Slf4j
+@RequestMapping("/chat-service")
+public class ImServiceController {
+
+    @Autowired
+    private ChatMapper chatMapper;
+
+    @PostMapping("/createChat/{goodsId}/{senderId}/{receiverId}")
+    public Response<Integer> createChat(@PathVariable("goodsId") int goodsId,
+                                        @PathVariable("senderId") String senderId,
+                                        @PathVariable("receiverId") String receiverId) {
+        //u1 < u2
+        Chat chat = new Chat();
+        chat.setGoodsId(goodsId);
+        if (senderId.compareTo(receiverId) < 0) {
+            chat.setU1(senderId);
+            chat.setU2(receiverId);
+            chat.setShowToU1(true);
+            chat.setShowToU1(false);
+        } else {
+            chat.setU1(receiverId);
+            chat.setU2(senderId);
+            chat.setShowToU1(false);
+            chat.setShowToU1(true);
+        }
+        chatMapper.addChat(chat);
+
+        log.info("创建聊天chatId={},发起人id={},接收方id={}", chat.getId(), senderId, receiverId);
+        return Response.ok(chat.getId());
+
+    }
+}

+ 8 - 6
im-service/src/main/java/io/github/nnkwrik/imservice/dao/ChatMapper.java

@@ -1,10 +1,7 @@
 package io.github.nnkwrik.imservice.dao;
 
 import io.github.nnkwrik.imservice.model.po.Chat;
-import org.apache.ibatis.annotations.Mapper;
-import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.annotations.Select;
-import org.apache.ibatis.annotations.SelectKey;
+import org.apache.ibatis.annotations.*;
 
 /**
  * @author nnkwrik
@@ -13,11 +10,16 @@ import org.apache.ibatis.annotations.SelectKey;
 @Mapper
 public interface ChatMapper {
 
-    @Select("insert into chat (u1, u2, goods_id)\n" +
-            "values (#{u1}, #{u2}, #{goodsId})")
+    @Select("insert into chat (u1, u2, goods_id, show_to_u1, show_to_u2)\n" +
+            "values (#{u1}, #{u2}, #{goodsId}, #{showToU1}, #{showToU2})")
     @SelectKey(resultType = Integer.class, before = false, keyProperty = "id", statement = "SELECT LAST_INSERT_ID()")
     void addChat(Chat chat);
 
+    @Update("update chat\n" +
+            "set show_to_u1 = true , show_to_u2 = true\n" +
+            "where id = #{chat_id}")
+    void showToBoth(@Param("chat_id") int chatId);
+
     @Select("select u1,u2,goods_id from chat where id = #{id}")
     Chat getChatById(@Param("id") int id);
 

+ 2 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/dao/HistoryMapper.java

@@ -16,6 +16,7 @@ import java.util.List;
 public interface HistoryMapper {
     /**
      * 添加一条聊天记录
+     *
      * @param history
      */
     @Select("insert into history (chat_id, u1_to_u2, message_type, message_body, send_time)\n" +
@@ -66,6 +67,7 @@ public interface HistoryMapper {
 
     /**
      * 根据chatId获取聊天记录
+     *
      * @param chat_id
      * @return
      */

+ 6 - 0
im-service/src/main/java/io/github/nnkwrik/imservice/service/impl/WebSocketServiceImpl.java

@@ -3,6 +3,7 @@ package io.github.nnkwrik.imservice.service.impl;
 import io.github.nnkwrik.common.dto.Response;
 import io.github.nnkwrik.common.exception.GlobalException;
 import io.github.nnkwrik.common.util.JsonUtil;
+import io.github.nnkwrik.imservice.constant.MessageType;
 import io.github.nnkwrik.imservice.dao.ChatMapper;
 import io.github.nnkwrik.imservice.dao.HistoryMapper;
 import io.github.nnkwrik.imservice.model.po.History;
@@ -100,6 +101,11 @@ public class WebSocketServiceImpl implements WebSocketService {
             u2 = message.getReceiverId();
         }
 
+        if (message.getMessageType() == MessageType.ESTABLISH_CHAT) {
+            //首次发送,设为双方可见
+            chatMapper.showToBoth(message.getChatId());
+        }
+
         //添加聊天记录
         History history = new History();
         BeanUtils.copyProperties(message, history);

+ 21 - 0
inner-api/src/main/java/fangxianyu/innerApi/im/ImClient.java

@@ -0,0 +1,21 @@
+package fangxianyu.innerApi.im;
+
+import io.github.nnkwrik.common.dto.Response;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/08 19:23
+ */
+@FeignClient(name = "chat-service")
+@RequestMapping("/chat-service")
+public interface ImClient {
+
+    @PostMapping("/createChat/{goodsId}/{senderId}/{receiverId}")
+    public Response<Integer> createChat(@PathVariable("goodsId") int goodsId,
+                                        @PathVariable("senderId") String senderId,
+                                        @PathVariable("receiverId") String receiverId);
+}

+ 27 - 0
inner-api/src/main/java/fangxianyu/innerApi/im/ImClientHandler.java

@@ -0,0 +1,27 @@
+package fangxianyu.innerApi.im;
+
+import io.github.nnkwrik.common.dto.Response;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author nnkwrik
+ * @date 18/12/08 19:23
+ */
+@Component
+@Slf4j
+public class ImClientHandler {
+    @Autowired
+    private ImClient imClient;
+
+    public Integer createChat(int goodsId, String senderId, String receiverId) {
+        log.info("让消息服务创建消息");
+        Response<Integer> response = imClient.createChat(goodsId, senderId, receiverId);
+        if (response.getErrno() != 0) {
+            log.info("消息服务创建消息失败,errno={},原因={}", response.getErrno(), response.getErrmsg());
+            return null;
+        }
+        return response.getData();
+    }
+}