chenhj
4 天以前 718e95b4d0fa5cda71724a48a4c34114c57fa523
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
package com.ruoyi.web.controller.init;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.basic.entity.City;
import com.ruoyi.basic.entity.District;
import com.ruoyi.basic.entity.Province;
import com.ruoyi.basic.mapper.CityMapper;
import com.ruoyi.basic.mapper.DistrictMapper;
import com.ruoyi.basic.mapper.ProvinceMapper;
import com.ruoyi.web.controller.init.dto.AreaDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
@Slf4j
@Component
public class MyStartupRunner implements CommandLineRunner {
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
 
    /**
     * 项目启动时需要执行的方法
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        // 初始化省市区
        initRegion();
    }
 
    private void initRegion() {
        // 判断是是否有省市区信息
        List<Province> olds = provinceMapper.selectList(null);
        if (olds.isEmpty()) {
            // 读取地区json文件
            List<AreaDTO> areaDTOS = null;
            try {
                // 从 resources/doc/ 加载 JSON 文件
                ClassPathResource resource = new ClassPathResource("doc/area.json");
 
                ObjectMapper objectMapper = new ObjectMapper();
                areaDTOS = objectMapper.readValue(
                        resource.getInputStream(),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, AreaDTO.class)
                );
            } catch (Exception e) {
                log.error("读取地区json文件失败:" + e);
            }
 
            List<Province> provinces = new ArrayList<>();
            List<City> cities = new ArrayList<>();
            List<District> districts = new ArrayList<>();
 
            if (!areaDTOS.isEmpty()) {
                log.info("地区信息初始化开始");
 
                // 省
                areaDTOS.forEach(areaDTO -> {
                    Province province = new Province();
                    province.setId(Long.parseLong(areaDTO.getNo()));
                    province.setName(areaDTO.getName());
                    provinces.add(province);
 
                    if (!areaDTO.getChildren().isEmpty()) {
                        // 市
                        areaDTO.getChildren().forEach(cityDTO -> {
                            City city = new City();
                            city.setId(Long.parseLong(cityDTO.getNo()));
                            city.setName(cityDTO.getName());
                            city.setProvinceId(Long.valueOf(areaDTO.getNo()));
                            cities.add(city);
 
                            if (!cityDTO.getChildren().isEmpty()) {
                                // 区
                                cityDTO.getChildren().forEach(districtDTO -> {
                                    District district = new District();
                                    district.setId(Long.parseLong(districtDTO.getNo()));
                                    district.setName(districtDTO.getName());
                                    district.setCityId(Long.valueOf(cityDTO.getNo()));
                                    districts.add(district);
                                });
                            }
                        });
                    }
                });
 
 
                provinceMapper.insertOrUpdate(provinces);
                cityMapper.insertOrUpdate(cities);
                districtMapper.insertOrUpdate(districts);
 
                log.info("地区信息初始化结束");
            }
        }
 
 
    }
}