diff --git a/mall-ums/ums-api/src/main/java/com/youlai/mall/ums/dto/MemberAddressDTO.java b/mall-ums/ums-api/src/main/java/com/youlai/mall/ums/dto/MemberAddressDTO.java index 2a54d6e83..a73422458 100644 --- a/mall-ums/ums-api/src/main/java/com/youlai/mall/ums/dto/MemberAddressDTO.java +++ b/mall-ums/ums-api/src/main/java/com/youlai/mall/ums/dto/MemberAddressDTO.java @@ -1,13 +1,6 @@ package com.youlai.mall.ums.dto; -import com.youlai.common.constraint.CheckCityValid; -import com.youlai.common.constraint.CityType; import lombok.Data; -import org.hibernate.validator.constraints.Length; - -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Pattern; -import javax.validation.constraints.Positive; /** * 会员地址传输层对象 @@ -18,25 +11,20 @@ import javax.validation.constraints.Positive; @Data public class MemberAddressDTO { - @NotNull(message = "{id.positive}") - @Positive(message = "{id.positive}") + private Long id; + private Long memberId; private String consigneeName; - @Pattern(regexp = "^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$", message = "{phone.valid}") private String consigneeMobile; - @CheckCityValid(CityType.PROVINCE) private String province; - @CheckCityValid(CityType.CITY) private String city; - @CheckCityValid(CityType.AREA) private String area; - @Length(min = 1, max = 100, message = "{text.length.min},{text.length.max}") private String detailAddress; private Integer defaulted; diff --git a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/app/AddressController.java b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/app/AddressController.java index 14450d91d..f6eb3e9c2 100644 --- a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/app/AddressController.java +++ b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/controller/app/AddressController.java @@ -1,10 +1,9 @@ package com.youlai.mall.ums.controller.app; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.youlai.common.result.Result; -import com.youlai.common.web.util.MemberUtils; import com.youlai.mall.ums.dto.MemberAddressDTO; import com.youlai.mall.ums.pojo.entity.UmsAddress; +import com.youlai.mall.ums.pojo.form.AddressForm; import com.youlai.mall.ums.service.IUmsAddressService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -30,7 +29,7 @@ public class AddressController { List addressList = addressService.listCurrentMemberAddresses(); return Result.success(addressList); } - + @ApiOperation(value = "获取地址详情") @GetMapping("/{addressId}") public Result getAddressDetail( @@ -43,9 +42,9 @@ public class AddressController { @ApiOperation(value = "新增地址") @PostMapping public Result addAddress( - @RequestBody @Validated UmsAddress address + @RequestBody @Validated AddressForm addressForm ) { - boolean result = addressService.addAddress(address); + boolean result = addressService.addAddress(addressForm); return Result.judge(result); } @@ -53,9 +52,9 @@ public class AddressController { @PutMapping("/{addressId}") public Result updateAddress( @ApiParam(value = "地址ID") @PathVariable Long addressId, - @RequestBody @Validated UmsAddress address + @RequestBody @Validated AddressForm addressForm ) { - boolean result = addressService.updateAddress(address); + boolean result = addressService.updateAddress(addressForm); return Result.judge(result); } diff --git a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/pojo/form/AddressForm.java b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/pojo/form/AddressForm.java new file mode 100644 index 000000000..867cc56cd --- /dev/null +++ b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/pojo/form/AddressForm.java @@ -0,0 +1,56 @@ +package com.youlai.mall.ums.pojo.form; + +import com.youlai.common.constraint.CheckCityValid; +import com.youlai.common.constraint.CityType; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Positive; + +/** + * 地址表单对象 + * + * @author haoxr + * @date 2022/2/12 15:57 + */ +@ApiModel("地址表单对象") +@Data +public class AddressForm { + + @ApiModelProperty("地址ID") + private Long id; + + @ApiModelProperty("收货人姓名") + private String consigneeName; + + @ApiModelProperty("收货人手机号") + @Pattern(regexp = "^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$", message = "{phone.valid}") + private String consigneeMobile; + + @ApiModelProperty("省") + @CheckCityValid(CityType.PROVINCE) + private String province; + + @ApiModelProperty("市") + @CheckCityValid(CityType.CITY) + private String city; + + @ApiModelProperty("区") + @CheckCityValid(CityType.AREA) + private String area; + + @ApiModelProperty("详细地址") + @Length(min = 1, max = 100, message = "{text.length.min},{text.length.max}") + private String detailAddress; + + @ApiModelProperty("是否默认地址(1:是;0:否)") + private Integer defaulted; + +} + + + diff --git a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/IUmsAddressService.java b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/IUmsAddressService.java index 9bcb9e264..7b2ac8b64 100644 --- a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/IUmsAddressService.java +++ b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/IUmsAddressService.java @@ -4,6 +4,7 @@ package com.youlai.mall.ums.service; import com.baomidou.mybatisplus.extension.service.IService; import com.youlai.mall.ums.dto.MemberAddressDTO; import com.youlai.mall.ums.pojo.entity.UmsAddress; +import com.youlai.mall.ums.pojo.form.AddressForm; import java.util.List; @@ -16,20 +17,20 @@ import java.util.List; public interface IUmsAddressService extends IService { /** - * 添加地址 + * 新增地址 * - * @param address + * @param addressForm * @return */ - boolean addAddress(UmsAddress address); + boolean addAddress(AddressForm addressForm); /** * 修改地址 * - * @param address + * @param addressForm * @return */ - boolean updateAddress(UmsAddress address); + boolean updateAddress(AddressForm addressForm); /** * 获取当前登录会员的地址列表 diff --git a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/impl/UmsAddressServiceImpl.java b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/impl/UmsAddressServiceImpl.java index bfbd67403..3ed31c188 100644 --- a/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/impl/UmsAddressServiceImpl.java +++ b/mall-ums/ums-boot/src/main/java/com/youlai/mall/ums/service/impl/UmsAddressServiceImpl.java @@ -9,9 +9,10 @@ import com.youlai.common.web.util.MemberUtils; import com.youlai.mall.ums.dto.MemberAddressDTO; import com.youlai.mall.ums.mapper.UmsAddressMapper; import com.youlai.mall.ums.pojo.entity.UmsAddress; +import com.youlai.mall.ums.pojo.form.AddressForm; import com.youlai.mall.ums.service.IUmsAddressService; -import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @@ -28,43 +29,59 @@ import java.util.stream.Collectors; public class UmsAddressServiceImpl extends ServiceImpl implements IUmsAddressService { /** - * 添加地址 + * 新增地址 * - * @param address + * @param addressForm * @return */ @Override - public boolean addAddress(UmsAddress address) { + @Transactional + public boolean addAddress(AddressForm addressForm) { Long memberId = MemberUtils.getMemberId(); - address.setMemberId(memberId); - if (GlobalConstants.STATUS_YES.equals(address.getDefaulted())) { // 修改其他默认地址为非默认 - this.update(new LambdaUpdateWrapper() - .eq(UmsAddress::getMemberId, memberId) - .eq(UmsAddress::getDefaulted, 1) - .set(UmsAddress::getDefaulted, 0) - ); + + UmsAddress umsAddress = new UmsAddress(); + BeanUtil.copyProperties(addressForm, umsAddress); + umsAddress.setMemberId(memberId); + boolean result = this.save(umsAddress); + if (result) { + // 修改其他默认地址为非默认 + if (GlobalConstants.STATUS_YES.equals(addressForm.getDefaulted())) { + this.update(new LambdaUpdateWrapper() + .eq(UmsAddress::getMemberId, memberId) + .eq(UmsAddress::getDefaulted, 1) + .set(UmsAddress::getDefaulted, 0) + ); + } } - return this.save(address); + return result; } /** * 修改地址 * - * @param address + * @param addressForm * @return */ @Override - public boolean updateAddress(UmsAddress address) { + public boolean updateAddress(AddressForm addressForm) { Long memberId = MemberUtils.getMemberId(); - // 修改其他默认地址为非默认 - if (GlobalConstants.STATUS_YES.equals(address.getDefaulted())) { - this.update(new LambdaUpdateWrapper() - .eq(UmsAddress::getMemberId, memberId) - .eq(UmsAddress::getDefaulted, 1) - .set(UmsAddress::getDefaulted, 0) - ); + + UmsAddress umsAddress = new UmsAddress(); + BeanUtil.copyProperties(addressForm, umsAddress); + + boolean result = this.updateById(umsAddress); + + if(result){ + // 修改其他默认地址为非默认 + if (GlobalConstants.STATUS_YES.equals(addressForm.getDefaulted())) { + this.update(new LambdaUpdateWrapper() + .eq(UmsAddress::getMemberId, memberId) + .eq(UmsAddress::getDefaulted, 1) + .set(UmsAddress::getDefaulted, 0) + ); + } } - return this.updateById(address); + return result; } /**