package com.trade.service.filestorage.entity;

import java.io.File;

import com.trade.common.core.domain.BizBaseEntity;
import org.springframework.web.multipart.MultipartFile;

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 io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 文件存储信息表
 * </p>
 *
 * @author baomidou
 * @since 2024-04-01
 */
@Getter
@Setter
@TableName("t_file_storage")
@ApiModel(value = "TFileStorage对象", description = "文件存储信息表")
public class TFileStorage extends BizBaseEntity {

    private static final long serialVersionUID = 1L;
    @ApiModelProperty("业务类型")
    @TableField("biz_type")
    private String bizType;
    @ApiModelProperty("业务编号")
    @TableField("biz_no")
    private String bizNo;
    @ApiModelProperty("文件名称")
    @TableField("`name`")
    private String name;
    @ApiModelProperty("文件类型")
    @TableField("file_type")
    private String fileType;
    @ApiModelProperty("文件后缀")
    @TableField("file_suffix")
    private String fileSuffix;
    @ApiModelProperty("原名称")
    @TableField("original_name")
    private String originalName;
    @ApiModelProperty("路径")
    @TableField("`path`")
    private String path;
    @ApiModelProperty("绝对路径")
    @TableField("absolute_path")
    private String absolutePath;
    @ApiModelProperty("oss ETag")
    @TableField("e_tag")
    private String eTag;
    @ApiModelProperty("大小")
    @TableField("size")
    private long size;

    public TFileStorage(){
    }

    /**
     * oss 存储构造方法
     *
     * @param eTag 文件ETag
     * @param fileName 文件名称
     * @param fileLength 文件大小
     * @param objName 云存储对象名称
     */
    public TFileStorage(String eTag, String fileName, String saveName, String objName, long fileLength){
        String suffix = FileUtil.getSuffix(fileName);
        setFileSuffix(suffix);
        setOriginalName(fileName);
        setName(saveName);
        setPath(objName);
        // setAbsolutePath(uri);
        setETag(eTag);
        setSize(fileLength);
    }

    public TFileStorage(String eTag, String fileName, String saveName, String objName, long fileLength, String bizNo,
            String bizType, String fileType){
        String suffix = FileUtil.getSuffix(fileName);
        setFileSuffix(suffix);
        setBizNo(bizNo);
        setBizType(bizType);
        setFileType(fileType);
        setOriginalName(fileName);
        setName(saveName);
        setPath(objName);
        // setAbsolutePath(uri);
        setETag(eTag);
        setSize(fileLength);
    }

    public TFileStorage(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);
        setFileSuffix(suffix);
    }

    public TFileStorage(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);
        setFileSuffix(suffix);
    }

    public TFileStorage(MultipartFile multipartFile, FileModuleEnum fileModule, String originalName){
        String fileName = FileUtil.randomFileName();
        String suffix = FileUtil.getSuffix(originalName);
        String path = FileUtil.getFilePath(fileName, suffix, fileModule);
        String absolutePath = FileUtil.getAbsolutePath(fileName, suffix, fileModule);
        setOriginalName(originalName);
        setSize(multipartFile.getSize());
        setName(fileName + suffix);
        setPath(path);
        setAbsolutePath(absolutePath);
        setFileSuffix(suffix);
    }
}
