diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java b/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java index 2aa9421..8eaf4a1 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserinfoShippingAddressService.java @@ -88,12 +88,19 @@ public class UserinfoShippingAddressService { UserLoginDto user = jwtUtil.getUser(); userinfoShippingAddress.setUserId(user.getUserId()); - List checkList = userinfoShippingAddressDao.findByUserIdAndAcquiesce(user.getUserId(), UserInfoShippingAddressEnums.DEFAULT_ADDRESS.getCode()); - if(checkList.isEmpty()) { + if (Objects.equals(UserInfoShippingAddressEnums.NORMAL_ADDRESS.getCode(), userinfoShippingAddress.getAcquiesce())) { + // 如果是非默认地址,直接保存即可 userinfoShippingAddressDao.save(userinfoShippingAddress); } else { - // 请勿重复添加默认地址! - throw new RuntimeException(ErrorConstants.DEFAULT_ADDRESS_CANNOT_BE_ADDED_AGAIN); + // 如果是默认地址,将原先的默认地址改为非默认地址 + List checkList = userinfoShippingAddressDao.findByUserIdAndAcquiesceAndIdIsNot(user.getUserId(), UserInfoShippingAddressEnums.DEFAULT_ADDRESS.getCode(), userinfoShippingAddress.getId()); + if(!checkList.isEmpty()) { + checkList.forEach(item -> { + item.setAcquiesce(UserInfoShippingAddressEnums.NORMAL_ADDRESS.getCode()); + userinfoShippingAddressDao.save(item); + }); + } + userinfoShippingAddressDao.save(userinfoShippingAddress); } }