package com.ningdatech.carapi.sys.helper;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.industry.model.dto.UseCertPageParamDTO;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * RegionsCacheHelperTest
 * </p>
 *
 * @author WendyYang
 * @since 17:50 2022/10/22
 */
class RegionsCacheHelperTest extends AppTests {

    @Autowired
    private RegionsCacheHelper regionsCacheHelper;

    @Test
    public void listChildren() {
        System.out.println("regionsCacheHelper.listParents(1L) = " + regionsCacheHelper.listChildRegionId(2L));
    }

    @Test
    public void listParents() {
        System.out.println("regionsCacheHelper.listParents(1L) = " + regionsCacheHelper.listParents(2L));
    }

    @Test
    public void getByRegionId() {
        System.out.println("regionsCacheHelper.getByRegionId(7) = " + regionsCacheHelper.getByRegionId(7L));
    }

    @Test
    public void downRegionJsonFile() {
        List<Long> longs = regionsCacheHelper.listChildRegionId(116L);
        List<String> regionCodes = longs.stream()
                .map(w -> regionsCacheHelper.getByRegionId(w).getRegionCode())
                .distinct()
                .collect(Collectors.toList());
        regionCodes.forEach(w -> {
            String name = w + (!w.substring(4).equals("00") ? ".json" : "_full.json");
            String url = String.format("https://geo.datav.aliyun.com/areas_v3/bound/%s", name);
            try {
                URL url1 = new URL(url);
                InputStream stream = url1.openConnection().getInputStream();
                FileUtils.copyInputStreamToFile(stream, new File("/Users/wendy/desktop/" + w + ".json"));
            } catch (Exception ignore) {
                System.out.println(w);
            }
        });
    }

}