package com.qmth.distributed.print;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.qmth.boot.core.fss.service.FileService;
import com.qmth.boot.core.fss.store.FileStore;
import com.qmth.boot.core.fss.utils.FssUtils;
import com.qmth.teachcloud.common.config.DictionaryConfig;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.UploadFileEnum;
import com.qmth.teachcloud.common.util.FileStoreUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;

/**
 * @Description: 新存储系统测试类
 * @Author: CaoZixuan
 * @Date: 2021-08-16
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class FssTest {
    private final static Logger log = LoggerFactory.getLogger(FssTest.class);
    @Resource
    private FileService fileService;
    @Resource
    private FileStoreUtil fileStoreUtil;
    @Resource
    private DictionaryConfig dictionaryConfig;

    @Test
    public void uploadFile1() throws Exception {
        FileStore fileStore1 = fileService.getFileStore("public");
        File file1 = new File("/Users/king/Downloads/distributed-print.log");
        fileStore1.write("distributed-print.log", new FileInputStream(file1), DigestUtils.md5Hex(new FileInputStream(file1)));

        FileStore fileStore2 = fileService.getFileStore("private");
        File file2 = new File("/Users/king/Downloads/dump-distributed-v2.2.0-202108121702.sql");
        fileStore2.write("dump-distributed-v2.2.0-202108121702.sql", new FileInputStream(file2), DigestUtils.md5Hex(new FileInputStream(file2)));
    }

    @Test
    public void uploadFile() throws Exception {
        UploadFileEnum type = UploadFileEnum.FILE;
        File file = new File("C:\\Users\\ASUS\\Desktop\\excel导入临时\\课程导入模板.xlsx");
//        File file = new File("C:\\Users\\ASUS\\Desktop\\分布式印刷.txt");
        String date = DateUtil.today().replace("-", "/");
        System.out.println(file.getName());
        System.out.println(date);
        String path = FssUtils.buildPath(type.getTitle(), date, SystemConstant.getUuid() + SystemConstant.XLSX);
        System.out.println(path);
//        fileService.getFileStore("public").write(path, new FileInputStream(file), DigestUtils.md5Hex(new FileInputStream(file)));
        fileStoreUtil.ossUpload(path,new FileInputStream(file),DigestUtils.md5Hex(new FileInputStream(file)),"public");
    }

    @Test
    public void findFile(){
        String path = "{\"path\":\"file/2021/08/18/7faa128fc0104e05b34b32961c221ac2.xlsx\",\"uploadType\":\"FILE\",\"type\":\"oss\"}";
        JSONObject jsonObject = JSONObject.parseObject(path);
        String attachmentType = (String) jsonObject.get(SystemConstant.TYPE);
        String filePath = (String) jsonObject.get(SystemConstant.PATH);
        UploadFileEnum uploadFileEnum = UploadFileEnum.valueOf((String) jsonObject.get(SystemConstant.UPLOAD_TYPE));

        String server;
        String config;
        if ("public".equals(uploadFileEnum.getFssType())){
            config = dictionaryConfig.fssPublicDomain().getConfig();
            server = dictionaryConfig.fssPublicDomain().getServer();
        }else if ("private".equals(uploadFileEnum.getFssType())){
            config = dictionaryConfig.fssPrivateDomain().getConfig();
            server = dictionaryConfig.fssPrivateDomain().getServer();
        }else {
            throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
        }

        String dirPath = server + "/" + filePath;
        System.out.println(config);
        System.out.println(server);
        System.out.println(dirPath);
    }

    @Test
    public void findType(){
        String path = "upload/2021/08/18/7faa128fc0104e05b34b32961c221ac2.xlsx";
        String target = path.substring(0,path.indexOf('/'));
        UploadFileEnum type = UploadFileEnum.valueOf(target.toUpperCase());
        System.out.println(type);
    }
}
