diff --git a/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java index 5f8253b..08b4773 100644 --- a/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java +++ b/src/main/java/jty/expressdistributionsystem/DTO/GoodsDTO.java @@ -11,10 +11,6 @@ public class GoodsDTO { @NotBlank(message = "请选择快递公司") private String company; - @JsonSerialize(using = ToStringSerializer.class) - @NotNull - private Long sendUserId; - @NotBlank(message = "收件人名字不能为空") private String getUserName; @@ -26,4 +22,8 @@ public class GoodsDTO { @NotBlank(message = "运送地址不能为空") private String address; + + @JsonSerialize(using = ToStringSerializer.class) + @NotNull + private Long expressId; } diff --git a/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java index 1bf6a1c..efc673d 100644 --- a/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java +++ b/src/main/java/jty/expressdistributionsystem/DTO/SendMessageDTO.java @@ -4,16 +4,10 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Data; -import java.io.Serial; -import java.io.Serializable; - @Data -public class SendMessageDTO implements Serializable { - @Serial - private static final long serialVersionUID = 1L; - +public class SendMessageDTO { @JsonSerialize(using = ToStringSerializer.class) - private Long id; + private Long Id; private String content; } diff --git a/src/main/java/jty/expressdistributionsystem/config/WebConfig.java b/src/main/java/jty/expressdistributionsystem/config/WebConfig.java index 03e9d58..c8f41ed 100644 --- a/src/main/java/jty/expressdistributionsystem/config/WebConfig.java +++ b/src/main/java/jty/expressdistributionsystem/config/WebConfig.java @@ -13,6 +13,6 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(loginInterceptor).excludePathPatterns("/common/login", "/common/loginByAccount"); + registry.addInterceptor(loginInterceptor).excludePathPatterns("/common/register", "/common/loginByAccount"); } } diff --git a/src/main/java/jty/expressdistributionsystem/controller/AdminController.java b/src/main/java/jty/expressdistributionsystem/controller/AdminController.java index 8b60919..4d197aa 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/AdminController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/AdminController.java @@ -22,7 +22,7 @@ public class AdminController { private MessageService messageService; // 管理员查看用户或者快递员信息 - @GetMapping("/userList") + @GetMapping("/infoList") public Result> getUserList(@RequestParam int page, @RequestParam int pageSize, @RequestParam int mark) { if (page <= 0 || pageSize <= 0) { return new Result<>(400, "页码和每页大小必须大于0", null); @@ -43,7 +43,7 @@ public class AdminController { @DeleteMapping("/delete/{id}") public Result delete(@PathVariable Long id, @RequestParam String operation) { User user = userService.getById(id); - switch(operation){ + switch (operation) { // 禁用 case "0" -> user.setDisabled(1); // 解除禁用 @@ -61,7 +61,7 @@ public class AdminController { // 管理员发送消息 @PostMapping("/sendMessage") public Result sendMessage(@RequestBody SendMessageDTO sendMessageDTO) { - Message message = SendMessageUtil.sendMessage(sendMessageDTO); + Message message = SendMessageUtil.sendMessage(sendMessageDTO); messageService.save(message); return new Result<>(200, "发送成功", ""); } diff --git a/src/main/java/jty/expressdistributionsystem/controller/CommonController.java b/src/main/java/jty/expressdistributionsystem/controller/CommonController.java index 8ab10e9..fa60b12 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/CommonController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/CommonController.java @@ -67,7 +67,7 @@ public class CommonController { return new Result<>(401, "账号或者密码错误", ""); } User user = optionalUser.get(); - if(user.getDisabled() == 1){ + if (user.getDisabled() == 1) { return new Result<>(403, "该账号已被禁用, 请联系管理员", ""); } user.setLoginTime(LocalDateTime.now()); diff --git a/src/main/java/jty/expressdistributionsystem/controller/UserController.java b/src/main/java/jty/expressdistributionsystem/controller/UserController.java index cf01613..610fe90 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/UserController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/UserController.java @@ -14,6 +14,7 @@ import jty.expressdistributionsystem.utils.ClaimCodeUtil; import jty.expressdistributionsystem.utils.ExpressCodeUtil; import jty.expressdistributionsystem.utils.GetIdUtil; import jty.expressdistributionsystem.utils.SendMessageUtil; +import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -22,6 +23,7 @@ import java.util.List; @RestController @RequestMapping("/user") +@Slf4j public class UserController { @Resource private UserService userService; @@ -51,87 +53,44 @@ public class UserController { // 给快递员点赞 @PostMapping("/likes") - public Result like(Long id) { + public Result like(@RequestParam Long id) { User employee = userService.getById(id); employee.setLikes(employee.getLikes() + 1); - userService.save(employee); + userService.updateById(employee); return new Result<>(200, "点赞成功", ""); } - // 用户查看自己的快递记录 - @GetMapping("/history") - public Result> getHistory(@RequestParam int page, @RequestParam int pageSize) { + // 用户查看发给别人消息 + @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 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); - } - - // 用户收件 - @PostMapping("/addressee") - public Result addressee(@Validated @RequestBody @NotNull AddresseeDTO addresseeDTO) { - Long goodsId = addresseeDTO.getGoodsId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("goods_id", goodsId); - Code code = codeService.getOne(queryWrapper); - // 判断取件码是否输入正确 - if (!code.getClaim().equals(addresseeDTO.getClaimCode())) { - return new Result<>(401, "请输入正确的取件码", ""); - } - QueryWrapper recordsQueryWrapper = new QueryWrapper<>(); - recordsQueryWrapper.eq("goods_id", goodsId); - Records records = recordsService.getOne(recordsQueryWrapper); - records.setMark(1); - // 设置取件人 - records.setAddresseeId(GetIdUtil.getId()); - recordsService.save(records); - return new Result<>(200, "取件成功", ""); - } - - // 用户代取快递 - @PostMapping("/pickUp") - public Result pickUpExpress(@RequestBody @Validated @NotNull PickUpExpressDTO pickUpExpressDTO) { - // 查询快递原主人信息 - QueryWrapper userQueryWrapper = new QueryWrapper<>(); - userQueryWrapper.eq("user_name", pickUpExpressDTO.getMaster()); - User master = userService.getOne(userQueryWrapper); - if (master == null) { - return new Result<>(404, "该用户不存在", ""); - } - // 查询快递信息 - QueryWrapper goodsQueryWrapper = new QueryWrapper<>(); - goodsQueryWrapper.eq("express_code", pickUpExpressDTO.getExpressCode()); - Goods goods = goodsService.getOne(goodsQueryWrapper); - if (goods == null) { - return new Result<>(404, "快递不存在", ""); - } - // 代取 - QueryWrapper recordsQueryWrapper = new QueryWrapper<>(); - recordsQueryWrapper.eq("goods_id", goods.getId()); - Records records = recordsService.getOne(recordsQueryWrapper); - records.setMark(1); - records.setAddresseeId(GetIdUtil.getId()); - recordsService.save(records); - return new Result<>(200, "待取成功", ""); + log.info("当前登录用户的id: {}", id); + Page messagePage = new Page<>(page, pageSize); + 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); } - // 用户查看消息 - @GetMapping("/getMessage") - public Result> getMessage(@RequestParam int page, @RequestParam int pageSize) { + // 用户查看别人发给自己的消息 + @GetMapping("/getFromMessage") + public Result> getFromMessage(@RequestParam int page, @RequestParam int pageSize) { if (page <= 0 || pageSize == 0) { return new Result<>(400, "页码和每页大小必须大于0", null); } Long id = GetIdUtil.getId(); + log.info("当前登录用户的id: {}", id); Page messagePage = new Page<>(page, pageSize); LambdaQueryWrapper messageLambdaQueryWrapper = new LambdaQueryWrapper<>(); // 添加查询条件 - messageLambdaQueryWrapper.eq(Message::getSendUserId, id); + messageLambdaQueryWrapper.eq(Message::getGetUserId, id); // 排序条件:先按 mark 对应值排序,然后按时间排序 messageLambdaQueryWrapper.orderByDesc(Message::getMark) // mark 值大的排前面 .orderByDesc(Message::getCreateTime); // 时间新的排前面 @@ -142,12 +101,26 @@ public class UserController { // 用户确认查看消息 @PostMapping("/confirmMessage") public Result confirmMessage(@RequestParam Long id) { - Records records = recordsService.getById(id); - records.setMark(0); - recordsService.save(records); + Message message = messageService.getById(id); + message.setMark(0); + messageService.updateById(message); return new Result<>(200, "消息查看成功", ""); } + // 用户新建快递地址 + @PostMapping("/addAddress") + public Result addAddress(@RequestBody @NotNull Address address) { + // 查询快递地址是否已经存在 + LambdaQueryWrapper
addressLambdaQueryWrapper = new LambdaQueryWrapper<>(); + addressLambdaQueryWrapper.eq(Address::getAddress, address.getAddress()); + Address getAddress = addressService.getOne(addressLambdaQueryWrapper); + if (getAddress != null) { + return new Result<>(409, "该地址已存在", ""); + } + addressService.save(address); + return new Result<>(200, "地址创建成功", ""); + } + // 用户获取自己写过的地址 @GetMapping("/getAddress") public Result> listAddress() { @@ -158,6 +131,21 @@ public class UserController { return new Result<>(200, "", list); } + // 用户查看自己的发送快递记录 + @GetMapping("/history") + 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); + } + // 用户寄件 @PostMapping("/shipment") public Result shipmentExpress(@RequestBody @Validated @NotNull GoodsDTO goodsDTO) { @@ -165,9 +153,16 @@ public class UserController { LambdaQueryWrapper userLambdaQueryWrapper = new LambdaQueryWrapper<>(); userLambdaQueryWrapper.eq(User::getUserName, goodsDTO.getGetUserName()); User getUser = userService.getOne(userLambdaQueryWrapper); - if(getUser == null) { + if (getUser == null) { return new Result<>(404, "用户不存在", null); } + // 判断地址是否存在 + LambdaQueryWrapper
addressLambdaQueryWrapper = new LambdaQueryWrapper<>(); + addressLambdaQueryWrapper.eq(Address::getAddress, goodsDTO.getAddress()); + Address address = addressService.getOne(addressLambdaQueryWrapper); + if (address == null) { + return new Result<>(404, "地址不存在", null); + } // 生成快递码 String expressCode = ExpressCodeUtil.getExpressCode(goodsDTO.getCompany()); // 生成取件码 @@ -183,11 +178,9 @@ public class UserController { Records records = new Records(); records.setSendUserId(GetIdUtil.getId()); records.setGetUserId(getUser.getId()); - LambdaQueryWrapper
addressLambdaQueryWrapper = new LambdaQueryWrapper<>(); - addressLambdaQueryWrapper.eq(Address::getAddress, goodsDTO.getAddress()); - Address address = addressService.getOne(addressLambdaQueryWrapper); - records.setAddresseeId(address.getId()); + records.setAddressId(address.getId()); records.setGoodsId(goods.getId()); + records.setExpressId(goodsDTO.getExpressId()); recordsService.save(records); Code code = new Code(); code.setClaim(claimCode); @@ -195,4 +188,60 @@ public class UserController { codeService.save(code); return new Result<>(200, "寄件成功", ""); } + + // 用户收件 + @PostMapping("/addressee") + public Result addressee(@Validated @RequestBody @NotNull AddresseeDTO addresseeDTO) { + // 判断是不是自己的快递 + Long goodsId = addresseeDTO.getGoodsId(); + Goods goods = goodsService.getById(goodsId); + if (goods.getGetUserId().compareTo(GetIdUtil.getId()) != 0) { + return new Result<>(401, "签收他人快递请选择代取功能", ""); + } + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("goods_id", goodsId); + Code code = codeService.getOne(queryWrapper); + // 判断取件码是否输入正确 + if (!code.getClaim().equals(addresseeDTO.getClaimCode())) { + return new Result<>(401, "请输入正确的取件码", ""); + } + QueryWrapper recordsQueryWrapper = new QueryWrapper<>(); + recordsQueryWrapper.eq("goods_id", goodsId); + Records records = recordsService.getOne(recordsQueryWrapper); + records.setMark(1); + // 设置取件人 + records.setAddresseeId(GetIdUtil.getId()); + recordsService.updateById(records); + return new Result<>(200, "取件成功", ""); + } + + // 用户代取快递 + @PostMapping("/pickUp") + public Result pickUpExpress(@RequestBody @Validated @NotNull PickUpExpressDTO pickUpExpressDTO) { + // 查询快递原主人信息 + QueryWrapper userQueryWrapper = new QueryWrapper<>(); + userQueryWrapper.eq("user_name", pickUpExpressDTO.getMaster()); + User master = userService.getOne(userQueryWrapper); + if (master == null) { + return new Result<>(404, "该用户不存在", ""); + } + // 查询快递信息 + QueryWrapper goodsQueryWrapper = new QueryWrapper<>(); + goodsQueryWrapper.eq("express_code", pickUpExpressDTO.getExpressCode()); + Goods goods = goodsService.getOne(goodsQueryWrapper); + if (goods == null) { + return new Result<>(404, "快递不存在", ""); + } + // 代取 + QueryWrapper recordsQueryWrapper = new QueryWrapper<>(); + recordsQueryWrapper.eq("goods_id", goods.getId()); + Records records = recordsService.getOne(recordsQueryWrapper); + if(records.getMark() == 1) { + return new Result<>(500, "快递已被签收", ""); + } + records.setMark(1); + records.setAddresseeId(GetIdUtil.getId()); + recordsService.updateById(records); + return new Result<>(200, "代取成功", ""); + } } diff --git a/src/main/java/jty/expressdistributionsystem/entity/Records.java b/src/main/java/jty/expressdistributionsystem/entity/Records.java index dd1107f..d9d20f4 100644 --- a/src/main/java/jty/expressdistributionsystem/entity/Records.java +++ b/src/main/java/jty/expressdistributionsystem/entity/Records.java @@ -33,6 +33,10 @@ public class Records implements Serializable { @NotNull private Long getUserId; + @JsonSerialize(using = ToStringSerializer.class) + @NotNull + private Long expressId; + @TableField("goods_id") @JsonSerialize(using = ToStringSerializer.class) @NotNull @@ -41,6 +45,10 @@ public class Records implements Serializable { @TableField("mark") private Integer mark; + @TableField + @JsonSerialize(using = ToStringSerializer.class) + private Long addressId; + @TableField("addressee_id") @JsonSerialize(using = ToStringSerializer.class) private Long addresseeId; diff --git a/src/main/java/jty/expressdistributionsystem/interceptors/LoginInterceptor.java b/src/main/java/jty/expressdistributionsystem/interceptors/LoginInterceptor.java index 86e27bc..e697551 100644 --- a/src/main/java/jty/expressdistributionsystem/interceptors/LoginInterceptor.java +++ b/src/main/java/jty/expressdistributionsystem/interceptors/LoginInterceptor.java @@ -21,13 +21,13 @@ public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) { - // 令牌验证 + // 令牌验证 String token = request.getHeader("Authorization"); try { // 从redis获取相同的token ValueOperations stringStringValueOperations = stringRedisTemplate.opsForValue(); String redisToken = stringStringValueOperations.get(token); - if(redisToken == null){ + if (redisToken == null) { // 说明token失效 throw new RuntimeException(); }