diff --git a/src/main/java/com/sxrekord/chatting/controller/MessageController.java b/src/main/java/com/sxrekord/chatting/controller/MessageController.java index 042d73f..9a83d40 100644 --- a/src/main/java/com/sxrekord/chatting/controller/MessageController.java +++ b/src/main/java/com/sxrekord/chatting/controller/MessageController.java @@ -3,9 +3,13 @@ import com.sxrekord.chatting.model.vo.ResponseJson; import com.sxrekord.chatting.service.MessageService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpSession; +import java.util.Date; + /** * @author Rekord @@ -29,4 +33,13 @@ public ResponseJson getFriendMessage(@RequestParam Long fromId, public ResponseJson getGroupMessage(@RequestParam Long groupId) { return messageService.getGroupMessage(groupId); } + + @PostMapping(value = "load") + @ResponseBody + public ResponseJson load(@RequestParam("type") Integer type, + @RequestParam("updateTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date updateTime, + @RequestParam("toId") Long toId, @RequestParam("count") Integer count, + HttpSession session) { + return messageService.loadMessage(type, updateTime, toId, count, session); + } } diff --git a/src/main/java/com/sxrekord/chatting/dao/MessageDao.java b/src/main/java/com/sxrekord/chatting/dao/MessageDao.java index 9eaffbc..f6ae0ad 100644 --- a/src/main/java/com/sxrekord/chatting/dao/MessageDao.java +++ b/src/main/java/com/sxrekord/chatting/dao/MessageDao.java @@ -4,6 +4,7 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.Date; import java.util.List; /** @@ -33,4 +34,16 @@ public interface MessageDao { * @return */ int insertMessage(Message message); + + /** + * 列出消息 + * @param type + * @param updateTime + * @param fromId + * @param toId + * @param count + * @return + */ + List listMessage(@Param("type") Integer type, @Param("updateTime") Date updateTime, + @Param("fromId") Long fromId, @Param("toId") Long toId, @Param("count") Integer count); } diff --git a/src/main/java/com/sxrekord/chatting/model/po/Message.java b/src/main/java/com/sxrekord/chatting/model/po/Message.java index 5ac3478..7e4c803 100644 --- a/src/main/java/com/sxrekord/chatting/model/po/Message.java +++ b/src/main/java/com/sxrekord/chatting/model/po/Message.java @@ -23,6 +23,9 @@ public class Message { @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTime; + public Message() {} public Message(Long fromId, Long toId, Integer type, Integer contentType, Long contentId) { diff --git a/src/main/java/com/sxrekord/chatting/service/MessageService.java b/src/main/java/com/sxrekord/chatting/service/MessageService.java index 1af0fe2..be7dcd7 100644 --- a/src/main/java/com/sxrekord/chatting/service/MessageService.java +++ b/src/main/java/com/sxrekord/chatting/service/MessageService.java @@ -2,6 +2,9 @@ import com.sxrekord.chatting.model.vo.ResponseJson; +import javax.servlet.http.HttpSession; +import java.util.Date; + /** * @author Rekord * @date 2022/9/13 21:21 @@ -21,4 +24,15 @@ public interface MessageService { * @return */ ResponseJson getGroupMessage(Long groupId); + + /** + * 加载历史消息 + * @param type + * @param updateTime + * @param toId + * @param count + * @param session + * @return + */ + ResponseJson loadMessage(Integer type, Date updateTime, Long toId, Integer count, HttpSession session); } diff --git a/src/main/java/com/sxrekord/chatting/service/impl/MessageServiceImpl.java b/src/main/java/com/sxrekord/chatting/service/impl/MessageServiceImpl.java index 2dcd1dd..842479f 100644 --- a/src/main/java/com/sxrekord/chatting/service/impl/MessageServiceImpl.java +++ b/src/main/java/com/sxrekord/chatting/service/impl/MessageServiceImpl.java @@ -10,9 +10,12 @@ import com.sxrekord.chatting.model.vo.ResponseJson; import com.sxrekord.chatting.service.MessageService; import com.sxrekord.chatting.util.ChatType; +import com.sxrekord.chatting.util.Constant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpSession; +import java.util.Date; import java.util.List; /** @@ -91,4 +94,13 @@ public ResponseJson getGroupMessage(Long groupId) { } return responseJson.success(); } + + @Override + public ResponseJson loadMessage(Integer type, Date updateTime, Long toId, Integer count, HttpSession session) { + ResponseJson responseJson = new ResponseJson(); + + responseJson.setData("messages", + this.messageDao.listMessage(type, updateTime, (long)session.getAttribute(Constant.USER_TOKEN), toId, count)); + return responseJson.success(); + } } diff --git a/src/main/resources/mapper/message_mapper.xml b/src/main/resources/mapper/message_mapper.xml index eb5a4c3..f55f3e7 100644 --- a/src/main/resources/mapper/message_mapper.xml +++ b/src/main/resources/mapper/message_mapper.xml @@ -5,7 +5,7 @@ - from_id, content_type, content_id + `from_id`, `content_type`, `content_id`, `update_time` @@ -20,6 +20,7 @@ + + select `from_id`, `type`, `content_type`, `update_time` from `message` + where type = #{type} and `update_time` < #{updateTime} and + (from_id = #{fromId} and to_id = #{toId} + + or from_id = #{toId} and to_id = #{fromId} + + ) + order by update_time + limit 0, #{count}; + + \ No newline at end of file