| | |
| | | package com.gkhy.assess.system.service.impl; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | 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.domain.SysUser; |
| | | import com.gkhy.assess.system.mapper.SysRegionMapper; |
| | | import com.gkhy.assess.system.service.SysRegionService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.springframework.data.domain.Sort; |
| | | 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; |
| | | |
| | | /** |
| | |
| | | @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(StrUtil.isNotBlank(region.getName())){ |
| | | 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); |
| | | //筛选出所有一级标签 |
| | | return regions.stream() |
| | | .filter(tagEntity -> tagEntity.getParentId()==0L) |
| | | .peek(tagEntity -> tagEntity.setChildren(this.listRegionChildren(tagEntity,regions))) |
| | | 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){ |
| | |
| | | |
| | | @Override |
| | | public int addRegion(SysRegion region) { |
| | | if(!checkRegionUnique(new SysRegion().setName(region.getName()))){ |
| | | 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.getParentId()==null?0L:region.getParentId()); |
| | | SysRegion info = baseMapper.checkRegionUnique(region.getName(),region.getRegionType(),region.getParentId()==null?0L:region.getParentId()); |
| | | if (info!=null && info.getId().longValue() != userId.longValue()) |
| | | { |
| | | return false; |
| | |
| | | |
| | | @Override |
| | | public int editRegion(SysRegion region) { |
| | | if(!checkRegionUnique(new SysRegion().setName(region.getName()))){ |
| | | 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; |
| | | } |
| | | |
| | |
| | | Long count=count(Wrappers.<SysRegion>lambdaQuery() |
| | | .eq(true,SysRegion::getParentId,reginId)); |
| | | if(count>0){ |
| | | throw new ApiException("下级存在区县数据"); |
| | | 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; |
| | | } |
| | | |