Commit b24803cf226bfc79466f6594aa0aca51715cbfd9
Committed by
Gitee
!129 [feature/1.8.0-uniapp][用户收件地址] code review 代码调整
Merge pull request !129 from 臭小子/feature/1.8.0-uniapp
Showing
7 changed files
with
86 additions
and
105 deletions
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java
| 1 | package cn.iocoder.yudao.module.member.controller.app.address; | 1 | package cn.iocoder.yudao.module.member.controller.app.address; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 5 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO; | 4 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO; |
| 6 | -import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressPageReqVO; | ||
| 7 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressRespVO; | 5 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressRespVO; |
| 8 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; | 6 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; |
| 9 | import cn.iocoder.yudao.module.member.convert.address.AddressConvert; | 7 | import cn.iocoder.yudao.module.member.convert.address.AddressConvert; |
| @@ -17,7 +15,6 @@ import org.springframework.web.bind.annotation.*; | @@ -17,7 +15,6 @@ import org.springframework.web.bind.annotation.*; | ||
| 17 | 15 | ||
| 18 | import javax.annotation.Resource; | 16 | import javax.annotation.Resource; |
| 19 | import javax.validation.Valid; | 17 | import javax.validation.Valid; |
| 20 | -import java.util.Collection; | ||
| 21 | import java.util.List; | 18 | import java.util.List; |
| 22 | 19 | ||
| 23 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 20 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java
| 1 | package cn.iocoder.yudao.module.member.controller.app.address.vo; | 1 | package cn.iocoder.yudao.module.member.controller.app.address.vo; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.validation.InEnum; | ||
| 4 | +import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; | ||
| 3 | import lombok.*; | 5 | import lombok.*; |
| 4 | -import java.util.*; | ||
| 5 | import io.swagger.annotations.*; | 6 | import io.swagger.annotations.*; |
| 6 | import javax.validation.constraints.*; | 7 | import javax.validation.constraints.*; |
| 7 | 8 | ||
| @@ -12,8 +13,6 @@ import javax.validation.constraints.*; | @@ -12,8 +13,6 @@ import javax.validation.constraints.*; | ||
| 12 | @Data | 13 | @Data |
| 13 | public class AppAddressBaseVO { | 14 | public class AppAddressBaseVO { |
| 14 | 15 | ||
| 15 | - // TODO @shuaidawang:swagger 注解的 example;其它 VO 类也要补充下 | ||
| 16 | - | ||
| 17 | @ApiModelProperty(value = "收件人名称", required = true) | 16 | @ApiModelProperty(value = "收件人名称", required = true) |
| 18 | @NotNull(message = "收件人名称不能为空") | 17 | @NotNull(message = "收件人名称不能为空") |
| 19 | private String name; | 18 | private String name; |
| @@ -30,8 +29,9 @@ public class AppAddressBaseVO { | @@ -30,8 +29,9 @@ public class AppAddressBaseVO { | ||
| 30 | @NotNull(message = "收件详细地址不能为空") | 29 | @NotNull(message = "收件详细地址不能为空") |
| 31 | private String detailAddress; | 30 | private String detailAddress; |
| 32 | 31 | ||
| 33 | - @ApiModelProperty(value = "地址类型", required = true) // TODO @shuaidawang:这个是枚举字段,最好说明下对应的枚举类 | 32 | + @ApiModelProperty(value = "地址类型", required = true) |
| 34 | @NotNull(message = "地址类型不能为空") | 33 | @NotNull(message = "地址类型不能为空") |
| 34 | + @InEnum(AddressTypeEnum.class) | ||
| 35 | private Integer type; | 35 | private Integer type; |
| 36 | 36 | ||
| 37 | } | 37 | } |
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java
| 1 | package cn.iocoder.yudao.module.member.dal.mysql.address; | 1 | package cn.iocoder.yudao.module.member.dal.mysql.address; |
| 2 | 2 | ||
| 3 | -import java.util.*; | ||
| 4 | 3 | ||
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 6 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 7 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | +import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; | ||
| 8 | import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; | 6 | import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; |
| 7 | +import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; | ||
| 9 | import org.apache.ibatis.annotations.Mapper; | 8 | import org.apache.ibatis.annotations.Mapper; |
| 10 | -import cn.iocoder.yudao.module.member.controller.app.address.vo.*; | 9 | + |
| 10 | +import java.util.List; | ||
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | * 用户收件地址 Mapper | 13 | * 用户收件地址 Mapper |
| @@ -17,16 +17,48 @@ import cn.iocoder.yudao.module.member.controller.app.address.vo.*; | @@ -17,16 +17,48 @@ import cn.iocoder.yudao.module.member.controller.app.address.vo.*; | ||
| 17 | @Mapper | 17 | @Mapper |
| 18 | public interface AddressMapper extends BaseMapperX<AddressDO> { | 18 | public interface AddressMapper extends BaseMapperX<AddressDO> { |
| 19 | 19 | ||
| 20 | - default PageResult<AddressDO> selectPage(AppAddressPageReqVO reqVO) { | ||
| 21 | - return selectPage(reqVO, new LambdaQueryWrapperX<AddressDO>() | ||
| 22 | - .eqIfPresent(AddressDO::getUserId, reqVO.getUserId()) | ||
| 23 | - .likeIfPresent(AddressDO::getName, reqVO.getName()) | ||
| 24 | - .eqIfPresent(AddressDO::getMobile, reqVO.getMobile()) | ||
| 25 | - .eqIfPresent(AddressDO::getAreaCode, reqVO.getAreaCode()) | ||
| 26 | - .eqIfPresent(AddressDO::getDetailAddress, reqVO.getDetailAddress()) | ||
| 27 | - .eqIfPresent(AddressDO::getType, reqVO.getType()) | ||
| 28 | - .betweenIfPresent(AddressDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) | ||
| 29 | - .orderByDesc(AddressDO::getId)); | 20 | + /** |
| 21 | + * 获取当前地址 根据id和userId | ||
| 22 | + * @param userId | ||
| 23 | + * @param id | ||
| 24 | + * @return | ||
| 25 | + */ | ||
| 26 | + default AddressDO getAddressByIdAndUserId(Long userId, Long id) { | ||
| 27 | + QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<>(); | ||
| 28 | + queryWrapperX.eq("user_id", userId).eq("id", id); | ||
| 29 | + return selectList(queryWrapperX).stream().findFirst().orElse(null); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 获取地址列表 | ||
| 34 | + * @param userId | ||
| 35 | + * @param type | ||
| 36 | + * @return | ||
| 37 | + */ | ||
| 38 | + default List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) { | ||
| 39 | + QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId) | ||
| 40 | + .eqIfPresent("type", type); | ||
| 41 | + return selectList(queryWrapperX); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * 获取默认地址 | ||
| 46 | + * @param userId | ||
| 47 | + * @return | ||
| 48 | + */ | ||
| 49 | + default AddressDO getDefaultUserAddress(Long userId) { | ||
| 50 | + List<AddressDO> addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); | ||
| 51 | + return addressDOList.stream().findFirst().orElse(null); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * 获取默认地址 | ||
| 56 | + * @param id | ||
| 57 | + * @param addressTypeEnum | ||
| 58 | + * @return | ||
| 59 | + */ | ||
| 60 | + default int updateTypeById(Long id, AddressTypeEnum addressTypeEnum) { | ||
| 61 | + return updateById(new AddressDO().setId(id).setType(addressTypeEnum.getType())); | ||
| 30 | } | 62 | } |
| 31 | 63 | ||
| 32 | } | 64 | } |
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java
| 1 | package cn.iocoder.yudao.module.member.enums; | 1 | package cn.iocoder.yudao.module.member.enums; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 3 | import lombok.AllArgsConstructor; | 4 | import lombok.AllArgsConstructor; |
| 4 | import lombok.Getter; | 5 | import lombok.Getter; |
| 5 | 6 | ||
| 7 | +import java.util.Arrays; | ||
| 8 | + | ||
| 6 | /** | 9 | /** |
| 7 | * 用户收件地址的类型枚举 | 10 | * 用户收件地址的类型枚举 |
| 8 | */ | 11 | */ |
| 9 | @Getter | 12 | @Getter |
| 10 | @AllArgsConstructor | 13 | @AllArgsConstructor |
| 11 | -public enum AddressTypeEnum { | 14 | +public enum AddressTypeEnum implements IntArrayValuable { |
| 12 | 15 | ||
| 13 | DEFAULT(1, "默认收件地址"), | 16 | DEFAULT(1, "默认收件地址"), |
| 14 | NORMAL(2, "普通收件地址"), // 即非默认收件地址 | 17 | NORMAL(2, "普通收件地址"), // 即非默认收件地址 |
| 15 | 18 | ||
| 16 | ; | 19 | ; |
| 17 | 20 | ||
| 21 | + public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AddressTypeEnum::getType).toArray(); | ||
| 22 | + | ||
| 18 | private final Integer type; | 23 | private final Integer type; |
| 19 | private final String desc; | 24 | private final String desc; |
| 20 | 25 | ||
| 26 | + @Override | ||
| 27 | + public int[] array() { | ||
| 28 | + return ARRAYS; | ||
| 29 | + } | ||
| 21 | } | 30 | } |
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java
| @@ -45,7 +45,7 @@ public interface AddressService { | @@ -45,7 +45,7 @@ public interface AddressService { | ||
| 45 | * @param id 编号 | 45 | * @param id 编号 |
| 46 | * @return 用户收件地址 | 46 | * @return 用户收件地址 |
| 47 | */ | 47 | */ |
| 48 | - AddressDO getAddress(Long id); | 48 | + AddressDO getAddress(Long userId, Long id); |
| 49 | 49 | ||
| 50 | /** | 50 | /** |
| 51 | * 获得用户收件地址列表 | 51 | * 获得用户收件地址列表 |
| @@ -54,15 +54,6 @@ public interface AddressService { | @@ -54,15 +54,6 @@ public interface AddressService { | ||
| 54 | * @return 用户收件地址列表 | 54 | * @return 用户收件地址列表 |
| 55 | */ | 55 | */ |
| 56 | List<AddressDO> getAddressList(Long userId); | 56 | List<AddressDO> getAddressList(Long userId); |
| 57 | - | ||
| 58 | - /** | ||
| 59 | - * 获得用户收件地址 | ||
| 60 | - * | ||
| 61 | - * @param userId 用户编号 | ||
| 62 | - * @param id 编号 | ||
| 63 | - * @return 用户收件地址 | ||
| 64 | - */ | ||
| 65 | - AddressDO getAddress(Long userId, Long id); | ||
| 66 | 57 | ||
| 67 | AddressDO getDefaultUserAddress(Long userId); | 58 | AddressDO getDefaultUserAddress(Long userId); |
| 68 | } | 59 | } |
yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java
| 1 | package cn.iocoder.yudao.module.member.service.address; | 1 | package cn.iocoder.yudao.module.member.service.address; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; | ||
| 4 | import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; | 3 | import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; |
| 5 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
| 6 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | 4 | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| 7 | -import org.checkerframework.checker.nullness.Opt; | ||
| 8 | import org.springframework.stereotype.Service; | 5 | import org.springframework.stereotype.Service; |
| 9 | 6 | ||
| 10 | -import javax.annotation.Nullable; | ||
| 11 | import javax.annotation.Resource; | 7 | import javax.annotation.Resource; |
| 8 | + | ||
| 9 | +import org.springframework.transaction.annotation.Transactional; | ||
| 12 | import org.springframework.validation.annotation.Validated; | 10 | import org.springframework.validation.annotation.Validated; |
| 13 | 11 | ||
| 14 | import java.util.*; | 12 | import java.util.*; |
| 15 | import cn.iocoder.yudao.module.member.controller.app.address.vo.*; | 13 | import cn.iocoder.yudao.module.member.controller.app.address.vo.*; |
| 16 | import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; | 14 | import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; |
| 17 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 18 | 15 | ||
| 19 | import cn.iocoder.yudao.module.member.convert.address.AddressConvert; | 16 | import cn.iocoder.yudao.module.member.convert.address.AddressConvert; |
| 20 | import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; | 17 | import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; |
| @@ -34,16 +31,21 @@ public class AddressServiceImpl implements AddressService { | @@ -34,16 +31,21 @@ public class AddressServiceImpl implements AddressService { | ||
| 34 | @Resource | 31 | @Resource |
| 35 | private AddressMapper addressMapper; | 32 | private AddressMapper addressMapper; |
| 36 | 33 | ||
| 37 | - @Override // TODO @shuaidawang:事务要加下哈 | 34 | + @Override |
| 35 | + @Transactional(rollbackFor = Exception.class) | ||
| 38 | public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) { | 36 | public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) { |
| 39 | // 如果添加的是默认收件地址,则将原默认地址修改为非默认 | 37 | // 如果添加的是默认收件地址,则将原默认地址修改为非默认 |
| 40 | if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) { | 38 | if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) { |
| 41 | - // TODO @shuaidawang:查询到一个,然后进行 update | 39 | + //查询到一个,然后进行 update |
| 42 | List<AddressDO> addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); | 40 | List<AddressDO> addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); |
| 41 | + AddressDO defaultUserAddress = addressMapper.getDefaultUserAddress(userId); | ||
| 43 | if (!CollectionUtils.isEmpty(addressDOs)) { | 42 | if (!CollectionUtils.isEmpty(addressDOs)) { |
| 44 | addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO() | 43 | addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO() |
| 45 | .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); | 44 | .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); |
| 46 | } | 45 | } |
| 46 | + Optional.ofNullable(defaultUserAddress) | ||
| 47 | + //更新为非默认 | ||
| 48 | + .ifPresent( u -> addressMapper.updateTypeById(u.getId(), AddressTypeEnum.NORMAL)); | ||
| 47 | } | 49 | } |
| 48 | // 插入 | 50 | // 插入 |
| 49 | AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); | 51 | AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); |
| @@ -53,30 +55,29 @@ public class AddressServiceImpl implements AddressService { | @@ -53,30 +55,29 @@ public class AddressServiceImpl implements AddressService { | ||
| 53 | return address.getId(); | 55 | return address.getId(); |
| 54 | } | 56 | } |
| 55 | 57 | ||
| 56 | - @Override // TODO @shuaidawang:事务要加下哈 | 58 | + @Override |
| 59 | + @Transactional(rollbackFor = Exception.class) | ||
| 57 | public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) { | 60 | public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) { |
| 58 | - // 校验存在,校验是否能够操作 TODO shuaidawang:改成基于 id + userId 查询,以前的做法不太好; | 61 | + // 校验存在,校验是否能够操作 |
| 59 | check(userId, updateReqVO.getId()); | 62 | check(userId, updateReqVO.getId()); |
| 60 | // 如果修改的是默认收件地址,则将原默认地址修改为非默认 | 63 | // 如果修改的是默认收件地址,则将原默认地址修改为非默认 |
| 61 | if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) { | 64 | if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) { |
| 62 | - // TODO @shuaidawang:查询到一个,然后进行 update,需要排除自己哈 | ||
| 63 | - List<AddressDO> addressDOs = selectListByUserIdAndType( | ||
| 64 | - userId, AddressTypeEnum.DEFAULT.getType()); | ||
| 65 | - if (!CollectionUtils.isEmpty(addressDOs)) { | ||
| 66 | - addressDOs.stream().filter(userAddressDO -> !userAddressDO.getId().equals(updateReqVO.getId())) // 过滤掉更新的收件地址 | ||
| 67 | - .forEach(userAddressDO -> addressMapper.updateById(new AddressDO() | ||
| 68 | - .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); | ||
| 69 | - } | 65 | + //获取默认地址 |
| 66 | + AddressDO defaultUserAddress = addressMapper.getDefaultUserAddress(userId); | ||
| 67 | + Optional.ofNullable(defaultUserAddress) | ||
| 68 | + //排除当前地址 | ||
| 69 | + .filter(u -> !u.getId().equals(updateReqVO.getId())) | ||
| 70 | + //更新为非默认 | ||
| 71 | + .ifPresent( u -> addressMapper.updateTypeById(u.getId(), AddressTypeEnum.NORMAL)); | ||
| 70 | } | 72 | } |
| 71 | // 更新 | 73 | // 更新 |
| 72 | AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO); | 74 | AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO); |
| 73 | - updateObj.setUserId(userId); // TODO @shuaidawang:不用加 userId | ||
| 74 | addressMapper.updateById(updateObj); | 75 | addressMapper.updateById(updateObj); |
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | @Override | 78 | @Override |
| 78 | public void deleteAddress(Long userId, Long id) { | 79 | public void deleteAddress(Long userId, Long id) { |
| 79 | - // 校验存在,校验是否能够操作 TODO shuaidawang:改成基于 id + userId 查询,以前的做法不太好; | 80 | + // 校验存在,校验是否能够操作 |
| 80 | check(userId, id); | 81 | check(userId, id); |
| 81 | // 删除 | 82 | // 删除 |
| 82 | addressMapper.deleteById(id); | 83 | addressMapper.deleteById(id); |
| @@ -89,7 +90,7 @@ public class AddressServiceImpl implements AddressService { | @@ -89,7 +90,7 @@ public class AddressServiceImpl implements AddressService { | ||
| 89 | * @param userAddressId 用户收件地址 | 90 | * @param userAddressId 用户收件地址 |
| 90 | */ | 91 | */ |
| 91 | private void check(Long userId, Long userAddressId) { | 92 | private void check(Long userId, Long userAddressId) { |
| 92 | - AddressDO addressDO = getAddress(userAddressId); | 93 | + AddressDO addressDO = getAddress(userId, userAddressId); |
| 93 | if(null == addressDO){ | 94 | if(null == addressDO){ |
| 94 | throw exception(ADDRESS_NOT_EXISTS); | 95 | throw exception(ADDRESS_NOT_EXISTS); |
| 95 | } | 96 | } |
| @@ -99,8 +100,8 @@ public class AddressServiceImpl implements AddressService { | @@ -99,8 +100,8 @@ public class AddressServiceImpl implements AddressService { | ||
| 99 | } | 100 | } |
| 100 | 101 | ||
| 101 | @Override | 102 | @Override |
| 102 | - public AddressDO getAddress(Long id) { | ||
| 103 | - return addressMapper.selectById(id); | 103 | + public AddressDO getAddress(Long userId, Long id) { |
| 104 | + return addressMapper.getAddressByIdAndUserId(userId, id); | ||
| 104 | } | 105 | } |
| 105 | 106 | ||
| 106 | @Override | 107 | @Override |
| @@ -108,13 +109,6 @@ public class AddressServiceImpl implements AddressService { | @@ -108,13 +109,6 @@ public class AddressServiceImpl implements AddressService { | ||
| 108 | return selectListByUserIdAndType(userId, null); | 109 | return selectListByUserIdAndType(userId, null); |
| 109 | } | 110 | } |
| 110 | 111 | ||
| 111 | - @Override | ||
| 112 | - public AddressDO getAddress(Long userId, Long id) { | ||
| 113 | - AddressDO address = getAddress(id); // TODO shuaidawang:改成基于 id + userId 查询,以前的做法不太好; | ||
| 114 | - check(userId, id); | ||
| 115 | - return address; | ||
| 116 | - } | ||
| 117 | - | ||
| 118 | /** | 112 | /** |
| 119 | * 获取默认地址 | 113 | * 获取默认地址 |
| 120 | * @param userId | 114 | * @param userId |
| @@ -122,12 +116,9 @@ public class AddressServiceImpl implements AddressService { | @@ -122,12 +116,9 @@ public class AddressServiceImpl implements AddressService { | ||
| 122 | */ | 116 | */ |
| 123 | @Override | 117 | @Override |
| 124 | public AddressDO getDefaultUserAddress(Long userId) { | 118 | public AddressDO getDefaultUserAddress(Long userId) { |
| 125 | - // TODO @shuaidawang:查询,都抽到 mapper 中 | ||
| 126 | - List<AddressDO> addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); | ||
| 127 | - return addressDOList.stream().findFirst().orElse(null); | 119 | + return addressMapper.getDefaultUserAddress(userId); |
| 128 | } | 120 | } |
| 129 | 121 | ||
| 130 | - // TODO @shuaidawang:查询,都抽到 mapper 中 | ||
| 131 | /** | 122 | /** |
| 132 | * 根据类型获取地址列表 | 123 | * 根据类型获取地址列表 |
| 133 | * @param userId | 124 | * @param userId |
| @@ -135,9 +126,7 @@ public class AddressServiceImpl implements AddressService { | @@ -135,9 +126,7 @@ public class AddressServiceImpl implements AddressService { | ||
| 135 | * @return | 126 | * @return |
| 136 | */ | 127 | */ |
| 137 | public List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) { | 128 | public List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) { |
| 138 | - QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId) | ||
| 139 | - .eqIfPresent("type", type); | ||
| 140 | - return addressMapper.selectList(queryWrapperX); | 129 | + return addressMapper.selectListByUserIdAndType(userId, type); |
| 141 | } | 130 | } |
| 142 | 131 | ||
| 143 | 132 |
yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java
| 1 | package cn.iocoder.yudao.module.member.service.address; | 1 | package cn.iocoder.yudao.module.member.service.address; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | 3 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 5 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO; | 4 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO; |
| 6 | -import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressExportReqVO; | ||
| 7 | -import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressPageReqVO; | ||
| 8 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; | 5 | import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; |
| 9 | import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; | 6 | import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; |
| 10 | import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; | 7 | import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; |
| @@ -13,7 +10,6 @@ import org.junit.jupiter.api.Test; | @@ -13,7 +10,6 @@ import org.junit.jupiter.api.Test; | ||
| 13 | import org.springframework.context.annotation.Import; | 10 | import org.springframework.context.annotation.Import; |
| 14 | 11 | ||
| 15 | import javax.annotation.Resource; | 12 | import javax.annotation.Resource; |
| 16 | -import java.util.List; | ||
| 17 | 13 | ||
| 18 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | 14 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| 19 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | 15 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| @@ -129,23 +125,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest { | @@ -129,23 +125,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest { | ||
| 129 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); | 125 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); |
| 130 | // 测试 createTime 不匹配 | 126 | // 测试 createTime 不匹配 |
| 131 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); | 127 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); |
| 132 | - // 准备参数 | ||
| 133 | - AppAddressPageReqVO reqVO = new AppAddressPageReqVO(); | ||
| 134 | - reqVO.setUserId(null); | ||
| 135 | - reqVO.setName(null); | ||
| 136 | - reqVO.setMobile(null); | ||
| 137 | - reqVO.setAreaCode(null); | ||
| 138 | - reqVO.setDetailAddress(null); | ||
| 139 | - reqVO.setType(null); | ||
| 140 | - reqVO.setBeginCreateTime(null); | ||
| 141 | - reqVO.setEndCreateTime(null); | ||
| 142 | - | ||
| 143 | - // 调用 | ||
| 144 | - PageResult<AddressDO> pageResult = addressService.getAddressPage(reqVO); | ||
| 145 | - // 断言 | ||
| 146 | - assertEquals(1, pageResult.getTotal()); | ||
| 147 | - assertEquals(1, pageResult.getList().size()); | ||
| 148 | - assertPojoEquals(dbAddress, pageResult.getList().get(0)); | ||
| 149 | } | 128 | } |
| 150 | 129 | ||
| 151 | @Test | 130 | @Test |
| @@ -176,22 +155,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest { | @@ -176,22 +155,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest { | ||
| 176 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); | 155 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); |
| 177 | // 测试 createTime 不匹配 | 156 | // 测试 createTime 不匹配 |
| 178 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); | 157 | addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); |
| 179 | - // 准备参数 | ||
| 180 | - AppAddressExportReqVO reqVO = new AppAddressExportReqVO(); | ||
| 181 | - reqVO.setUserId(null); | ||
| 182 | - reqVO.setName(null); | ||
| 183 | - reqVO.setMobile(null); | ||
| 184 | - reqVO.setAreaCode(null); | ||
| 185 | - reqVO.setDetailAddress(null); | ||
| 186 | - reqVO.setType(null); | ||
| 187 | - reqVO.setBeginCreateTime(null); | ||
| 188 | - reqVO.setEndCreateTime(null); | ||
| 189 | - | ||
| 190 | - // 调用 | ||
| 191 | - List<AddressDO> list = addressService.getAddressList(reqVO); | ||
| 192 | - // 断言 | ||
| 193 | - assertEquals(1, list.size()); | ||
| 194 | - assertPojoEquals(dbAddress, list.get(0)); | ||
| 195 | } | 158 | } |
| 196 | 159 | ||
| 197 | } | 160 | } |