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

    public SecurityGroupPermissionDependentViewEntity() 
    {
    }

    [Column("group_name")]
    public string GroupName  { get; set; }
    [Column("permission_name")]
    public string PermissionName  { get; set; }
    [Column("view_indc")]
    public string ViewIndc  { get; set; }
    [Column("create_indc")]
    public string CreateIndc  { get; set; }
    [Column("amend_indc")]
    public string AmendIndc  { get; set; }
    [Column("delete_indc")]
    public string DeleteIndc  { get; set; }
    [Column("dependent_detail_name")]
    public string DependentDetailName  { get; set; }
    [Column("permission_description")]
    public string? PermissionDescription  { get; set; }
    [Column("dependent_detail_description")]
    public string? DependentDetailDescription  { get; set; }
    [Column("dependent")]
    public string Dependent  { get; set; }

    public const string GROUP_NAME = "GroupName";
    public const string PERMISSION_NAME = "PermissionName";
    public const string VIEW_INDC = "ViewIndc";
    public const string CREATE_INDC = "CreateIndc";
    public const string AMEND_INDC = "AmendIndc";
    public const string DELETE_INDC = "DeleteIndc";
    public const string DEPENDENT_DETAIL_NAME = "DependentDetailName";
    public const string PERMISSION_DESCRIPTION = "PermissionDescription";
    public const string DEPENDENT_DETAIL_DESCRIPTION = "DependentDetailDescription";
    public const string DEPENDENT = "Dependent";

    public const string DB_NAME_SECURITY_GROUP_PERMISSION_DEPENDENT_VIEW = "security_group_permission_dependent_view";
    public const string DB_NAME_FIELDS="group_name,permission_name,view_indc,create_indc,amend_indc,delete_indc,dependent_detail_name,permission_description,dependent_detail_description,dependent";

    public const string DB_GROUP_NAME = "group_name";
    public const string DB_PERMISSION_NAME = "permission_name";
    public const string DB_VIEW_INDC = "view_indc";
    public const string DB_CREATE_INDC = "create_indc";
    public const string DB_AMEND_INDC = "amend_indc";
    public const string DB_DELETE_INDC = "delete_indc";
    public const string DB_DEPENDENT_DETAIL_NAME = "dependent_detail_name";
    public const string DB_PERMISSION_DESCRIPTION = "permission_description";
    public const string DB_DEPENDENT_DETAIL_DESCRIPTION = "dependent_detail_description";
    public const string DB_DEPENDENT = "dependent";


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

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

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

        this.GroupName =  source.GroupName;
        this.PermissionName =  source.PermissionName;
        this.ViewIndc =  source.ViewIndc;
        this.CreateIndc =  source.CreateIndc;
        this.AmendIndc =  source.AmendIndc;
        this.DeleteIndc =  source.DeleteIndc;
        this.DependentDetailName =  source.DependentDetailName;
        this.PermissionDescription =  source.PermissionDescription;
        this.DependentDetailDescription =  source.DependentDetailDescription;
        this.Dependent =  source.Dependent;

        if (includeSystemFields)
        {
        }
    }

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

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

        return result;
    }

    }
}
