| package com.mzl.flower.service.customer; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.mzl.flower.config.exception.ValidationException; | 
| import com.mzl.flower.config.security.SecurityUtils; | 
| import com.mzl.flower.dto.request.customer.CreateAddressDTO; | 
| import com.mzl.flower.dto.request.customer.UpdateAddressDTO; | 
| import com.mzl.flower.entity.customer.Address; | 
| import com.mzl.flower.mapper.customer.AddressMapper; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.util.List; | 
|   | 
| @Service | 
| @Transactional | 
| public class AddressService { | 
|   | 
|     private final AddressMapper addressMapper; | 
|   | 
|     public AddressService(AddressMapper addressMapper) { | 
|         this.addressMapper = addressMapper; | 
|     } | 
|   | 
|   | 
|     public void add(CreateAddressDTO dto) { | 
|         Address address  = new Address(); | 
|         BeanUtils.copyProperties(dto,address); | 
|         address.create(SecurityUtils.getUserId()); | 
|         updateDefault(dto.getIsDefault()); | 
|         addressMapper.insert(address); | 
|     } | 
|   | 
|     public void update(UpdateAddressDTO dto) { | 
|         Address address = addressMapper.selectById(dto.getId()); | 
|         if (address == null){ | 
|             throw new ValidationException("地址不存在"); | 
|         } | 
|         if(!address.getCreateBy().equals(SecurityUtils.getUserId())){ | 
|             throw new ValidationException("无权限修改"); | 
|         } | 
|         BeanUtils.copyProperties(dto,address); | 
|         address.update(SecurityUtils.getUserId()); | 
|         updateDefault(dto.getIsDefault()); | 
|         addressMapper.updateById(address); | 
|     } | 
|   | 
|     private void updateDefault(Boolean isDefault) { | 
|         if(isDefault!=null && isDefault){ | 
|             LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>(); | 
|             queryWrapper.eq(Address::getIsDefault,true); | 
|             queryWrapper.eq(Address::getCreateBy,SecurityUtils.getUserId()); | 
|             List<Address> list = addressMapper.selectList(queryWrapper); | 
|             if(list!=null && list.size()>0){ | 
|                 for (Address oldDefault : list) { | 
|                     oldDefault.setIsDefault(false); | 
|                     addressMapper.updateById(oldDefault); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     public void delete(Long id) { | 
|         Address address = addressMapper.selectById(id); | 
|         if (address == null){ | 
|             throw new ValidationException("地址不存在"); | 
|         } | 
|         if(!address.getCreateBy().equals(SecurityUtils.getUserId())){ | 
|             throw new ValidationException("无权限删除"); | 
|         } | 
|         addressMapper.deleteById(id); | 
|     } | 
|   | 
|     public List<Address> query(String userId,Boolean isDefault) { | 
|         LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>(); | 
|         queryWrapper.eq(Address::getCreateBy,userId); | 
|         if(isDefault!=null){ | 
|             queryWrapper.eq(Address::getIsDefault,isDefault); | 
|         } | 
|         queryWrapper.orderByDesc(Address::getIsDefault); | 
|         return addressMapper.selectList(queryWrapper); | 
|     } | 
|   | 
|     public Address findDetail(Long id) { | 
|         return addressMapper.selectById(id); | 
|     } | 
|   | 
|     public void setDefault(Long id) { | 
|         Address address = addressMapper.selectById(id); | 
|         if (address == null){ | 
|             throw new ValidationException("地址不存在"); | 
|         } | 
|         if(!address.getCreateBy().equals(SecurityUtils.getUserId())){ | 
|             throw new ValidationException("无权限修改"); | 
|         } | 
|         address.setIsDefault(true); | 
|         address.update(SecurityUtils.getUserId()); | 
|         updateDefault(true); | 
|         addressMapper.updateById(address); | 
|     } | 
| } |