|
@@ -1,10 +1,15 @@
|
|
|
package com.ylx.massage.service.impl;
|
|
package com.ylx.massage.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.ylx.common.utils.StringUtils;
|
|
import com.ylx.common.utils.StringUtils;
|
|
|
|
|
+import com.ylx.useradress.domain.dto.UserAddressAddDto;
|
|
|
|
|
+import com.ylx.useradress.domain.dto.UserAddressDeleteDto;
|
|
|
import com.ylx.useradress.domain.dto.UserAddressDto;
|
|
import com.ylx.useradress.domain.dto.UserAddressDto;
|
|
|
|
|
+import com.ylx.useradress.domain.dto.UserAddressUpdateDto;
|
|
|
import com.ylx.useradress.domain.vo.UserAddressVo;
|
|
import com.ylx.useradress.domain.vo.UserAddressVo;
|
|
|
import com.ylx.massage.mapper.TAddressMapper;
|
|
import com.ylx.massage.mapper.TAddressMapper;
|
|
|
import com.ylx.massage.domain.TAddress;
|
|
import com.ylx.massage.domain.TAddress;
|
|
@@ -27,6 +32,18 @@ import java.util.stream.Collectors;
|
|
|
@Service("tAddressService")
|
|
@Service("tAddressService")
|
|
|
public class TAddressServiceImpl extends ServiceImpl<TAddressMapper, TAddress> implements TAddressService {
|
|
public class TAddressServiceImpl extends ServiceImpl<TAddressMapper, TAddress> implements TAddressService {
|
|
|
|
|
|
|
|
|
|
+ //0=未删除
|
|
|
|
|
+ private static final Integer NO_DELETE = 0;
|
|
|
|
|
+ //1=用户
|
|
|
|
|
+ private static final Integer USER_TYPE = 1;
|
|
|
|
|
+ //1=真实地址
|
|
|
|
|
+ private static final Integer TYPE = 1;
|
|
|
|
|
+ //1=默认地址
|
|
|
|
|
+ private static final Integer IS_DEFAULT = 1;
|
|
|
|
|
+ //0=不是默认地址
|
|
|
|
|
+ private static final Integer NO_DEFAULT = 0;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public TAddress getByOpenId(String openId) {
|
|
public TAddress getByOpenId(String openId) {
|
|
|
LambdaQueryWrapper<TAddress> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<TAddress> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
@@ -138,5 +155,132 @@ public class TAddressServiceImpl extends ServiceImpl<TAddressMapper, TAddress> i
|
|
|
}
|
|
}
|
|
|
return this.updateById(tAddress);
|
|
return this.updateById(tAddress);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户端新增地址
|
|
|
|
|
+ * @param dto
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addUserAddress(UserAddressAddDto dto) {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 1. DTO -> 实体 转换
|
|
|
|
|
+ TAddress address = new TAddress();
|
|
|
|
|
+ BeanUtil.copyProperties(dto, address);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 补充实体中缺失但需要入库的字段
|
|
|
|
|
+ address.setIsDelete(NO_DELETE);
|
|
|
|
|
+ address.setUserType(USER_TYPE);
|
|
|
|
|
+ address.setType(TYPE);
|
|
|
|
|
+ address.setDetailAddress(address.getAddress() + " " + address.getRoomNumber());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 处理默认地址逻辑
|
|
|
|
|
+ // 如果当前新增的地址是默认地址,需要先将该用户下的其他地址设为非默认
|
|
|
|
|
+ if (IS_DEFAULT.equals(dto.getIsDefault())) {
|
|
|
|
|
+ LambdaQueryWrapper<TAddress> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(TAddress::getUserId, dto.getUserId())
|
|
|
|
|
+ .eq(TAddress::getIsDefault, IS_DEFAULT);
|
|
|
|
|
+ TAddress defaultExists = this.baseMapper.selectOne(wrapper);
|
|
|
|
|
+ if (defaultExists != null) {
|
|
|
|
|
+ defaultExists.setIsDefault(NO_DEFAULT);
|
|
|
|
|
+ this.updateById(defaultExists);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 执行入库
|
|
|
|
|
+ this.baseMapper.insert(address);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户端更新地址
|
|
|
|
|
+ * @param dto
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updateUserAddress(UserAddressUpdateDto dto) {
|
|
|
|
|
+ // 1. 参数校验:地址ID不能为空
|
|
|
|
|
+ if (StrUtil.isBlank(dto.getId())) {
|
|
|
|
|
+ throw new RuntimeException("地址ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 查询原有地址记录
|
|
|
|
|
+ TAddress existingAddress = this.baseMapper.selectById(dto.getId());
|
|
|
|
|
+ if (existingAddress == null) {
|
|
|
|
|
+ throw new RuntimeException("地址不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 权限校验:只能修改自己的地址
|
|
|
|
|
+ if (!existingAddress.getUserId().equals(dto.getUserId())) {
|
|
|
|
|
+ throw new RuntimeException("无权修改该地址");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 4. 将DTO中非空字段拷贝到实体(忽略为null的字段)
|
|
|
|
|
+ BeanUtil.copyProperties(dto, existingAddress, "id", "userId", "openid", "merchantId",
|
|
|
|
|
+ "userType", "type", "createTime", "isDelete");
|
|
|
|
|
+
|
|
|
|
|
+ // 6. 拼接详细地址(address + 空格 + roomNumber)
|
|
|
|
|
+ String detailAddress = "";
|
|
|
|
|
+ if (StrUtil.isNotBlank(existingAddress.getAddress())
|
|
|
|
|
+ && StrUtil.isNotBlank(existingAddress.getRoomNumber())) {
|
|
|
|
|
+ detailAddress = existingAddress.getAddress() + " " + existingAddress.getRoomNumber();
|
|
|
|
|
+ } else if (StrUtil.isNotBlank(existingAddress.getAddress())) {
|
|
|
|
|
+ detailAddress = existingAddress.getAddress();
|
|
|
|
|
+ } else if (StrUtil.isNotBlank(existingAddress.getRoomNumber())) {
|
|
|
|
|
+ detailAddress = existingAddress.getRoomNumber();
|
|
|
|
|
+ }
|
|
|
|
|
+ existingAddress.setDetailAddress(detailAddress);
|
|
|
|
|
+
|
|
|
|
|
+ // 8. 执行更新(根据ID更新)
|
|
|
|
|
+ int rows = this.baseMapper.updateById(existingAddress);
|
|
|
|
|
+ if (rows != 1) {
|
|
|
|
|
+ throw new RuntimeException("更新地址失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户端删除地址
|
|
|
|
|
+ * @param dto
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteUserAddress(UserAddressDeleteDto dto) {
|
|
|
|
|
+ String addressId = dto.getId();
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 查询地址是否存在且未被删除
|
|
|
|
|
+ TAddress address = this.baseMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<TAddress>()
|
|
|
|
|
+ .eq(TAddress::getId, addressId)
|
|
|
|
|
+ .eq(TAddress::getIsDelete, 0)
|
|
|
|
|
+ );
|
|
|
|
|
+ if (address == null) {
|
|
|
|
|
+ throw new RuntimeException("地址不存在或已被删除");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 权限校验:只能删除自己的地址
|
|
|
|
|
+ if (!address.getUserId().equals(dto.getUserId())) {
|
|
|
|
|
+ throw new RuntimeException("无权删除该地址");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 执行逻辑删除(MyBatis-Plus 会自动将 isDelete 设为 1,并更新 updateTime)
|
|
|
|
|
+ int rows = this.baseMapper.deleteById(addressId);
|
|
|
|
|
+ if (rows != 1) {
|
|
|
|
|
+ throw new RuntimeException("删除地址失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 【可选】如果删除的是默认地址,将用户的其他最新地址设为默认
|
|
|
|
|
+ if (IS_DEFAULT.equals(address.getIsDefault())) {
|
|
|
|
|
+ // 查询该用户下未被删除的其他地址,按创建时间倒序取第一条
|
|
|
|
|
+ LambdaQueryWrapper<TAddress> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(TAddress::getUserId, dto.getUserId())
|
|
|
|
|
+ .eq(TAddress::getIsDelete, 0)
|
|
|
|
|
+ .orderByDesc(TAddress::getCreateTime)
|
|
|
|
|
+ .last("limit 1");
|
|
|
|
|
+ TAddress newDefault = this.baseMapper.selectOne(wrapper);
|
|
|
|
|
+ if (newDefault != null) {
|
|
|
|
|
+ newDefault.setIsDefault(IS_DEFAULT);
|
|
|
|
|
+ this.baseMapper.updateById(newDefault);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|