package com.ruoyi.errand.service.impl; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.errand.constant.DelFlagConstant; import com.ruoyi.errand.constant.IsDefaultConstant; import com.ruoyi.errand.domain.AddressBook; import com.ruoyi.errand.domain.AppUser; import com.ruoyi.errand.domain.Community; import com.ruoyi.errand.mapper.AddressBookMapper; import com.ruoyi.errand.mapper.CommunityMapper; import com.ruoyi.errand.object.dto.app.AddAddressBookDTO; import com.ruoyi.errand.object.dto.app.UpdateAddressBookDTO; import com.ruoyi.errand.object.vo.app.AddressBookByCommunityIdVO; import com.ruoyi.errand.object.vo.app.AddressBookListVO; import com.ruoyi.errand.service.AddressBookService; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.List; @Service public class AddressBookServiceImpl extends ServiceImpl implements AddressBookService { @Resource private CommunityMapper communityMapper; @Override public List addressBookByCommunityId(Integer communityId) { //校验一下communityId Community community = communityMapper.selectById(communityId); if (community == null|| community.getDelFlag().equals(DelFlagConstant.DELETE)) { throw new ServiceException("小区不存在"); } AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return this.getBaseMapper().addressBookByCommunityId(communityId,appuser.getId()); } @Override public List addressBookList() { AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); return this.getBaseMapper().addressBookList(appuser.getId()); } @Override public void setDefaultAddress(Integer id) { AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //查询这个默认地址 AddressBook addressBook = this.getById(id); if (null==addressBook||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)) { throw new ServiceException("该地址不存在"); } //查看原本小区内是否有默认地址 List addressBooks = this.getBaseMapper().selectList(new LambdaUpdateWrapper() .eq(AddressBook::getCommunityId, addressBook.getCommunityId()) .eq(AddressBook::getApp_user_id, appuser.getId()) .eq(AddressBook::getDel_flag, DelFlagConstant.UNDELETE) .eq(AddressBook::getIs_default, IsDefaultConstant.TRUE) ); //将原本地址修改掉 addressBooks.forEach(x->{ x.setIs_default(IsDefaultConstant.FALSE); x.setUpdate_time(LocalDateTime.now()); this.getBaseMapper().updateById(x);} ); //将新地址设置为默认地址 addressBook.setIs_default(IsDefaultConstant.TRUE); addressBook.setUpdate_time(LocalDateTime.now()); this.getBaseMapper().updateById(addressBook); } @Override public void add(AddAddressBookDTO addAddressBookDTO) { AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //判断小区是否存在 Community community = communityMapper.selectById(addAddressBookDTO.getCommunityId()); if (null==community|| community.getDelFlag().equals(DelFlagConstant.DELETE)) { throw new ServiceException("小区不存在"); } AddressBook addressBook = new AddressBook(); BeanUtils.copyProperties(addAddressBookDTO,addressBook); addressBook.setIs_default(IsDefaultConstant.FALSE); addressBook.setCreate_time(LocalDateTime.now()); addressBook.setApp_user_id(appuser.getId()); this.save(addressBook); } @Override public void set(UpdateAddressBookDTO updateAddressBookDTO) { AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //判断小区是否存在 Community community = communityMapper.selectById(updateAddressBookDTO.getCommunityId()); if (null==community|| community.getDelFlag().equals(DelFlagConstant.DELETE)) { throw new ServiceException("小区不存在"); } //判断地址是否存在 AddressBook addressBook = this.getById(updateAddressBookDTO.getId()); if (null==addressBook||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)) { throw new ServiceException("地址不存在"); } //判断用户是否同一个 if (!appuser.getId().equals(addressBook.getApp_user_id())) { throw new ServiceException("该地址不属于您"); } BeanUtils.copyProperties(updateAddressBookDTO,addressBook); addressBook.setUpdate_time(LocalDateTime.now()); this.updateById(addressBook); } @Override public void delete(Integer id) { AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //查看该地址是否存在 AddressBook addressBook = this.getById(id); if (null==addressBook||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)) { throw new ServiceException("该地址不存在"); } //判断用户是否同一个 if (!appuser.getId().equals(addressBook.getApp_user_id())) { throw new ServiceException("该地址不属于您"); } addressBook.setDel_flag(DelFlagConstant.DELETE); this.updateById(addressBook); } }