puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
package com.sinata.rest.modular.system.controller;
 
import com.sinata.rest.common.ApiUtils;
import com.sinata.rest.modular.system.model.CityRegion;
import com.sinata.rest.modular.system.service.ICityRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/city")
//@Api(tags = "省市区相关", description = "省市区")
public class CityRegionController {
 
    //省市区全部数据缓存
    private Object allData;
 
    @Autowired
    private ICityRegionService cityRegionService;
 
    @GetMapping(value = "/vant_all")
    @ApiOperation(value = "返回Vant使用的省市区", notes = "返回Vant使用的省市区", response = ApiUtils.class)
    public Object getAll() {
        if (allData == null) {
            synchronized (this) {
                if (allData == null) {
                    List<CityRegion> all = cityRegionService.list();
                    HashMap<String, String> province = new HashMap<>();
                    HashMap<String, String> cities = new HashMap<>();
                    HashMap<String, String> areas = new HashMap<>();
                    for (CityRegion r : all) {
                        if (r.getParentId() == 0) {
                            province.put(r.getCode(), r.getName());
                        } else if (r.getCode().startsWith("00", 4)) {
                            cities.put(r.getCode(), r.getName());
                        } else {
                            areas.put(r.getCode(), r.getName());
                        }
                    }
                    Map<String, Object> r = new HashMap<>();
                    r.put("province_list", province);
                    r.put("city_list", cities);
                    r.put("county_list", areas);
                    allData = r;
                }
            }
        }
        return ApiUtils.returnOK(allData);
    }
 
}