!129 [feature/1.8.0-uniapp][用户收件地址] code review 代码调整

Merge pull request !129 from 臭小子/feature/1.8.0-uniapp
This commit is contained in:
芋道源码 2022-04-19 02:16:41 +00:00 committed by Gitee
commit b24803cf22
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 86 additions and 105 deletions

View File

@ -1,9 +1,7 @@
package cn.iocoder.yudao.module.member.controller.app.address; package cn.iocoder.yudao.module.member.controller.app.address;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; 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.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.AppAddressRespVO;
import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO; import cn.iocoder.yudao.module.member.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.address.AddressConvert; 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.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.Collection;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

View File

@ -1,7 +1,8 @@
package cn.iocoder.yudao.module.member.controller.app.address.vo; 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 lombok.*;
import java.util.*;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import javax.validation.constraints.*; import javax.validation.constraints.*;
@ -12,8 +13,6 @@ import javax.validation.constraints.*;
@Data @Data
public class AppAddressBaseVO { public class AppAddressBaseVO {
// TODO @shuaidawangswagger 注解的 example其它 VO 类也要补充下
@ApiModelProperty(value = "收件人名称", required = true) @ApiModelProperty(value = "收件人名称", required = true)
@NotNull(message = "收件人名称不能为空") @NotNull(message = "收件人名称不能为空")
private String name; private String name;
@ -30,8 +29,9 @@ public class AppAddressBaseVO {
@NotNull(message = "收件详细地址不能为空") @NotNull(message = "收件详细地址不能为空")
private String detailAddress; private String detailAddress;
@ApiModelProperty(value = "地址类型", required = true) // TODO @shuaidawang这个是枚举字段最好说明下对应的枚举类 @ApiModelProperty(value = "地址类型", required = true)
@NotNull(message = "地址类型不能为空") @NotNull(message = "地址类型不能为空")
@InEnum(AddressTypeEnum.class)
private Integer type; private Integer type;
} }

View File

@ -1,13 +1,13 @@
package cn.iocoder.yudao.module.member.dal.mysql.address; 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.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.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.enums.AddressTypeEnum;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.member.controller.app.address.vo.*;
import java.util.List;
/** /**
* 用户收件地址 Mapper * 用户收件地址 Mapper
@ -17,16 +17,48 @@ import cn.iocoder.yudao.module.member.controller.app.address.vo.*;
@Mapper @Mapper
public interface AddressMapper extends BaseMapperX<AddressDO> { public interface AddressMapper extends BaseMapperX<AddressDO> {
default PageResult<AddressDO> selectPage(AppAddressPageReqVO reqVO) { /**
return selectPage(reqVO, new LambdaQueryWrapperX<AddressDO>() * 获取当前地址 根据id和userId
.eqIfPresent(AddressDO::getUserId, reqVO.getUserId()) * @param userId
.likeIfPresent(AddressDO::getName, reqVO.getName()) * @param id
.eqIfPresent(AddressDO::getMobile, reqVO.getMobile()) * @return
.eqIfPresent(AddressDO::getAreaCode, reqVO.getAreaCode()) */
.eqIfPresent(AddressDO::getDetailAddress, reqVO.getDetailAddress()) default AddressDO getAddressByIdAndUserId(Long userId, Long id) {
.eqIfPresent(AddressDO::getType, reqVO.getType()) QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<>();
.betweenIfPresent(AddressDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) queryWrapperX.eq("user_id", userId).eq("id", id);
.orderByDesc(AddressDO::getId)); return selectList(queryWrapperX).stream().findFirst().orElse(null);
}
/**
* 获取地址列表
* @param userId
* @param type
* @return
*/
default List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) {
QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId)
.eqIfPresent("type", type);
return selectList(queryWrapperX);
}
/**
* 获取默认地址
* @param userId
* @return
*/
default AddressDO getDefaultUserAddress(Long userId) {
List<AddressDO> 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()));
} }
} }

View File

