using CloudBuilder.Core.Authorization;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CloudBuilder.Security.Entity
 {
    [TypeScript]
    [Table("security_master")]
    public class SecurityMasterEntity : EntityBase<SecurityMasterEntityKey>
    {

    public SecurityMasterEntity() 
    {
    }

    [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; }
    [Column("master_category")]
    public string MasterCategory  { get; set; }
    [Column("master_subcategory")]
    public string MasterSubcategory  { get; set; }
    [Column("master_code_item")]
    public string MasterCodeItem  { get; set; }
    [Column("local_description")]
    public string? LocalDescription  { get; set; }
    [Column("eng_description")]
    public string? EngDescription  { get; set; }
    [Column("value")]
    public string Value  { get; set; }
    [Column("order_by")]
    public int OrderBy  { get; set; }
    [Column("valid_indc")]
    public string ValidIndc  { get; set; }
    [Column("remark")]
    public string? Remark  { 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 MASTER_CATEGORY = "MasterCategory";
    public const string MASTER_SUBCATEGORY = "MasterSubcategory";
    public const string MASTER_CODE_ITEM = "MasterCodeItem";
    public const string LOCAL_DESCRIPTION = "LocalDescription";
    public const string ENG_DESCRIPTION = "EngDescription";
    public const string VALUE = "Value";
    public const string ORDER_BY = "OrderBy";
    public const string VALID_INDC = "ValidIndc";
    public const string REMARK = "Remark";

    public const string DB_NAME_SECURITY_MASTER = "security_master";
    public const string DB_NAME_FIELDS="created_datetime,created_by,updated_datetime,updated_by,master_category,master_subcategory,master_code_item,local_description,eng_description,value,order_by,valid_indc,remark";

    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_MASTER_CATEGORY = "master_category";
    public const string DB_MASTER_SUBCATEGORY = "master_subcategory";
    public const string DB_MASTER_CODE_ITEM = "master_code_item";
    public const string DB_LOCAL_DESCRIPTION = "local_description";
    public const string DB_ENG_DESCRIPTION = "eng_description";
    public const string DB_VALUE = "value";
    public const string DB_ORDER_BY = "order_by";
    public const string DB_VALID_INDC = "valid_indc";
    public const string DB_REMARK = "remark";


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

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

    public virtual void CopyFrom(SecurityMasterEntity 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.MasterCategory =  source.MasterCategory;
        this.MasterSubcategory =  source.MasterSubcategory;
        this.MasterCodeItem =  source.MasterCodeItem;
        this.LocalDescription =  source.LocalDescription;
        this.EngDescription =  source.EngDescription;
        this.Value =  source.Value;
        this.OrderBy =  source.OrderBy;
        this.ValidIndc =  source.ValidIndc;
        this.Remark =  source.Remark;

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

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

        return result;
    }

    }
}
