kongzy
2024-06-03 022b17044ab6bb284fd6313da91d1d1dfb2d5079
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
package com.gkhy.admin;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gkhy.assess.admin.GkhyAdminApplication;
import com.gkhy.assess.system.domain.SysRegion;
import com.gkhy.assess.system.service.SysRegionService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GkhyAdminApplication.class)
//@ActiveProfiles("dev")
@Slf4j
public class RegionTest {
 
    @Autowired
    private SysRegionService regionService;
 
    //添加疆外数据
    @Test
    public void addRegion() throws IOException {
        String path="F:\\kzy\\codes\\java\\smart_assess\\assess-admin\\src\\main\\resources\\region.json";
        StringBuilder stringBuilder=new StringBuilder();
        BufferedReader br=new BufferedReader(new FileReader(new File(path)));
        String s=null;
        while ((s=br.readLine())!=null){
            stringBuilder.append(s);
        }
        br.close();
        JSONArray jsonArray = JSONObject.parseArray(stringBuilder.toString());
 
        for(Object obj : jsonArray){
            JSONObject jsonObject= (JSONObject) obj;
            String pro= (String) jsonObject.get("province");
            SysRegion region=new SysRegion()
                    .setName(pro)
                    .setRegionType(1);
            regionService.save(region);
            List<JSONObject> citys= (List<JSONObject>) jsonObject.get("citys");
            if(citys.size()==1){
                continue;
            }
            List<SysRegion> childRegion=new ArrayList<>();
            for(JSONObject cityObject:citys){
                String cityName= (String) cityObject.get("city");
                SysRegion region2=new SysRegion()
                        .setName(cityName)
                        .setRegionType(1)
                        .setParentId(region.getId());
                childRegion.add(region2);
            }
            regionService.saveBatch(childRegion);
        }
    }
    //添加疆内数据
    @Test
    public void addRegion2() throws IOException {
        String path="F:\\kzy\\codes\\java\\smart_assess\\assess-admin\\src\\main\\resources\\region.json";
        StringBuilder stringBuilder=new StringBuilder();
        BufferedReader br=new BufferedReader(new FileReader(new File(path)));
        String s=null;
        while ((s=br.readLine())!=null){
            stringBuilder.append(s);
        }
        br.close();
        JSONArray jsonArray = JSONObject.parseArray(stringBuilder.toString());
 
        for(Object obj : jsonArray){
            JSONObject jsonObject= (JSONObject) obj;
            String pro= (String) jsonObject.get("province");
            if(!pro.equalsIgnoreCase("新疆维吾尔自治区")){
                continue;
            }
            List<JSONObject> citys= (List<JSONObject>) jsonObject.get("citys");
            for(JSONObject cityObject:citys){
                String cityName= (String) cityObject.get("city");
                SysRegion region=new SysRegion()
                        .setName(cityName)
                        .setRegionType(0);
                regionService.save(region);
                List<JSONObject> areas= (List<JSONObject>) cityObject.get("areas");
                List<SysRegion> childRegion=new ArrayList<>();
                for(JSONObject areaObject:areas){
                    String areaName= (String) areaObject.get("area");
                    SysRegion region2=new SysRegion()
                            .setName(areaName)
                            .setRegionType(0)
                            .setParentId(region.getId());
                    childRegion.add(region2);
                }
                regionService.saveBatch(childRegion);
            }
 
        }
    }
 
    @Test
    public void testLog(){
        try {
            int i = 1 / 0;
        }catch (Exception e){
            log.error("error=",e);
        }
    }
}