huangzhen
2023-09-26 28231163c688c379a688ce6878a1126ee218aa52
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
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.resp.DistrictTreeRespDTO;
import com.ruoyi.system.mapper.SysDistrictMapper;
import com.ruoyi.system.domain.SysDistrict;
import com.ruoyi.system.service.SysDistrictService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
 
import static com.ruoyi.common.utils.DictUtils.clearDictCache;
 
/**
 * @author hz
 * @since 2023-09-07 13:13:58
 */
@Service("sysDistrictService")
public class SysDistrictServiceImpl extends ServiceImpl<SysDistrictMapper, SysDistrict> implements SysDistrictService {
 
    @PostConstruct
    public void init()
    {
        loadingDistrictCache();
    }
 
    @Override
    public List<SysDistrict> selectAll() {
        if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_LIST_KEY)){
            List<SysDistrict> sysDistricts = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheConstants.SYS_DISTRICT_LIST_KEY);
            return sysDistricts;
        }
        List<SysDistrict> list = this.list();
        SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_LIST_KEY, list);
        return list;
    }
 
    @Override
    public void loadingDistrictCache()
    {
        List<SysDistrict> list = this.list();
        if (!CollectionUtils.isEmpty(list)) {
            SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_LIST_KEY, list);
        }
        List<DistrictTreeRespDTO> treeRespDTOS = this.selectAllByTree();
        if (!CollectionUtils.isEmpty(treeRespDTOS)) {
            SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_TREE_KEY, list);
        }
    }
 
 
    @Override
    public void clearDistrictCache()
    {
        if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_LIST_KEY)){
            SpringUtils.getBean(RedisCache.class).deleteObject(CacheConstants.SYS_DISTRICT_LIST_KEY);
        }
        if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_TREE_KEY)){
            SpringUtils.getBean(RedisCache.class).deleteObject(CacheConstants.SYS_DISTRICT_TREE_KEY);
        }
    }
 
    /**
     * 重置区划缓存数据
     */
    @Override
    public void resetDistrictCache()
    {
        clearDistrictCache();
        loadingDistrictCache();
    }
 
    @Override
    public List<DistrictTreeRespDTO> selectAllByTree() {
        if (SpringUtils.getBean(RedisCache.class).hasKey(CacheConstants.SYS_DISTRICT_TREE_KEY)){
            List<DistrictTreeRespDTO> sysDistrictTree = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheConstants.SYS_DISTRICT_TREE_KEY);
            return sysDistrictTree;
        }
        List<SysDistrict> districts = this.selectAll();
        if (CollectionUtils.isEmpty(districts)){
            throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL,"区划信息获取失败");
        }
        //获取父节点,0表示父节点,让此次递归有个开头
        List<DistrictTreeRespDTO> collect = districts.stream()
                //父节点的id = 0,根据它开始进行实现。
                .filter(e -> e.getParentcode().equals("0"))
                .map(e ->{
                    DistrictTreeRespDTO dto = new DistrictTreeRespDTO();
                    BeanUtils.copyProperties(e,dto);
                    //将过滤之后的数据来进行方法调用,拿到他的所有的子节点信息并进行封装
                    dto.setChildren(getchildrens(dto, districts));
                    return dto;
                }).collect(Collectors.toList());
        SpringUtils.getBean(RedisCache.class).setCacheObject(CacheConstants.SYS_DISTRICT_TREE_KEY, collect);
        return collect;
    }
 
    private List<DistrictTreeRespDTO> getchildrens(DistrictTreeRespDTO root,List<SysDistrict> districts){
        //root为每次最新的传递过来的数据,也就是上面过滤之后的 e ;
        List<DistrictTreeRespDTO> collect = districts.stream()
                //根据传递过来的 e ,拿到他的id,来查询出他的子节点id 这里有个特点 e.id = 子节点的父节点id
                .filter(e -> Objects.equals(e.getParentcode(), root.getCode()))
                .map(e -> {
                    DistrictTreeRespDTO dto = new DistrictTreeRespDTO();
                    BeanUtils.copyProperties(e,dto);
                    dto.setChildren(getchildrens(dto, districts));
                    //递归找到他的子节点,直到找到最后一个子节点为止,饼进行封装。
                    return dto;
                }).collect(Collectors.toList());
        return collect;
    }
 
    @Override
    public List<Long> selectChildrenIdsById(Long id) {
        List<SysDistrict> districts = this.selectAll();
        if (CollectionUtils.isEmpty(districts)){
            throw new BusinessException(this.getClass(), ResultConstants.SYSTEM_ERROR_DATABASE_FAIL,"区划信息获取失败");
        }
        List<Long> list = new ArrayList<>();
        districts.stream()
                //父节点的id = 0,根据它开始进行实现。
                .filter(e -> e.getId().equals(id))
                .map(e ->{
                    list.add(e.getId());
                    getchildrenIds(list,e,districts);
                    return e;
                }).collect(Collectors.toList());
        return list;
    }
 
    private void getchildrenIds(List<Long> list,SysDistrict root,List<SysDistrict> districts){
        //root为每次最新的传递过来的数据,也就是上面过滤之后的 e ;
        List<SysDistrict> collect = districts.stream()
                //根据传递过来的 e ,拿到他的id,来查询出他的子节点id 这里有个特点 e.id = 子节点的父节点id
                .filter(e -> Objects.equals(e.getParentcode(), root.getCode()))
                .map(e -> {
                    list.add(e.getId());
                    getchildrenIds(list,e,districts);
                    //递归找到他的子节点,直到找到最后一个子节点为止,饼进行封装。
                    return e;
                }).collect(Collectors.toList());
    }
}