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.domain.Region;
|
import com.ruoyi.errand.mapper.AddressBookMapper;
|
import com.ruoyi.errand.mapper.CommunityMapper;
|
import com.ruoyi.errand.mapper.RegionMapper;
|
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<AddressBookMapper, AddressBook> implements AddressBookService {
|
@Resource
|
private CommunityMapper communityMapper;
|
@Resource
|
private RegionMapper regionMapper;
|
|
@Override
|
public List<AddressBookByCommunityIdVO> 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<AddressBookListVO> addressBookList() {
|
AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
List<AddressBookListVO> voList = this.getBaseMapper().addressBookList(appuser.getId());
|
voList.forEach(vo->{
|
String regionFullName = getRegionFullName(vo.getRegionId());
|
vo.setRegionName(regionFullName);
|
});
|
return voList;
|
}
|
|
/**
|
* 根据区ID查询完整的省市区名称
|
*/
|
public String getRegionFullName(Integer districtId) {
|
Region district = regionMapper.selectById(districtId);
|
if (district == null) {
|
return "";
|
}
|
|
Region city = regionMapper.selectById(district.getParentId());
|
if (city == null) {
|
return district.getName();
|
}
|
|
Region province = regionMapper.selectById(city.getParentId());
|
if (province == null) {
|
return city.getName() + district.getName();
|
}
|
|
return province.getName() + city.getName() + district.getName();
|
}
|
@Override
|
public void setDefaultAddress(Integer id) {
|
AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
//查询这个默认地址
|
AddressBook addressBook = this.getById(id);
|
if (null==addressBook||addressBook.getDelFlag().equals(DelFlagConstant.DELETE)) {
|
throw new ServiceException("该地址不存在");
|
}
|
//查看原本小区内是否有默认地址
|
List<AddressBook> addressBooks = this.getBaseMapper().selectList(new LambdaUpdateWrapper<AddressBook>()
|
.eq(AddressBook::getCommunityId, addressBook.getCommunityId())
|
.eq(AddressBook::getAppUserId, appuser.getId())
|
.eq(AddressBook::getDelFlag, DelFlagConstant.UNDELETE)
|
.eq(AddressBook::getIsDefault, IsDefaultConstant.TRUE)
|
);
|
//将原本地址修改掉
|
addressBooks.forEach(x->{
|
x.setIsDefault(IsDefaultConstant.FALSE);
|
x.setUpdateTime(LocalDateTime.now());
|
this.getBaseMapper().updateById(x);}
|
);
|
//将新地址设置为默认地址
|
addressBook.setIsDefault(IsDefaultConstant.TRUE);
|
addressBook.setUpdateTime(LocalDateTime.now());
|
this.getBaseMapper().updateById(addressBook);
|
}
|
|
@Override
|
public Integer 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.setIsDefault(IsDefaultConstant.FALSE);
|
addressBook.setCreateTime(LocalDateTime.now());
|
addressBook.setAppUserId(appuser.getId());
|
this.save(addressBook);
|
return addressBook.getId();
|
}
|
|
@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.getDelFlag().equals(DelFlagConstant.DELETE)) {
|
throw new ServiceException("地址不存在");
|
}
|
//判断用户是否同一个
|
if (!appuser.getId().equals(addressBook.getAppUserId())) {
|
throw new ServiceException("该地址不属于您");
|
}
|
BeanUtils.copyProperties(updateAddressBookDTO,addressBook);
|
addressBook.setUpdateTime(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.getDelFlag().equals(DelFlagConstant.DELETE)) {
|
throw new ServiceException("该地址不存在");
|
}
|
//判断用户是否同一个
|
if (!appuser.getId().equals(addressBook.getAppUserId())) {
|
throw new ServiceException("该地址不属于您");
|
}
|
|
addressBook.setDelFlag(DelFlagConstant.DELETE);
|
this.updateById(addressBook);
|
}
|
}
|