package com.bjtdba.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class uploadDownload {

    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI5tRfUFtmr5VFjqJ9D4dA";
    private static String accessKeySecret = "o6KrEEHr6WcpP0nu7TnP6ywnXPpVQm";
    private static String bucketName = "bjtdba";
    /**
     *
     */
    public static Map<String,String> qrCode(MultipartFile file){
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        Map<String,String> urlMap = new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        df.format(new Date());
        try {
            // 判断Bucket是否存在。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            // 链接地址是：https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //fileName = fileName;
            String filePath = "yihucha/qrCode/"+df.format(new Date()) + "/" +  fileName;
            ossClient.putObject(bucketName, filePath, inputStream);
            ossClient.shutdown();
            String url = "https://"+"cdn." + bucketName +".com"+ "/" + filePath;
            urlMap.put("url",url);
        }catch (IOException oe) {
            oe.printStackTrace();
        }
        return urlMap;
    }
    /**
     * 上传照片
     * @param file
     * @param uid
     * @return
     */
    public static Map<String,String> uploadImg(MultipartFile file, String uid){
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        Map<String,String> urlMap = new HashMap<>();
        try {
            // 判断Bucket是否存在。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            // 链接地址是：https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;
            String filePath = "yihucha/images/data/"+uid + "/" +  fileName;
            ossClient.putObject(bucketName, filePath, inputStream);
            ossClient.shutdown();
            String url = "https://"+"cdn." + bucketName +".com"+ "/" + filePath;
            urlMap.put("url",url);
        }catch (IOException oe) {
            oe.printStackTrace();
        }
        return urlMap;
    }

    /**
     * 下载照片
     * @param files
     * @return
     */
    public static String downloadImg(String[] files,String uid){
        String pathName = "";
        for(int i=0;i<files.length;i++) {
            //URL url = new URL(files[i]);
            // 创建 OSSClient 实例
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            // 下载Object到本地文件，并保存到指定的本地路径中。如果指定的本地文件存在会覆盖，不存在则新建。
            // 如果未指定本地路径，则下载后的文件默认保存到示例程序所属项目对应本地路径中。
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            pathName = "/Users/hanyuxing/"+uid;
            File parentDir = new File(pathName);
            if(!parentDir.exists()) {
                parentDir.mkdirs();
            }
            ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, String.valueOf(files[i])), new File(pathName+"/"+uuid+".png"));
            // 关闭OSSClient。
            ossClient.shutdown();
        }
        return pathName;
    }

    /**
     * 上传zip压缩文件
     * @param file
     * @param mer_id
     * @return
     * @throws IOException
     */
    public static String uploadZip(MultipartFile file, String mer_id) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        InputStream inputStream = file.getInputStream();
        String fileName = file.getOriginalFilename();
        String filePath = "yihucha/images/data/"+mer_id + "/" +  fileName;
        ossClient.putObject(bucketName, filePath, inputStream);
        ossClient.shutdown();
        String url = "https://"+"cdn." + bucketName +".com"+ "/" + filePath;
        return url;
    }
}
