package com.sunxung.factoring.entity.sys;

import com.alibaba.fastjson.annotation.JSONField;
import com.sunxung.factoring.component.util.FileUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.BaseEntity;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 文件存储
 */
public class FileStorage extends BaseEntity {

    private static final long serialVersionUID = 1L;
    /**
     * 文件附属对象
     */
    private Belong belong;
    /**
     * 文件类型
     *
     * @see ModuleTypeEnum
     */
    private String fileType;
    @NotBlank(message = "名称不能为空")
    private String name;
    @NotBlank(message = "原名称不能为空")
    private String originalName;
    @NotBlank(message = "路径不能为空")
    private String path;
    @NotBlank(message = "绝对路径不能为空")
    @JSONField(serialize = false)
    private String absolutePath;
    private long size;

    public FileStorage() {
    }

    public FileStorage(MultipartFile multipartFile, String moudleName) {
        String suffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        String fileName = FileUtil.randomFileName();
        String path = FileUtil.getFilePath(fileName, suffix, moudleName);
        String absolutePath = FileUtil.getAbsolutePath(fileName, suffix, moudleName);
        Belong belong = new Belong();
        belong.setBelongType(moudleName);
        setBelong(belong);
        setOriginalName(multipartFile.getOriginalFilename());
        setSize(multipartFile.getSize());
        setName(fileName + suffix);
        setPath(path);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
    }

    public FileStorage(MultipartFile multipartFile, FileModuleDict.ChildEnum moduleType) {
        String fileName = FileUtil.randomFileName();
        String suffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        String path = FileUtil.getFilePath(fileName, suffix, moduleType);
        String absolutePath = FileUtil.getAbsolutePath(fileName, suffix, moduleType);
        setOriginalName(multipartFile.getOriginalFilename());
        setSize(multipartFile.getSize());
        setName(fileName + suffix);
        setPath(path);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
    }

    public FileStorage(String originalName, String absolutePath, FileModuleDict.ChildEnum moduleType) {
        String fileName = FileUtil.randomFileName();
        String suffix = FileUtil.getSuffix(originalName);
        String path = FileUtil.getFilePath(fileName, suffix, moduleType);
        setOriginalName(originalName);
        setPath(path);
        setName(fileName + suffix);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
    }

    public FileStorage(String contractId, FileModuleDict.ChildEnum moduleType, String newName, String suffix) {
        String fileName = FileUtil.randomFileName();
        String path = FileUtil.getFilePath(fileName, suffix, moduleType);
        String absolutePath = FileUtil.getAbsolutePath(fileName, suffix, moduleType);
        setOriginalName(newName + suffix);
        setName(fileName + suffix);
        setPath(path);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
        Belong b = new Belong();
        b.setBelongId(contractId);
        b.setBelongType(moduleType.getCode());
        setBelong(b);
    }

    public FileStorage(String fileName, String originalName, String absolutePath, FileModuleDict.ChildEnum moduleType) {
        String suffix = FileUtil.getSuffix(originalName);
        String path = FileUtil.getFilePath(fileName, suffix, moduleType);
        setOriginalName(originalName);
        setPath(path);
        setName(fileName + suffix);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
    }

    public FileStorage(FileStorage fileStorage, String localFilePath, FileModuleDict.ChildEnum moduleType) {
        String path = FileUtil.getFilePath(fileStorage.getName(), "", moduleType);
        setOriginalName(fileStorage.getOriginalName());
        setSize(fileStorage.getSize());
        setName(fileStorage.getName());
        setPath(path);
        setAbsolutePath(localFilePath);
        setFileType(fileStorage.getFileType());
    }

    public FileStorage(File file, FileModuleDict.ChildEnum moudleName) {
        String suffix = FileUtil.getSuffix(file.getName());
        String fileName = FileUtil.randomFileName();
        String path = FileUtil.getFilePath(fileName, suffix, moudleName);
        String absolutePath = file.getAbsolutePath();
        setOriginalName(file.getName());
        setSize(file.length());
        setName(fileName + suffix);
        setPath(path);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
    }

    public FileStorage(File file,String filePath ) {
        String suffix = FileUtil.getSuffix(file.getName());
        String fileName = file.getName().replace(suffix,"");
        String path = filePath;
        String absolutePath = file.getAbsolutePath();
        setOriginalName(file.getName());
        setSize(file.length());
        setName(fileName + suffix);
        setPath(path);
        setAbsolutePath(absolutePath);
        setFileType(suffix);
    }

    public Belong getBelong() {
        return belong;
    }

    public void setBelong(Belong belong) {
        this.belong = belong;
    }

    public String getFileType() {
        return fileType != null ? fileType.toLowerCase() : null;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOriginalName() {
        return originalName;
    }

    public void setOriginalName(String originalName) {
        this.originalName = originalName;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getAbsolutePath() {
        return absolutePath;
    }

    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    /**
     * 文件附属对象
     */
    public static class Belong {

        /**
         * 类型
         *
         * @see ModuleTypeEnum
         */
        @NotBlank(message = "附属对象类型不能为空", groups = {
                Update.class
        })
        private String belongType;
        @NotBlank(message = "附属对象不能为空", groups = {
                Update.class
        })
        /** 附属对象id */
        private String belongId;

        public Belong() {
        }

        /**
         * 构造附属对象
         *
         * @param belongId   附属对象Id
         * @param moduleType 附属对象类型
         */
        public Belong(String belongId, FileModuleDict.ChildEnum moduleType) {
            this.belongId = belongId;
            this.belongType = moduleType.getCode();
        }

        public String getBelongType() {
            return belongType;
        }

        public void setBelongType(String belongType) {
            this.belongType = belongType;
        }

        public String getBelongId() {
            return belongId;
        }

        public void setBelongId(String belongId) {
            this.belongId = belongId;
        }
    }
    // /** 文件所属模块枚举 */
    // public static enum ModuleTypeEnum {
    // /** 其他文件 */
    // OTHER("其他文件", "other"),
    // /** 垃圾分类评价 */
    // GARBAGE_EVALUATION("垃圾分类评价", "garbageEvaluation"),
    // /** 任务下发 */
    // TASK_ISSUED("任务下发", "taskIssued"),
    // /** 意见反馈 */
    // FEEDBACK("意见反馈", "feedback"),
    // /** 报废垃圾桶 */
    // SCRAPPED_TRASH("报废垃圾桶", "scrappedTrash");
    //
    // /** 类型 */
    // private String type;
    // /** 目录 */
    // private String dir;
    //
    // ModuleTypeEnum(String type, String dir){
    // this.type = type;
    // this.dir = dir;
    // }
    //
    // /** 类型 */
    // public String getType() {
    // return type;
    // }
    //
    // /** 目录 */
    // public String getDir() {
    // return dir;
    // }
    // }
}
