From 1deb04d2561891aab3efe610faf18aaf6f131b62 Mon Sep 17 00:00:00 2001 From: shuaidawang Date: Tue, 12 Apr 2022 11:57:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=94=B6=E4=BB=B6?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/enums/ErrorCodeConstants.java | 3 + .../app/address/AppAddressController.java | 84 ++++++++ .../controller/app/address/package-info.java | 1 - .../app/address/vo/AppAddressBaseVO.java | 39 ++++ .../app/address/vo/AppAddressCreateReqVO.java | 14 ++ .../app/address/vo/AppAddressExportReqVO.java | 41 ++++ .../app/address/vo/AppAddressPageReqVO.java | 43 ++++ .../app/address/vo/AppAddressRespVO.java | 19 ++ .../app/address/vo/AppAddressUpdateReqVO.java | 18 ++ .../convert/address/AddressConvert.java | 32 +++ .../dal/dataobject/address/AddressDO.java | 55 +++++ .../dal/dataobject/address/package-info.java | 1 - .../dal/mysql/address/AddressMapper.java | 44 ++++ .../module/member/enums/AddressTypeEnum.java | 21 ++ .../service/address/AddressService.java | 70 +++++++ .../service/address/AddressServiceImpl.java | 82 ++++++++ .../address/AddressServiceImplTest.java | 196 ++++++++++++++++++ .../src/test/resources/sql/clean.sql | 1 + .../src/test/resources/sql/create_tables.sql | 16 ++ 19 files changed, 778 insertions(+), 2 deletions(-) create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java delete mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/package-info.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressCreateReqVO.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressRespVO.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressUpdateReqVO.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/convert/address/AddressConvert.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java delete mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/package-info.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java create mode 100644 yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java 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 9970c1ade..aaa477816 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 @@ -21,4 +21,7 @@ public interface ErrorCodeConstants { ErrorCode AUTH_TOKEN_EXPIRED = new ErrorCode(1004003004, "Token 已经过期"); ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1004003005, "未绑定账号,需要进行绑定"); + // ========== 用户收件地址 1004004000 ========== + ErrorCode ADDRESS_NOT_EXISTS = new ErrorCode(1004004000, "用户收件地址不存在"); + } 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 new file mode 100644 index 000000000..8407e8987 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.java @@ -0,0 +1,84 @@ +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; +import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; +import cn.iocoder.yudao.module.member.service.address.AddressService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.springframework.validation.annotation.Validated; +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; + +@Api(tags = "用户 APP - 用户收件地址") +@RestController +@RequestMapping("/member/address") +@Validated +public class AppAddressController { + + @Resource + private AddressService addressService; + + @PostMapping("/create") + @ApiOperation("创建用户收件地址") + + public CommonResult createAddress(@Valid @RequestBody AppAddressCreateReqVO createReqVO) { + return success(addressService.createAddress(createReqVO)); + } + + @PutMapping("/update") + @ApiOperation("更新用户收件地址") + + public CommonResult updateAddress(@Valid @RequestBody AppAddressUpdateReqVO updateReqVO) { + addressService.updateAddress(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @ApiOperation("删除用户收件地址") + @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) + + public CommonResult deleteAddress(@RequestParam("id") Long id) { + addressService.deleteAddress(id); + return success(true); + } + + @GetMapping("/get") + @ApiOperation("获得用户收件地址") + @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) + + public CommonResult getAddress(@RequestParam("id") Long id) { + AddressDO address = addressService.getAddress(id); + 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); + 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/package-info.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/package-info.java deleted file mode 100644 index c8c102186..000000000 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.yudao.module.member.controller.app.address; 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 new file mode 100644 index 000000000..72589bb8b --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressBaseVO.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.module.member.controller.app.address.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +/** +* 用户收件地址 Base VO,提供给添加、修改、详细的子 VO 使用 +* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 +*/ +@Data +public class AppAddressBaseVO { + + @ApiModelProperty(value = "用户编号", required = true) + @NotNull(message = "用户编号不能为空") + private Integer userId; + + @ApiModelProperty(value = "收件人名称", required = true) + @NotNull(message = "收件人名称不能为空") + private String name; + + @ApiModelProperty(value = "手机号", required = true) + @NotNull(message = "手机号不能为空") + private String mobile; + + @ApiModelProperty(value = "地区编码", required = true) + @NotNull(message = "地区编码不能为空") + private Integer areaCode; + + @ApiModelProperty(value = "收件详细地址", required = true) + @NotNull(message = "收件详细地址不能为空") + private String detailAddress; + + @ApiModelProperty(value = "地址类型", required = true) + @NotNull(message = "地址类型不能为空") + private Integer type; + +} diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressCreateReqVO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressCreateReqVO.java new file mode 100644 index 000000000..31fd89fa3 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressCreateReqVO.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.module.member.controller.app.address.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +@ApiModel("用户 APP - 用户收件地址创建 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AppAddressCreateReqVO extends AppAddressBaseVO { + +} 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 new file mode 100644 index 000000000..278479552 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressExportReqVO.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.module.member.controller.app.address.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import org.springframework.format.annotation.DateTimeFormat; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel(value = "用户 APP - 用户收件地址 Excel 导出 Request VO", description = "参数和 AddressPageReqVO 是一致的") +@Data +public class AppAddressExportReqVO { + + @ApiModelProperty(value = "用户编号") + private Integer userId; + + @ApiModelProperty(value = "收件人名称") + private String name; + + @ApiModelProperty(value = "手机号") + private String mobile; + + @ApiModelProperty(value = "地区编码") + private Integer areaCode; + + @ApiModelProperty(value = "收件详细地址") + private String detailAddress; + + @ApiModelProperty(value = "地址类型") + private Integer type; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始创建时间") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束创建时间") + private Date endCreateTime; + +} 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 new file mode 100644 index 000000000..90fc176f8 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressPageReqVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.yudao.module.member.controller.app.address.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import org.springframework.format.annotation.DateTimeFormat; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel("用户 APP - 用户收件地址分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AppAddressPageReqVO extends PageParam { + + @ApiModelProperty(value = "用户编号") + private Integer userId; + + @ApiModelProperty(value = "收件人名称") + private String name; + + @ApiModelProperty(value = "手机号") + private String mobile; + + @ApiModelProperty(value = "地区编码") + private Integer areaCode; + + @ApiModelProperty(value = "收件详细地址") + private String detailAddress; + + @ApiModelProperty(value = "地址类型") + private Integer type; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始创建时间") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束创建时间") + private Date endCreateTime; + +} diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressRespVO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressRespVO.java new file mode 100644 index 000000000..1005b288c --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressRespVO.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.member.controller.app.address.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; + +@ApiModel("用户 APP - 用户收件地址 Response VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AppAddressRespVO extends AppAddressBaseVO { + + @ApiModelProperty(value = "编号", required = true) + private Long id; + + @ApiModelProperty(value = "创建时间", required = true) + private Date createTime; + +} diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressUpdateReqVO.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressUpdateReqVO.java new file mode 100644 index 000000000..b4100c76a --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/vo/AppAddressUpdateReqVO.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.module.member.controller.app.address.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +@ApiModel("用户 APP - 用户收件地址更新 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class AppAddressUpdateReqVO extends AppAddressBaseVO { + + @ApiModelProperty(value = "编号", required = true) + @NotNull(message = "编号不能为空") + private Long id; + +} diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/convert/address/AddressConvert.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/convert/address/AddressConvert.java new file mode 100644 index 000000000..fdc01e5a8 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/convert/address/AddressConvert.java @@ -0,0 +1,32 @@ +package cn.iocoder.yudao.module.member.convert.address; + +import java.util.*; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; +import cn.iocoder.yudao.module.member.controller.app.address.vo.*; +import cn.iocoder.yudao.module.member.dal.dataobject.address.AddressDO; + +/** + * 用户收件地址 Convert + * + * @author 芋道源码 + */ +@Mapper +public interface AddressConvert { + + AddressConvert INSTANCE = Mappers.getMapper(AddressConvert.class); + + AddressDO convert(AppAddressCreateReqVO bean); + + AddressDO convert(AppAddressUpdateReqVO bean); + + AppAddressRespVO convert(AddressDO bean); + + List convertList(List list); + + PageResult convertPage(PageResult page); + +} 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 new file mode 100644 index 000000000..c3a525674 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/AddressDO.java @@ -0,0 +1,55 @@ +package cn.iocoder.yudao.module.member.dal.dataobject.address; + +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.module.member.enums.AddressTypeEnum; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.*; + +/** + * 用户收件地址 DO + * + * @author 芋道源码 + */ +@TableName("member_address") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class AddressDO extends BaseDO { + + /** + * 编号 + */ + @TableId + private Long id; + /** + * 用户编号 + */ + private Integer userId; + /** + * 收件人名称 + */ + private String name; + /** + * 手机号 + */ + private String mobile; + /** + * 地区编码 + */ + private Integer areaCode; + /** + * 收件详细地址 + */ + private String detailAddress; + /** + * 地址类型,主要分为默认地址,和普通地址 + * + * 外键 {@link AddressTypeEnum} + */ + private Integer type; + +} diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/package-info.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/package-info.java deleted file mode 100644 index c318d2c94..000000000 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/dataobject/address/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.yudao.module.member.dal.dataobject.address; 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 new file mode 100644 index 000000000..81de47665 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/address/AddressMapper.java @@ -0,0 +1,44 @@ +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.module.member.dal.dataobject.address.AddressDO; +import org.apache.ibatis.annotations.Mapper; +import cn.iocoder.yudao.module.member.controller.app.address.vo.*; + +/** + * 用户收件地址 Mapper + * + * @author 芋道源码 + */ +@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)); + } + + default List selectList(AppAddressExportReqVO reqVO) { + return selectList(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)); + } + +} 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 new file mode 100644 index 000000000..f5eeb8382 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/enums/AddressTypeEnum.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.member.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 用户收件地址的类型枚举 + */ +@Getter +@AllArgsConstructor +public enum AddressTypeEnum { + + DEFAULT(1, "默认收件地址"), + NORMAL(2, "普通收件地址"), // 即非默认收件笛之爱 + + ; + + private final Integer type; + private final String desc; + +} \ No newline at end of file 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 new file mode 100644 index 000000000..67e93e7da --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressService.java @@ -0,0 +1,70 @@ +package cn.iocoder.yudao.module.member.service.address; + +import java.util.*; +import javax.validation.*; +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; + +/** + * 用户收件地址 Service 接口 + * + * @author 芋道源码 + */ +public interface AddressService { + + /** + * 创建用户收件地址 + * + * @param createReqVO 创建信息 + * @return 编号 + */ + Long createAddress(@Valid AppAddressCreateReqVO createReqVO); + + /** + * 更新用户收件地址 + * + * @param updateReqVO 更新信息 + */ + void updateAddress(@Valid AppAddressUpdateReqVO updateReqVO); + + /** + * 删除用户收件地址 + * + * @param id 编号 + */ + void deleteAddress(Long id); + + /** + * 获得用户收件地址 + * + * @param id 编号 + * @return 用户收件地址 + */ + AddressDO getAddress(Long id); + + /** + * 获得用户收件地址列表 + * + * @param ids 编号 + * @return 用户收件地址列表 + */ + List getAddressList(Collection ids); + + /** + * 获得用户收件地址分页 + * + * @param pageReqVO 分页查询 + * @return 用户收件地址分页 + */ + PageResult getAddressPage(AppAddressPageReqVO pageReqVO); + + /** + * 获得用户收件地址列表, 用于 Excel 导出 + * + * @param exportReqVO 查询条件 + * @return 用户收件地址列表 + */ + List getAddressList(AppAddressExportReqVO exportReqVO); + +} 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 new file mode 100644 index 000000000..5f6c5350e --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImpl.java @@ -0,0 +1,82 @@ +package cn.iocoder.yudao.module.member.service.address; + +import org.springframework.stereotype.Service; +import javax.annotation.Resource; +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; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*; + +/** + * 用户收件地址 Service 实现类 + * + * @author 芋道源码 + */ +@Service +@Validated +public class AddressServiceImpl implements AddressService { + + @Resource + private AddressMapper addressMapper; + + @Override + public Long createAddress(AppAddressCreateReqVO createReqVO) { + // 插入 + AddressDO address = AddressConvert.INSTANCE.convert(createReqVO); + addressMapper.insert(address); + // 返回 + return address.getId(); + } + + @Override + public void updateAddress(AppAddressUpdateReqVO updateReqVO) { + // 校验存在 + this.validateAddressExists(updateReqVO.getId()); + // 更新 + AddressDO updateObj = AddressConvert.INSTANCE.convert(updateReqVO); + addressMapper.updateById(updateObj); + } + + @Override + public void deleteAddress(Long id) { + // 校验存在 + this.validateAddressExists(id); + // 删除 + addressMapper.deleteById(id); + } + + private void validateAddressExists(Long id) { + if (addressMapper.selectById(id) == null) { + throw exception(ADDRESS_NOT_EXISTS); + } + } + + @Override + public AddressDO getAddress(Long id) { + return addressMapper.selectById(id); + } + + @Override + public List getAddressList(Collection ids) { + return addressMapper.selectBatchIds(ids); + } + + @Override + public PageResult getAddressPage(AppAddressPageReqVO pageReqVO) { + return addressMapper.selectPage(pageReqVO); + } + + @Override + public List getAddressList(AppAddressExportReqVO exportReqVO) { + return addressMapper.selectList(exportReqVO); + } + +} 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 new file mode 100644 index 000000000..d741269a1 --- /dev/null +++ b/yudao-module-member/yudao-module-member-impl/src/test/java/cn/iocoder/yudao/module/member/service/address/AddressServiceImplTest.java @@ -0,0 +1,196 @@ +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; +import org.junit.jupiter.api.Disabled; +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.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; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; +import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ADDRESS_NOT_EXISTS; +import static org.junit.jupiter.api.Assertions.*; + +/** +* {@link AddressServiceImpl} 的单元测试类 +* +* @author 芋道源码 +*/ +@Import(AddressServiceImpl.class) +public class AddressServiceImplTest extends BaseDbUnitTest { + + @Resource + private AddressServiceImpl addressService; + + @Resource + private AddressMapper addressMapper; + + @Test + public void testCreateAddress_success() { + // 准备参数 + AppAddressCreateReqVO reqVO = randomPojo(AppAddressCreateReqVO.class); + + // 调用 + Long addressId = addressService.createAddress(reqVO); + // 断言 + assertNotNull(addressId); + // 校验记录的属性是否正确 + AddressDO address = addressMapper.selectById(addressId); + assertPojoEquals(reqVO, address); + } + + @Test + public void testUpdateAddress_success() { + // mock 数据 + AddressDO dbAddress = randomPojo(AddressDO.class); + addressMapper.insert(dbAddress);// @Sql: 先插入出一条存在的数据 + // 准备参数 + AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class, o -> { + o.setId(dbAddress.getId()); // 设置更新的 ID + }); + + // 调用 + addressService.updateAddress(reqVO); + // 校验是否更新正确 + AddressDO address = addressMapper.selectById(reqVO.getId()); // 获取最新的 + assertPojoEquals(reqVO, address); + } + + @Test + public void testUpdateAddress_notExists() { + // 准备参数 + AppAddressUpdateReqVO reqVO = randomPojo(AppAddressUpdateReqVO.class); + + // 调用, 并断言异常 + assertServiceException(() -> addressService.updateAddress(reqVO), ADDRESS_NOT_EXISTS); + } + + @Test + public void testDeleteAddress_success() { + // mock 数据 + AddressDO dbAddress = randomPojo(AddressDO.class); + addressMapper.insert(dbAddress);// @Sql: 先插入出一条存在的数据 + // 准备参数 + Long id = dbAddress.getId(); + + // 调用 + addressService.deleteAddress(id); + // 校验数据不存在了 + assertNull(addressMapper.selectById(id)); + } + + @Test + public void testDeleteAddress_notExists() { + // 准备参数 + Long id = randomLongId(); + + // 调用, 并断言异常 + assertServiceException(() -> addressService.deleteAddress(id), ADDRESS_NOT_EXISTS); + } + + @Test + @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 + public void ins() { + // mock 数据 + AddressDO dbAddress = randomPojo(AddressDO.class, o -> { // 等会查询到 + o.setUserId(null); + o.setName(null); + o.setMobile(null); + o.setAreaCode(null); + o.setDetailAddress(null); + o.setType(null); + o.setCreateTime(null); + }); + addressMapper.insert(dbAddress); + // 测试 userId 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setUserId(null))); + // 测试 name 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setName(null))); + // 测试 mobile 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setMobile(null))); + // 测试 areaCode 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setAreaCode(null))); + // 测试 detailAddress 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setDetailAddress(null))); + // 测试 type 不匹配 + 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 + @Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 + public void testGetAddressList() { + // mock 数据 + AddressDO dbAddress = randomPojo(AddressDO.class, o -> { // 等会查询到 + o.setUserId(null); + o.setName(null); + o.setMobile(null); + o.setAreaCode(null); + o.setDetailAddress(null); + o.setType(null); + o.setCreateTime(null); + }); + addressMapper.insert(dbAddress); + // 测试 userId 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setUserId(null))); + // 测试 name 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setName(null))); + // 测试 mobile 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setMobile(null))); + // 测试 areaCode 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setAreaCode(null))); + // 测试 detailAddress 不匹配 + addressMapper.insert(cloneIgnoreId(dbAddress, o -> o.setDetailAddress(null))); + // 测试 type 不匹配 + 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)); + } + +} diff --git a/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/clean.sql b/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/clean.sql index 92f559dc1..bb8eddf50 100644 --- a/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/clean.sql +++ b/yudao-module-member/yudao-module-member-impl/src/test/resources/sql/clean.sql @@ -1 +1,2 @@ DELETE FROM "member_user"; +DELETE FROM "member_address"; 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 85925f30b..0beed9646 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 @@ -30,3 +30,19 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( PRIMARY KEY ("id") ) COMMENT '文件表'; +CREATE TABLE IF NOT EXISTS "member_address" ( + "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "user_id" int(11) NOT NULL, + "name" varchar(10) NOT NULL, + "mobile" varchar(20) NOT NULL, + "area_code" int(11) NOT NULL, + "detail_address" varchar(250) NOT NULL, + "type" tinyint(4) NOT NULL, + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + "creator" varchar(64) DEFAULT '', + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + "deleted" bit NOT NULL DEFAULT FALSE, + "updater" varchar(64) DEFAULT '', + PRIMARY KEY ("id") + ) COMMENT '用户收件地址'; + From 4a4097cfb9e122124b7b7ec85556b1d981c74f2c Mon Sep 17 00:00:00 2001 From: shuaidawang Date: Thu, 14 Apr 2022 15:47:37 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=94=B6=E4=BB=B6?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E6=8E=A5=E5=8F=A3=E5=BC=80=E5=8F=91=E8=B0=83?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/enums/ErrorCodeConstants.java | 1 + .../app/address/AppAddressController.http | 54 +++++++++++ .../app/address/AppAddressController.java | 30 +++---- .../app/address/vo/AppAddressBaseVO.java | 4 - .../app/address/vo/AppAddressExportReqVO.java | 2 +- .../app/address/vo/AppAddressPageReqVO.java | 2 +- .../dal/dataobject/address/AddressDO.java | 2 +- .../service/address/AddressService.java | 24 +++-- .../service/address/AddressServiceImpl.java | 90 ++++++++++++++++--- .../address/AddressServiceImplTest.java | 11 +-- .../src/test/resources/sql/create_tables.sql | 3 +- 11 files changed, 179 insertions(+), 44 deletions(-) create mode 100644 yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/controller/app/address/AppAddressController.http 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 '用户收件地址'; From 57ea98b508faa97745b7259cd6811c907350aa7e Mon Sep 17 00:00:00 2001 From: shuaidawang Date: Thu, 14 Apr 2022 15:49:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=94=B6=E4=BB=B6?= =?UTF-8?q?=E5=9C=B0=E5=9D=80sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/ruoyi-vue-pro.sql | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sql/ruoyi-vue-pro.sql b/sql/ruoyi-vue-pro.sql index 96183a703..46520aef1 100644 --- a/sql/ruoyi-vue-pro.sql +++ b/sql/ruoyi-vue-pro.sql @@ -1923,6 +1923,28 @@ INSERT INTO `member_user` VALUES (245, 'yunai222', 'http://pic.616pic.com/ys_b_i INSERT INTO `member_user` VALUES (246, '', '', 0, '15601691301', '$2a$10$KLvmwoU.bvjU2u/MeWa1iOX2GDRJ2P9YqaCad10bYQCiyOaPexGwW', '127.0.0.1', '127.0.0.1', '2021-10-10 22:36:27', NULL, '2021-10-10 22:36:27', NULL, '2022-02-27 04:14:35', b'0', 1); COMMIT; +-- ---------------------------- +-- Table structure for member_address +-- ---------------------------- +DROP TABLE IF EXISTS `member_address`; +CREATE TABLE `member_address` ( + `id` bigint NOT NULL AUTO_INCREMENT COMMENT '收件地址编号', + `user_id` bigint NOT NULL COMMENT '用户编号', + `name` varchar(10) COLLATE utf8mb4_bin NOT NULL COMMENT '收件人名称', + `mobile` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '手机号', + `area_code` int(11) NOT NULL COMMENT '地区编码', + `detail_address` varchar(250) COLLATE utf8mb4_bin NOT NULL COMMENT '收件详细地址', + `type` tinyint(4) NOT NULL COMMENT '地址类型', + `creator` varchar(64) DEFAULT '' COMMENT '创建者', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `updater` varchar(64) DEFAULT '' COMMENT '更新者', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '删除状态', + `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号', + PRIMARY KEY (`id`) USING BTREE, + KEY `idx_userId` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='用户收件地址'; + -- ---------------------------- -- Table structure for pay_app -- ----------------------------