!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;
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;

View File

@ -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 @shuaidawangswagger 注解的 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;
}

View File

@ -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<AddressDO> {
default PageResult<AddressDO> selectPage(AppAddressPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<AddressDO>()
.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<AddressDO> 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<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;
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;
}
}

View File

@ -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<AddressDO> getAddressList(Long userId);
/**
* 获得用户收件地址
*
* @param userId 用户编号
* @param id 编号
* @return 用户收件地址
*/
AddressDO getAddress(Long userId, Long id);
AddressDO getDefaultUserAddress(Long userId);
}

View File

@ -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<AddressDO> 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<AddressDO> 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<AddressDO> 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<AddressDO> selectListByUserIdAndType(Long userId, Integer type) {
QueryWrapperX<AddressDO> queryWrapperX = new QueryWrapperX<AddressDO>().eq("user_id", userId)
.eqIfPresent("type", type);
return addressMapper.selectList(queryWrapperX);
return addressMapper.selectListByUserIdAndType(userId, type);
}

View File

@ -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<AddressDO> 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<AddressDO> list = addressService.getAddressList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbAddress, list.get(0));
}
}