package com.microservices.system.api.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.microservices.common.core.utils.file.FileUtils;
import com.microservices.common.core.web.domain.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.File;

/**
 * 文件管理对象 sys_file_info
 *
 * @author microservices
 * @date 2021-12-29
 */
@Data
public class SysFileInfo extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 文件主键id
     */
    @ApiModelProperty(value = "文件主键id")
    private Long fileId;
    /**
     * 文件标识
     */
    @ApiModelProperty(value = "文件标识")
    private String fileIdentifier;

    /**
     * 文件名称
     */
    @ApiModelProperty(value = "文件名称")
    private String fileOriginName;

    /**
     * 文件类型，例如txt
     */
    @ApiModelProperty(value = "文件类型，例如txt")
    private String fileSuffix;

    /**
     * 文件大小
     */
    @ApiModelProperty(value = "文件大小")
    private String fileSizeInfo;

    /**
     * 存储文件名称
     */
    @ApiModelProperty(value = "存储文件名称")
    private String fileObjectName;

    /**
     * 存储路径
     */
    @ApiModelProperty(value = "存储路径")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String filePath;

    /**
     * 下载地址
     */
    @ApiModelProperty(value = "下载地址")
    private String downloadUrl;

    /**
     * 是否删除：Y-被删除，N-未删除
     */
    @ApiModelProperty(value = "是否删除：Y-被删除，N-未删除")
    private String delFlag;

    /**
     * 下载次数
     */
    private Integer downloadCount;
    /**
     * 文件内容类型
     */
    @ApiModelProperty(value = "文件内容类型")
    private String contentType;

    /**
     * 文件内容类型名称
     */
    @ApiModelProperty(value = "文件内容类型名称")
    private String contentTypeName;

    /**
     * 文件内容类型描述
     */
    @ApiModelProperty(value = "文件内容类型描述")
    private String contentTypeMessage;

    /**
     * MIME类型
     */
    @ApiModelProperty(value = "MIME类型")
    private String mimeType;

    /**
     * 是否部分匹配
     */
    @ApiModelProperty(value = "是否部分匹配")
    private Integer isPartialMatch;

    public SysFileInfo() {
    }

    public SysFileInfo(String fileIdentifier, File file, String filePath) {
        System.out.println(file.getPath());
        this.setFileIdentifier(fileIdentifier);
        this.setFilePath(filePath);
        this.setFileSizeInfo(FileUtils.calcFileSize(file.length()));
        this.setFileOriginName(file.getName());
        this.setDelFlag("N");
        this.setFileOriginName(file.getName());
        int separatorIndex = file.getName().lastIndexOf(".");
        this.setFileSuffix(file.getName().substring(separatorIndex + 1).toLowerCase());
    }

    public SimpleGitlinkFileInfo toSimpleGitlinkFileInfo(){
        SimpleGitlinkFileInfo simpleGitlinkFileInfo=new SimpleGitlinkFileInfo();
        simpleGitlinkFileInfo.setId(this.getFileId());
        simpleGitlinkFileInfo.setContent_type(this.getFileSuffix());
        simpleGitlinkFileInfo.setFilesize(this.getFileSizeInfo());
        simpleGitlinkFileInfo.setCreated_on(this.getCreateTime());
        simpleGitlinkFileInfo.setUrl(this.getDownloadUrl());
        simpleGitlinkFileInfo.setTitle(this.getFileOriginName());
        simpleGitlinkFileInfo.setIdentifier(this.getFileIdentifier());
        return simpleGitlinkFileInfo;
    }
}
