From c38fadcae153144740411c6d3c6296b8be7eb3d3 Mon Sep 17 00:00:00 2001 From: shuaidawang Date: Mon, 18 Apr 2022 15:13:32 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=E4=BB=A3=E7=A0=81=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/address/AppAddressController.java | 3 - .../app/address/vo/AppAddressBaseVO.java | 8 +-- .../dal/mysql/address/AddressMapper.java | 60 +++++++++++++----- .../module/member/enums/AddressTypeEnum.java | 11 +++- .../service/address/AddressService.java | 11 +--- .../service/address/AddressServiceImpl.java | 61 ++++++++----------- .../address/AddressServiceImplTest.java | 37 ----------- 7 files changed, 86 insertions(+), 105 deletions(-) diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java index ed7c157d2..ee75982fa 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java @@ -1,9 +1,7 @@ package cn.iocoder.yudao.module.member.controller.app.address; import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO; -import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressPageReqVO; import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressRespVO; import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; import cn.iocoder.yudao.module.member.convert.address.AddressConvert; @@ -17,7 +15,6 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; -import java.util.Collection; import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java index a1439f8bc..81231a881 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java @@ -1,7 +1,8 @@ package cn.iocoder.yudao.module.member.controller.app.address.vo; +import cn.iocoder.yudao.framework.common.validation.InEnum; +import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; import lombok.*; -import java.util.*; import io.swagger.annotations.*; import javax.validation.constraints.*; @@ -12,8 +13,6 @@ import javax.validation.constraints.*; @Data public class AppAddressBaseVO { - // TODO @shuaidawang:swagger 注解的 example;其它 VO 类也要补充下 - @ApiModelProperty(value = "收件人名称", required = true) @NotNull(message = "收件人名称不能为空") private String name; @@ -30,8 +29,9 @@ public class AppAddressBaseVO { @NotNull(message = "收件详细地址不能为空") private String detailAddress; - @ApiModelProperty(value = "地址类型", required = true) // TODO @shuaidawang:这个是枚举字段,最好说明下对应的枚举类 + @ApiModelProperty(value = "地址类型", required = true) @NotNull(message = "地址类型不能为空") + @InEnum(AddressTypeEnum.class) private Integer type; } diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java index 2925ada1f..fc02e5879 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java @@ -1,13 +1,13 @@ package cn.iocoder.yudao.module.member.dal.mysql.address; -import java.util.*; -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; +import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; import org.apache.ibatis.annotations.Mapper; -import cn.iocoder.yudao.module.member.controller.app.address.vo.*; + +import java.util.List; /** * 用户收件地址 Mapper @@ -17,16 +17,48 @@ import cn.iocoder.yudao.module.member.controller.app.address.vo.*; @Mapper public interface AddressMapper extends BaseMapperX { - default PageResult selectPage(AppAddressPageReqVO reqVO) { - return selectPage(reqVO, new LambdaQueryWrapperX() - .eqIfPresent(AddressDO::getUserId, reqVO.getUserId()) - .likeIfPresent(AddressDO::getName, reqVO.getName()) - .eqIfPresent(AddressDO::getMobile, reqVO.getMobile()) - .eqIfPresent(AddressDO::getAreaCode, reqVO.getAreaCode()) - .eqIfPresent(AddressDO::getDetailAddress, reqVO.getDetailAddress()) - .eqIfPresent(AddressDO::getType, reqVO.getType()) - .betweenIfPresent(AddressDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) - .orderByDesc(AddressDO::getId)); + /** + * 获取当前地址 根据id和userId + * @param userId + * @param id + * @return + */ + default AddressDO getAddressByIdAndUserId(Long userId, Long id) { + QueryWrapperX queryWrapperX = new QueryWrapperX<>(); + queryWrapperX.eq("user_id", userId).eq("id", id); + return selectList(queryWrapperX).stream().findFirst().orElse(null); + } + + /** + * 获取地址列表 + * @param userId + * @param type + * @return + */ + default List selectListByUserIdAndType(Long userId, Integer type) { + QueryWrapperX queryWrapperX = new QueryWrapperX().eq("user_id", userId) + .eqIfPresent("type", type); + return selectList(queryWrapperX); + } + + /** + * 获取默认地址 + * @param userId + * @return + */ + default AddressDO getDefaultUserAddress(Long userId) { + List addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); + return addressDOList.stream().findFirst().orElse(null); + } + + /** + * 获取默认地址 + * @param id + * @param addressTypeEnum + * @return + */ + default int updateTypeById(Long id, AddressTypeEnum addressTypeEnum) { + return updateById(new AddressDO().setId(id).setType(addressTypeEnum.getType())); } } diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java index c7d199241..59465e495 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java @@ -1,21 +1,30 @@ package cn.iocoder.yudao.module.member.enums; +import cn.iocoder.yudao.framework.common.core.IntArrayValuable; import lombok.AllArgsConstructor; import lombok.Getter; +import java.util.Arrays; + /** * 用户收件地址的类型枚举 */ @Getter @AllArgsConstructor -public enum AddressTypeEnum { +public enum AddressTypeEnum implements IntArrayValuable { DEFAULT(1, "默认收件地址"), NORMAL(2, "普通收件地址"), // 即非默认收件地址 ; + public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AddressTypeEnum::getType).toArray(); + private final Integer type; private final String desc; + @Override + public int[] array() { + return ARRAYS; + } } diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java index 0a0f3c8d0..e45b075fe 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java +++ b/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 { * @param id 编号 * @return 用户收件地址 */ - AddressDO getAddress(Long id); + AddressDO getAddress(Long userId, Long id); /** * 获得用户收件地址列表 @@ -54,15 +54,6 @@ public interface AddressService { * @return 用户收件地址列表 */ List getAddressList(Long userId); - - /** - * 获得用户收件地址 - * - * @param userId 用户编号 - * @param id 编号 - * @return 用户收件地址 - */ - AddressDO getAddress(Long userId, Long id); AddressDO getDefaultUserAddress(Long userId); } diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java index 37ecc503b..a9cd91d59 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java @@ -1,20 +1,17 @@ package cn.iocoder.yudao.module.member.service.address; -import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import org.checkerframework.checker.nullness.Opt; import org.springframework.stereotype.Service; -import javax.annotation.Nullable; import javax.annotation.Resource; + +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.util.*; import cn.iocoder.yudao.module.member.controller.app.address.vo.*; import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.member.convert.address.AddressConvert; import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; @@ -34,16 +31,21 @@ public class AddressServiceImpl implements AddressService { @Resource private AddressMapper addressMapper; - @Override // TODO @shuaidawang:事务要加下哈 + @Override + @Transactional(rollbackFor = Exception.class) public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) { // 如果添加的是默认收件地址,则将原默认地址修改为非默认 if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) { - // TODO @shuaidawang:查询到一个,然后进行 update + //查询到一个,然后进行 update List addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); + AddressDO defaultUserAddress = addressMapper.getDefaultUserAddress(userId); if (!CollectionUtils.isEmpty(addressDOs)) { addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO() .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); } + Optional.ofNullable(defaultUserAddress) + //更新为非默认 + .ifPresent( u -> addressMapper.updateTypeById(u.getId(), AddressTypeEnum.NORMAL)); } // 插入 AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); @@ -53,30 +55,29 @@ public class AddressServiceImpl implements AddressService { return address.getId(); } - @Override // TODO @shuaidawang:事务要加下哈 + @Override + @Transactional(rollbackFor = Exception.class) public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) { - // 校验存在,校验是否能够操作 TODO shuaidawang:改成基于 id + userId 查询,以前的做法不太好; + // 校验存在,校验是否能够操作 check(userId, updateReqVO.getId()); // 如果修改的是默认收件地址,则将原默认地址修改为非默认 if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) { - // TODO @shuaidawang:查询到一个,然后进行 update,需要排除自己哈 - List addressDOs = selectListByUserIdAndType( - userId, AddressTypeEnum.DEFAULT.getType()); - if (!CollectionUtils.isEmpty(addressDOs)) { - addressDOs.stream().filter(userAddressDO -> !userAddressDO.getId().equals(updateReqVO.getId())) // 过滤掉更新的收件地址 - .forEach(userAddressDO -> addressMapper.updateById(new AddressDO() - .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); - } + //获取默认地址 + AddressDO defaultUserAddress = addressMapper.getDefaultUserAddress(userId); + Optional.ofNullable(defaultUserAddress) + //排除当前地址 + .filter(u -> !u.getId().equals(updateReqVO.getId())) + //更新为非默认 + .ifPresent( u -> addressMapper.updateTypeById(u.getId(), AddressTypeEnum.NORMAL)); } // 更新 AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO); - updateObj.setUserId(userId); // TODO @shuaidawang:不用加 userId addressMapper.updateById(updateObj); } @Override public void deleteAddress(Long userId, Long id) { - // 校验存在,校验是否能够操作 TODO shuaidawang:改成基于 id + userId 查询,以前的做法不太好; + // 校验存在,校验是否能够操作 check(userId, id); // 删除 addressMapper.deleteById(id); @@ -89,7 +90,7 @@ public class AddressServiceImpl implements AddressService { * @param userAddressId 用户收件地址 */ private void check(Long userId, Long userAddressId) { - AddressDO addressDO = getAddress(userAddressId); + AddressDO addressDO = getAddress(userId, userAddressId); if(null == addressDO){ throw exception(ADDRESS_NOT_EXISTS); } @@ -99,8 +100,8 @@ public class AddressServiceImpl implements AddressService { } @Override - public AddressDO getAddress(Long id) { - return addressMapper.selectById(id); + public AddressDO getAddress(Long userId, Long id) { + return addressMapper.getAddressByIdAndUserId(userId, id); } @Override @@ -108,13 +109,6 @@ public class AddressServiceImpl implements AddressService { return selectListByUserIdAndType(userId, null); } - @Override - public AddressDO getAddress(Long userId, Long id) { - AddressDO address = getAddress(id); // TODO shuaidawang:改成基于 id + userId 查询,以前的做法不太好; - check(userId, id); - return address; - } - /** * 获取默认地址 * @param userId @@ -122,12 +116,9 @@ public class AddressServiceImpl implements AddressService { */ @Override public AddressDO getDefaultUserAddress(Long userId) { - // TODO @shuaidawang:查询,都抽到 mapper 中 - List addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); - return addressDOList.stream().findFirst().orElse(null); + return addressMapper.getDefaultUserAddress(userId); } - // TODO @shuaidawang:查询,都抽到 mapper 中 /** * 根据类型获取地址列表 * @param userId @@ -135,9 +126,7 @@ public class AddressServiceImpl implements AddressService { * @return */ public List selectListByUserIdAndType(Long userId, Integer type) { - QueryWrapperX queryWrapperX = new QueryWrapperX().eq("user_id", userId) - .eqIfPresent("type", type); - return addressMapper.selectList(queryWrapperX); + return addressMapper.selectListByUserIdAndType(userId, type); } diff --git a/yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java b/yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java index 8812e3c97..eefa2e13e 100644 --- a/yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java +++ b/yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java @@ -1,10 +1,7 @@ package cn.iocoder.yudao.module.member.service.address; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressCreateReqVO; -import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressExportReqVO; -import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressPageReqVO; import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; @@ -13,7 +10,6 @@ import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; import javax.annotation.Resource; -import java.util.List; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @@ -129,23 +125,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest { addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); // 测试 createTime 不匹配 addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); - // 准备参数 - AppAddressPageReqVO reqVO = new AppAddressPageReqVO(); - reqVO.setUserId(null); - reqVO.setName(null); - reqVO.setMobile(null); - reqVO.setAreaCode(null); - reqVO.setDetailAddress(null); - reqVO.setType(null); - reqVO.setBeginCreateTime(null); - reqVO.setEndCreateTime(null); - - // 调用 - PageResult pageResult = addressService.getAddressPage(reqVO); - // 断言 - assertEquals(1, pageResult.getTotal()); - assertEquals(1, pageResult.getList().size()); - assertPojoEquals(dbAddress, pageResult.getList().get(0)); } @Test @@ -176,22 +155,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest { addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); // 测试 createTime 不匹配 addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); - // 准备参数 - AppAddressExportReqVO reqVO = new AppAddressExportReqVO(); - reqVO.setUserId(null); - reqVO.setName(null); - reqVO.setMobile(null); - reqVO.setAreaCode(null); - reqVO.setDetailAddress(null); - reqVO.setType(null); - reqVO.setBeginCreateTime(null); - reqVO.setEndCreateTime(null); - - // 调用 - List list = addressService.getAddressList(reqVO); - // 断言 - assertEquals(1, list.size()); - assertPojoEquals(dbAddress, list.get(0)); } }