@ -1,21 +1,30 @@
package cn.iocoder.yudao.module.member.enums; package cn.iocoder.yudao.module.member.enums;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import java.util.Arrays;
/** /**
* 用户收件地址的类型枚举 * 用户收件地址的类型枚举
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum AddressTypeEnum { public enum AddressTypeEnum implements IntArrayValuable {
DEFAULT(1, "默认收件地址"), DEFAULT(1, "默认收件地址"),
NORMAL(2, "普通收件地址"), // 即非默认收件地址 NORMAL(2, "普通收件地址"), // 即非默认收件地址
; ;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AddressTypeEnum::getType).toArray();
private final Integer type; private final Integer type;
private final String desc; private final String desc;
@Override
public int[] array() {
return ARRAYS;
}
} }

View File

@ -45,7 +45,7 @@ public interface AddressService {
* @param id 编号 * @param id 编号
* @return 用户收件地址 * @return 用户收件地址
*/ */
AddressDO getAddress(Long id); AddressDO getAddress(Long userId, Long id);
/** /**
* 获得用户收件地址列表 * 获得用户收件地址列表
@ -54,15 +54,6 @@ public interface AddressService {
* @return 用户收件地址列表 * @return 用户收件地址列表
*/ */
List<AddressDO> getAddressList(Long userId); List<AddressDO> getAddressList(Long userId);
/**
* 获得用户收件地址
*
* @param userId 用户编号
* @param id 编号
* @return 用户收件地址
*/
AddressDO getAddress(Long userId, Long id);
AddressDO getDefaultUserAddress(Long userId); AddressDO getDefaultUserAddress(Long userId);
} }

View File

