package com.platform.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author abnerhou
 * @date 2020/5/25 16:26
 * @desciption
 */
@Slf4j
@Component
public class UploadToOssUtil {

    @Value("${oss.point}")
    private String ossEndPoint;

    @Value("${oss.key}")
    private String ossAppkey;

    @Value("${oss.secrey}")
    private String ossAppSecret;

    @Value("${oss.bucket}")
    private String ossBucket;


    public void delete(String key1) throws IOException {
        OSSClient ossClient = new OSSClient(ossEndPoint, ossAppkey, ossAppSecret);
        try {
            // bucketMgr.delete(bucketname, key);

            // 2019-2-28 启动aliyun oss 空间
            ossClient.deleteObject(ossBucket, key1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void uploadThenDelete(String filePath, String key1) {
        OSSClient ossClient = new OSSClient(ossEndPoint, ossAppkey, ossAppSecret);
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                log.error("要上传的文件不存在：" + filePath);
            }

            ossClient.putObject(ossBucket, key1, new File(filePath));

        } catch (Exception e) {
            log.error(e.toString() + filePath);
        }finally {
            if(null != ossClient){
                ossClient.shutdown();
                FileUtils.deleteFile(filePath);
                log.info("关闭oss 客户端，并且删除上传的本地文件");
            }
        }
    }

    public void upload2(String filePath, String key1) {
        OSSClient ossClient = new OSSClient(ossEndPoint, ossAppkey, ossAppSecret);
        try {
            // 调用put方法上传
            // Response res = uploadManager.put(FilePath, key, getUpToken(key));
            // 打印返回的信息
            // log.info(res.bodyString());

            // 2019-2-28 启动aliyun oss 空间
            ossClient.putObject(ossBucket, key1, new File(filePath));
        } catch (Exception e) {
            log.error(e.toString() + filePath);
        }
    }

    //上传的数据是文件夹，参数是文件夹路径，key是上传后的文件名
    public void uploadMulFiles(Map<String, String> filepaths) {
        if (filepaths == null) {
            return;
        }
//		log.info("开始批量上传到七牛：" + new Date().toString());
        Long start = System.currentTimeMillis();
        log.info("开始批量上传到阿里云：" + start);
        if (filepaths.size() > 50) {
            for (String filePath : filepaths.keySet()) {
                // log.info("文件："+key);
                upload2(filePath, filepaths.get(filePath));
            }
        } else {
            for (String filePath : filepaths.keySet()) {
//				log.info("文件：" + filePath + "到七牛：" + filepaths.get(filePath));
                log.info("文件：" + filePath + "到阿里云：" + filepaths.get(filePath));
                uploadThenDelete(filePath, filepaths.get(filePath));
            }
        }
        log.info("批量上传阿里云完毕：" + (System.currentTimeMillis() - start));
    }

    public int deleteFile(String prefix){
        OSSClient ossClient = new OSSClient(ossEndPoint, ossAppkey, ossAppSecret);
        ObjectListing objectListing = ossClient.listObjects(ossBucket, prefix);
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        try {
            for (OSSObjectSummary s : sums) {
                delete(s.getKey());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sums.size();
    }


    public String upload5(String filePath, String key1) {
        OSSClient ossClient = new OSSClient(ossEndPoint, ossAppkey, ossAppSecret);
        PutObjectResult result = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                log.error("要上传的文件不存在：" + filePath);
            }


            result = ossClient.putObject(ossBucket, key1, new File(filePath));

        } catch (Exception e) {
            log.error(e.toString() + filePath);
        }

        log.info(" getETag : " + result.getETag());
        log.info("1 : " + result.toString());
        log.info("2 : " + result.getRequestId());
        log.info("3 : " + result.getClientCRC());
        log.info("4 : " + result.getResponse());
        log.info("5 : " + result.getServerCRC());
        return result.getETag();
    }
}
