package com.dxhy;

import java.io.File;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.dxhy.oss.model.FileModel;
import com.dxhy.oss.service.FileService;

import cn.hutool.core.io.FileUtil;

/**
 * @author jiaohongyang（焦红阳）
 * @date 2020-09-30
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DxhyOssApplication.class)
public class OssTest {
    @Resource
    private FileService fileService;

    @Test
    public void ossUploadTest() {
        String fileName = "/GYL/YONGHUI/2022/09/08/update.zip";
        String originFileName = "/Users/jiaohongyang/product/project/project/jxpt/ofd/pdf/update.zip";
        FileModel fileModel = new FileModel();
        try {
            fileModel = fileService.uploadFile(fileName, originFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("文件上传返回结果:" + fileModel.getCode() + "  " + fileModel.getFilePath());

    }

    @Test
    public void ossGylUploadTest() {
        String fileName = "/GYL/DUODIAN/2022/12/07/update.zip";
        String originFileName = "/Users/jiaohongyang/product/project/project/jxpt/ofd/pdf/update.zip";
        FileModel fileModel = new FileModel();
        try {
            fileModel = fileService.uploadFile(fileName, originFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("文件上传返回结果:" + fileModel.getCode() + "  " + fileModel.getFilePath());

    }

    @Test
    public void ossGylSdlpUploadTest() {
        String fileName = "/GYL/SDLP/2022/09/20/51发票助手_local_J_1.4.8.exe";
        String originFileName = "/Users/jiaohongyang/product/project/project/jxpt/ofd/pdf/51发票助手_local_J_1.4.8.exe";
        FileModel fileModel = new FileModel();
        try {
            fileModel = fileService.uploadFile(fileName, originFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("文件上传返回结果:" + fileModel.getCode() + "  " + fileModel.getFilePath());

    }

    @Test
    public void ossDownloadTest() {
        String downloadFile = "/jxfp/pdf/2022/1111/dxhy-admin.jar";
        // String downloadFile = "/jxfp/excel/2020/10/12/1282510566564315137_进项票据采集_2020-10-12_1602489969696.xlsx";
        String saveFile = "/Users/jiaohongyang/product/project/project/jxpt/ofd/pdf/dxhy-admin.jar";
        FileModel fileModel = new FileModel();
        try {
            fileModel = fileService.downFile(downloadFile, saveFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("文件下载返回结果" + fileModel.getCode() + "   " + fileModel.getFilePath());
    }

    @Test
    public void ossgetUrlTest() {
        String key = "/GYL/SDLP/2022/09/14/供应链协同开票助手-v1.1.0.1.20220914-x86_Setup.zip";
        FileModel fileModel = fileService.getUrl(key);
        System.out.println(fileModel.getCode() + "   " + fileModel.getFilePath());

    }

    @Test
    public void ossDeleteTest() {
        String downloadFile = "GYL/DUODIAN/2022/12/01/20221201（1.1.0.2）.zip";
        FileModel fileModel = new FileModel();
        try {
            fileModel = fileService.deleteFile(downloadFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("文件删除返回结果" + fileModel.getCode());

    }

    @Test
    public void ossPush() {
        String path = "/Volumes/product/PDF/";
        try {
            File[] ls = FileUtil.ls(path);
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2000, 10000, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(ls.length), r -> new Thread(r, "oss推送-" + r.hashCode()));
            for (File file : ls) {
                if (!threadPoolExecutor.isShutdown()) {
                    threadPoolExecutor.execute(new OfdHandle(file));
                }
            }
            threadPoolExecutor.shutdown();
            threadPoolExecutor.awaitTermination(60, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
