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_user")]
    public class SecurityUserEntity : EntityBase<string>
    {

    public SecurityUserEntity() 
    {
    }

    [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]
    [Column("user_name")]
    public string UserName  { get; set; }
    [Column("user_local_name")]
    public string UserLocalName  { get; set; }
    [Column("user_eng_name")]
    public string? UserEngName  { get; set; }
    [Column("company_name")]
    public string? CompanyName  { get; set; }
    [Column("industry")]
    public string? Industry  { get; set; }
    [Column("user_password")]
    public string? UserPassword  { get; set; }
    [Column("user_mobile")]
    public string? UserMobile  { get; set; }
    [Column("user_address")]
    public string? UserAddress  { get; set; }
    [Column("postcode")]
    public string? Postcode  { get; set; }
    [Column("community")]
    public int Community  { get; set; }
    [Column("user_grade")]
    public int UserGrade  { get; set; }
    [Column("user_type")]
    public string? UserType  { get; set; }
    [Column("user_wechat_account")]
    public string? UserWechatAccount  { get; set; }
    [Column("user_alipay_account")]
    public string? UserAlipayAccount  { get; set; }
    [Column("user_alipay_account_img_path")]
    public string? UserAlipayAccountImgPath  { get; set; }
    [Column("user_wechat_account_img_path")]
    public string? UserWechatAccountImgPath  { get; set; }
    [Column("user_icon_img_path")]
    public string? UserIconImgPath  { get; set; }
    [Column("user_accounts_expire_datetime")]
    public Nullable<DateTime> UserAccountsExpireDatetime  { get; set; }
    [Column("user_is_valid_indc")]
    public string? UserIsValidIndc  { get; set; }
    [Column("is_forece_change_password_indc")]
    public string? IsForeceChangePasswordIndc  { get; set; }
    [Column("is_accounts_locked_indc")]
    public string? IsAccountsLockedIndc  { get; set; }
    [Column("accounts_locked_datetime")]
    public Nullable<DateTime> AccountsLockedDatetime  { get; set; }
    [Column("user_remark")]
    public string? UserRemark  { get; set; }
    [Column("welcome_page")]
    public string? WelcomePage  { get; set; }
    [Column("user_type_end_datetime")]
    public Nullable<DateTime> UserTypeEndDatetime  { get; set; }
    [Column("room_name")]
    public string? RoomName  { get; set; }
    [Column("last_login_datetime")]
    public Nullable<DateTime> LastLoginDatetime  { get; set; }
    [Column("error_login_count")]
    public int ErrorLoginCount  { 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 USER_NAME = "UserName";
    public const string USER_LOCAL_NAME = "UserLocalName";
    public const string USER_ENG_NAME = "UserEngName";
    public const string COMPANY_NAME = "CompanyName";
    public const string INDUSTRY = "Industry";
    public const string USER_PASSWORD = "UserPassword";
    public const string USER_MOBILE = "UserMobile";
    public const string USER_ADDRESS = "UserAddress";
    public const string POSTCODE = "Postcode";
    public const string COMMUNITY = "Community";
    public const string USER_GRADE = "UserGrade";
    public const string USER_TYPE = "UserType";
    public const string USER_WECHAT_ACCOUNT = "UserWechatAccount";
    public const string USER_ALIPAY_ACCOUNT = "UserAlipayAccount";
    public const string USER_ALIPAY_ACCOUNT_IMG_PATH = "UserAlipayAccountImgPath";
    public const string USER_WECHAT_ACCOUNT_IMG_PATH = "UserWechatAccountImgPath";
    public const string USER_ICON_IMG_PATH = "UserIconImgPath";
    public const string USER_ACCOUNTS_EXPIRE_DATETIME = "UserAccountsExpireDatetime";
    public const string USER_IS_VALID_INDC = "UserIsValidIndc";
    public const string IS_FORECE_CHANGE_PASSWORD_INDC = "IsForeceChangePasswordIndc";
    public const string IS_ACCOUNTS_LOCKED_INDC = "IsAccountsLockedIndc";
    public const string ACCOUNTS_LOCKED_DATETIME = "AccountsLockedDatetime";
    public const string USER_REMARK = "UserRemark";
    public const string WELCOME_PAGE = "WelcomePage";
    public const string USER_TYPE_END_DATETIME = "UserTypeEndDatetime";
    public const string ROOM_NAME = "RoomName";
    public const string LAST_LOGIN_DATETIME = "LastLoginDatetime";
    public const string ERROR_LOGIN_COUNT = "ErrorLoginCount";

    public const string DB_NAME_SECURITY_USER = "security_user";
    public const string DB_NAME_FIELDS="created_datetime,created_by,updated_datetime,updated_by,user_name,user_local_name,user_eng_name,company_name,industry,user_password,user_mobile,user_address,postcode,community,user_grade,user_type,user_wechat_account,user_alipay_account,user_alipay_account_img_path,user_wechat_account_img_path,user_icon_img_path,user_accounts_expire_datetime,user_is_valid_indc,is_forece_change_password_indc,is_accounts_locked_indc,accounts_locked_datetime,user_remark,welcome_page,user_type_end_datetime,room_name,last_login_datetime,error_login_count";

    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_USER_NAME = "user_name";
    public const string DB_USER_LOCAL_NAME = "user_local_name";
    public const string DB_USER_ENG_NAME = "user_eng_name";
    public const string DB_COMPANY_NAME = "company_name";
    public const string DB_INDUSTRY = "industry";
    public const string DB_USER_PASSWORD = "user_password";
    public const string DB_USER_MOBILE = "user_mobile";
    public const string DB_USER_ADDRESS = "user_address";
    public const string DB_POSTCODE = "postcode";
    public const string DB_COMMUNITY = "community";
    public const string DB_USER_GRADE = "user_grade";
    public const string DB_USER_TYPE = "user_type";
    public const string DB_USER_WECHAT_ACCOUNT = "user_wechat_account";
    public const string DB_USER_ALIPAY_ACCOUNT = "user_alipay_account";
    public const string DB_USER_ALIPAY_ACCOUNT_IMG_PATH = "user_alipay_account_img_path";
    public const string DB_USER_WECHAT_ACCOUNT_IMG_PATH = "user_wechat_account_img_path";
    public const string DB_USER_ICON_IMG_PATH = "user_icon_img_path";
    public const string DB_USER_ACCOUNTS_EXPIRE_DATETIME = "user_accounts_expire_datetime";
    public const string DB_USER_IS_VALID_INDC = "user_is_valid_indc";
    public const string DB_IS_FORECE_CHANGE_PASSWORD_INDC = "is_forece_change_password_indc";
    public const string DB_IS_ACCOUNTS_LOCKED_INDC = "is_accounts_locked_indc";
    public const string DB_ACCOUNTS_LOCKED_DATETIME = "accounts_locked_datetime";
    public const string DB_USER_REMARK = "user_remark";
    public const string DB_WELCOME_PAGE = "welcome_page";
    public const string DB_USER_TYPE_END_DATETIME = "user_type_end_datetime";
    public const string DB_ROOM_NAME = "room_name";
    public const string DB_LAST_LOGIN_DATETIME = "last_login_datetime";
    public const string DB_ERROR_LOGIN_COUNT = "error_login_count";


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

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

    public virtual void CopyFrom(SecurityUserEntity 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.UserName =  source.UserName;
        this.UserLocalName =  source.UserLocalName;
        this.UserEngName =  source.UserEngName;
        this.CompanyName =  source.CompanyName;
        this.Industry =  source.Industry;
        this.UserPassword =  source.UserPassword;
        this.UserMobile =  source.UserMobile;
        this.UserAddress =  source.UserAddress;
        this.Postcode =  source.Postcode;
        this.Community =  source.Community;
        this.UserGrade =  source.UserGrade;
        this.UserType =  source.UserType;
        this.UserWechatAccount =  source.UserWechatAccount;
        this.UserAlipayAccount =  source.UserAlipayAccount;
        this.UserAlipayAccountImgPath =  source.UserAlipayAccountImgPath;
        this.UserWechatAccountImgPath =  source.UserWechatAccountImgPath;
        this.UserIconImgPath =  source.UserIconImgPath;
        this.UserAccountsExpireDatetime =  source.UserAccountsExpireDatetime;
        this.UserIsValidIndc =  source.UserIsValidIndc;
        this.IsForeceChangePasswordIndc =  source.IsForeceChangePasswordIndc;
        this.IsAccountsLockedIndc =  source.IsAccountsLockedIndc;
        this.AccountsLockedDatetime =  source.AccountsLockedDatetime;
        this.UserRemark =  source.UserRemark;
        this.WelcomePage =  source.WelcomePage;
        this.UserTypeEndDatetime =  source.UserTypeEndDatetime;
        this.RoomName =  source.RoomName;
        this.LastLoginDatetime =  source.LastLoginDatetime;
        this.ErrorLoginCount =  source.ErrorLoginCount;

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

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

        return result;
    }

    }
}
