diff --git a/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java index b828540..96bf8e5 100644 --- a/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java +++ b/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java @@ -1,18 +1,12 @@ package jty.expressdistributionsystem.DTO; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; import lombok.Data; -import java.io.Serializable; - @Data -public class SendMessageDTO implements Serializable { - @JsonSerialize(using = ToStringSerializer.class) - @NotNull(message = "请选择你的聊天对象") - private Long Id; +public class SendMessageDTO { + @NotBlank(message = "请选择你的聊天对象") + private String toUserName; @NotBlank(message = "消息不能为空") private String content; diff --git a/src/main/java/jty/expressdistributionsystem/controller/AdminController.java b/src/main/java/jty/expressdistributionsystem/controller/AdminController.java index a69c40e..9eaf765 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/AdminController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/AdminController.java @@ -80,7 +80,7 @@ public class AdminController { // 管理员发送消息(finish) @PostMapping("/sendMessage") public Result sendMessage(@RequestBody @Validated SendMessageDTO sendMessageDTO) { - Message message = SendMessageUtil.sendMessage(sendMessageDTO); + Message message = SendMessageUtil.sendMessage(sendMessageDTO, userService); messageService.save(message); return new Result<>(200, "发送成功", ""); } diff --git a/src/main/java/jty/expressdistributionsystem/controller/EmployeeController.java b/src/main/java/jty/expressdistributionsystem/controller/EmployeeController.java index c215297..f184190 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/EmployeeController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/EmployeeController.java @@ -70,12 +70,12 @@ public class EmployeeController { @PostMapping("/sendMessage") public Result sendMessageToUser(@RequestBody @NotNull SendMessageDTO sendMessageDTO) { LambdaQueryWrapper userLambdaQueryWrapper = new LambdaQueryWrapper<>(); - userLambdaQueryWrapper.eq(User::getId, sendMessageDTO.getId()); + userLambdaQueryWrapper.eq(User::getUserName, sendMessageDTO.getToUserName()); User user = userService.getOne(userLambdaQueryWrapper); if (user == null) { return new Result<>(404, "用户不存在", ""); } - Message message = SendMessageUtil.sendMessage(sendMessageDTO); + Message message = SendMessageUtil.sendMessage(sendMessageDTO, userService); messageService.save(message); return new Result<>(200, "发送成功", ""); } diff --git a/src/main/java/jty/expressdistributionsystem/controller/UserController.java b/src/main/java/jty/expressdistributionsystem/controller/UserController.java index b31e89b..99bcad3 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/UserController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/UserController.java @@ -39,15 +39,15 @@ public class UserController { @Resource private AddressService addressService; - // 用户给快递员发消息 + // 用户发消息(finish) @PostMapping("/sendMessage") public Result sendMessage(@Validated @RequestBody SendMessageDTO sendMessageDTO) { - Message message = SendMessageUtil.sendMessage(sendMessageDTO); + Message message = SendMessageUtil.sendMessage(sendMessageDTO, userService); messageService.save(message); return new Result<>(200, "发送成功", ""); } - // 给快递员点赞 + // 给快递员点赞(finish) @PostMapping("/likes") public Result like(@RequestParam Long id) { User employee = userService.getById(id); @@ -56,22 +56,18 @@ public class UserController { return new Result<>(200, "点赞成功", ""); } - // 用户查看发给别人消息 + // 用户查看发给别人消息(finish) @GetMapping("/sendToMessage") - public Result> sendToMessage(@RequestParam int page, @RequestParam int pageSize) { - if (page <= 0 || pageSize == 0) { - return new Result<>(400, "页码和每页大小必须大于0", null); - } - Long id = GetIdUtil.getId(); - Page messagePage = new Page<>(page, pageSize); + public Result> sendToMessage(@RequestParam String toUserName) { + LambdaQueryWrapper userLambdaQueryWrapper = new LambdaQueryWrapper<>(); + userLambdaQueryWrapper.eq(User::getUserName, toUserName); + User toUser = userService.getOne(userLambdaQueryWrapper); + Long userId = GetIdUtil.getId(); LambdaQueryWrapper messageLambdaQueryWrapper = new LambdaQueryWrapper<>(); - // 添加查询条件 - messageLambdaQueryWrapper.eq(Message::getSendUserId, id); - // 排序条件:先按 mark 对应值排序,然后按时间排序 - messageLambdaQueryWrapper.orderByDesc(Message::getMark) // mark 值大的排前面 - .orderByDesc(Message::getCreateTime); // 时间新的排前面 - messageService.page(messagePage, messageLambdaQueryWrapper); - return new Result<>(200, "获取成功", messagePage); + messageLambdaQueryWrapper.eq(Message::getSendUserId, userId) + .eq(Message::getGetUserId, toUser.getId()); + List list = messageService.list(messageLambdaQueryWrapper); + return new Result<>(200, "", list); } // 用户查看别人发给自己的消息(finish) diff --git a/src/main/java/jty/expressdistributionsystem/utils/SendMessageUtil.java b/src/main/java/jty/expressdistributionsystem/utils/SendMessageUtil.java index ae0a40e..0dbb147 100644 --- a/src/main/java/jty/expressdistributionsystem/utils/SendMessageUtil.java +++ b/src/main/java/jty/expressdistributionsystem/utils/SendMessageUtil.java @@ -1,16 +1,22 @@ package jty.expressdistributionsystem.utils; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import jty.expressdistributionsystem.DTO.SendMessageDTO; import jty.expressdistributionsystem.entity.Message; +import jty.expressdistributionsystem.entity.User; +import jty.expressdistributionsystem.service.UserService; import org.jetbrains.annotations.NotNull; public class SendMessageUtil { - public static Message sendMessage(@NotNull SendMessageDTO sendMessageDTO) { + public static Message sendMessage(@NotNull SendMessageDTO sendMessageDTO, UserService userService) { Long sendId = GetIdUtil.getId(); Message message = new Message(); message.setSendUserId(sendId); - message.setGetUserId(sendMessageDTO.getId()); + LambdaQueryWrapper userLambdaQueryWrapper = new LambdaQueryWrapper<>(); + userLambdaQueryWrapper.eq(User::getUserName, sendMessageDTO.getToUserName()); + User user = userService.getOne(userLambdaQueryWrapper); + message.setGetUserId(user.getId()); message.setContent(sendMessageDTO.getContent()); return message; }