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 com.gkhy.assess.system.service.impl.SysRegionServiceImpl; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import sun.swing.plaf.GTKKeybindings; 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 citys= (List) jsonObject.get("citys"); if(citys.size()==1){ continue; } List 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 citys= (List) 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 areas= (List) cityObject.get("areas"); List 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); } } } }