对话相关上传
Change-Id: I4eb3223e650cd40905f51691e1d5d3204d981693
diff --git a/src/main/java/com/pt5/pthouduan/controller/ChatInformationController.java b/src/main/java/com/pt5/pthouduan/controller/ChatInformationController.java
new file mode 100644
index 0000000..81756a6
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/controller/ChatInformationController.java
@@ -0,0 +1,58 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.ChatInformation;
+import com.pt5.pthouduan.service.ChatInformationService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.stereotype.Controller;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 聊天记录前端控制器
+ * </p>
+ *
+ * 功能:添加聊天记录、删除聊天记录、查询用户与所有人的聊天、查询两个用户之间的聊天
+ *
+ * @author 杨蔓
+ * @since 2025-06-03
+ */
+@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:3000"})
+@Controller
+@RequestMapping("/chat")
+public class ChatInformationController {
+
+ @Autowired
+ private ChatInformationService chatInformationService;
+
+ // 创建聊天记录
+ @PostMapping("/create")
+ @ResponseBody
+ public ChatInformation createChat(@RequestBody ChatInformation chatInformation) {
+ return chatInformationService.createChat(chatInformation);
+ }
+
+ // 删除聊天记录(根据信息ID)
+ @DeleteMapping("/delete/{informationid}")
+ @ResponseBody
+ public String deleteChat(@PathVariable Integer informationid) {
+ boolean deleted = chatInformationService.deleteChatById(informationid);
+ return deleted ? "删除成功" : "删除失败";
+ }
+
+ // 获取某个用户与所有人的聊天记录(作为发送者或接收者)
+ @GetMapping("/user/{userId}")
+ @ResponseBody
+ public List<ChatInformation> getChatsByUser(@PathVariable Long userId) {
+ return chatInformationService.getChatsByUser(userId);
+ }
+
+ // 获取两个用户之间的聊天记录
+ @GetMapping("/between")
+ @ResponseBody
+ public List<ChatInformation> getChatsBetweenUsers(@RequestParam Long user1,
+ @RequestParam Long user2) {
+ return chatInformationService.getChatsBetweenUsers(user1, user2);
+ }
+}
diff --git a/src/main/java/com/pt5/pthouduan/entity/ChatInformation.java b/src/main/java/com/pt5/pthouduan/entity/ChatInformation.java
index 03ed650..0659d73 100644
--- a/src/main/java/com/pt5/pthouduan/entity/ChatInformation.java
+++ b/src/main/java/com/pt5/pthouduan/entity/ChatInformation.java
@@ -1,46 +1,83 @@
package com.pt5.pthouduan.entity;
+import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
+
import java.io.Serializable;
+import java.time.LocalDateTime;
/**
* <p>
- *
+ * 聊天信息表
* </p>
*
- * @author ljx
- * @since 2025-04-14
+ * @author ym
+ * @since 2025-06-03
*/
@TableName("chat_information")
public class ChatInformation implements Serializable {
private static final long serialVersionUID = 1L;
- private Long userid;
+ @TableId("informationid")
+ private Integer informationid;
- private String chatimformation;
+ private Long senderId;
- public Long getUserid() {
- return userid;
+ private Long receiverId;
+
+ private String content;
+
+ private LocalDateTime talkTime;
+
+ public Integer getInformationid() {
+ return informationid;
}
- public void setUserid(Long userid) {
- this.userid = userid;
+ public void setInformationid(Integer informationid) {
+ this.informationid = informationid;
}
- public String getChatimformation() {
- return chatimformation;
+ public Long getSenderId() {
+ return senderId;
}
- public void setChatimformation(String chatimformation) {
- this.chatimformation = chatimformation;
+ public void setSenderId(Long senderId) {
+ this.senderId = senderId;
+ }
+
+ public Long getReceiverId() {
+ return receiverId;
+ }
+
+ public void setReceiverId(Long receiverId) {
+ this.receiverId = receiverId;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+ public LocalDateTime getTalkTime() {
+ return talkTime;
+ }
+
+ public void setTalkTime(LocalDateTime talkTime) {
+ this.talkTime = talkTime;
}
@Override
public String toString() {
return "ChatInformation{" +
- "userid = " + userid +
- ", chatimformation = " + chatimformation +
- "}";
+ "informationid=" + informationid +
+ ", senderId=" + senderId +
+ ", receiverId=" + receiverId +
+ ", content='" + content + '\'' +
+ ", talkTime=" + talkTime +
+ '}';
}
}
diff --git a/src/main/java/com/pt5/pthouduan/mapper/ChatInformationMapper.java b/src/main/java/com/pt5/pthouduan/mapper/ChatInformationMapper.java
index f410562..54e748a 100644
--- a/src/main/java/com/pt5/pthouduan/mapper/ChatInformationMapper.java
+++ b/src/main/java/com/pt5/pthouduan/mapper/ChatInformationMapper.java
@@ -1,18 +1,34 @@
package com.pt5.pthouduan.mapper;
-import com.pt5.pthouduan.entity.ChatInformation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.pt5.pthouduan.entity.ChatInformation;
import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
/**
* <p>
- * Mapper 接口
+ * 聊天记录 Mapper 接口
* </p>
*
- * @author ljx
- * @since 2025-04-14
+ * 功能:添加、删除、查询(按用户ID、用户对)
+ *
+ * @author ym
+ * @since 2025-06-03
*/
@Mapper
public interface ChatInformationMapper extends BaseMapper<ChatInformation> {
+ // 创建聊天记录
+ void insertChat(ChatInformation chatInformation);
+
+ // 根据信息ID删除聊天记录
+ int deleteChatById(@Param("informationid") Integer informationid);
+
+ // 获取某个用户参与的所有聊天记录(作为发送方或接收方)
+ List<ChatInformation> selectChatsByUser(@Param("userId") Long userId);
+
+ // 获取两个用户之间的聊天记录(无论谁是发送方或接收方)
+ List<ChatInformation> selectChatsBetweenUsers(@Param("user1") Long user1, @Param("user2") Long user2);
}
diff --git a/src/main/java/com/pt5/pthouduan/service/ChatInformationService.java b/src/main/java/com/pt5/pthouduan/service/ChatInformationService.java
new file mode 100644
index 0000000..67613f4
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/ChatInformationService.java
@@ -0,0 +1,30 @@
+package com.pt5.pthouduan.service;
+
+import com.pt5.pthouduan.entity.ChatInformation;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 聊天记录服务接口
+ * </p>
+ *
+ * 功能:添加聊天记录、删除聊天记录、查询用户与所有人的聊天、查询两个用户之间的聊天
+ *
+ * @author ym
+ * @since 2025-06-03
+ */
+public interface ChatInformationService {
+
+ // 创建聊天记录
+ ChatInformation createChat(ChatInformation chatInformation);
+
+ // 删除聊天记录
+ boolean deleteChatById(Integer informationid);
+
+ // 获取某个用户与所有人的聊天记录
+ List<ChatInformation> getChatsByUser(Long userId);
+
+ // 获取两个用户之间的聊天记录
+ List<ChatInformation> getChatsBetweenUsers(Long user1, Long user2);
+}
diff --git a/src/main/java/com/pt5/pthouduan/service/impl/ChatInformationServiceImpl.java b/src/main/java/com/pt5/pthouduan/service/impl/ChatInformationServiceImpl.java
new file mode 100644
index 0000000..a4f84bb
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/impl/ChatInformationServiceImpl.java
@@ -0,0 +1,47 @@
+package com.pt5.pthouduan.service.impl;
+
+import com.pt5.pthouduan.entity.ChatInformation;
+import com.pt5.pthouduan.mapper.ChatInformationMapper;
+import com.pt5.pthouduan.service.ChatInformationService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 聊天记录服务实现类
+ * </p>
+ *
+ * 实现了聊天记录的增删查逻辑
+ *
+ * @author ym
+ * @since 2025-06-03
+ */
+@Service
+public class ChatInformationServiceImpl implements ChatInformationService {
+
+ @Autowired
+ private ChatInformationMapper chatInformationMapper;
+
+ @Override
+ public ChatInformation createChat(ChatInformation chatInformation) {
+ chatInformationMapper.insertChat(chatInformation);
+ return chatInformation;
+ }
+
+ @Override
+ public boolean deleteChatById(Integer informationid) {
+ return chatInformationMapper.deleteChatById(informationid) > 0;
+ }
+
+ @Override
+ public List<ChatInformation> getChatsByUser(Long userId) {
+ return chatInformationMapper.selectChatsByUser(userId);
+ }
+
+ @Override
+ public List<ChatInformation> getChatsBetweenUsers(Long user1, Long user2) {
+ return chatInformationMapper.selectChatsBetweenUsers(user1, user2);
+ }
+}
diff --git a/src/main/resources/mapper/ChatInformationMapper.xml b/src/main/resources/mapper/ChatInformationMapper.xml
new file mode 100644
index 0000000..d9aac6f
--- /dev/null
+++ b/src/main/resources/mapper/ChatInformationMapper.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.pt5.pthouduan.mapper.ChatInformationMapper">
+
+ <!-- 插入聊天记录 -->
+ <insert id="insertChat" parameterType="com.pt5.pthouduan.entity.ChatInformation" useGeneratedKeys="true" keyProperty="informationid">
+ INSERT INTO chat_information (sender_id, receiver_id, content)
+ VALUES (#{senderId}, #{receiverId}, #{content})
+ </insert>
+
+ <!-- 删除聊天记录 -->
+ <delete id="deleteChatById" parameterType="int">
+ DELETE FROM chat_information WHERE informationid = #{value}
+ </delete>
+
+ <!-- 查询某用户参与的所有聊天记录 -->
+ <select id="selectChatsByUser" parameterType="long" resultType="com.pt5.pthouduan.entity.ChatInformation">
+ SELECT * FROM chat_information
+ WHERE sender_id = #{userId} OR receiver_id = #{userId}
+ ORDER BY talk_time ASC
+ </select>
+
+ <!-- 查询两个用户之间的聊天记录 -->
+ <select id="selectChatsBetweenUsers" parameterType="map" resultType="com.pt5.pthouduan.entity.ChatInformation">
+ SELECT * FROM chat_information
+ WHERE (sender_id = #{user1} AND receiver_id = #{user2})
+ OR (sender_id = #{user2} AND receiver_id = #{user1})
+ ORDER BY talk_time ASC
+ </select>
+
+</mapper>
diff --git a/src/test/java/com/pt5/pthouduan/controller/ChatInformationControllerTest.java b/src/test/java/com/pt5/pthouduan/controller/ChatInformationControllerTest.java
new file mode 100644
index 0000000..f78c88d
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/controller/ChatInformationControllerTest.java
@@ -0,0 +1,103 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.ChatInformation;
+import com.pt5.pthouduan.service.ChatInformationService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
+
+class ChatInformationControllerTest {
+
+ @Mock
+ private ChatInformationService chatInformationService;
+
+ @InjectMocks
+ private ChatInformationController chatInformationController;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void createChat_ShouldReturnSavedChat() {
+ ChatInformation chat = new ChatInformation();
+ chat.setSenderId(1L);
+ chat.setReceiverId(2L);
+ chat.setContent("Hello");
+ chat.setTalkTime(LocalDateTime.now());
+
+ when(chatInformationService.createChat(any(ChatInformation.class))).thenReturn(chat);
+
+ ChatInformation result = chatInformationController.createChat(chat);
+
+ assertNotNull(result);
+ assertEquals("Hello", result.getContent());
+ verify(chatInformationService).createChat(any(ChatInformation.class));
+ }
+
+ @Test
+ void deleteChat_ShouldReturnSuccessMessage_WhenDeleted() {
+ when(chatInformationService.deleteChatById(1)).thenReturn(true);
+
+ String result = chatInformationController.deleteChat(1);
+
+ assertEquals("删除成功", result);
+ verify(chatInformationService).deleteChatById(1);
+ }
+
+ @Test
+ void deleteChat_ShouldReturnFailureMessage_WhenNotDeleted() {
+ when(chatInformationService.deleteChatById(99)).thenReturn(false);
+
+ String result = chatInformationController.deleteChat(99);
+
+ assertEquals("删除失败", result);
+ verify(chatInformationService).deleteChatById(99);
+ }
+
+ @Test
+ void getChatsByUser_ShouldReturnChatList() {
+ ChatInformation chat1 = new ChatInformation();
+ chat1.setSenderId(1L);
+ chat1.setReceiverId(2L);
+ chat1.setContent("Hi");
+
+ ChatInformation chat2 = new ChatInformation();
+ chat2.setSenderId(2L);
+ chat2.setReceiverId(1L);
+ chat2.setContent("Hello");
+
+ when(chatInformationService.getChatsByUser(1L)).thenReturn(Arrays.asList(chat1, chat2));
+
+ List<ChatInformation> result = chatInformationController.getChatsByUser(1L);
+
+ assertEquals(2, result.size());
+ assertEquals("Hi", result.get(0).getContent());
+ }
+
+ @Test
+ void getChatsBetweenUsers_ShouldReturnChatListBetweenTwoUsers() {
+ ChatInformation chat = new ChatInformation();
+ chat.setSenderId(1L);
+ chat.setReceiverId(2L);
+ chat.setContent("Hey");
+
+ when(chatInformationService.getChatsBetweenUsers(1L, 2L)).thenReturn(List.of(chat));
+
+ List<ChatInformation> result = chatInformationController.getChatsBetweenUsers(1L, 2L);
+
+ assertEquals(1, result.size());
+ assertEquals("Hey", result.get(0).getContent());
+ }
+}