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);
|
}
|
}
|
}
|