diff --git a/src/main/java/jty/expressdistributionsystem/DTO/RecordsListDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/RecordsListDTO.java new file mode 100644 index 0000000..f3ff67c --- /dev/null +++ b/src/main/java/jty/expressdistributionsystem/DTO/RecordsListDTO.java @@ -0,0 +1,17 @@ +package jty.expressdistributionsystem.DTO; + +import jty.expressdistributionsystem.entity.Address; +import jty.expressdistributionsystem.entity.Goods; +import jty.expressdistributionsystem.entity.User; +import lombok.Data; + +@Data +public class RecordsListDTO { + private Goods goods; // 查看货物名称 + private Address addressFrom; // 查看发货地 + private Address addressTo; // 查看发到哪里 + private User employee; // 查看快递员 + private User addressee; // 查看谁签收的 + private User sendUser; // 查看谁发的 + private User getUserId; // 查看发给谁的 +} diff --git a/src/main/java/jty/expressdistributionsystem/controller/UserController.java b/src/main/java/jty/expressdistributionsystem/controller/UserController.java index ea25d23..07bb504 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/UserController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/UserController.java @@ -4,10 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import jakarta.annotation.Resource; -import jty.expressdistributionsystem.DTO.AddresseeDTO; -import jty.expressdistributionsystem.DTO.GoodsDTO; -import jty.expressdistributionsystem.DTO.PickUpExpressDTO; -import jty.expressdistributionsystem.DTO.SendMessageDTO; +import jty.expressdistributionsystem.DTO.*; import jty.expressdistributionsystem.entity.*; import jty.expressdistributionsystem.service.*; import jty.expressdistributionsystem.utils.ClaimCodeUtil; @@ -18,9 +15,7 @@ import org.jetbrains.annotations.NotNull; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.Collections; -import java.util.List; -import java.util.Objects; +import java.util.*; @RestController @RequestMapping("/user") @@ -78,7 +73,7 @@ public class UserController { return new Result<>(200, "获取成功", messagePage); } - // 用户查看别人发给自己的消息 + // 用户查看别人发给自己的消息(finish) @GetMapping("/getFromMessage") public Result> getFromMessage(@RequestParam Long id) { LambdaQueryWrapper messageLambdaQueryWrapper = new LambdaQueryWrapper<>(); @@ -120,19 +115,58 @@ public class UserController { return new Result<>(200, "", list); } - // 用户查看自己的发送快递记录 + // 用户获取自己所有的快递记录 @GetMapping("/history") - public Result> getHistory(@RequestParam int page, @RequestParam int pageSize) { - if (page <= 0 || pageSize == 0) { + public Result> getHistory(@RequestParam int page, @RequestParam int pageSize) { + if (page <= 0 || pageSize <= 0) { return new Result<>(400, "页码和每页大小必须大于0", null); } - Long id = GetIdUtil.getId(); - Page pageInfo = new Page<>(page, pageSize); - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(Records::getSendUserId, id); - queryWrapper.orderByDesc(Records::getCreateTime); - recordsService.page(pageInfo, queryWrapper); - return new Result<>(200, "", pageInfo); + Long userId = GetIdUtil.getId(); // 获取当前用户的ID + List recordsList = new ArrayList<>(); + // 查询作为收件人的快递记录 + LambdaQueryWrapper recordsLambdaQueryWrapper = new LambdaQueryWrapper<>(); + recordsLambdaQueryWrapper.eq(Records::getGetUserId, userId); + // 添加排序条件 + recordsLambdaQueryWrapper.orderByAsc(Records::getMark) + .orderByDesc(Records::getCreateTime); + List receivedRecords = recordsService.list(recordsLambdaQueryWrapper); + recordsLambdaQueryWrapper.clear(); + recordsList.addAll(receivedRecords); + // 查询作为寄件人的快递记录 + recordsLambdaQueryWrapper.eq(Records::getSendUserId, userId); + // 添加排序条件 + recordsLambdaQueryWrapper.orderByAsc(Records::getMark) + .orderByDesc(Records::getCreateTime); + List sentRecords = recordsService.list(recordsLambdaQueryWrapper); + recordsList.addAll(sentRecords); + // 分页处理 + int totalRecords = recordsList.size(); + int fromIndex = Math.min(pageSize * (page - 1), totalRecords); + int toIndex = Math.min(fromIndex + pageSize, totalRecords); + List pagedRecords = recordsList.subList(fromIndex, toIndex); + // 封装成GoodsListDTO + List recordsListDTOList = new ArrayList<>(); + for (Records records : pagedRecords) { + Goods goods = goodsService.getById(records.getGoodsId()); + User sender = userService.getById(records.getSendUserId()); + User recipient = userService.getById(records.getGetUserId()); + User addressee = userService.getById(records.getAddresseeId()); + User courier = userService.getById(records.getExpressId()); + Address addressFrom = addressService.getById(records.getAddressFrom()); + Address addressTo = addressService.getById(records.getAddressId()); + RecordsListDTO recordsListDTO = new RecordsListDTO(); + recordsListDTO.setGoods(goods); + recordsListDTO.setAddressFrom(addressFrom); + recordsListDTO.setAddressTo(addressTo); + recordsListDTO.setEmployee(courier); + recordsListDTO.setAddressee(addressee); + recordsListDTO.setSendUser(sender); + recordsListDTO.setGetUserId(recipient); + recordsListDTOList.add(recordsListDTO); + } + Page goodsDTOPage = new Page<>(page, pageSize, totalRecords); + goodsDTOPage.setRecords(recordsListDTOList); + return new Result<>(200, "", goodsDTOPage); } // 用户寄件(完成)