Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Commit

Permalink
feat: implement /message/load interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Crazyokd committed Mar 11, 2023
1 parent 508b164 commit 088bd39
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/sxrekord/chatting/dao/MessageDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -33,4 +34,16 @@ public interface MessageDao {
* @return
*/
int insertMessage(Message message);

/**
* 列出消息
* @param type
* @param updateTime
* @param fromId
* @param toId
* @param count
* @return
*/
List<Message> listMessage(@Param("type") Integer type, @Param("updateTime") Date updateTime,
@Param("fromId") Long fromId, @Param("toId") Long toId, @Param("count") Integer count);
}
3 changes: 3 additions & 0 deletions src/main/java/com/sxrekord/chatting/model/po/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/sxrekord/chatting/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}
}
15 changes: 14 additions & 1 deletion src/main/resources/mapper/message_mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<mapper namespace="com.sxrekord.chatting.dao.MessageDao">
<sql id="selectFields">
from_id, content_type, content_id
`from_id`, `content_type`, `content_id`, `update_time`
</sql>

<sql id="insertFields">
Expand All @@ -20,6 +20,7 @@
<result column="content_type" jdbcType="INTEGER" property="contentType"/>
<result column="content_id" jdbcType="BIGINT" property="contentId"/>
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
</resultMap>

<select id="getMessageByFromIdAndToId" resultMap="MessageDao">
Expand All @@ -40,4 +41,16 @@
values(#{fromId}, #{toId}, #{type}, #{contentType}, #{contentId})
</insert>

<select id="listMessage" resultMap="MessageDao">
select `from_id`, `type`, `content_type`, `update_time` from `message`
where type = #{type} and `update_time` &lt; #{updateTime} and
(from_id = #{fromId} and to_id = #{toId}
<if test="type == 0">
or from_id = #{toId} and to_id = #{fromId}
</if>
)
order by update_time
limit 0, #{count};
</select>

</mapper>

0 comments on commit 088bd39

Please sign in to comment.