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")]
    public class SecurityGroupPermissionEntity : EntityBase<SecurityGroupPermissionEntityKey>
    {

    public SecurityGroupPermissionEntity() 
    {
    }

    [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("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")]
    public string? Dependent  { 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 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 = "Dependent";

    public const string DB_NAME_SECURITY_GROUP_PERMISSION = "security_group_permission";
    public const string DB_NAME_FIELDS="created_datetime,created_by,updated_datetime,updated_by,group_name,permission_name,view_indc,create_indc,amend_indc,delete_indc,dependent";

    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_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 = "dependent";


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

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

    public virtual void CopyFrom(SecurityGroupPermissionEntity 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.GroupName =  source.GroupName;
        this.PermissionName =  source.PermissionName;
        this.ViewIndc =  source.ViewIndc;
        this.CreateIndc =  source.CreateIndc;
        this.AmendIndc =  source.AmendIndc;
        this.DeleteIndc =  source.DeleteIndc;
        this.Dependent =  source.Dependent;

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

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

        return result;
    }

    }
}
