package com.shiep.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shiep.dao.IUserDao;
import com.shiep.entity.*;
import com.shiep.mapper.*;
import com.shiep.service.IAnimalSearchService;
import com.shiep.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class AnimalSearchServiceImpl implements IAnimalSearchService {
@Resource
private IAnimalSearchMapper animalSearchMapper;
@Resource
private IUserDao userDao;
@Resource
private ILocationDao locationDao;
@Resource
private IAnimalCategoryDetailsMapper animalCategoryDetailsMapper;
@Resource
private IPhotoAnimalSearchMapper photoAnimalSearchMapper;
@Resource
private ICountyMapper countyMapper;
@Resource
private IAnimalSearchCommentMapper animalSearchCommentMapper;
@Resource
private IUserMapper userMapper;
@Override
public boolean saveAnimalSearch(AnimalSearch animalSearch) {
if (animalSearch == null) {
return false;
}
return this.animalSearchMapper.insert(animalSearch) > 0;
}
@Override
public List<AnimalSearchVo> queryAll(
Long cityId, Integer categoryId, Long countyId, Integer searchAnimal) {
List<AnimalSearch> animalSearches = null;
// 1、)什么都不做 首先必须处理的是cityId 如果为null 查询所有adopt
if (cityId == null) {
QueryWrapper<AnimalSearch> qw = new QueryWrapper<>();
// 没有被删除 并且审核通过的
qw.lambda().eq(AnimalSearch::getDeleteStatus, 0).eq(AnimalSearch::getVerifyStatus, 1);
animalSearches = this.animalSearchMapper.selectList(qw);
List<AnimalSearchVo> animalSearchVos = this.getAnimalSearchVo(animalSearches);
return animalSearchVos;
}
// 2、) 选择地区后:其他都为null
if (cityId != null && (categoryId == null || categoryId == 0) && countyId == null && searchAnimal == null) {
// 首先根据cityId查询出所有的县区id
QueryWrapper<County> qwCounty = new QueryWrapper<>();
qwCounty.lambda().eq(County::getCityId, cityId);
List<County> counties = this.countyMapper.selectList(qwCounty);
List<Long> countyIds = counties.stream().map(County::getId).collect(Collectors.toList());
if (countyIds.isEmpty()) {
return new ArrayList<>();
}
// 查找
QueryWrapper<AnimalSearch> qw = new QueryWrapper<>();
qw.lambda().eq(AnimalSearch::getDeleteStatus, 0).eq(AnimalSearch::getVerifyStatus, 1)
.in(AnimalSearch::getCountyId, countyIds);
animalSearches = this.animalSearchMapper.selectList(qw);
List<AnimalSearchVo> AnimalSearchVos = this.getAnimalSearchVo(animalSearches);
return AnimalSearchVos;
}
// 3、) 选择了具体的县区
if (cityId != null && (categoryId == null || categoryId == 0) && countyId != null && searchAnimal == null) {
QueryWrapper<AnimalSearch> qw = new QueryWrapper<>();
// 没有被删除 并且审核通过的 h有具体县区
qw.lambda().eq(AnimalSearch::getDeleteStatus, 0).eq(AnimalSearch::getVerifyStatus, 1)
.eq(AnimalSearch::getCountyId, countyId);
animalSearches = this.animalSearchMapper.selectList(qw);
List<AnimalSearchVo> animalSearchVos = this.getAnimalSearchVo(animalSearches);
return animalSearchVos;
}
// 4、)选择了宠类
if (cityId != null && categoryId != null && countyId == null && searchAnimal == null) {
// 首先根据cityId查询出所有的县区id
QueryWrapper<County> qwCounty = new QueryWrapper<>();
qwCounty.lambda().eq(County::getCityId, cityId);
List<County> counties = this.countyMapper.selectList(qwCounty);
List<Long> countyIds = counties.stream().map(County::getId).collect(Collectors.toList());
if (countyIds.isEmpty()) {
return new ArrayList<>();
}
// 其次根据宠类ID选择出具体的宠类id
QueryWrapper<AnimalCategoryDetails> qwCategory = new QueryWrapper<>();
qwCategory.lambda().eq(AnimalCategoryDetails::getCategoryId, categoryId);
List<AnimalCategoryDetails> details = this.animalCategoryDetailsMapper.selectList(qwCategory);
List<Integer> detailsIds = details.stream().map(AnimalCategoryDetails::getId).collect(Collectors.toList());
// 查找
// 记住,mybatisPlus 在查询来的集合为null的前提下 不会组装in的SQL,所以,所有的in我们要自己判断一下
if (detailsIds.isEmpty()) {
return new ArrayList<>();
}
QueryWrapper<AnimalSearch> qw = new QueryWrapper<>();
qw.lambda().eq(AnimalSearch::getDeleteStatus, 0).eq(AnimalSearch::getVerifyStatus, 1)
.in(AnimalSearch::getCountyId, countyIds).in(AnimalSearch::getCategoryId, detailsIds);
animalSearches = this.animalSearchMapper.selectList(qw);
List<AnimalSearchVo> animalSearchVos = this.getAnimalSearchVo(animalSearches);
return animalSearchVos;
}
// 5、)选择了搜索类型 --宠物、主人
if (cityId != null && (categoryId == null || categoryId == 0) && countyId == null && searchAnimal != null) {
// 首先根据cityId查询出所有的县区id
QueryWrapper<County> qwCounty = new QueryWrapper<>();
qwCounty.lambda().eq(County::getCityId, cityId);
List<County> counties = this.countyMapper.selectList(qwCounty);
List<Long> countyIds = counties.stream().map(County::getId).collect(Collectors.toList());
if (countyIds.isEmpty()) {
return new ArrayList<>();
}
// 查找
QueryWrapper<AnimalSearch> qw = new QueryWrapper<>();
qw.lambda().eq(AnimalSearch::getDeleteStatus, 0).eq(AnimalSearch::getVerifyStatus, 1)
.in(AnimalSearch::getCountyId, countyIds).eq(AnimalSearch::getSearchAnimal, searchAnimal);
animalSearches = this.animalSearchMapper.selectList(qw);
List<AnimalSearchVo> animalSearchVos = this.getAnimalSearchVo(animalSearches);
return animalSearchVos;
}
// 6、)选了具体的县区+宠类
if (cityId != null && categoryId != null && countyId != null && searchAnimal == null) {
// 先根据宠类ID选择出具体的宠类id
QueryWrapper<AnimalCategoryDetails> qwCategory = new QueryWrapper<>();
qwCategory.lambda().eq(AnimalCategoryDetails::getCategoryId, categoryId);
List<AnimalCategoryDetails> details = this.animalCategoryDetailsMapper.selectList(qwCategory);
List<Integer> detailsIds = details.stream().map(AnimalCategoryDetails::getId).collect(Collectors.toList());
if (detailsIds.isEmpty()) {
return new ArrayList<>();
}
QueryWrapper<AnimalSearch> qw = new QueryWrapper<>();
// 没有被删除 并且审核通过的 h有具体县区 + 宠类
qw.lambda().eq(AnimalSearch::getDeleteStatus, 0).eq(AnimalSearch::getVerifyStatus, 1)
.eq(AnimalSearch::getCountyId, countyId).in(AnimalSearch::getCategoryId, detailsIds);
animalSearches = this.animalSearchMapper.selectList(qw);
List<AnimalSearchVo> animal