content: package com.sky.service.impl;
  
  import com.sky.context.BaseContext;
  import com.sky.entity.AddressBook;
  import com.sky.mapper.AddressBookMapper;
  import com.sky.service.AddressBookService;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.List;
  
  /**
   * @version 1.0
   * @Author Imak
   * @Date 2024/8/12 13:48
   * @注释
   */
  @Service
  @Slf4j
  public class AddressBookServiceImpl implements AddressBookService {
  
      @Autowired
      private AddressBookMapper addressBookMapper;
      /**
       * 条件查询
       * @param addressBook
       * @return
       */
      public List<AddressBook> list(AddressBook addressBook) {
          return addressBookMapper.list(addressBook);
      }
  
      /**
       * 新增地址
       * @param addressBook
       */
      public void save(AddressBook addressBook) {
          addressBook.setUserId(BaseContext.getCurrentId());
          addressBook.setIsDefault(0);
          addressBookMapper.insert(addressBook);
      }
  
      /**
       * 根据id查询地址
       * @param id
       * @return
       */
      public AddressBook getById(Long id) {
          return addressBookMapper.getById(id);
      }
  
      /**
       * 更新地址
       * @param addressBook
       */
      public void update(AddressBook addressBook) {
          addressBookMapper.update(addressBook);
      }
  
      /**
       * 设置默认地址
       * @param addressBook
       */
      @Transactional
      public void setDefault(AddressBook addressBook) {
          //第一次更新，将该用户的所有地址设为非默认地址，is_default=0
          addressBook.setIsDefault(0);
          addressBook.setUserId(BaseContext.getCurrentId());
          addressBookMapper.updateIsDefaultByUserId(addressBook);
  
          //将当前地址设置为默认地址
          addressBook.setIsDefault(1);
          addressBookMapper.update(addressBook);
      }
  
      /**
       * 根据id删除地址
       * @param id
       */
      public void deleteById(Long id) {
          addressBookMapper.deleteById(id);
      }
  }
  