package com.steerinfo.util;


import com.google.common.io.Files;
import com.steerinfo.framework.utils.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

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

@Component
public class UploadHelper {

    @Value("${web.upload-path}")
    String root;

    /**
     * 保存文件
     *
     * @param file
     * @param category        文件类别
     * @param parentPath      父文件夹名称 可不传
     * @param isGeneratorName 是否生成文件名
     * @return 文件相对路径 FP/xxxx.jpg
     * @throws Exception
     */
    public String save(MultipartFile file, Category category, String parentPath, boolean isGeneratorName) throws Exception {
        String rootPath = root + File.separator + Const.AppName + File.separator;
        String profile = "";
        if (file != null) {
            String fileFullName = file.getOriginalFilename();
            String fileName = Files.getNameWithoutExtension(fileFullName);
            if (isGeneratorName) {
                fileName = String.valueOf(System.currentTimeMillis());
            }
            String ext = Files.getFileExtension(fileFullName.toLowerCase());
            try {
                String filePath = rootPath + category.getValue() + File.separator + fileName + "." + ext;
                profile = category.getValue() + "/" + fileName + "." + ext;
                if (!"".equals(parentPath)) {
                    filePath = rootPath + category.getValue() + File.separator + parentPath + File.separator + fileName + "." + ext;
                    profile = category.getValue() + "/" + parentPath + "/" + fileName + "." + ext;
                }
                File newFile = new File(filePath);
                FileUtils.makesureParentDirExists(newFile);
                Files.write(file.getBytes(), newFile);
            } catch (IOException e) {
                throw new Exception("文件为空:" + e.getMessage());
            }
        }

        return profile;
    }

    /**
     * 删除文件
     *
     * @param path 文件夹或者文件名
     * @throws IOException
     */
    public void delete(String path) throws IOException {
        String filePath = root + File.separator + Const.AppName + File.separator + path;
        File file = new File(filePath);
        if (FileUtils.isFileExists(file)) {
            FileUtils.deleteFile(file);
        } else if (FileUtils.isDirExists(file)) {
            FileUtils.deleteDir(file);
        }
    }

    /**
     * 获取文件夹下 文件名列表
     *
     * @param path
     * @return
     */
    public List<MyFile> getFiles(String path) {
        String folderPath = root + File.separator + Const.AppName + File.separator + path;
        File folder = new File(folderPath);
        List<MyFile> files = new ArrayList<>();
        if (FileUtils.isDirExists(folder)) {
            File[] list = folder.listFiles();
            for (File f : list) {
                if (f.isFile()) {
                    files.add(new MyFile(f.getName(), FileUtils.getFileExtension(f), "file", path + f.getName()));
                } else if (f.isDirectory()) {
                    files.add(new MyFile(f.getName(), "", "folder", path + f.getName()));
                }
            }
        }
        return files;
    }

    /**
     * 文件类别
     */
    public enum Category {
        GF("GF"),//供方
        DH("DH"),//到货
        SH("SH"),//售后
        FP("FP"),//发票
        FK("FK"),//付款
        LT("LT");
        private final String value;

        private Category(String value) {
            this.value = value;
        }

        public String getValue() {
            return this.value;
        }
    }

    public class MyFile {
        private String name;
        private String type;
        private String ext;
        private String path;

        public MyFile(String name, String ext, String type, String path) {
            this.name = name;
            this.type = type;
            this.ext = ext;
            this.path = path;
        }

        public String getPath() {
            return path;
        }

        public String getName() {
            return this.name;
        }

        public String getType() {
            return this.type;
        }

        public String getExt() {
            return this.ext;
        }
    }

}
