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

    public SecurityInitializerTickViewEntity() 
    {
    }

    [Column("security_actor_upd")]
    public Nullable<DateTime> SecurityActorUpd  { get; set; }
    [Column("security_actor_group_upd")]
    public Nullable<DateTime> SecurityActorGroupUpd  { get; set; }
    [Column("security_function_upd")]
    public Nullable<DateTime> SecurityFunctionUpd  { get; set; }
    [Column("security_function_map_upd")]
    public Nullable<DateTime> SecurityFunctionMapUpd  { get; set; }
    [Column("product_upd")]
    public Nullable<DateTime> ProductUpd  { get; set; }
    [Column("security_group_permission_upd")]
    public Nullable<DateTime> SecurityGroupPermissionUpd  { get; set; }
    [Column("security_group_permission_dependent_upd")]
    public Nullable<DateTime> SecurityGroupPermissionDependentUpd  { get; set; }
    [Column("security_master_upd")]
    public Nullable<DateTime> SecurityMasterUpd  { get; set; }
    [Column("security_permission_upd")]
    public Nullable<DateTime> SecurityPermissionUpd  { get; set; }
    [Column("security_permission_dependent_upd")]
    public Nullable<DateTime> SecurityPermissionDependentUpd  { get; set; }
    [Column("security_user_upd")]
    public Nullable<DateTime> SecurityUserUpd  { get; set; }
    [Column("security_user_actor_upd")]
    public Nullable<DateTime> SecurityUserActorUpd  { get; set; }

    public const string SECURITY_ACTOR_UPD = "SecurityActorUpd";
    public const string SECURITY_ACTOR_GROUP_UPD = "SecurityActorGroupUpd";
    public const string SECURITY_FUNCTION_UPD = "SecurityFunctionUpd";
    public const string SECURITY_FUNCTION_MAP_UPD = "SecurityFunctionMapUpd";
    public const string PRODUCT_UPD = "ProductUpd";
    public const string SECURITY_GROUP_PERMISSION_UPD = "SecurityGroupPermissionUpd";
    public const string SECURITY_GROUP_PERMISSION_DEPENDENT_UPD = "SecurityGroupPermissionDependentUpd";
    public const string SECURITY_MASTER_UPD = "SecurityMasterUpd";
    public const string SECURITY_PERMISSION_UPD = "SecurityPermissionUpd";
    public const string SECURITY_PERMISSION_DEPENDENT_UPD = "SecurityPermissionDependentUpd";
    public const string SECURITY_USER_UPD = "SecurityUserUpd";
    public const string SECURITY_USER_ACTOR_UPD = "SecurityUserActorUpd";

    public const string DB_NAME_SECURITY_INITIALIZER_TICK_VIEW = "security_initializer_tick_view";
    public const string DB_NAME_FIELDS="security_actor_upd,security_actor_group_upd,security_function_upd,security_function_map_upd,product_upd,security_group_permission_upd,security_group_permission_dependent_upd,security_master_upd,security_permission_upd,security_permission_dependent_upd,security_user_upd,security_user_actor_upd";

    public const string DB_SECURITY_ACTOR_UPD = "security_actor_upd";
    public const string DB_SECURITY_ACTOR_GROUP_UPD = "security_actor_group_upd";
    public const string DB_SECURITY_FUNCTION_UPD = "security_function_upd";
    public const string DB_SECURITY_FUNCTION_MAP_UPD = "security_function_map_upd";
    public const string DB_PRODUCT_UPD = "product_upd";
    public const string DB_SECURITY_GROUP_PERMISSION_UPD = "security_group_permission_upd";
    public const string DB_SECURITY_GROUP_PERMISSION_DEPENDENT_UPD = "security_group_permission_dependent_upd";
    public const string DB_SECURITY_MASTER_UPD = "security_master_upd";
    public const string DB_SECURITY_PERMISSION_UPD = "security_permission_upd";
    public const string DB_SECURITY_PERMISSION_DEPENDENT_UPD = "security_permission_dependent_upd";
    public const string DB_SECURITY_USER_UPD = "security_user_upd";
    public const string DB_SECURITY_USER_ACTOR_UPD = "security_user_actor_upd";


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

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

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

        this.SecurityActorUpd =  source.SecurityActorUpd;
        this.SecurityActorGroupUpd =  source.SecurityActorGroupUpd;
        this.SecurityFunctionUpd =  source.SecurityFunctionUpd;
        this.SecurityFunctionMapUpd =  source.SecurityFunctionMapUpd;
        this.ProductUpd =  source.ProductUpd;
        this.SecurityGroupPermissionUpd =  source.SecurityGroupPermissionUpd;
        this.SecurityGroupPermissionDependentUpd =  source.SecurityGroupPermissionDependentUpd;
        this.SecurityMasterUpd =  source.SecurityMasterUpd;
        this.SecurityPermissionUpd =  source.SecurityPermissionUpd;
        this.SecurityPermissionDependentUpd =  source.SecurityPermissionDependentUpd;
        this.SecurityUserUpd =  source.SecurityUserUpd;
        this.SecurityUserActorUpd =  source.SecurityUserActorUpd;

        if (includeSystemFields)
        {
        }
    }

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

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

        return result;
    }

    }
}
