using CloudBuilder.Core.Authorization;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CloudBuilder.AI.Entity
 {
    [TypeScript]
    [Table("dms_file")]
    public class DmsFileEntity : EntityBase<int>
    {

    public DmsFileEntity() 
    {
    }

    [Column("created_datetime")]
    public DateTime CreatedDatetime  { get; set; }
    [Column("created_by")]
    public string CreatedBy  { get; set; }
    [Column("updated_datetime")]
    public DateTime UpdatedDatetime  { get; set; }
    [Column("updated_by")]
    public string UpdatedBy  { get; set; }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("file_id")]
    public int FileId  { get; set; }
    [Column("file_name")]
    public string FileName  { get; set; }
    [Column("file_type")]
    public string? FileType  { get; set; }
    [Column("landscape_indc")]
    public string? LandscapeIndc  { get; set; }
    [Column("transcode_indc")]
    public string? TranscodeIndc  { get; set; }
    [Column("linked_file_id")]
    public int LinkedFileId  { get; set; }
    [Column("org_file_name")]
    public string OrgFileName  { get; set; }
    [Column("order_key")]
    public string? OrderKey  { get; set; }
    [Column("file_tag")]
    public string? FileTag  { get; set; }
    [Column("allow_rename_indc")]
    public string AllowRenameIndc  { get; set; }
    [Column("allow_delete_indc")]
    public string AllowDeleteIndc  { get; set; }
    [Column("file_path")]
    public string FilePath  { get; set; }
    [Column("file_size")]
    public int FileSize  { get; set; }
    [Column("ower_id")]
    public string OwerId  { get; set; }
    [Column("revision_no")]
    public int RevisionNo  { get; set; }
    [Column("is_directory_indc")]
    public string IsDirectoryIndc  { get; set; }
    [Column("is_link_indc")]
    public string IsLinkIndc  { get; set; }

    public const string CREATED_DATETIME = "CreatedDatetime";
    public const string CREATED_BY = "CreatedBy";
    public const string UPDATED_DATETIME = "UpdatedDatetime";
    public const string UPDATED_BY = "UpdatedBy";
    public const string FILE_ID = "FileId";
    public const string FILE_NAME = "FileName";
    public const string FILE_TYPE = "FileType";
    public const string LANDSCAPE_INDC = "LandscapeIndc";
    public const string TRANSCODE_INDC = "TranscodeIndc";
    public const string LINKED_FILE_ID = "LinkedFileId";
    public const string ORG_FILE_NAME = "OrgFileName";
    public const string ORDER_KEY = "OrderKey";
    public const string FILE_TAG = "FileTag";
    public const string ALLOW_RENAME_INDC = "AllowRenameIndc";
    public const string ALLOW_DELETE_INDC = "AllowDeleteIndc";
    public const string FILE_PATH = "FilePath";
    public const string FILE_SIZE = "FileSize";
    public const string OWER_ID = "OwerId";
    public const string REVISION_NO = "RevisionNo";
    public const string IS_DIRECTORY_INDC = "IsDirectoryIndc";
    public const string IS_LINK_INDC = "IsLinkIndc";

    public const string DB_NAME_DMS_FILE = "dms_file";
    public const string DB_NAME_FIELDS="created_datetime,created_by,updated_datetime,updated_by,file_id,file_name,file_type,landscape_indc,transcode_indc,linked_file_id,org_file_name,order_key,file_tag,allow_rename_indc,allow_delete_indc,file_path,file_size,ower_id,revision_no,is_directory_indc,is_link_indc";

    public const string DB_CREATED_DATETIME = "created_datetime";
    public const string DB_CREATED_BY = "created_by";
    public const string DB_UPDATED_DATETIME = "updated_datetime";
    public const string DB_UPDATED_BY = "updated_by";
    public const string DB_FILE_ID = "file_id";
    public const string DB_FILE_NAME = "file_name";
    public const string DB_FILE_TYPE = "file_type";
    public const string DB_LANDSCAPE_INDC = "landscape_indc";
    public const string DB_TRANSCODE_INDC = "transcode_indc";
    public const string DB_LINKED_FILE_ID = "linked_file_id";
    public const string DB_ORG_FILE_NAME = "org_file_name";
    public const string DB_ORDER_KEY = "order_key";
    public const string DB_FILE_TAG = "file_tag";
    public const string DB_ALLOW_RENAME_INDC = "allow_rename_indc";
    public const string DB_ALLOW_DELETE_INDC = "allow_delete_indc";
    public const string DB_FILE_PATH = "file_path";
    public const string DB_FILE_SIZE = "file_size";
    public const string DB_OWER_ID = "ower_id";
    public const string DB_REVISION_NO = "revision_no";
    public const string DB_IS_DIRECTORY_INDC = "is_directory_indc";
    public const string DB_IS_LINK_INDC = "is_link_indc";


    public static DmsFileEntity GetInstance()
    {
        return new DmsFileEntity();
    }

    public virtual void CopyFrom(DmsFileEntity source)
    {
        CopyFrom(source, true);
    }

    public virtual void CopyFrom(DmsFileEntity source, bool includeSystemFields)
    {
        if (source == null)
        {
            throw new NullReferenceException("Source entity is null.");
        }

        this.CreatedDatetime =  source.CreatedDatetime;
        this.CreatedBy =  source.CreatedBy;
        this.UpdatedDatetime =  source.UpdatedDatetime;
        this.UpdatedBy =  source.UpdatedBy;
        this.FileId =  source.FileId;
        this.FileName =  source.FileName;
        this.FileType =  source.FileType;
        this.LandscapeIndc =  source.LandscapeIndc;
        this.TranscodeIndc =  source.TranscodeIndc;
        this.LinkedFileId =  source.LinkedFileId;
        this.OrgFileName =  source.OrgFileName;
        this.OrderKey =  source.OrderKey;
        this.FileTag =  source.FileTag;
        this.AllowRenameIndc =  source.AllowRenameIndc;
        this.AllowDeleteIndc =  source.AllowDeleteIndc;
        this.FilePath =  source.FilePath;
        this.FileSize =  source.FileSize;
        this.OwerId =  source.OwerId;
        this.RevisionNo =  source.RevisionNo;
        this.IsDirectoryIndc =  source.IsDirectoryIndc;
        this.IsLinkIndc =  source.IsLinkIndc;

        if (includeSystemFields)
        {
        this.CreatedDatetime =  source.CreatedDatetime;
        this.CreatedBy =  source.CreatedBy;
        this.UpdatedDatetime =  source.UpdatedDatetime;
        this.UpdatedBy =  source.UpdatedBy;
        }
    }

    public override string GetSelectFrom()
    {
        return $" select {DB_NAME_FIELDS} from { DB_NAME_DMS_FILE}  with(nolock) ";
    }
	
    public override string GetSelectCountFrom(string sqlWhere)
    {
        return $" select count(*) from { DB_NAME_DMS_FILE}  with(nolock) where 1=1 {sqlWhere}";
    }
	
    public override string GetSelectTopFrom(int limit, int start,string orderby,string orderbytag,  string sqlWhere)
    {
            if (string.IsNullOrEmpty(orderbytag)  || orderbytag.Trim() != "desc") orderbytag = "asc";
            if (string.IsNullOrEmpty(orderby) || !DB_NAME_FIELDS.Contains(orderby)) orderby =  DB_FILE_ID ;  		
		string top = limit > 0 ? $"top {limit}" : "";
        return $" select {top} {DB_NAME_FIELDS} from (select {DB_NAME_FIELDS}  ,row_number() over(order by {orderby} {orderbytag}) as num from { DB_NAME_DMS_FILE}  with(nolock) where 1=1 {sqlWhere} ) a where num> {limit * (start - 1)}";
    }

    public DmsFileEntity Clone()
    {
        DmsFileEntity result = new DmsFileEntity();
        result.CopyFrom(this);

        return result;
    }

    }
}
