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_web_login")] public class SecurityUserWebLoginEntity : EntityBase { public SecurityUserWebLoginEntity() { } [Column("session_id")] public string SessionId { get; set; } [Column("username")] public string Username { get; set; } [Column("host_ip")] public string? HostIp { get; set; } [Column("host_user_agent")] public string? HostUserAgent { get; set; } [Column("login_time")] public DateTime LoginTime { get; set; } [Column("logout_time")] public Nullable LogoutTime { get; set; } [Column("refresh_token")] public string RefreshToken { get; set; } [Column("refresh_token_refreshed_time")] public DateTime RefreshTokenRefreshedTime { get; set; } public const string SESSION_ID = "SessionId"; public const string USERNAME = "Username"; public const string HOST_IP = "HostIp"; public const string HOST_USER_AGENT = "HostUserAgent"; public const string LOGIN_TIME = "LoginTime"; public const string LOGOUT_TIME = "LogoutTime"; public const string REFRESH_TOKEN = "RefreshToken"; public const string REFRESH_TOKEN_REFRESHED_TIME = "RefreshTokenRefreshedTime"; public const string DB_NAME_SECURITY_USER_WEB_LOGIN = "security_user_web_login"; public const string DB_NAME_FIELDS="session_id,username,host_ip,host_user_agent,login_time,logout_time,refresh_token,refresh_token_refreshed_time"; public const string DB_SESSION_ID = "session_id"; public const string DB_USERNAME = "username"; public const string DB_HOST_IP = "host_ip"; public const string DB_HOST_USER_AGENT = "host_user_agent"; public const string DB_LOGIN_TIME = "login_time"; public const string DB_LOGOUT_TIME = "logout_time"; public const string DB_REFRESH_TOKEN = "refresh_token"; public const string DB_REFRESH_TOKEN_REFRESHED_TIME = "refresh_token_refreshed_time"; public string ProviderName { get; set; } public static SecurityUserWebLoginEntity GetInstance(string providerName) { return new SecurityUserWebLoginEntity() { ProviderName = providerName }; } public virtual void CopyFrom(SecurityUserWebLoginEntity source) { CopyFrom(source, true); } public virtual void CopyFrom(SecurityUserWebLoginEntity source, bool includeSystemFields) { if (source == null) { throw new NullReferenceException("Source entity is null."); } this.SessionId = source.SessionId; this.Username = source.Username; this.HostIp = source.HostIp; this.HostUserAgent = source.HostUserAgent; this.LoginTime = source.LoginTime; this.LogoutTime = source.LogoutTime; this.RefreshToken = source.RefreshToken; this.RefreshTokenRefreshedTime = source.RefreshTokenRefreshedTime; if (includeSystemFields) { } } public override string GetSelectFrom() { if (ProviderName.Contains("MySql")) { return GetSelectFromByMySql(); } else if (ProviderName.Contains("SqlServer")) { return GetSelectFromByBySqlServer(); } else { throw new NotSupportedException($"不支持的数据库提供程序:{ProviderName}"); } } public string GetSelectFromByMySql() { return $" select {DB_NAME_FIELDS} from { DB_NAME_SECURITY_USER_WEB_LOGIN} with(nolock) "; } public string GetSelectFromByBySqlServer() { return $" select {DB_NAME_FIELDS} from { DB_NAME_SECURITY_USER_WEB_LOGIN} "; } public override string GetSelectCountFrom(string sqlWhere) { if (ProviderName.Contains("MySql")) { return GetSelectCountFromByMySql(sqlWhere); } else if (ProviderName.Contains("SqlServer")) { return GetSelectCountFromBySqlServer(sqlWhere); } else { throw new NotSupportedException($"不支持的数据库提供程序:{ProviderName}"); } } public string GetSelectCountFromByMySql(string sqlWhere) { return $" select count(*) from { DB_NAME_SECURITY_USER_WEB_LOGIN} where 1=1 {sqlWhere}"; } public string GetSelectCountFromBySqlServer(string sqlWhere) { return $" select count(*) from { DB_NAME_SECURITY_USER_WEB_LOGIN} with(nolock) where 1=1 {sqlWhere}"; } public override string GetSelectTopFrom(int limit, int start, string orderby, string orderbytag, string sqlWhere) { if (ProviderName.Contains("MySql")) { return GetSelectTopFromByMySql(limit, start, orderby, orderbytag, sqlWhere); } else if (ProviderName.Contains("SqlServer")) { return GetSelectTopFromBySqlServer(limit, start, orderby, orderbytag, sqlWhere); } else { throw new NotSupportedException($"不支持的数据库提供程序:{ProviderName}"); } } public string GetSelectTopFromByMySql(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_SESSION_ID ; int offset = limit * (start - 1); return $" select {DB_NAME_FIELDS} from (select {DB_NAME_FIELDS} ,row_number() over(order by {orderby} {orderbytag}) as num from { DB_NAME_SECURITY_USER_WEB_LOGIN} where 1=1 {sqlWhere} ) a where num> {offset} LIMIT {offset},{limit} "; } public string GetSelectTopFromBySqlServer(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_SESSION_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_USER_WEB_LOGIN} with(nolock) where 1=1 {sqlWhere} ) a where num> {limit * (start - 1)}"; } public SecurityUserWebLoginEntity Clone() { SecurityUserWebLoginEntity result = new SecurityUserWebLoginEntity(); result.CopyFrom(this); return result; } } }