package yem.base.common.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 文件打包工具类
 *
 * @author longh
 * @description:
 * @date 2021年8月20日 下午3:33:04
 */
public class ZipUtil extends FileUtil {

    /**
     * 实例化工具类
     *
     * @param formId 单据标识
     */
    public ZipUtil(String formId) {
        super(formId);
    }

    /**
     * 下载文件并打包到指定位置
     *
     * @param srcFiles key是打包的文件名，value是文件下载地址
     * @param zipFile
     * @return
     * @throws IOException
     */
    public static String zipFiles(Map<String, String> srcFiles, File zipFile) throws IOException {
        File fileParent = zipFile.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdirs();
        }
        zipFile.createNewFile();
        if (isLinux()) {
            zipFile.setExecutable(true);//设置可执行权限
            zipFile.setReadable(true);//设置可读权限
            zipFile.setWritable(true);//设置可写权限
        }

        FileOutputStream fileOutputStream = null;
        ZipOutputStream zipOutputStream = null;
        fileOutputStream = new FileOutputStream(zipFile);
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        ZipEntry zipEntry = null;
        // 遍历文件下载地址
        for (Entry<String, String> set : srcFiles.entrySet()) {
            URL url = new URL(set.getValue());
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            httpURLConnection.setConnectTimeout(1000 * 5);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.connect();
            // 文件大小
            int fileLength = httpURLConnection.getContentLength();
            System.out.println("您要下载的文件大小为:" + fileLength / 1024 + "K");

            BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());

            // 实例化 ZipEntry 对象，源文件数组中的当前文件
            zipEntry = new ZipEntry(set.getKey());
            zipOutputStream.putNextEntry(zipEntry);
            int len;
            byte[] buffer = new byte[1024];
            while ((len = bin.read(buffer)) > 0) {
                zipOutputStream.write(buffer, 0, len);
            }

        }
        zipOutputStream.closeEntry();
        zipOutputStream.close();
        fileOutputStream.close();

        return zipFile.getPath();
    }

    /**
     * 打包文件
     *
     * @param srcFiles
     * @param zipFile
     * @throws IOException
     */
    public static void zipFiles(File[] srcFiles, File zipFile) throws IOException {
        File fileParent = zipFile.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdirs();
        }
        zipFile.createNewFile();
        if (isLinux()) {
            zipFile.setExecutable(true);//设置可执行权限
            zipFile.setReadable(true);//设置可读权限
            zipFile.setWritable(true);//设置可写权限
        }

        FileOutputStream fileOutputStream = null;
        ZipOutputStream zipOutputStream = null;
        FileInputStream fileInputStream = null;

        fileOutputStream = new FileOutputStream(zipFile);
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        ZipEntry zipEntry = null;
        for (int i = 0; i < srcFiles.length; i++) {
            // 将源文件数组中的当前文件读入 FileInputStream 流中
            fileInputStream = new FileInputStream(srcFiles[i]);
            // 实例化 ZipEntry 对象，源文件数组中的当前文件
            zipEntry = new ZipEntry(srcFiles[i].getName());
            zipOutputStream.putNextEntry(zipEntry);
            // 该变量记录每次真正读的字节个数
            int len;
            // 定义每次读取的字节数组
            byte[] buffer = new byte[1024];
            while ((len = fileInputStream.read(buffer)) > 0) {
                zipOutputStream.write(buffer, 0, len);
            }
        }
        zipOutputStream.closeEntry();
        zipOutputStream.close();
        fileInputStream.close();
        fileOutputStream.close();
    }

    /**
     * 打包附件面板的附件到指定位置
     *
     * @param appId
     * @param formId
     * @param pkId
     * @param fileName
     * @param attachmentInfo
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void zipFilesByAttachmentInfo(String appId, String formId, String pkId, String attachmentKey, String fileName, List<Map<String, Object>> attachmentInfo) throws IOException {
        Map<String, String> srcFiles = new HashMap<>();
        for (Map<String, Object> map : attachmentInfo) {
            srcFiles.put(map.get("name").toString(), map.get("url").toString());
        }
        AttachmentUtil.uploadAttachment(appId, formId, pkId, attachmentKey, fileName, zipFiles(srcFiles, fileName));

    }

    /**
     * 下载文件并打包到指定位置
     *
     * @param srcFiles    key是打包的文件名，value是文件下载地址
     * @param zipFileName 压缩包文件名
     * @return
     * @throws IOException
     */
    public String zipFiles(Map<String, String> srcFiles, String zipFileName) throws IOException {
        return zipFiles(srcFiles, new File(realePath + zipFileName));
    }

}
