import com.CWebApplication;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.dao.SecRegionMapper;
import com.hfkj.entity.SecRegion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.io.*;
/**
 * @ClassName RegionTest
 * @Description: TODO ()
 * @Author 胡锐
 * @Date 2020/12/29
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CWebApplication.class)
@WebAppConfiguration
public class RegionTest {

    @Resource
    SecRegionMapper secRegionMapper;

    @Test
    public void test(){
        try {

            String jsonStr = readJsonFile(new File("C:/Users/hurui/Desktop/regions.json/regions.json"));
            JSONArray jsonArray = JSONArray.parseArray(jsonStr);

            SecRegion region;

            for (Object provinceObject : jsonArray) {
                JSONObject province = (JSONObject)provinceObject;
                region = new SecRegion();
                region.setRegionId(province.getLong("code"));
                region.setRegionName(province.getString("name"));
                region.setStatus(1);
                secRegionMapper.insert(region);

                JSONArray cityArray = province.getJSONArray("childs");
                for (Object cityObject : cityArray) {
                    JSONObject city = (JSONObject)cityObject;
                    SecRegion cityRegion = new SecRegion();
                    cityRegion.setParentId(region.getRegionId());
                    cityRegion.setRegionId(city.getLong("code"));
                    cityRegion.setRegionName(city.getString("name"));
                    cityRegion.setStatus(1);
                    secRegionMapper.insert(cityRegion);

                    JSONArray areaArray = city.getJSONArray("childs");
                    for (Object areaObject : areaArray) {
                        JSONObject area = (JSONObject)areaObject;
                        SecRegion areaRegion = new SecRegion();
                        areaRegion.setParentId(cityRegion.getRegionId());
                        areaRegion.setRegionId(area.getLong("code"));
                        areaRegion.setRegionName(area.getString("name"));
                        areaRegion.setStatus(1);
                        secRegionMapper.insert(areaRegion);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String readJsonFile(File jsonFile) {
        String jsonStr = "";
        try {
            //File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
