package cn.reghao.tnb.oss.store.model.po;

import cn.reghao.tnb.oss.store.util.AuthContext;
import cn.reghao.jutil.jdk.web.db.BaseObject;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

/**
 * 文件元数据
 *
 * @author reghao
 * @date 2022-11-21 10:53:10
 */
@NoArgsConstructor
@Setter
@Getter
public class FileMeta extends BaseObject<Integer> {
    @NotBlank
    @Size(max = 255)
    private String objectName;
    @NotBlank
    @Size(max = 255)
    private String objectId;
    @NotBlank
    @Size(max = 255)
    private String pid;
    @NotBlank
    @Size(max = 255)
    private String contentId;
    @NotBlank
    @Size(max = 255)
    private String sha256sum;
    @NotBlank
    @Size(max = 255)
    private String filename;
    @NotNull
    private Long size;
    @NotNull
    private Integer fileType;
    @NotBlank
    @Size(max = 255)
    private String contentType;
    @NotNull
    private Integer scope;
    @NotNull
    private Integer uploadBy;

    // 目录对象
    public FileMeta(int owner, String objectName, String objectId, String filename, String pid, int scope) {
        this.objectName = objectName;
        this.objectId = objectId;
        this.contentId = "0";
        this.filename = filename;
        this.size = 0L;
        this.fileType = 1000;
        this.contentType = "0";
        this.sha256sum = "0";
        this.pid = pid;
        this.uploadBy = owner;
        this.scope = scope;
    }

    public FileMeta(String objectName, String objectId, String contentId, String filename, long size,
                    int fileType, String contentType, String sha256sum, String pid, int scope, boolean deleted) {
        this.objectName = objectName;
        this.objectId = objectId;
        this.contentId = contentId;
        this.filename = filename;
        this.size = size;
        this.fileType = fileType;
        this.contentType = contentType;
        this.sha256sum = sha256sum;
        this.pid = pid;
        this.uploadBy = AuthContext.getUserId();
        this.scope = scope;
        this.deleted = deleted;
    }

    /**
     * 复制已存在的 object 时调用
     *
     * @param
     * @return
     * @date 2025-10-25 13:10:924
     */
    public FileMeta(String objectName, String objectId, String filename, FileMeta fileMeta, String pid, int scope, boolean deleted) {
        this.objectName = objectName;
        this.objectId = objectId;
        this.contentId = fileMeta.getContentId();
        this.filename = filename;
        this.size = fileMeta.getSize();
        this.fileType = fileMeta.getFileType();
        this.contentType = fileMeta.getContentType();
        this.sha256sum = fileMeta.getSha256sum();
        this.pid = pid;
        this.uploadBy = AuthContext.getUserId();
        this.scope = scope;
        this.deleted = deleted;
    }
}
