using CloudBuilder.Core.Authorization;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CloudBuilder.AI.Entity
 {
    [TypeScript]
    [Table("book_dms_file_view")]
    public class BookDmsFileViewEntity : ViewEntityBase
    {

    public BookDmsFileViewEntity() 
    {
    }

    [Column("created_datetime")]
    public DateTime CreatedDatetime  { get; set; }
    [Column("created_by")]
    public string CreatedBy  { get; set; }
    [Column("file_id")]
    public int FileId  { get; set; }
    [Column("upload_name")]
    public string UploadName  { get; set; }
    [Column("file_category")]
    public string FileCategory  { get; set; }
    [Column("org_file_name")]
    public string OrgFileName  { get; set; }
    [Column("allow_delete_indc")]
    public string AllowDeleteIndc  { 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("url")]
    public string Url  { get; set; }
    [Column("is_directory_indc")]
    public string IsDirectoryIndc  { get; set; }
    [Column("is_link_indc")]
    public string IsLinkIndc  { get; set; }
    [Column("linked_file_id")]
    public int LinkedFileId  { 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("file_name")]
    public string FileName  { get; set; }
    [Column("file_path")]
    public string FilePath  { get; set; }
    [Column("folder_local_name")]
    public string? FolderLocalName  { 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; }

    public const string CREATED_DATETIME = "CreatedDatetime";
    public const string CREATED_BY = "CreatedBy";
    public const string FILE_ID = "FileId";
    public const string UPLOAD_NAME = "UploadName";
    public const string FILE_CATEGORY = "FileCategory";
    public const string ORG_FILE_NAME = "OrgFileName";
    public const string ALLOW_DELETE_INDC = "AllowDeleteIndc";
    public const string FILE_TYPE = "FileType";
    public const string LANDSCAPE_INDC = "LandscapeIndc";
    public const string TRANSCODE_INDC = "TranscodeIndc";
    public const string URL = "Url";
    public const string IS_DIRECTORY_INDC = "IsDirectoryIndc";
    public const string IS_LINK_INDC = "IsLinkIndc";
    public const string LINKED_FILE_ID = "LinkedFileId";
    public const string ORDER_KEY = "OrderKey";
    public const string FILE_TAG = "FileTag";
    public const string ALLOW_RENAME_INDC = "AllowRenameIndc";
    public const string FILE_NAME = "FileName";
    public const string FILE_PATH = "FilePath";
    public const string FOLDER_LOCAL_NAME = "FolderLocalName";
    public const string FILE_SIZE = "FileSize";
    public const string OWER_ID = "OwerId";
    public const string REVISION_NO = "RevisionNo";

    public const string DB_NAME_BOOK_DMS_FILE_VIEW = "book_dms_file_view";
    public const string DB_NAME_FIELDS="created_datetime,created_by,file_id,upload_name,file_category,org_file_name,allow_delete_indc,file_type,landscape_indc,transcode_indc,url,is_directory_indc,is_link_indc,linked_file_id,order_key,file_tag,allow_rename_indc,file_name,file_path,folder_local_name,file_size,ower_id,revision_no";

    public const string DB_CREATED_DATETIME = "created_datetime";
    public const string DB_CREATED_BY = "created_by";
    public const string DB_FILE_ID = "file_id";
    public const string DB_UPLOAD_NAME = "upload_name";
    public const string DB_FILE_CATEGORY = "file_category";
    public const string DB_ORG_FILE_NAME = "org_file_name";
    public const string DB_ALLOW_DELETE_INDC = "allow_delete_indc";
    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_URL = "url";
    public const string DB_IS_DIRECTORY_INDC = "is_directory_indc";
    public const string DB_IS_LINK_INDC = "is_link_indc";
    public const string DB_LINKED_FILE_ID = "linked_file_id";
    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_FILE_NAME = "file_name";
    public const string DB_FILE_PATH = "file_path";
    public const string DB_FOLDER_LOCAL_NAME = "folder_local_name";
    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 static BookDmsFileViewEntity GetInstance()
    {
        return new BookDmsFileViewEntity();
    }

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

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

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

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

    public override string GetSelectFrom()
    {
        return $" select {DB_NAME_FIELDS} from { DB_NAME_BOOK_DMS_FILE_VIEW}  with(nolock) ";
    }
	
    public override string GetSelectCountFrom(string sqlWhere)
    {
        return $" select count(*) from { DB_NAME_BOOK_DMS_FILE_VIEW}  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_CREATED_DATETIME ;  		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_BOOK_DMS_FILE_VIEW}  with(nolock) where 1=1 {sqlWhere} ) a where num> {limit * (start - 1)}";
    }

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

        return result;
    }

    }
}
