- 用户地址 添加 hasDefault

This commit is contained in:
sin 2019-04-10 22:12:57 +08:00
parent dd452f81e6
commit 22d736050d
11 changed files with 120 additions and 37 deletions

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.user.application.controller.users; package cn.iocoder.mall.user.application.controller.users;
import cn.iocoder.common.framework.vo.CommonResult; 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.convert.UserAddressConvert;
import cn.iocoder.mall.user.application.po.UserAddressAddPO; import cn.iocoder.mall.user.application.po.UserAddressAddPO;
import cn.iocoder.mall.user.application.po.UserAddressUpdatePO; 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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
/** /**
* 用户地址 * 用户地址
* *
@ -55,7 +58,7 @@ public class UserAddressController {
@GetMapping("list") @GetMapping("list")
@ApiOperation(value = "用户地址列表") @ApiOperation(value = "用户地址列表")
public CommonResult addressList() { public CommonResult<List<UserAddressBO>> addressList() {
Integer userId = UserSecurityContextHolder.getContext().getUserId(); Integer userId = UserSecurityContextHolder.getContext().getUserId();
return userAddressService.addressList(userId); return userAddressService.addressList(userId);
} }

View File

@ -20,7 +20,6 @@ import java.io.Serializable;
@Accessors(chain = true) @Accessors(chain = true)
public class UserAddressUpdatePO implements Serializable { public class UserAddressUpdatePO implements Serializable {
/** /**
* 收件区域编号 * 收件区域编号
*/ */
@ -53,5 +52,10 @@ public class UserAddressUpdatePO implements Serializable {
@NotNull(message = "详细地址不能为空") @NotNull(message = "详细地址不能为空")
@Size(min = 10, max = 100, message = "地址在 10 ~ 100 字之间!") @Size(min = 10, max = 100, message = "地址在 10 ~ 100 字之间!")
private String address; private String address;
/**
* 是否设置默认
*/
@ApiModelProperty("是否设置默认")
@NotNull(message = "是否设置默认不能为空")
private Integer hasDefault;
} }

View File

@ -13,7 +13,6 @@ import java.io.Serializable;
@Accessors(chain = true) @Accessors(chain = true)
public class UserAddressBO implements Serializable { public class UserAddressBO implements Serializable {
/** /**
* 编号 * 编号
*/ */
@ -38,5 +37,8 @@ public class UserAddressBO implements Serializable {
* 收件详细地址 * 收件详细地址
*/ */
private String address; private String address;
/**
* 是否默认
*/
private Integer hasDefault;
} }

View File

@ -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<UserAddressBO> list;
}

View File

@ -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;
}
}

View File

@ -35,5 +35,11 @@ public class UserAddressAddDTO implements Serializable {
* 收件详细地址 * 收件详细地址
*/ */
private String address; private String address;
/**
* 是否默认
*
* - 1 不是
* - 2
*/
private Integer hasDefault;
} }

View File

@ -39,5 +39,8 @@ public class UserAddressUpdateDTO implements Serializable {
* 收件详细地址 * 收件详细地址
*/ */
private String address; private String address;
/**
* 是否默认地址
*/
private Integer hasDefault;
} }

View File

@ -31,4 +31,10 @@ public interface UserAddressMapper {
Integer userId, Integer userId,
Integer id Integer id
); );
UserAddressDO selectHasDefault(
Integer deleted,
Integer userId,
Integer hasDefault
);
} }

View File

@ -38,5 +38,9 @@ public class UserAddressDO extends DeletableDO {
* 收件详细地址 * 收件详细地址
*/ */
private String address; private String address;
/**
* 是否为默认
*/
private Integer hasDefault;
} }

View File

@ -3,6 +3,7 @@ package cn.iocoder.mall.user.biz.service;
import cn.iocoder.common.framework.constant.DeletedStatusEnum; import cn.iocoder.common.framework.constant.DeletedStatusEnum;
import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult; 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.api.constant.UserErrorCodeEnum;
import cn.iocoder.mall.user.biz.convert.UserAddressConvert; import cn.iocoder.mall.user.biz.convert.UserAddressConvert;
import cn.iocoder.mall.user.biz.dao.UserAddressMapper; 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 cn.iocoder.mall.user.api.dto.UserAddressUpdateDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -31,12 +33,28 @@ public class UserAddressServiceImpl implements UserAddressService {
private UserAddressMapper userAddressMapper; private UserAddressMapper userAddressMapper;
@Override @Override
@Transactional
public CommonResult addAddress(UserAddressAddDTO userAddressAddDTO) { public CommonResult addAddress(UserAddressAddDTO userAddressAddDTO) {
UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO); UserAddressDO userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setCreateTime(new Date()); userAddressDO.setCreateTime(new Date());
userAddressDO.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()); 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 @Override
@ -52,6 +70,32 @@ public class UserAddressServiceImpl implements UserAddressService {
return ServiceExceptionUtil.error(UserErrorCodeEnum.USER_ADDRESS_NOT_EXISTENT.getCode()); 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 userAddressDO = UserAddressConvert.INSTANCE.convert(userAddressAddDTO);
userAddressDO.setUpdateTime(new Date()); userAddressDO.setUpdateTime(new Date());
userAddressMapper.updateById(userAddressDO.getId(), userAddressDO); userAddressMapper.updateById(userAddressDO.getId(), userAddressDO);

View File

@ -57,4 +57,13 @@
WHERE deleted = #{deleted} WHERE deleted = #{deleted}
AND `user_id` = #{userId} AND `user_id` = #{userId}
</select> </select>
<select id="selectHasDefault" resultType="cn.iocoder.mall.user.biz.dataobject.UserAddressDO">
SELECT
<include refid="FIELDS" />
FROM user_address
WHERE deleted = #{deleted}
AND `user_id` = #{userId}
AND `has_default` = #{hasDefault}
</select>
</mapper> </mapper>