package com.gkhy.assess.system.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.gkhy.assess.common.constant.CacheConstant;
|
import com.gkhy.assess.common.enums.RegionTypeEnum;
|
import com.gkhy.assess.common.exception.ApiException;
|
import com.gkhy.assess.common.utils.RedisUtils;
|
import com.gkhy.assess.common.utils.StringUtils;
|
import com.gkhy.assess.system.domain.SysRegion;
|
import com.gkhy.assess.system.mapper.SysRegionMapper;
|
import com.gkhy.assess.system.service.SysRegionService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Comparator;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 系统地区表 服务实现类
|
* </p>
|
*
|
* @author kzy
|
* @since 2023-11-24 13:45:45
|
*/
|
@Service
|
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {
|
|
@Autowired
|
private RedisUtils redisUtils;
|
@Override
|
public List<SysRegion> regionTree(SysRegion region) {
|
LambdaQueryWrapper<SysRegion> lambdaQueryWrapper = Wrappers.<SysRegion>lambdaQuery();
|
if(StringUtils.isNotBlank(region.getName())){
|
lambdaQueryWrapper.like(SysRegion::getName,region.getName());
|
}
|
Integer regionType=region.getRegionType();
|
if(regionType==null){
|
regionType= RegionTypeEnum.INSIDE.getCode();
|
}
|
String key=redisUtils.generateKey(CacheConstant.SYS_REGION_KEY+regionType);
|
List<SysRegion> regionList= (List<SysRegion>) redisUtils.get(key);
|
if(regionList!=null&®ionList.size()>0){
|
return regionList;
|
}
|
lambdaQueryWrapper.eq(SysRegion::getRegionType,regionType);
|
lambdaQueryWrapper.orderBy(true, true,SysRegion::getSort);
|
List<SysRegion> regions= list(lambdaQueryWrapper);
|
//筛选出所有一级标签
|
regionList= regions.stream()
|
.filter(regionEntity -> regionEntity.getParentId()==0L)
|
.peek(regionEntity -> regionEntity.setChildren(this.listRegionChildren(regionEntity,regions)))
|
.sorted(Comparator.comparing(SysRegion::getSort))
|
.collect(Collectors.toList());
|
redisUtils.set(key,regionList,60, TimeUnit.MINUTES);
|
return regionList;
|
}
|
|
public List<SysRegion> listRegionChildren(SysRegion region,List<SysRegion> regions){
|
//递归查找子类
|
return regions.stream()
|
.filter(regionEntity -> Objects.equals(regionEntity.getParentId(), region.getId()))
|
.peek(regionEntity -> regionEntity.setChildren(this.listRegionChildren(regionEntity,regions)))
|
//.sorted(Comparator.comparing(SysRegion::getSort))
|
.collect(Collectors.toList());
|
}
|
|
@Override
|
public int addRegion(SysRegion region) {
|
if(!checkRegionUnique(new SysRegion().setName(region.getName()).setRegionType(RegionTypeEnum.INSIDE.getCode()))){
|
throw new ApiException("已存在相同地区名称");
|
}
|
region.setRegionType(RegionTypeEnum.INSIDE.getCode());
|
boolean b=save(region);
|
if(!b){
|
throw new ApiException("新增地区失败");
|
}
|
deleteRedisCache(RegionTypeEnum.INSIDE.getCode());
|
return 0;
|
}
|
|
private void deleteRedisCache(Integer regionType){
|
String key=redisUtils.generateKey(CacheConstant.SYS_REGION_KEY+regionType);
|
redisUtils.del(key);
|
}
|
|
public boolean checkRegionUnique(SysRegion region){
|
Long userId = region.getId()==null? -1L : region.getId();
|
SysRegion info = baseMapper.checkRegionUnique(region.getName(),region.getRegionType(),region.getParentId()==null?0L:region.getParentId());
|
if (info!=null && info.getId().longValue() != userId.longValue())
|
{
|
return false;
|
}
|
return true;
|
}
|
|
@Override
|
public int editRegion(SysRegion region) {
|
if(!region.getRegionType().equals(RegionTypeEnum.INSIDE.getCode())){
|
throw new ApiException("疆外数据不能修改");
|
}
|
if(!checkRegionUnique(region)){
|
throw new ApiException("已存在相同地区名称");
|
}
|
boolean b=updateById(region);
|
if(!b){
|
throw new ApiException("修改地区失败");
|
}
|
deleteRedisCache(RegionTypeEnum.INSIDE.getCode());
|
return 0;
|
}
|
|
@Override
|
public int deleteRegionById(Long reginId) {
|
Long count=count(Wrappers.<SysRegion>lambdaQuery()
|
.eq(true,SysRegion::getParentId,reginId));
|
if(count>0){
|
throw new ApiException("下级存在地区数据,不能删除");
|
}
|
SysRegion region=getById(reginId);
|
if(region.getRegionType().equals(RegionTypeEnum.OUTSIDE.getCode())){
|
throw new ApiException("疆外地区数据,不能删除");
|
}
|
boolean b=removeById(reginId);
|
if(!b){
|
throw new ApiException("删除地区失败");
|
}
|
deleteRedisCache(RegionTypeEnum.INSIDE.getCode());
|
return 0;
|
}
|
|
@Override
|
public List<SysRegion> getChildRegionById(Long regionId) {
|
return list(Wrappers.<SysRegion>lambdaQuery()
|
.eq(true,SysRegion::getParentId,regionId));
|
}
|
}
|