fix: 新增和修改会员地址问题修复

This commit is contained in:
郝先瑞 2022-02-26 23:18:31 +08:00
parent a2b4f38526
commit eebfdd48e4
5 changed files with 109 additions and 48 deletions

View File

@ -1,13 +1,6 @@
package com.youlai.mall.ums.dto; package com.youlai.mall.ums.dto;
import com.youlai.common.constraint.CheckCityValid;
import com.youlai.common.constraint.CityType;
import lombok.Data; 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 @Data
public class MemberAddressDTO { public class MemberAddressDTO {
@NotNull(message = "{id.positive}") private Long id;
@Positive(message = "{id.positive}")
private Long memberId; private Long memberId;
private String consigneeName; 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; private String consigneeMobile;
@CheckCityValid(CityType.PROVINCE)
private String province; private String province;
@CheckCityValid(CityType.CITY)
private String city; private String city;
@CheckCityValid(CityType.AREA)
private String area; private String area;
@Length(min = 1, max = 100, message = "{text.length.min}{text.length.max}")
private String detailAddress; private String detailAddress;
private Integer defaulted; private Integer defaulted;

View File

@ -1,10 +1,9 @@
package com.youlai.mall.ums.controller.app; 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.result.Result;
import com.youlai.common.web.util.MemberUtils;
import com.youlai.mall.ums.dto.MemberAddressDTO; import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.pojo.entity.UmsAddress; import com.youlai.mall.ums.pojo.entity.UmsAddress;
import com.youlai.mall.ums.pojo.form.AddressForm;
import com.youlai.mall.ums.service.IUmsAddressService; import com.youlai.mall.ums.service.IUmsAddressService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -30,7 +29,7 @@ public class AddressController {
List<MemberAddressDTO> addressList = addressService.listCurrentMemberAddresses(); List<MemberAddressDTO> addressList = addressService.listCurrentMemberAddresses();
return Result.success(addressList); return Result.success(addressList);
} }
@ApiOperation(value = "获取地址详情") @ApiOperation(value = "获取地址详情")
@GetMapping("/{addressId}") @GetMapping("/{addressId}")
public Result<UmsAddress> getAddressDetail( public Result<UmsAddress> getAddressDetail(
@ -43,9 +42,9 @@ public class AddressController {
@ApiOperation(value = "新增地址") @ApiOperation(value = "新增地址")
@PostMapping @PostMapping
public Result addAddress( 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); return Result.judge(result);
} }
@ -53,9 +52,9 @@ public class AddressController {
@PutMapping("/{addressId}") @PutMapping("/{addressId}")
public Result updateAddress( public Result updateAddress(
@ApiParam(value = "地址ID") @PathVariable Long addressId, @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); return Result.judge(result);
} }

View File

@ -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 <a href="mailto:xianrui0365@163.com">haoxr</a>
* @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;
}

View File

@ -4,6 +4,7 @@ package com.youlai.mall.ums.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.mall.ums.dto.MemberAddressDTO; import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.pojo.entity.UmsAddress; import com.youlai.mall.ums.pojo.entity.UmsAddress;
import com.youlai.mall.ums.pojo.form.AddressForm;
import java.util.List; import java.util.List;
@ -16,20 +17,20 @@ import java.util.List;
public interface IUmsAddressService extends IService<UmsAddress> { public interface IUmsAddressService extends IService<UmsAddress> {
/** /**
* 添加地址 * 新增地址
* *
* @param address * @param addressForm
* @return * @return
*/ */
boolean addAddress(UmsAddress address); boolean addAddress(AddressForm addressForm);
/** /**
* 修改地址 * 修改地址
* *
* @param address * @param addressForm
* @return * @return
*/ */
boolean updateAddress(UmsAddress address); boolean updateAddress(AddressForm addressForm);
/** /**
* 获取当前登录会员的地址列表 * 获取当前登录会员的地址列表

View File

@ -9,9 +9,10 @@ import com.youlai.common.web.util.MemberUtils;
import com.youlai.mall.ums.dto.MemberAddressDTO; import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.mapper.UmsAddressMapper; import com.youlai.mall.ums.mapper.UmsAddressMapper;
import com.youlai.mall.ums.pojo.entity.UmsAddress; import com.youlai.mall.ums.pojo.entity.UmsAddress;
import com.youlai.mall.ums.pojo.form.AddressForm;
import com.youlai.mall.ums.service.IUmsAddressService; import com.youlai.mall.ums.service.IUmsAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -28,43 +29,59 @@ import java.util.stream.Collectors;
public class UmsAddressServiceImpl extends ServiceImpl<UmsAddressMapper, UmsAddress> implements IUmsAddressService { public class UmsAddressServiceImpl extends ServiceImpl<UmsAddressMapper, UmsAddress> implements IUmsAddressService {
/** /**
* 添加地址 * 新增地址
* *
* @param address * @param addressForm
* @return * @return
*/ */
@Override @Override
public boolean addAddress(UmsAddress address) { @Transactional
public boolean addAddress(AddressForm addressForm) {
Long memberId = MemberUtils.getMemberId(); Long memberId = MemberUtils.getMemberId();
address.setMemberId(memberId);
if (GlobalConstants.STATUS_YES.equals(address.getDefaulted())) { // 修改其他默认地址为非默认 UmsAddress umsAddress = new UmsAddress();
this.update(new LambdaUpdateWrapper<UmsAddress>() BeanUtil.copyProperties(addressForm, umsAddress);
.eq(UmsAddress::getMemberId, memberId) umsAddress.setMemberId(memberId);
.eq(UmsAddress::getDefaulted, 1) boolean result = this.save(umsAddress);
.set(UmsAddress::getDefaulted, 0) if (result) {
); // 修改其他默认地址为非默认
if (GlobalConstants.STATUS_YES.equals(addressForm.getDefaulted())) {
this.update(new LambdaUpdateWrapper<UmsAddress>()
.eq(UmsAddress::getMemberId, memberId)
.eq(UmsAddress::getDefaulted, 1)
.set(UmsAddress::getDefaulted, 0)
);
}
} }
return this.save(address); return result;
} }
/** /**
* 修改地址 * 修改地址
* *
* @param address * @param addressForm
* @return * @return
*/ */
@Override @Override
public boolean updateAddress(UmsAddress address) { public boolean updateAddress(AddressForm addressForm) {
Long memberId = MemberUtils.getMemberId(); Long memberId = MemberUtils.getMemberId();
// 修改其他默认地址为非默认
if (GlobalConstants.STATUS_YES.equals(address.getDefaulted())) { UmsAddress umsAddress = new UmsAddress();
this.update(new LambdaUpdateWrapper<UmsAddress>() BeanUtil.copyProperties(addressForm, umsAddress);
.eq(UmsAddress::getMemberId, memberId)
.eq(UmsAddress::getDefaulted, 1) boolean result = this.updateById(umsAddress);
.set(UmsAddress::getDefaulted, 0)
); if(result){
// 修改其他默认地址为非默认
if (GlobalConstants.STATUS_YES.equals(addressForm.getDefaulted())) {
this.update(new LambdaUpdateWrapper<UmsAddress>()
.eq(UmsAddress::getMemberId, memberId)
.eq(UmsAddress::getDefaulted, 1)
.set(UmsAddress::getDefaulted, 0)
);
}
} }
return this.updateById(address); return result;
} }
/** /**