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_view")]
    public class SecurityFunctionMapViewEntity : ViewEntityBase
    {

    public SecurityFunctionMapViewEntity() 
    {
    }

    [Column("orderby")]
    public int Orderby  { get; set; }
    [Column("parent_function_name")]
    public string ParentFunctionName  { get; set; }
    [Column("parent_function_local_name")]
    public string? ParentFunctionLocalName  { get; set; }
    [Column("son_function_name")]
    public string SonFunctionName  { get; set; }
    [Column("son_function_local_name")]
    public string? SonFunctionLocalName  { get; set; }
    [Column("son_page_name")]
    public string? SonPageName  { get; set; }

    public const string ORDERBY = "Orderby";
    public const string PARENT_FUNCTION_NAME = "ParentFunctionName";
    public const string PARENT_FUNCTION_LOCAL_NAME = "ParentFunctionLocalName";
    public const string SON_FUNCTION_NAME = "SonFunctionName";
    public const string SON_FUNCTION_LOCAL_NAME = "SonFunctionLocalName";
    public const string SON_PAGE_NAME = "SonPageName";

    public const string DB_NAME_SECURITY_FUNCTION_MAP_VIEW = "security_function_map_view";
    public const string DB_NAME_FIELDS="orderby,parent_function_name,parent_function_local_name,son_function_name,son_function_local_name,son_page_name";

    public const string DB_ORDERBY = "orderby";
    public const string DB_PARENT_FUNCTION_NAME = "parent_function_name";
    public const string DB_PARENT_FUNCTION_LOCAL_NAME = "parent_function_local_name";
    public const string DB_SON_FUNCTION_NAME = "son_function_name";
    public const string DB_SON_FUNCTION_LOCAL_NAME = "son_function_local_name";
    public const string DB_SON_PAGE_NAME = "son_page_name";


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

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

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

        this.Orderby =  source.Orderby;
        this.ParentFunctionName =  source.ParentFunctionName;
        this.ParentFunctionLocalName =  source.ParentFunctionLocalName;
        this.SonFunctionName =  source.SonFunctionName;
        this.SonFunctionLocalName =  source.SonFunctionLocalName;
        this.SonPageName =  source.SonPageName;

        if (includeSystemFields)
        {
        }
    }

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

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

        return result;
    }

    }
}
