|
|
@@ -0,0 +1,190 @@
|
|
|
+package com.ylx.product.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ylx.common.core.domain.model.WxLoginUser;
|
|
|
+import com.ylx.common.utils.DateUtils;
|
|
|
+import com.ylx.common.utils.SecurityUtils;
|
|
|
+import com.ylx.product.domain.ProductOrderAddress;
|
|
|
+import com.ylx.product.domain.dto.ProductOrderAddressDTO;
|
|
|
+import com.ylx.product.domain.dto.ProductOrderAddressPageDTO;
|
|
|
+import com.ylx.product.domain.vo.ProductOrderAddressVo;
|
|
|
+import com.ylx.product.mapper.ProductOrderAddressMapper;
|
|
|
+import com.ylx.product.service.IProductOrderAddressService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 订单收货地址Service业务层处理
|
|
|
+ *
|
|
|
+ * @author wzj
|
|
|
+ * @date 2026-04-03
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ProductOrderAddressServiceImpl extends ServiceImpl<ProductOrderAddressMapper, ProductOrderAddress> implements IProductOrderAddressService {
|
|
|
+ @Resource
|
|
|
+ private ProductOrderAddressMapper productOrderAddressMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void add(ProductOrderAddressDTO dto) {
|
|
|
+
|
|
|
+ WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
|
|
|
+ if (ObjectUtil.isNull(wxLoginUser)) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+ String openId = wxLoginUser.getCOpenid();
|
|
|
+
|
|
|
+ ProductOrderAddress entity = new ProductOrderAddress();
|
|
|
+ BeanUtil.copyProperties(dto, entity);
|
|
|
+ entity.setOpenId(openId);
|
|
|
+ entity.setIsDelete(0);
|
|
|
+ // 拼接完整地址
|
|
|
+ String fullAddress = entity.getProvinceName()
|
|
|
+ + entity.getCityName()
|
|
|
+ + entity.getDistrictName()
|
|
|
+ + entity.getDetailAddress();
|
|
|
+ entity.setFullAddress(fullAddress);
|
|
|
+ entity.setCreateTime(DateUtils.getNowDate());
|
|
|
+ productOrderAddressMapper.insert(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void edit(ProductOrderAddressDTO dto) {
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(dto.getId())) {
|
|
|
+ throw new RuntimeException("id不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ ProductOrderAddress entity = this.productOrderAddressMapper.selectById(dto.getId());
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(entity)) {
|
|
|
+ throw new RuntimeException("参数有误,查询数据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
|
|
|
+ if (ObjectUtil.isNull(wxLoginUser)) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+ String openId = wxLoginUser.getCOpenid();
|
|
|
+
|
|
|
+ BeanUtil.copyProperties(dto, entity);
|
|
|
+ entity.setOpenId(openId);
|
|
|
+ entity.setUpdateTime(DateUtils.getNowDate());
|
|
|
+ // 拼接完整地址
|
|
|
+ String fullAddress = entity.getProvinceName()
|
|
|
+ + entity.getCityName()
|
|
|
+ + entity.getDistrictName()
|
|
|
+ + entity.getDetailAddress();
|
|
|
+ entity.setFullAddress(fullAddress);
|
|
|
+ productOrderAddressMapper.updateById(entity);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProductOrderAddressVo findById(Long id) {
|
|
|
+
|
|
|
+ ProductOrderAddress entity = this.productOrderAddressMapper.selectById(id);
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(entity)) {
|
|
|
+ throw new RuntimeException("参数有误,查询数据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ ProductOrderAddressVo vo = new ProductOrderAddressVo();
|
|
|
+ BeanUtil.copyProperties(entity, vo);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int deleteById(Long id) {
|
|
|
+
|
|
|
+ ProductOrderAddress entity = this.productOrderAddressMapper.selectById(id);
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(entity)) {
|
|
|
+ throw new RuntimeException("参数有误,查询数据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
|
|
|
+ if (ObjectUtil.isNull(wxLoginUser)) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+ String openId = wxLoginUser.getCOpenid();
|
|
|
+ entity.setOpenId(openId);
|
|
|
+ entity.setIsDelete(1);
|
|
|
+ entity.setUpdateTime(DateUtils.getNowDate());
|
|
|
+
|
|
|
+ return this.productOrderAddressMapper.updateById(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int defaultAddress(Long id) {
|
|
|
+ ProductOrderAddress entity = this.productOrderAddressMapper.selectById(id);
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(entity)) {
|
|
|
+ throw new RuntimeException("参数有误,查询数据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
|
|
|
+ if (ObjectUtil.isNull(wxLoginUser)) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+ String openId = wxLoginUser.getCOpenid();
|
|
|
+
|
|
|
+ Integer currentIsDefault = entity.getIsDefault();
|
|
|
+
|
|
|
+ // ==============================================
|
|
|
+ // 核心逻辑:切换默认状态
|
|
|
+ // ==============================================
|
|
|
+ if (currentIsDefault == 1) {
|
|
|
+ // --------------------------
|
|
|
+ // 当前是默认 → 改为 非默认
|
|
|
+ // --------------------------
|
|
|
+ LambdaUpdateWrapper<ProductOrderAddress> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(ProductOrderAddress::getId, id);
|
|
|
+ wrapper.set(ProductOrderAddress::getIsDefault, 0);
|
|
|
+ wrapper.set(ProductOrderAddress::getOpenId, openId);
|
|
|
+ return productOrderAddressMapper.update(null, wrapper);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // --------------------------
|
|
|
+ // 当前非默认 → 设为默认
|
|
|
+ // 同时把该用户其他所有地址 → 非默认
|
|
|
+ // --------------------------
|
|
|
+ // 第一步:把该用户所有地址设为非默认
|
|
|
+ LambdaUpdateWrapper<ProductOrderAddress> updateAll = new LambdaUpdateWrapper<>();
|
|
|
+ updateAll.eq(ProductOrderAddress::getOpenId, openId);
|
|
|
+ updateAll.set(ProductOrderAddress::getIsDefault, 0);
|
|
|
+ updateAll.set(ProductOrderAddress::getOpenId, openId);
|
|
|
+ productOrderAddressMapper.update(null, updateAll);
|
|
|
+
|
|
|
+ // 第二步:把当前设为默认
|
|
|
+ LambdaUpdateWrapper<ProductOrderAddress> updateOne = new LambdaUpdateWrapper<>();
|
|
|
+ updateOne.eq(ProductOrderAddress::getId, id);
|
|
|
+ updateOne.set(ProductOrderAddress::getIsDefault, 1);
|
|
|
+ updateOne.set(ProductOrderAddress::getOpenId, openId);
|
|
|
+ return productOrderAddressMapper.update(null, updateOne);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ProductOrderAddressVo> list(ProductOrderAddressPageDTO dto) {
|
|
|
+
|
|
|
+ WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
|
|
|
+ if (ObjectUtil.isNull(wxLoginUser)) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+ String openId = wxLoginUser.getCOpenid();
|
|
|
+ dto.setOpenId(openId);
|
|
|
+ return productOrderAddressMapper.list(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|