package cn.lihongjie.coal.netDisk.entity;

import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import cn.lihongjie.coal.common.FormatUtils;

import jakarta.persistence.*;

import lombok.Data;

import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;

import java.util.List;

@Data
@Entity()
public class NetDiskEntity extends OrgCommonEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    private NetDiskEntity parent;

    @Comment("条目类型, 文件夹/目录/分片文件")
    private String entryType;

    @Formula(
            "(select i.name\n"
                    + "from t_dictionary d,\n"
                    + "     t_dictionary_item i\n"
                    + "where d.id = i.dictionary_id\n"
                    + "  and d.code = 'netDisk.entryType'\n"
                    + "  and i.code = entry_type)")
    private String entryTypeName;

    @OneToMany(
            mappedBy = "parent",
            cascade = {CascadeType.REMOVE})
    private List<NetDiskEntity> children;

    @Comment("文件/文件夹 大小, 单位 byte")
    @Access(AccessType.PROPERTY)

    private Long size;

    //    @Formula("(pg_size_pretty(size))")
    @Transient private String sizeHumanReadable;

    public void setSize(Long size) {
        this.size = size;
        try {

            this.sizeHumanReadable = FormatUtils.humanReadableByteCountBin(size);
        } catch (Exception e) {

        }
    }

    @Comment("文件 sha256 值, 文件夹没有")
    private String sha256;

    @Comment("文件 mime 类型, 文件夹没有")
    private String mimeType;

    private String ossKey;

    @Comment("分片序号")
    private Long sliceIndex;

    @Comment("分片 sha256 值")
    private String sliceSha256;

    @Comment("分片文件大小")
    private Long sliceSize;

    @Comment("分片上传任务ID")
    private String sliceTaskId;

    @Override
    public void prePersist() {
        super.prePersist();
        if (this.size == null) {
            this.size = 0L;
        }
    }
}
