AddressController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.cyksj.web.controller.user;
  2. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  3. import com.cyksj.common.exception.BusinessRuntimeException;
  4. import com.cyksj.dto.Result;
  5. import com.cyksj.enums.GatewayResponse;
  6. import com.cyksj.model.entity.Address;
  7. import com.cyksj.service.user.AddressService;
  8. import com.cyksj.service.user.UserBindRelationService;
  9. import com.cyksj.web.util.StpUserUtil;
  10. import lombok.RequiredArgsConstructor;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import org.springframework.web.bind.annotation.*;
  13. import java.util.List;
  14. /**
  15. * @author chan
  16. * @date 2022/10/13 3:07 PM
  17. */
  18. @RestController
  19. @RequestMapping("/applets/address")
  20. @RequiredArgsConstructor
  21. public class AddressController {
  22. private final AddressService addressService;
  23. private final UserBindRelationService userBindRelationService;
  24. @GetMapping("/get")
  25. public Result<List<Address>> get(){
  26. long userId = StpUserUtil.getLoginIdAsLong();
  27. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  28. List<Address> addressList = addressService.list(Wrappers.lambdaQuery(Address.class).in(Address::getUserId, userIdList));
  29. return GatewayResponse.SUCCESS.newBuilder().toResult(addressList);
  30. }
  31. @Transactional(rollbackFor = Throwable.class)
  32. @PostMapping("/post")
  33. public Result<Address> save(@RequestBody Address address){
  34. long userId = StpUserUtil.getLoginIdAsLong();
  35. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  36. int count = addressService.count(Wrappers.lambdaQuery(Address.class).in(Address::getUserId, userIdList));
  37. if (count >= 20) {
  38. throw new BusinessRuntimeException("收货地址最多可添加20条, 请清理后再添加.");
  39. }
  40. if (count == 0) {
  41. // 当前新增的地址是唯一一个地址时 无论用户是否设为默认地址 都设为默认地址
  42. address.setIsDefault(true);
  43. }
  44. if(address.getIsDefault() != null && address.getIsDefault() ){
  45. addressService.update(Wrappers.lambdaUpdate(Address.class).in(Address::getUserId,userIdList).set(Address::getIsDefault,false));
  46. }
  47. address.setId(null);
  48. address.setUserId(userId);
  49. addressService.save(address);
  50. return GatewayResponse.SUCCESS.newBuilder().toResult(address);
  51. }
  52. @Transactional(rollbackFor = Throwable.class)
  53. @PutMapping("/update")
  54. public Result<Address> update(@RequestBody Address address){
  55. long userId = StpUserUtil.getLoginIdAsLong();
  56. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  57. if (address.getId() == null || userIdList == null || userIdList.isEmpty()) {
  58. throw new BusinessRuntimeException("地址不存在或无权操作");
  59. }
  60. Address existing = addressService.getOne(Wrappers.lambdaQuery(Address.class)
  61. .eq(Address::getId, address.getId()).in(Address::getUserId, userIdList));
  62. if (existing == null) {
  63. throw new BusinessRuntimeException("地址不存在或无权操作");
  64. }
  65. // 只复制允许编辑的字段,拒绝userId和BaseEntity字段的批量赋值。
  66. Address changes = new Address();
  67. changes.setReceiver(address.getReceiver());
  68. changes.setPhone(address.getPhone());
  69. changes.setProvince(address.getProvince());
  70. changes.setCity(address.getCity());
  71. changes.setDistrict(address.getDistrict());
  72. changes.setAddress(address.getAddress());
  73. changes.setCode(address.getCode());
  74. changes.setIsDefault(address.getIsDefault());
  75. if (!addressService.update(changes, Wrappers.lambdaUpdate(Address.class)
  76. .eq(Address::getId, existing.getId()).in(Address::getUserId, userIdList))) {
  77. throw new BusinessRuntimeException("地址不存在或无权操作");
  78. }
  79. if (Boolean.TRUE.equals(address.getIsDefault())) {
  80. addressService.update(Wrappers.lambdaUpdate(Address.class)
  81. .in(Address::getUserId, userIdList).ne(Address::getId, existing.getId())
  82. .set(Address::getIsDefault, false));
  83. }
  84. address = addressService.getById(existing.getId());
  85. return GatewayResponse.SUCCESS.newBuilder().toResult(address);
  86. }
  87. @DeleteMapping("/delete/{id}")
  88. public Result<String> delete(@PathVariable Long id){
  89. long userId = StpUserUtil.getLoginIdAsLong();
  90. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  91. Address one = addressService.getOne(Wrappers.lambdaQuery(Address.class)
  92. .eq(Address::getId, id)
  93. .in(Address::getUserId, userIdList));
  94. if (one != null) {
  95. addressService.removeById(id);
  96. }
  97. return GatewayResponse.SUCCESS.newBuilder().toResult();
  98. }
  99. }