Pu Zhibing
2024-09-23 e49f212c5ae9c5382386ddc0d34e346799720116
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.ruoyi.system.service.impl.config;
 
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.constant.SecurityConstant;
import com.ruoyi.system.domain.pojo.config.Region;
import com.ruoyi.system.domain.vo.RegionVo;
import com.ruoyi.system.mapper.config.RegionMapper;
import com.ruoyi.system.service.config.RegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.util.HttpUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/**
 * <p>
 * 省市区三级联动 服务实现类
 * </p>
 *
 * @author jqs
 * @since 2023-06-12
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
 
    @Resource
    private RegionMapper regionMapper;
 
    @Resource
    private RedisService redisService;
 
    /**
     * @param
     * @return List<RegionVo>
     * @description
     * @author jqs
     * @date 2023/6/12 15:18
     */
    @Override
    public List<RegionVo> listCityVo() {
        if (redisService.hasKey(SecurityConstant.CITY_LIST)) {
            // 如果存在,则直接从缓存中获取并返回
            List<RegionVo> result = redisService.getCacheList(SecurityConstant.CITY_LIST);
            return result;
        } else {
            // 如果不存在,则从数据库中查询并进行处理
            List<RegionVo> regionVoList = regionMapper.listRegionVo();
            List<RegionVo> regionVoList1 = new ArrayList<>();
            List<RegionVo> regionVoList2 = new ArrayList<>();
            List<RegionVo> regionVoList3 = new ArrayList<>();
            // 将地区按照级别分别存放到不同的列表中
            for (RegionVo regionVo : regionVoList) {
                if (regionVo.getLevel() == 1) {
                    regionVoList1.add(regionVo);
                }
                if (regionVo.getLevel() == 2) {
                    regionVoList2.add(regionVo);
                }
                if (regionVo.getLevel() == 3) {
                    regionVoList3.add(regionVo);
                }
            }
            RegionVo children;
            Iterator<RegionVo> regionVoIterator2;
 
            for (RegionVo regionVo : regionVoList2) {
                if (null == regionVo.getChildren()) {
                    regionVo.setChildren(new ArrayList<>());
                }
                regionVoIterator2 = regionVoList3.iterator();
                while (regionVoIterator2.hasNext()) {
                    children = regionVoIterator2.next();
                    if (regionVo.getId().equals(children.getParentId())) {
                        regionVo.getChildren().add(children);
                        regionVoIterator2.remove();
                    }
                }
            }
 
            // 将第二级地区挂到第一级地区下面
            for (RegionVo regionVo : regionVoList1) {
                if (null == regionVo.getChildren()) {
                    regionVo.setChildren(new ArrayList<>());
                }
                regionVoIterator2 = regionVoList2.iterator();
                while (regionVoIterator2.hasNext()) {
                    children = regionVoIterator2.next();
                    if (regionVo.getId().equals(children.getParentId())) {
                        regionVo.getChildren().add(children);
                        regionVoIterator2.remove();
                    }
                }
            }
            // 将处理后的地区列表存入缓存中
            redisService.setCacheList(SecurityConstant.CITY_LIST, regionVoList1);
            // 返回处理后的地区列表
            return regionVoList1;
        }
    }
    
    
    @Override
    public List<RegionVo> listRegions(Long pid) {
        return regionMapper.listRegions(pid);
    }
    
    /**
     * @param
     * @return List<RegionVo>
     * @description
     * @author jqs
     * @date 2023/6/12 15:18
     */
    @Override
    public List<RegionVo> listRegionVo() {
        if (redisService.hasKey(SecurityConstant.REGION_LIST)) {
            // 如果存在,则直接从缓存中获取并返回
            List<RegionVo> result = redisService.getCacheList(SecurityConstant.REGION_LIST);
            return result;
        } else {
            // 如果不存在,则从数据库中查询并进行处理
            List<RegionVo> regionVoList = regionMapper.listRegionVo();
            List<RegionVo> regionVoList1 = new ArrayList<>();
            List<RegionVo> regionVoList2 = new ArrayList<>();
            List<RegionVo> regionVoList3 = new ArrayList<>();
            // 将地区按照级别分别存放到不同的列表中
            for (RegionVo regionVo : regionVoList) {
                if (regionVo.getLevel() == 1) {
                    regionVoList1.add(regionVo);
                }
                if (regionVo.getLevel() == 2) {
                    regionVoList2.add(regionVo);
                }
                if (regionVo.getLevel() == 3) {
                    regionVoList3.add(regionVo);
                }
            }
            Iterator<RegionVo> regionVoIterator3;
            RegionVo children;
            // 将第三级地区挂到第二级地区下面
            for (RegionVo regionVo : regionVoList2) {
                if (null == regionVo.getChildren()) {
                    regionVo.setChildren(new ArrayList<>());
                }
                regionVoIterator3 = regionVoList3.iterator();
                while (regionVoIterator3.hasNext()) {
                    children = regionVoIterator3.next();
                    if (regionVo.getId().equals(children.getParentId())) {
                        regionVo.getChildren().add(children);
                        regionVoIterator3.remove();
                    }
                }
            }
            Iterator<RegionVo> regionVoIterator2;
            // 将第二级地区挂到第一级地区下面
            for (RegionVo regionVo : regionVoList1) {
                if (null == regionVo.getChildren()) {
                    regionVo.setChildren(new ArrayList<>());
                }
                regionVoIterator2 = regionVoList2.iterator();
                while (regionVoIterator2.hasNext()) {
                    children = regionVoIterator2.next();
                    if (regionVo.getId().equals(children.getParentId())) {
                        regionVo.getChildren().add(children);
                        regionVoIterator2.remove();
                    }
                }
            }
            // 将处理后的地区列表存入缓存中
            redisService.setCacheList(SecurityConstant.REGION_LIST, regionVoList1);
            // 返回处理后的地区列表
            return regionVoList1;
        }
    }
 
 
 
//    public R renewalRegion(){
//        HttpUtils.doPost()
//    }
}