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_function_map")]
    public class SecurityFunctionMapEntity : EntityBase<SecurityFunctionMapEntityKey>
    {

    public SecurityFunctionMapEntity() 
    {
    }

    [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("function_name_parent")]
    public string FunctionNameParent  { get; set; }
    [Column("function_name_son")]
    public string FunctionNameSon  { get; set; }
    [Column("orderby")]
    public int Orderby  { 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 FUNCTION_NAME_PARENT = "FunctionNameParent";
    public const string FUNCTION_NAME_SON = "FunctionNameSon";
    public const string ORDERBY = "Orderby";

    public const string DB_NAME_SECURITY_FUNCTION_MAP = "security_function_map";
    public const string DB_NAME_FIELDS="created_datetime,created_by,updated_datetime,updated_by,function_name_parent,function_name_son,orderby";

    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_FUNCTION_NAME_PARENT = "function_name_parent";
    public const string DB_FUNCTION_NAME_SON = "function_name_son";
    public const string DB_ORDERBY = "orderby";


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

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

    public virtual void CopyFrom(SecurityFunctionMapEntity 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.FunctionNameParent =  source.FunctionNameParent;
        this.FunctionNameSon =  source.FunctionNameSon;
        this.Orderby =  source.Orderby;

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

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

        return result;
    }

    }
}
