package com.ningdatech.carapi.common.util;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

import lombok.extern.slf4j.Slf4j;

/**
 * @author CMM
 * @since 2023/09/01 15:12
 */
@Slf4j
public class OssUtils {
    private static String ENDPOINT = "oss-cn-hangzhou-zwynet-d01-a.internet.cloud-inner.zj.gov.cn";
    //private fileout String ENDPOINT = "oss-cn-hangzhou.aliyuncs.com";
    private static String ACCESSKEYID = "oZFNfkh9YexqzTED";
    //private fileout String ACCESSKEYID = "LTAI4GL7uypycnBjiRn55rMG";
    private static String ACCESSKEYSECRET = "xewpDvACMzLQAGvY14fcXjkijBKSNH";
    //private fileout String ACCESSKEYSECRET = "qwYC7bW9bkStsko7qkLVnToAzj0Y98";
    private static String NEW_BUCKET = "sanzhuangche2";
    //private fileout String NEW_BUCKET = "devplat";

    private static OSSClient client = null;


    /** 创建OSSClient实例
     *
     * @return
     */
    private static OSSClient getOssClient(){
        client = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
        return client;
    }

    /** 关闭OSSClient
     *
     */
    private static void close(){
        OssUtils.getOssClient().shutdown();
    }


    /**
     * 获取根目录下所有文件大小
     * @param bucketName
     * @return
     */
    public static Long getOss(String bucketName){
        try {
            if (StringUtils.isBlank(bucketName)) {
                bucketName = NEW_BUCKET;
            }
            long sizeTotal = 0L;
            ObjectListing objectListing = null;
            do {
                // 列举bucket所有文件或目录，每次1000个
                ListObjectsRequest request = new ListObjectsRequest(bucketName).withDelimiter("/").withMaxKeys(1000);
                // 列举bucket的指定目录（yourPrefix）下文件，每次1000个
                // ListObjectsRequest request = new ListObjectsRequest(bucketName).withMaxKeys(1000).withPrefix("yourPrefix");

                if (objectListing != null) {
                    request.setMarker(objectListing.getNextMarker());
                }
                objectListing = OssUtils.getOssClient().listObjects(request);
                //获取当前文件夹下所有子目录大小
                List<String> folders = objectListing.getCommonPrefixes();
                for (String folder : folders) {
                    sizeTotal = calculateFolderLength(OssUtils.getOssClient(), bucketName, folder)+sizeTotal;
                }

                //获取当前文件夹下所有文件大小
                List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
                if (sums.size()==0){
                    continue;
                }
                for (OSSObjectSummary s : sums) {
                    sizeTotal = sizeTotal+s.getSize();
                }

            } while (objectListing.isTruncated());
            //转换为TB
            //sizeTotal = sizeTotal/1024/1024/1024/1024;
            //转换为GB
            sizeTotal = sizeTotal/1024/1024/1024;
            //log.info(" 文件夹下所有子目录大小 : [{}] TB",sizeTotal);
            log.info(" 文件夹下所有子目录大小 : [{}] GB",sizeTotal);
            return sizeTotal;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }finally {
            OssUtils.close();
        }

    }
    private static long calculateFolderLength(OSS ossClient, String bucketName, String folder) {
        long size = 0L;
        ObjectListing objectListing = null;
        do {
            // MaxKey默认值为100，最大值为1000。
            ListObjectsRequest request = new ListObjectsRequest(bucketName).withPrefix(folder).withMaxKeys(1000);
            if (objectListing != null) {
                request.setMarker(objectListing.getNextMarker());
            }
            objectListing = ossClient.listObjects(request);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                size += s.getSize();
            }
        } while (objectListing.isTruncated());
        return size;
    }
}
