kongzy
2023-12-08 ca5445257b1fdeceddf3fcc2dea18c442023aeb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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&&regionList.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));
    }
}