@ -1,20 +1,17 @@
package cn.iocoder.yudao.module.member.service.address; 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 cn.iocoder.yudao.module.member.enums.AddressTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.checkerframework.checker.nullness.Opt;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Nullable;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.util.*; import java.util.*;
import cn.iocoder.yudao.module.member.controller.app.address.vo.*; import cn.iocoder.yudao.module.member.controller.app.address.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; 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.convert.address.AddressConvert;
import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper;
@ -34,16 +31,21 @@ public class AddressServiceImpl implements AddressService {
@Resource @Resource
private AddressMapper addressMapper; private AddressMapper addressMapper;
@Override // TODO @shuaidawang事务要加下哈 @Override
@Transactional(rollbackFor = Exception.class)
public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) { public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) {
// 如果添加的是默认收件地址则将原默认地址修改为非默认 // 如果添加的是默认收件地址则将原默认地址修改为非默认
if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) { if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) {
// TODO @shuaidawang查询到一个然后进行 update //查询到一个然后进行 update
List<AddressDO> addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); List<AddressDO> addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType());
AddressDO defaultUserAddress = addressMapper.getDefaultUserAddress(userId);
if (!CollectionUtils.isEmpty(addressDOs)) { if (!CollectionUtils.isEmpty(addressDOs)) {
addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO() addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO()
.setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType())));
} }
Optional.ofNullable(defaultUserAddress)
//更新为非默认
.ifPresent( u -> addressMapper.updateTypeById(u.getId(), AddressTypeEnum.NORMAL));
} }
// 插入 // 插入
AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); AddressDO address = AddressConvert.INSTANCE.convert(createReqVO);
@ -53,30 +55,29 @@ public class AddressServiceImpl implements AddressService {
return address.getId(); return address.getId();
} }
@Override // TODO @shuaidawang事务要加下哈 @Override
@Transactional(rollbackFor = Exception.class)
public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) { public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) {
// 校验存在,校验是否能够操作 TODO shuaidawang改成基于 id + userId 查询以前的做法不太好 // 校验存在,校验是否能够操作
check(userId, updateReqVO.getId()); check(userId, updateReqVO.getId());
// 如果修改的是默认收件地址则将原默认地址修改为非默认 // 如果修改的是默认收件地址则将原默认地址修改为非默认
if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) { if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) {
// TODO @shuaidawang查询到一个然后进行 update需要排除自己哈 //获取默认地址
List<AddressDO> addressDOs = selectListByUserIdAndType( AddressDO defaultUserAddress = addressMapper.getDefaultUserAddress(userId);
userId, AddressTypeEnum.DEFAULT.getType()); Optional.ofNullable(defaultUserAddress)
if (!CollectionUtils.isEmpty(addressDOs)) { //排除当前地址
addressDOs.stream().filter(userAddressDO -> !userAddressDO.getId().equals(updateReqVO.getId())) // 过滤掉更新的收件地址 .filter(u -> !u.getId().equals(updateReqVO.getId()))
.forEach(userAddressDO -> addressMapper.updateById(new AddressDO() //更新为非默认
.setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); .ifPresent( u -> addressMapper.updateTypeById(u.getId(), AddressTypeEnum.NORMAL));
}
} }
// 更新 // 更新
AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO); AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO);
updateObj.setUserId(userId); // TODO @shuaidawang不用加 userId
addressMapper.updateById(updateObj); addressMapper.updateById(updateObj);
} }
@Override @Override
public void deleteAddress(Long userId, Long id) { public void deleteAddress(Long userId, Long id) {
// 校验存在,校验是否能够操作 TODO shuaidawang改成基于 id + userId 查询以前的做法不太好 // 校验存在,校验是否能够操作
check(userId, id); check(userId, id);
// 删除 // 删除
addressMapper.deleteById(id); addressMapper.deleteById(id);
@ -89,7 +90,7 @@ public class AddressServiceImpl implements AddressService {
* @param userAddressId 用户收件地址 * @param userAddressId 用户收件地址
*/ */
private void check(Long userId, Long userAddressId) { private void check(Long userId, Long userAddressId) {
AddressDO addressDO = getAddress(userAddressId); AddressDO addressDO = getAddress(userId, userAddressId);
if(null == addressDO){ if(null == addressDO){
throw exception(ADDRESS_NOT_EXISTS); throw exception(ADDRESS_NOT_EXISTS);
} }
@ -99,8 +100,8 @@ public class AddressServiceImpl implements AddressService {
} }
@Override @Override
public AddressDO getAddress(Long id) { public AddressDO getAddress(Long userId, Long id) {
return addressMapper.selectById(id); return addressMapper.getAddressByIdAndUserId(userId, id);
} }
@Override @Override
@ -108,13 +109,6 @@ public class AddressServiceImpl implements AddressService {
return selectListByUserIdAndType(userId, null); 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 * @param userId
@ -122,12 +116,9 @@ public class AddressServiceImpl implements AddressService {
*/ */
@Override @Override
public AddressDO getDefaultUserAddress(Long userId) { public AddressDO getDefaultUserAddress(Long userId) {
// TODO @shuaidawang查询都抽到 mapper return addressMapper.getDefaultUserAddress(userId);
List<AddressDO> addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType());
return addressDOList.stream().findFirst().orElse(null);
} }
// TODO @shuaidawang查询都抽到 mapper
/** /**
* 根据类型获取地址列表 * 根据类型获取地址列表
* @param userId * @param userId
@ -135,9 +126,7 @@ public class AddressServiceImpl implements AddressService {
* @return * @return
*/ */
public List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) { public List<AddressDO> selectListByUserIdAndType(Long userId, Integer type) {
QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId) return addressMapper.selectListByUserIdAndType(userId, type);
.eqIfPresent("type", type);
return addressMapper.selectList(queryWrapperX);
} }

View File

@ -1,10 +1,7 @@
package cn.iocoder.yudao.module.member.service.address; 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.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.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.controller.app.address.vo.AppAddressUpdateReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO;
import cn.iocoder.yudao.module.member.dal.mysql.address.AddressMapper; 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 org.springframework.context.annotation.Import;
import javax.annotation.Resource; 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.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; 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))); addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null)));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); 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<AddressDO> pageResult = addressService.getAddressPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbAddress, pageResult.getList().get(0));
} }
@Test @Test
@ -176,22 +155,6 @@ public class AddressServiceImplTest extends BaseDbUnitTest {
addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null))); addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setType(null)));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setCreateTime(null))); 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<AddressDO> list = addressService.getAddressList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbAddress, list.get(0));
} }
} }