散户地址业务功能开发v2

wxpay
LI-CCONG\李聪聪 7 months ago
parent b3495b88e9
commit cc1dec1d5a

@ -72,8 +72,8 @@
</dependency>
<!-- validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- swagger -->
<dependency>

@ -5,10 +5,7 @@ import cc.yunxi.common.domain.CommonResult;
import cc.yunxi.common.utils.BeanUtils;
import cc.yunxi.domain.dto.LocationDTO;
import cc.yunxi.domain.dto.UserDTO;
import cc.yunxi.domain.po.Client;
import cc.yunxi.domain.po.ClientAddress;
import cc.yunxi.domain.vo.client.ClientRespVO;
import cc.yunxi.domain.vo.client.ClientUpdateVO;
import cc.yunxi.domain.vo.clientaddress.ClientAddressReqVO;
import cc.yunxi.service.IClientAddressService;
import cc.yunxi.utils.UserContext;
@ -37,7 +34,7 @@ public class ClientAddressController {
@ApiOperation("散户地址列表")
@GetMapping("/list")
public CommonResult<List<ClientAddressReqVO>> queryClientByPage(@RequestParam("clientId") String clientId) {
public CommonResult<List<ClientAddressReqVO>> queryAddressList(@RequestParam("clientId") String clientId) {
List<ClientAddress> clientAddresses = addressService.queryAddressList(clientId);
List<ClientAddressReqVO> addressRespVOList = BeanUtils.copyList(clientAddresses, ClientAddressReqVO.class, (s, t) -> {
t.setLocationDTO(new LocationDTO(s.getLongitude(), s.getLatitude()));
@ -47,26 +44,26 @@ public class ClientAddressController {
@ApiOperation("添加地址")
@GetMapping("/info")
public CommonResult<String> getClientInfo(@RequestBody ClientAddressReqVO reqVO) {
@PostMapping("/add")
public CommonResult<String> addAddress(@RequestBody ClientAddressReqVO addressCreateVO) {
UserDTO userDTO = UserContext.getUser();
reqVO.setClientId(userDTO.getId());
String addressId = addressService.addAddress(reqVO);
addressCreateVO.setClientId(userDTO.getId());
String addressId = addressService.addAddress(addressCreateVO);
return CommonResult.success(addressId);
}
@ApiOperation("更新地址")
@PostMapping("/update")
public CommonResult<Boolean> updateClientInfo(@RequestBody ClientAddressReqVO reqVO) {
addressService.updateAddress(reqVO);
public CommonResult<Boolean> updateAddress(@RequestBody ClientAddressReqVO addressUpdateVO) {
addressService.updateAddress(addressUpdateVO);
return CommonResult.success(true);
}
@ApiOperation("删除地址")
@PostMapping("/remove")
public CommonResult<Boolean> updateClientInfo(@RequestParam("addressId") String addressId) {
@GetMapping("/remove")
public CommonResult<Boolean> deleteAddress(@RequestParam("addressId") String addressId) {
addressService.deleteAddress(addressId);
return CommonResult.success(true);
}

@ -7,49 +7,50 @@ import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
@ApiModel(description = "添加散户地址 Request VO")
@Data
public class ClientAddressReqVO {
interface addAddress {}
public interface AddAddressGroup extends Default {}
interface updateAddress {}
public interface UpdateAddressGroup extends Default {}
@ApiModelProperty("id")
@NotBlank(message = "id不能为空", groups = updateAddress.class)
@ApiModelProperty(value = "id 添加时忽略,更新时必填")
@NotBlank(message = "id不能为空", groups = UpdateAddressGroup.class)
private String id;
@ApiModelProperty("散户id")
@NotBlank(message = "散户id不能为空", groups = addAddress.class)
@ApiModelProperty(value = "散户id", hidden = true)
@NotBlank(message = "散户id不能为空", groups = AddAddressGroup.class)
private String clientId;
@ApiModelProperty("联系人姓名")
@NotBlank(message = "联系人姓名不能为空")
@ApiModelProperty(value = "联系人姓名", required = true)
@NotBlank(message = "联系人姓名不能为空", groups = AddAddressGroup.class)
private String receiveUserName;
@ApiModelProperty("联系人手机号")
@NotBlank(message = "联系人手机号不能为空")
@ApiModelProperty(value = "联系人手机号",required = true)
@NotBlank(message = "联系人手机号不能为空", groups = AddAddressGroup.class)
private String receiveMobilePhone;
@ApiModelProperty("城市")
@NotBlank(message = "城市不能为空")
@ApiModelProperty(value = "城市",required = true)
@NotBlank(message = "城市不能为空", groups = AddAddressGroup.class)
private String receiveCity;
@ApiModelProperty("地址")
@NotBlank(message = "地址不能为空")
@ApiModelProperty(value = "地址",required = true)
@NotBlank(message = "地址不能为空", groups = AddAddressGroup.class)
private String receiveStreet;
@ApiModelProperty("门牌号")
@NotBlank(message = "门牌号不能为空")
@ApiModelProperty(value = "门牌号",required = true)
@NotBlank(message = "门牌号不能为空", groups = AddAddressGroup.class)
private String receiveHouseNumber;
@ApiModelProperty("位置信息")
@ApiModelProperty(value = "位置信息",required = true)
@NotNull(message = "位置信息不能为空")
private LocationDTO locationDTO;
@ApiModelProperty("是否默认1是0否")
@NotNull(message = "默认地址不能为空")
@ApiModelProperty(value = "是否默认1是0否",required = true)
@NotNull(message = "默认地址不能为空", groups = AddAddressGroup.class)
private Integer isDefault;
}

@ -2,8 +2,8 @@ package cc.yunxi.service;
import cc.yunxi.domain.po.ClientAddress;
import cc.yunxi.domain.vo.clientaddress.ClientAddressReqVO;
import cc.yunxi.domain.vo.clientaddress.ClientAddressRespVO;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import java.util.List;
@ -38,12 +38,14 @@ public interface IClientAddressService extends IService<ClientAddress> {
/**
*
*/
@Validated({ClientAddressReqVO.AddAddressGroup.class})
String addAddress(@Valid ClientAddressReqVO addressCreateVO);
/**
*
*/
@Validated({ClientAddressReqVO.UpdateAddressGroup.class})
void updateAddress(@Valid ClientAddressReqVO addressUpdateVO);

@ -3,10 +3,9 @@ package cc.yunxi.service.impl;
import cc.yunxi.common.domain.LambdaQueryWrapperX;
import cc.yunxi.common.exception.BizIllegalException;
import cc.yunxi.common.utils.BeanUtils;
import cc.yunxi.domain.po.Client;
import cc.yunxi.common.utils.CollUtils;
import cc.yunxi.domain.po.ClientAddress;
import cc.yunxi.domain.vo.clientaddress.ClientAddressReqVO;
import cc.yunxi.domain.vo.clientaddress.ClientAddressRespVO;
import cc.yunxi.mapper.ClientAddressMapper;
import cc.yunxi.service.IClientAddressService;
import cn.hutool.core.util.StrUtil;
@ -15,7 +14,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
@ -32,12 +30,17 @@ import java.util.List;
public class ClientAddressServiceImpl extends ServiceImpl<ClientAddressMapper, ClientAddress> implements IClientAddressService {
private static final Integer LIMIT = 3; // 限制条数
private static final Integer LIMIT = 5; // 限制条数
@Override
public List<ClientAddress> queryAddressList(String clientId) {
return queryAddressList(clientId, null);
}
private List<ClientAddress> queryAddressList(String clientId, String excludeId) {
LambdaQueryWrapperX<ClientAddress> wrapperX = new LambdaQueryWrapperX<>();
wrapperX.eq(ClientAddress::getClientId, clientId).orderByDesc(ClientAddress::getIsDefault);
wrapperX.neIfPresent(ClientAddress::getId, excludeId);
return list(wrapperX);
}
@ -56,9 +59,20 @@ public class ClientAddressServiceImpl extends ServiceImpl<ClientAddressMapper, C
@Override
@Transactional(rollbackFor = Exception.class)
public String addAddress(ClientAddressReqVO addressCreateVO) {
ClientAddress clientAddress = BeanUtils.copyBean(addressCreateVO, ClientAddress.class);
ClientAddress clientAddress = BeanUtils.copyBean(addressCreateVO, ClientAddress.class, (s, t) -> {
t.setLongitude(s.getLocationDTO().getLongitude());
t.setLatitude(s.getLocationDTO().getLatitude());
});
List<ClientAddress> addressList = queryAddressList(addressCreateVO.getClientId());
if(addressList.size() > LIMIT) {
throw new BizIllegalException("地址最多为"+ LIMIT +"个");
}
if (addressCreateVO.getIsDefault() == 1) { // 添加为默认地址
this.changeDefaultAddress(addressCreateVO.getClientId());
this.changeDefaultAddress(addressList);
} else {
if(CollUtils.isEmpty(addressList)) {
throw new BizIllegalException("首地址必须为默认地址");
}
}
clientAddress.setCreatorTime(LocalDateTime.now());
this.save(clientAddress);
@ -67,12 +81,22 @@ public class ClientAddressServiceImpl extends ServiceImpl<ClientAddressMapper, C
@Override
@Transactional(rollbackFor = Exception.class)
public void updateAddress(@Valid ClientAddressReqVO addressUpdateVO) {
public void updateAddress(ClientAddressReqVO addressUpdateVO) {
ClientAddress addressInfo = getAddressInfo(addressUpdateVO.getId());
if(!addressInfo.getIsDefault().equals(addressUpdateVO.getIsDefault())) {
List<ClientAddress> addressList = queryAddressList(addressInfo.getClientId(), addressUpdateVO.getId());
if (addressUpdateVO.getIsDefault() == 1) { // 修改为默认地址
this.changeDefaultAddress(addressInfo.getClientId());
this.changeDefaultAddress(addressList);
} else {
if (addressList.stream().noneMatch(v -> v.getIsDefault() == 1)) {
throw new BizIllegalException("地址列表中需存在一个默认地址");
}
}
ClientAddress clientAddress = BeanUtils.copyBean(addressUpdateVO, ClientAddress.class);
}
ClientAddress clientAddress = BeanUtils.copyBean(addressUpdateVO, ClientAddress.class, (s, t) -> {
t.setLongitude(s.getLocationDTO().getLongitude());
t.setLatitude(s.getLocationDTO().getLatitude());
});
clientAddress.setUpdateTime(LocalDateTime.now());
this.updateById(clientAddress);
}
@ -89,8 +113,7 @@ public class ClientAddressServiceImpl extends ServiceImpl<ClientAddressMapper, C
// 动态切换默认地址
private void changeDefaultAddress(String clientId) {
List<ClientAddress> clientAddresses = queryAddressList(clientId);
private void changeDefaultAddress(List<ClientAddress> clientAddresses) {
LocalDateTime now = LocalDateTime.now();
clientAddresses.forEach(v -> {
v.setIsDefault(0);

Loading…
Cancel
Save