package com.trade.service.filestorage.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.trade.common.core.domain.BaseEntity;
import com.trade.service.filestorage.enums.FileModuleEnum;
import com.trade.service.filestorage.util.FileUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 文件存储持久化
 */
@TableName("t_file_storage")
public class FileStorage extends BaseEntity<FileStorage> {

    private static final long serialVersionUID = -8252098981356951712L;

    @TableField("biz_type")
    private String bizType;// 业务类型
    private String name;// 文件名
    @TableField("file_type")
    private String fileType;// 文件类型
    @TableField("original_name")
    private String originalName;// 原始文件名
    private String path;// 文件路径
    @TableField("absolute_path")
    private String absolutePath;// 文件绝对路径
    private long size;// 文件大小
    // 以下辅助字段
    /**
     * 附件
     */
    @TableField(exist = false)
    private MultipartFile fileContent;
    // 其他属性...


    public FileStorage(){

    }

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

    public FileStorage(MultipartFile multipartFile, FileModuleEnum 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 String getBizType() {
        return bizType;
    }

    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    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 String getFileType() {
        return fileType;
    }

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

    public long getSize() {
        return size;
    }

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

    public MultipartFile getFileContent() {
        return fileContent;
    }

    public void setFileContent(MultipartFile fileContent) {
        this.fileContent = fileContent;
    }
}
