package common;

import com.BWebApplication;
import com.alibaba.fastjson.JSON;
import com.hfkj.entity.SecRegion;
import com.hfkj.service.CommonService;

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.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RegionTest
 * @Description: TODO ()
 * @Author 胡锐
 * @Date 2020/12/29
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BWebApplication.class)
@WebAppConfiguration
public class RegionTest {

    @Resource
    private CommonService commonService;

    @Test
    public void addLogs(){
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("exampleWrite.json"),"UTF-8");
            List<Map<String,Object>> jobTypeList = new ArrayList<>();
            List<Map<String,Object>> children1;

            List<SecRegion> parentRegion = commonService.getCities();
            for (SecRegion parent : parentRegion) {
                Map<String,Object> map = new HashMap<>();
                map.put("value", parent.getRegionId());
                map.put("label", parent.getRegionName());

                // 查询二级
                List<SecRegion> chinRegion = commonService.getRegionsByParentId(parent.getRegionId());
                children1 = new ArrayList<>();
                for (SecRegion chin : chinRegion) {
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("value", chin.getRegionId());
                    map1.put("label", chin.getRegionName());
                    children1.add(map1);
                }

                map.put("children", children1);
                jobTypeList.add(map);
            }

            osw.write(JSON.toJSONString(jobTypeList));
            osw.flush();//清空缓冲区，强制输出数据
            osw.close();//关闭输出流
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
