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_generator_number")]
    public class SecurityGeneratorNumberEntity : EntityBase<int>
    {

    public SecurityGeneratorNumberEntity() 
    {
    }

    [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; }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("generator_id")]
    public int GeneratorId  { get; set; }
    [Column("system_id")]
    public string SystemId  { get; set; }
    [Column("subsystem_id")]
    public string SubsystemId  { get; set; }
    [Column("prefix")]
    public string? Prefix  { get; set; }
    [Column("refresh_period")]
    public string RefreshPeriod  { get; set; }
    [Column("format")]
    public string Format  { get; set; }
    [Column("last_generator_date")]
    public DateTime LastGeneratorDate  { get; set; }
    [Column("next_id")]
    public int NextId  { 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 GENERATOR_ID = "GeneratorId";
    public const string SYSTEM_ID = "SystemId";
    public const string SUBSYSTEM_ID = "SubsystemId";
    public const string PREFIX = "Prefix";
    public const string REFRESH_PERIOD = "RefreshPeriod";
    public const string FORMAT = "Format";
    public const string LAST_GENERATOR_DATE = "LastGeneratorDate";
    public const string NEXT_ID = "NextId";

    public const string DB_NAME_SECURITY_GENERATOR_NUMBER = "security_generator_number";
    public const string DB_NAME_FIELDS="created_datetime,created_by,updated_datetime,updated_by,generator_id,system_id,subsystem_id,prefix,refresh_period,format,last_generator_date,next_id";

    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_GENERATOR_ID = "generator_id";
    public const string DB_SYSTEM_ID = "system_id";
    public const string DB_SUBSYSTEM_ID = "subsystem_id";
    public const string DB_PREFIX = "prefix";
    public const string DB_REFRESH_PERIOD = "refresh_period";
    public const string DB_FORMAT = "format";
    public const string DB_LAST_GENERATOR_DATE = "last_generator_date";
    public const string DB_NEXT_ID = "next_id";


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

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

    public virtual void CopyFrom(SecurityGeneratorNumberEntity 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.GeneratorId =  source.GeneratorId;
        this.SystemId =  source.SystemId;
        this.SubsystemId =  source.SubsystemId;
        this.Prefix =  source.Prefix;
        this.RefreshPeriod =  source.RefreshPeriod;
        this.Format =  source.Format;
        this.LastGeneratorDate =  source.LastGeneratorDate;
        this.NextId =  source.NextId;

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

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

        return result;
    }

    }
}
