diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java index aaa477816..57821c43a 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java @@ -23,5 +23,6 @@ public interface ErrorCodeConstants { // ========== 用户收件地址 1004004000 ========== ErrorCode ADDRESS_NOT_EXISTS = new ErrorCode(1004004000, "用户收件地址不存在"); + ErrorCode ADDRESS_FORBIDDEN = new ErrorCode(1004004001, "没有该操作权限"); } diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.http b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.http new file mode 100644 index 000000000..7f943448a --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.http @@ -0,0 +1,54 @@ +### 请求 /create 接口 => 成功 +POST {{appApi}}//member/address/create +Content-Type: application/json +tenant-id: {{appTenentId}} +Authorization: Bearer 2510e2e4287346eb8e36353a55e27fd6 + +{ + "userId": "245", + "name": "yunai", + "mobile": "15601691300", + "areaCode": "610632", + "detailAddress": "芋道源码 233 号 666 室", + "type": "1" +} + +### 请求 /update 接口 => 成功 +PUT {{appApi}}//member/address/update +Content-Type: application/json +tenant-id: {{appTenentId}} +Authorization: Bearer 2510e2e4287346eb8e36353a55e27fd6 + +{ + "id": "1", + "userId": "245", + "name": "yunai888", + "mobile": "15601691300", + "areaCode": "610632", + "detailAddress": "芋道源码 233 号 666 室", + "type": "1" +} + +### 请求 /delete 接口 => 成功 +DELETE {{appApi}}//member/address/delete?id=2 +Content-Type: application/json +tenant-id: {{appTenentId}} +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8 + +### 请求 /get 接口 => 成功 +GET {{appApi}}//member/address/get?id=1 +Content-Type: application/json +tenant-id: {{appTenentId}} +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8 + +### 请求 /get-default 接口 => 成功 +GET {{appApi}}//member/address/get-default +Content-Type: application/json +tenant-id: {{appTenentId}} +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8 + +### 请求 /list 接口 => 成功 +GET {{appApi}}//member/address/list +Content-Type: application/json +tenant-id: {{appTenentId}} +Authorization: Bearer fa4848b001de4eae9faf516c0c8520f8 \ No newline at end of file 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 8407e8987..74e9f8acd 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 @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Api(tags = "用户 APP - 用户收件地址") @RestController @@ -35,14 +36,14 @@ public class AppAddressController { @ApiOperation("创建用户收件地址") public CommonResult createAddress(@Valid @RequestBody AppAddressCreateReqVO createReqVO) { - return success(addressService.createAddress(createReqVO)); + return success(addressService.createAddress(getLoginUserId(), createReqVO)); } @PutMapping("/update") @ApiOperation("更新用户收件地址") public CommonResult updateAddress(@Valid @RequestBody AppAddressUpdateReqVO updateReqVO) { - addressService.updateAddress(updateReqVO); + addressService.updateAddress(getLoginUserId(), updateReqVO); return success(true); } @@ -51,7 +52,7 @@ public class AppAddressController { @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) public CommonResult deleteAddress(@RequestParam("id") Long id) { - addressService.deleteAddress(id); + addressService.deleteAddress(getLoginUserId(), id); return success(true); } @@ -60,25 +61,24 @@ public class AppAddressController { @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) public CommonResult getAddress(@RequestParam("id") Long id) { - AddressDO address = addressService.getAddress(id); + AddressDO address = addressService.getAddress(getLoginUserId(), id); + return success(AddressConvert.INSTANCE.convert(address)); + } + + @GetMapping("/get-default") + @ApiOperation("获得默认的用户收件地址") + + public CommonResult getDefaultUserAddress() { + AddressDO address = addressService.getDefaultUserAddress(getLoginUserId()); return success(AddressConvert.INSTANCE.convert(address)); } @GetMapping("/list") @ApiOperation("获得用户收件地址列表") - @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) - public CommonResult> getAddressList(@RequestParam("ids") Collection ids) { - List list = addressService.getAddressList(ids); + public CommonResult> getAddressList() { + List list = addressService.getAddressList(getLoginUserId()); return success(AddressConvert.INSTANCE.convertList(list)); } - @GetMapping("/page") - @ApiOperation("获得用户收件地址分页") - - public CommonResult> getAddressPage(@Valid AppAddressPageReqVO pageVO) { - PageResult pageResult = addressService.getAddressPage(pageVO); - return success(AddressConvert.INSTANCE.convertPage(pageResult)); - } - } 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 72589bb8b..b66248996 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 @@ -12,10 +12,6 @@ import javax.validation.constraints.*; @Data public class AppAddressBaseVO { - @ApiModelProperty(value = "用户编号", required = true) - @NotNull(message = "用户编号不能为空") - private Integer userId; - @ApiModelProperty(value = "收件人名称", required = true) @NotNull(message = "收件人名称不能为空") private String name; diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java index 278479552..e0982b6c4 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java @@ -13,7 +13,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ public class AppAddressExportReqVO { @ApiModelProperty(value = "用户编号") - private Integer userId; + private Long userId; @ApiModelProperty(value = "收件人名称") private String name; diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java index 90fc176f8..402e52296 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java @@ -15,7 +15,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ public class AppAddressPageReqVO extends PageParam { @ApiModelProperty(value = "用户编号") - private Integer userId; + private Long userId; @ApiModelProperty(value = "收件人名称") private String name; diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java index c3a525674..fe02a1e47 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java @@ -28,7 +28,7 @@ public class AddressDO extends BaseDO { /** * 用户编号 */ - private Integer userId; + private Long userId; /** * 收件人名称 */ 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 67e93e7da..d446e128f 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 @@ -16,24 +16,28 @@ public interface AddressService { /** * 创建用户收件地址 * + * + * @param userId 用户编号 * @param createReqVO 创建信息 * @return 编号 */ - Long createAddress(@Valid AppAddressCreateReqVO createReqVO); + Long createAddress(Long userId, @Valid AppAddressCreateReqVO createReqVO); /** * 更新用户收件地址 * + * @param userId 用户编号 * @param updateReqVO 更新信息 */ - void updateAddress(@Valid AppAddressUpdateReqVO updateReqVO); + void updateAddress(Long userId, @Valid AppAddressUpdateReqVO updateReqVO); /** * 删除用户收件地址 * + * @param userId 用户编号 * @param id 编号 */ - void deleteAddress(Long id); + void deleteAddress(Long userId, Long id); /** * 获得用户收件地址 @@ -46,10 +50,10 @@ public interface AddressService { /** * 获得用户收件地址列表 * - * @param ids 编号 + * @param userId 用户编号 * @return 用户收件地址列表 */ - List getAddressList(Collection ids); + List getAddressList(Long userId); /** * 获得用户收件地址分页 @@ -67,4 +71,14 @@ public interface AddressService { */ List getAddressList(AppAddressExportReqVO exportReqVO); + /** + * 获得用户收件地址 + * + * @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 5f6c5350e..bdc094f3a 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,6 +1,13 @@ 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.validation.annotation.Validated; @@ -28,35 +35,65 @@ public class AddressServiceImpl implements AddressService { private AddressMapper addressMapper; @Override - public Long createAddress(AppAddressCreateReqVO createReqVO) { + public Long createAddress(Long userId, AppAddressCreateReqVO createReqVO) { + // 如果添加的是默认收件地址,则将原默认地址修改为非默认 + if (AddressTypeEnum.DEFAULT.getType().equals(createReqVO.getType())) { + List addressDOs = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); + if (!CollectionUtils.isEmpty(addressDOs)) { + addressDOs.forEach(userAddressDO -> addressMapper.updateById(new AddressDO() + .setId(userAddressDO.getId()).setType(AddressTypeEnum.NORMAL.getType()))); + } + } // 插入 AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); + address.setUserId(userId); addressMapper.insert(address); // 返回 return address.getId(); } @Override - public void updateAddress(AppAddressUpdateReqVO updateReqVO) { - // 校验存在 - this.validateAddressExists(updateReqVO.getId()); + public void updateAddress(Long userId, AppAddressUpdateReqVO updateReqVO) { + // 校验存在,校验是否能够操作 + check(userId, updateReqVO.getId()); + // 如果修改的是默认收件地址,则将原默认地址修改为非默认 + if (AddressTypeEnum.DEFAULT.getType().equals(updateReqVO.getType())) { + 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 updateObj = AddressConvert.INSTANCE.convert(updateReqVO); + updateObj.setUserId(userId); addressMapper.updateById(updateObj); } @Override - public void deleteAddress(Long id) { - // 校验存在 - this.validateAddressExists(id); + public void deleteAddress(Long userId, Long id) { + // 校验存在,校验是否能够操作 + check(userId, id); // 删除 addressMapper.deleteById(id); } - private void validateAddressExists(Long id) { - if (addressMapper.selectById(id) == null) { + /** + * 校验用户收件地址是不是属于该用户 + * + * @param userId 用户编号 + * @param userAddressId 用户收件地址 + */ + private void check(Long userId, Long userAddressId) { + AddressDO addressDO = getAddress(userAddressId); + if(null == addressDO){ throw exception(ADDRESS_NOT_EXISTS); } + if (!addressDO.getUserId().equals(userId)) { + throw exception(ADDRESS_FORBIDDEN); + } } @Override @@ -65,8 +102,8 @@ public class AddressServiceImpl implements AddressService { } @Override - public List getAddressList(Collection ids) { - return addressMapper.selectBatchIds(ids); + public List getAddressList(Long userId) { + return selectListByUserIdAndType(userId, null); } @Override @@ -79,4 +116,35 @@ public class AddressServiceImpl implements AddressService { return addressMapper.selectList(exportReqVO); } + @Override + public AddressDO getAddress(Long userId, Long id) { + AddressDO address = getAddress(id); + check(userId, id); + return address; + } + + /** + * 获取默认地址 + * @param userId + * @return + */ + @Override + public AddressDO getDefaultUserAddress(Long userId) { + List addressDOList = selectListByUserIdAndType(userId, AddressTypeEnum.DEFAULT.getType()); + return addressDOList.stream().findFirst().orElse(null); + } + + /** + * 根据类型获取地址列表 + * @param userId + * @param type null则查询全部 + * @return + */ + public List selectListByUserIdAndType(Long userId, Integer type) { + QueryWrapperX queryWrapperX = new QueryWrapperX().eq("user_id", userId) + .eqIfPresent("type", type); + return addressMapper.selectList(queryWrapperX); + } + + } 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 d741269a1..8812e3c97 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 @@ -16,6 +16,7 @@ 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; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; @@ -43,7 +44,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest { AppAddressCreateReqVO reqVO = randomPojo(AppAddressCreateReqVO.class); // 调用 - Long addressId = addressService.createAddress(reqVO); + Long addressId = addressService.createAddress(getLoginUserId(), reqVO); // 断言 assertNotNull(addressId); // 校验记录的属性是否正确 @@ -62,7 +63,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest { }); // 调用 - addressService.updateAddress(reqVO); + addressService.updateAddress(getLoginUserId(), reqVO); // 校验是否更新正确 AddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的 assertPojoEquals(reqVO, address); @@ -74,7 +75,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest { AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class); // 调用, 并断言异常 - assertServiceException(() -> addressService.updateAddress(reqVO), ADDRESS_NOT_EXISTS); + assertServiceException(() -> addressService.updateAddress(getLoginUserId(), reqVO), ADDRESS_NOT_EXISTS); } @Test @@ -86,7 +87,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest { Long id = dbAddress.getId(); // 调用 - addressService.deleteAddress(id); + addressService.deleteAddress(getLoginUserId(), id); // 校验数据不存在了 assertNull(addressMapper.selectById(id)); } @@ -97,7 +98,7 @@ public class AddressServiceImplTest extends BaseDbUnitTest { Long id = randomLongId(); // 调用, 并断言异常 - assertServiceException(() -> addressService.deleteAddress(id), ADDRESS_NOT_EXISTS); + assertServiceException(() -> addressService.deleteAddress(getLoginUserId(), id), ADDRESS_NOT_EXISTS); } @Test diff --git a/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/create_tables.sql b/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/create_tables.sql index 0beed9646..608a3bddf 100644 --- a/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/create_tables.sql +++ b/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/create_tables.sql @@ -32,7 +32,7 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( CREATE TABLE IF NOT EXISTS "member_address" ( "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, - "user_id" int(11) NOT NULL, + "user_id" bigint(20) NOT NULL, "name" varchar(10) NOT NULL, "mobile" varchar(20) NOT NULL, "area_code" int(11) NOT NULL, @@ -43,6 +43,7 @@ CREATE TABLE IF NOT EXISTS "member_address" ( "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, "deleted" bit NOT NULL DEFAULT FALSE, "updater" varchar(64) DEFAULT '', + "tenant_id" bigint(20) NOT NULL, PRIMARY KEY ("id") ) COMMENT '用户收件地址';