package com.cyksj.web.controller.user; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.cyksj.common.exception.BusinessRuntimeException; import com.cyksj.dto.Result; import com.cyksj.enums.GatewayResponse; import com.cyksj.model.entity.Address; import com.cyksj.service.user.AddressService; import com.cyksj.service.user.UserBindRelationService; import com.cyksj.web.util.StpUserUtil; import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @author chan * @date 2022/10/13 3:07 PM */ @RestController @RequestMapping("/applets/address") @RequiredArgsConstructor public class AddressController { private final AddressService addressService; private final UserBindRelationService userBindRelationService; @GetMapping("/get") public Result> get(){ long userId = StpUserUtil.getLoginIdAsLong(); List userIdList = userBindRelationService.getRelationUserIdList(userId, null); List
addressList = addressService.list(Wrappers.lambdaQuery(Address.class).in(Address::getUserId, userIdList)); return GatewayResponse.SUCCESS.newBuilder().toResult(addressList); } @Transactional(rollbackFor = Throwable.class) @PostMapping("/post") public Result
save(@RequestBody Address address){ long userId = StpUserUtil.getLoginIdAsLong(); List userIdList = userBindRelationService.getRelationUserIdList(userId, null); int count = addressService.count(Wrappers.lambdaQuery(Address.class).in(Address::getUserId, userIdList)); if (count >= 20) { throw new BusinessRuntimeException("收货地址最多可添加20条, 请清理后再添加."); } if (count == 0) { // 当前新增的地址是唯一一个地址时 无论用户是否设为默认地址 都设为默认地址 address.setIsDefault(true); } if(address.getIsDefault() != null && address.getIsDefault() ){ addressService.update(Wrappers.lambdaUpdate(Address.class).in(Address::getUserId,userIdList).set(Address::getIsDefault,false)); } address.setId(null); address.setUserId(userId); addressService.save(address); return GatewayResponse.SUCCESS.newBuilder().toResult(address); } @Transactional(rollbackFor = Throwable.class) @PutMapping("/update") public Result
update(@RequestBody Address address){ long userId = StpUserUtil.getLoginIdAsLong(); List userIdList = userBindRelationService.getRelationUserIdList(userId, null); if (address.getId() == null || userIdList == null || userIdList.isEmpty()) { throw new BusinessRuntimeException("地址不存在或无权操作"); } Address existing = addressService.getOne(Wrappers.lambdaQuery(Address.class) .eq(Address::getId, address.getId()).in(Address::getUserId, userIdList)); if (existing == null) { throw new BusinessRuntimeException("地址不存在或无权操作"); } // 只复制允许编辑的字段,拒绝userId和BaseEntity字段的批量赋值。 Address changes = new Address(); changes.setReceiver(address.getReceiver()); changes.setPhone(address.getPhone()); changes.setProvince(address.getProvince()); changes.setCity(address.getCity()); changes.setDistrict(address.getDistrict()); changes.setAddress(address.getAddress()); changes.setCode(address.getCode()); changes.setIsDefault(address.getIsDefault()); if (!addressService.update(changes, Wrappers.lambdaUpdate(Address.class) .eq(Address::getId, existing.getId()).in(Address::getUserId, userIdList))) { throw new BusinessRuntimeException("地址不存在或无权操作"); } if (Boolean.TRUE.equals(address.getIsDefault())) { addressService.update(Wrappers.lambdaUpdate(Address.class) .in(Address::getUserId, userIdList).ne(Address::getId, existing.getId()) .set(Address::getIsDefault, false)); } address = addressService.getById(existing.getId()); return GatewayResponse.SUCCESS.newBuilder().toResult(address); } @DeleteMapping("/delete/{id}") public Result delete(@PathVariable Long id){ long userId = StpUserUtil.getLoginIdAsLong(); List userIdList = userBindRelationService.getRelationUserIdList(userId, null); Address one = addressService.getOne(Wrappers.lambdaQuery(Address.class) .eq(Address::getId, id) .in(Address::getUserId, userIdList)); if (one != null) { addressService.removeById(id); } return GatewayResponse.SUCCESS.newBuilder().toResult(); } }