diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserAddressController.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserAddressController.java index 38778ddc8..d2ceb0623 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserAddressController.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/controller/users/UserAddressController.java @@ -1,6 +1,7 @@ package cn.iocoder.mall.user.application.controller.users; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.api.bo.UserAddressBO; import cn.iocoder.mall.user.application.convert.UserAddressConvert; import cn.iocoder.mall.user.application.po.UserAddressAddPO; import cn.iocoder.mall.user.application.po.UserAddressUpdatePO; @@ -14,6 +15,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + /** * 用户地址 * @@ -55,7 +58,7 @@ public class UserAddressController { @GetMapping("list") @ApiOperation(value = "用户地址列表") - public CommonResult addressList() { + public CommonResult> addressList() { Integer userId = UserSecurityContextHolder.getContext().getUserId(); return userAddressService.addressList(userId); } diff --git a/user/user-application/src/main/java/cn/iocoder/mall/user/application/po/UserAddressUpdatePO.java b/user/user-application/src/main/java/cn/iocoder/mall/user/application/po/UserAddressUpdatePO.java index 634f93a7b..600e4a0b1 100644 --- a/user/user-application/src/main/java/cn/iocoder/mall/user/application/po/UserAddressUpdatePO.java +++ b/user/user-application/src/main/java/cn/iocoder/mall/user/application/po/UserAddressUpdatePO.java @@ -20,7 +20,6 @@ import java.io.Serializable; @Accessors(chain = true) public class UserAddressUpdatePO implements Serializable { - /** * 收件区域编号 */ @@ -53,5 +52,10 @@ public class UserAddressUpdatePO implements Serializable { @NotNull(message = "详细地址不能为空") @Size(min = 10, max = 100, message = "地址在 10 ~ 100 字之间!") private String address; - + /** + * 是否设置默认 + */ + @ApiModelProperty("是否设置默认") + @NotNull(message = "是否设置默认不能为空") + private Integer hasDefault; } diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressBO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressBO.java index 20c8fe825..e49ac0970 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressBO.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressBO.java @@ -13,7 +13,6 @@ import java.io.Serializable; @Accessors(chain = true) public class UserAddressBO implements Serializable { - /** * 编号 */ @@ -38,5 +37,8 @@ public class UserAddressBO implements Serializable { * 收件详细地址 */ private String address; - + /** + * 是否默认 + */ + private Integer hasDefault; } diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressPageBO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressPageBO.java deleted file mode 100644 index c729b5fb1..000000000 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/bo/UserAddressPageBO.java +++ /dev/null @@ -1,28 +0,0 @@ -package cn.iocoder.mall.user.api.bo; - -import lombok.Data; -import lombok.experimental.Accessors; - -import java.io.Serializable; -import java.util.List; - -/** - * 用户地址 page - * - * @author Sin - * @time 2019-04-06 13:56 - */ -@Data -@Accessors(chain = true) -public class UserAddressPageBO implements Serializable { - - /** - * 总量 - */ - private Integer total; - /** - * 地址 - */ - private List list; - -} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/constant/UserAddressHasDefaultEnum.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/constant/UserAddressHasDefaultEnum.java new file mode 100644 index 000000000..a7ee9eb70 --- /dev/null +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/constant/UserAddressHasDefaultEnum.java @@ -0,0 +1,30 @@ +package cn.iocoder.mall.user.api.constant; + +/** + * 用户地址 - 用户默认地址 + * + * @author Sin + * @time 2019-04-10 22:02 + */ +public enum UserAddressHasDefaultEnum { + + DEFAULT_ADDRESS_NO (1, "不是默认地址"), + DEFAULT_ADDRESS_YES (2, "不是默认地址") + ; + + private final int value; + private final String name; + + UserAddressHasDefaultEnum(int value, String name) { + this.value = value; + this.name = name; + } + + public int getValue() { + return value; + } + + public String getName() { + return name; + } +} diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressAddDTO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressAddDTO.java index d85c7bd98..c85aa5509 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressAddDTO.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressAddDTO.java @@ -35,5 +35,11 @@ public class UserAddressAddDTO implements Serializable { * 收件详细地址 */ private String address; - + /** + * 是否默认 + * + * - 1 不是 + * - 2 是 + */ + private Integer hasDefault; } diff --git a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressUpdateDTO.java b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressUpdateDTO.java index 00f58eece..c3617cabf 100644 --- a/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressUpdateDTO.java +++ b/user/user-service-api/src/main/java/cn/iocoder/mall/user/api/dto/UserAddressUpdateDTO.java @@ -39,5 +39,8 @@ public class UserAddressUpdateDTO implements Serializable { * 收件详细地址 */ private String address; - + /** + * 是否默认地址 + */ + private Integer hasDefault; } diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserAddressMapper.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserAddressMapper.java index d39ff22e6..8aaaa74fb 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserAddressMapper.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dao/UserAddressMapper.java @@ -31,4 +31,10 @@ public interface UserAddressMapper { Integer userId, Integer id ); + + UserAddressDO selectHasDefault( + Integer deleted, + Integer userId, + Integer hasDefault + ); } diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserAddressDO.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserAddressDO.java index 7d5a05efc..66a6eeb36 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserAddressDO.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/dataobject/UserAddressDO.java @@ -38,5 +38,9 @@ public class UserAddressDO extends DeletableDO { * 收件详细地址 */ private String address; + /** + * 是否为默认 + */ + private Integer hasDefault; } diff --git a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAddressServiceImpl.java b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAddressServiceImpl.java index 59ebbfb81..0c0cef806 100644 --- a/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAddressServiceImpl.java +++ b/user/user-service-impl/src/main/java/cn/iocoder/mall/user/biz/service/UserAddressServiceImpl.java @@ -3,6 +3,7 @@ package cn.iocoder.mall.user.biz.service; import cn.iocoder.common.framework.constant.DeletedStatusEnum; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.user.api.constant.UserAddressHasDefaultEnum; import cn.iocoder.mall.user.api.constant.UserErrorCodeEnum; import cn.iocoder.mall.user.biz.convert.UserAddressConvert; import cn.iocoder.mall.user.biz.dao.UserAddressMapper; @@ -13,6 +14,7 @@ import cn.iocoder.mall.user.api.dto.UserAddressAddDTO; import cn.iocoder.mall.user.api.dto.UserAddressUpdateDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; @@ -31,12 +33,28 @@ public class UserAddressServiceImpl implements UserAddressService { private UserAddressMapper userAddressMapper; @Override + @Transactional public CommonResult addAddress(UserAddressAddDTO userAddressAddDTO) { UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO); userAddressDO.setCreateTime(new Date()); userAddressDO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); - userAddressMapper.insert(userAddressDO); - return CommonResult.success(null); + + // 检查是否设置为默认地址 + if (UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue() == userAddressAddDTO.getHasDefault()) { + UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault( + DeletedStatusEnum.DELETED_NO.getValue(), + userAddressAddDTO.getUserId(), UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue()); + + if (defaultUserAddress != null) { + userAddressMapper.updateById(defaultUserAddress.getId(), + new UserAddressDO() + .setHasDefault(UserAddressHasDefaultEnum.DEFAULT_ADDRESS_NO.getValue()) + ); + } + } + + int result = userAddressMapper.insert(userAddressDO); + return CommonResult.success(result); } @Override @@ -52,6 +70,32 @@ public class UserAddressServiceImpl implements UserAddressService { return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode()); } + // 检查是否设置为默认地址 + if (UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue() == userAddressAddDTO.getHasDefault()) { + UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault( + DeletedStatusEnum.DELETED_NO.getValue(), + userAddressAddDTO.getUserId(), UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue()); + + if (defaultUserAddress != null && !userAddressAddDTO.getId().equals(defaultUserAddress.getId())) { + userAddressMapper.updateById(defaultUserAddress.getId(), + new UserAddressDO() + .setHasDefault(UserAddressHasDefaultEnum.DEFAULT_ADDRESS_NO.getValue()) + ); + } + } + + + UserAddressDO defaultUserAddress = userAddressMapper.selectHasDefault( + DeletedStatusEnum.DELETED_NO.getValue(), + userAddressAddDTO.getUserId(), UserAddressHasDefaultEnum.DEFAULT_ADDRESS_YES.getValue()); + + if (defaultUserAddress != null && !userAddressAddDTO.getId().equals(defaultUserAddress.getId())) { + userAddressMapper.updateById(defaultUserAddress.getId(), + new UserAddressDO() + .setHasDefault(UserAddressHasDefaultEnum.DEFAULT_ADDRESS_NO.getValue()) + ); + } + UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO); userAddressDO.setUpdateTime(new Date()); userAddressMapper.updateById(userAddressDO.getId(), userAddressDO); diff --git a/user/user-service-impl/src/main/resources/mapper/UserAddress.xml b/user/user-service-impl/src/main/resources/mapper/UserAddress.xml index baec051d8..277caa851 100644 --- a/user/user-service-impl/src/main/resources/mapper/UserAddress.xml +++ b/user/user-service-impl/src/main/resources/mapper/UserAddress.xml @@ -57,4 +57,13 @@ WHERE deleted = #{deleted} AND `user_id` = #{userId} + + \ No newline at end of file