package com.ruoyi.wayline.utils;

import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtil {

    private static final String FILE_EXTENSION = ".kmz";

    /**
     * 获取文件名并拼接后缀
     * @param path 文件的路径
     * @return 完整的文件名
     */
    public static String getFileName(Path path) {
        String fileName = path.getFileName().toString();
        if (!fileName.endsWith(FILE_EXTENSION)) {
            fileName += FILE_EXTENSION;
        }
        return fileName;
    }

    /**
     * 获取文件所在的目录路径
     * @param path 文件的完整路径
     * @return 目录路径
     */
    public static Path getDirectoryPath(Path path) {
        return path.getParent();
    }

    /**
     * 把 byte[] 中的内容写入指定路径的文件
     * @param path 文件路径
     * @param bytes 要写入的字节数据
     * @throws IOException
     */
    public static void transferStreamToFile(Path path, byte[] bytes) throws IOException {
        try (FileOutputStream fileOutputStream = new FileOutputStream(path.toFile())) {
            fileOutputStream.write(bytes);
        }
    }


    public static void main(String[] args) {
        ConcurrencyTester tester = ThreadUtil.concurrencyTest(10000, () -> {
            String body = HttpRequest.get("http://192.168.110.23:9099/ruoyi/system/books/list")
                    .header("authorization",
                            "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjVkMjNlZWFjLTQ5MmItNDIyZS1hMzBhLWM1MzZjNDU5OGQyZiJ9._UQYWmYQGddpgjzhczoh_C5HpxMVUUKJRIPvVOQFsrfHIG8-EBMwW7VM3pS6yaQwjzOCE-upSX1JwTa86LdAbw")
                    .execute().body();
            Console.log(body);
        });
        // 获取总的执行时间，单位毫秒
        Console.log(tester.getInterval());
    }
}
