😎实体调整及代码优化

This commit is contained in:
zuohuaijun 2025-09-05 14:35:50 +08:00
parent d6acf55255
commit d08a642514
7 changed files with 262 additions and 251 deletions

View File

@ -91,24 +91,24 @@ public abstract class EntityBase : EntityBaseId, IDeletedFilter
public abstract class EntityBaseData : EntityBase, IOrgIdFilter public abstract class EntityBaseData : EntityBase, IOrgIdFilter
{ {
/// <summary> /// <summary>
/// 创建者部门Id /// 创建者机构Id
/// </summary> /// </summary>
[OwnerOrg] [OwnerOrg]
[SugarColumn(ColumnDescription = "创建者部门Id")] [SugarColumn(ColumnDescription = "创建者机构Id")]
public virtual long? CreateOrgId { get; set; } public virtual long? CreateOrgId { get; set; }
/// <summary> /// <summary>
/// 创建者部门 /// 创建者机构
/// </summary> /// </summary>
[Newtonsoft.Json.JsonIgnore] [Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore]
[Navigate(NavigateType.OneToOne, nameof(CreateOrgId))] [Navigate(NavigateType.OneToOne, nameof(CreateOrgId))]
public virtual SysOrg CreateOrg { get; set; } public virtual SysOrg SysOrg { get; set; }
/// <summary> /// <summary>
/// 创建者部门名称 /// 创建者机构名称
/// </summary> /// </summary>
[SugarColumn(ColumnDescription = "创建者部门名称", Length = 64)] [SugarColumn(ColumnDescription = "创建者机构名称", Length = 64)]
public virtual string? CreateOrgName { get; set; } public virtual string? CreateOrgName { get; set; }
} }
@ -122,6 +122,14 @@ public abstract class EntityTenant : EntityBase, ITenantIdFilter
/// </summary> /// </summary>
[SugarColumn(ColumnDescription = "租户Id", IsOnlyIgnoreUpdate = true)] [SugarColumn(ColumnDescription = "租户Id", IsOnlyIgnoreUpdate = true)]
public virtual long? TenantId { get; set; } public virtual long? TenantId { get; set; }
/// <summary>
/// 租户
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[Navigate(NavigateType.OneToOne, nameof(TenantId))]
public SysTenant SysTenant { get; set; }
} }
/// <summary> /// <summary>
@ -134,6 +142,14 @@ public abstract class EntityTenantId : EntityBaseId, ITenantIdFilter
/// </summary> /// </summary>
[SugarColumn(ColumnDescription = "租户Id", IsOnlyIgnoreUpdate = true)] [SugarColumn(ColumnDescription = "租户Id", IsOnlyIgnoreUpdate = true)]
public virtual long? TenantId { get; set; } public virtual long? TenantId { get; set; }
/// <summary>
/// 租户
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[Navigate(NavigateType.OneToOne, nameof(TenantId))]
public SysTenant SysTenant { get; set; }
} }
/// <summary> /// <summary>
@ -146,4 +162,12 @@ public abstract class EntityTenantBaseData : EntityBaseData, ITenantIdFilter
/// </summary> /// </summary>
[SugarColumn(ColumnDescription = "租户Id", IsOnlyIgnoreUpdate = true)] [SugarColumn(ColumnDescription = "租户Id", IsOnlyIgnoreUpdate = true)]
public virtual long? TenantId { get; set; } public virtual long? TenantId { get; set; }
/// <summary>
/// 租户
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[Navigate(NavigateType.OneToOne, nameof(TenantId))]
public SysTenant SysTenant { get; set; }
} }

View File

@ -19,12 +19,28 @@ public partial class SysTenant : EntityBase
[SugarColumn(ColumnDescription = "租管用户Id")] [SugarColumn(ColumnDescription = "租管用户Id")]
public long UserId { get; set; } public long UserId { get; set; }
/// <summary>
/// 租管用户
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[Navigate(NavigateType.OneToOne, nameof(UserId))]
public SysUser SysUser { get; set; }
/// <summary> /// <summary>
/// 机构Id /// 机构Id
/// </summary> /// </summary>
[SugarColumn(ColumnDescription = "机构Id")] [SugarColumn(ColumnDescription = "机构Id")]
public long OrgId { get; set; } public long OrgId { get; set; }
/// <summary>
/// 机构
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[Navigate(NavigateType.OneToOne, nameof(OrgId))]
public SysOrg SysOrg { get; set; }
/// <summary> /// <summary>
/// 域名 /// 域名
/// </summary> /// </summary>

View File

@ -186,7 +186,7 @@ public class SysAuthService : IDynamicApiController, ITransient
{ {
VerifyPassword(password, keyPasswordErrorTimes, passwordErrorTimes, user); VerifyPassword(password, keyPasswordErrorTimes, passwordErrorTimes, user);
} }
else if (!await App.GetRequiredService<SysLdapService>().AuthAccount(user.TenantId.Value, userLdap.Account, CryptogramHelper.Decrypt(password))) else if (!await App.GetRequiredService<SysLdapService>().AuthAccount(user.TenantId!.Value, userLdap.Account, CryptogramHelper.Decrypt(password)))
{ {
_sysCacheService.Set(keyPasswordErrorTimes, ++passwordErrorTimes, TimeSpan.FromMinutes(30)); _sysCacheService.Set(keyPasswordErrorTimes, ++passwordErrorTimes, TimeSpan.FromMinutes(30));
throw Oops.Oh(ErrorCodeEnum.D1000); throw Oops.Oh(ErrorCodeEnum.D1000);
@ -256,9 +256,9 @@ public class SysAuthService : IDynamicApiController, ITransient
// 更新用户登录信息 // 更新用户登录信息
user.LastLoginIp = _httpContextAccessor.HttpContext.GetRemoteIpAddressToIPv4(true); user.LastLoginIp = _httpContextAccessor.HttpContext.GetRemoteIpAddressToIPv4(true);
(user.LastLoginAddress, double? longitude, double? latitude) = CommonHelper.GetIpAddress(user.LastLoginIp); (user.LastLoginAddress, double? _, double? _) = CommonHelper.GetIpAddress(user.LastLoginIp);
user.LastLoginTime = DateTime.Now; user.LastLoginTime = DateTime.Now;
user.LastLoginDevice = CommonHelper.GetClientDeviceInfo(_httpContextAccessor.HttpContext?.Request?.Headers?.UserAgent); user.LastLoginDevice = CommonHelper.GetClientDeviceInfo(_httpContextAccessor.HttpContext?.Request.Headers.UserAgent);
await _sysUserRep.AsUpdateable(user).UpdateColumns(u => new await _sysUserRep.AsUpdateable(user).UpdateColumns(u => new
{ {
u.LastLoginIp, u.LastLoginIp,
@ -387,7 +387,7 @@ public class SysAuthService : IDynamicApiController, ITransient
[AllowAnonymous] [AllowAnonymous]
public int SwaggerCheckUrl() public int SwaggerCheckUrl()
{ {
return _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated ? 200 : 401; return _httpContextAccessor.HttpContext!.User.Identity!.IsAuthenticated ? 200 : 401;
} }
/// <summary> /// <summary>
@ -405,7 +405,7 @@ public class SysAuthService : IDynamicApiController, ITransient
// 关闭默认租户验证码验证 // 关闭默认租户验证码验证
var tenantList = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant); var tenantList = _sysCacheService.Get<List<SysTenant>>(CacheConst.KeyTenant);
var tenant = tenantList.FirstOrDefault(u => u.Id == SqlSugarConst.DefaultTenantId); var tenant = tenantList.FirstOrDefault(u => u.Id == SqlSugarConst.DefaultTenantId);
var tmpCaptcha = tenant.Captcha; var tmpCaptcha = tenant!.Captcha;
tenant.Captcha = false; tenant.Captcha = false;
_sysCacheService.Set(CacheConst.KeyTenant, tenantList); _sysCacheService.Set(CacheConst.KeyTenant, tenantList);

View File

@ -436,6 +436,7 @@ public static class SqlSugarExtension
// 执行前处理种子数据 // 执行前处理种子数据
if (handleBefore != null) foreach (var sd in seedData) handleBefore(sd); if (handleBefore != null) foreach (var sd in seedData) handleBefore(sd);
dbProvider.QueryFilter.ClearAndBackup();
int total, insertCount = 0, updateCount = 0; int total, insertCount = 0, updateCount = 0;
if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null) if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
{ {
@ -475,6 +476,7 @@ public static class SqlSugarExtension
} }
} }
} }
dbProvider.QueryFilter.Restore();
return (total, insertCount, updateCount); return (total, insertCount, updateCount);
} }
@ -515,16 +517,16 @@ public static class SqlSugarExtension
#region #region
/// <summary>
/// 延迟初始化流水号服务实例
/// </summary>
private static readonly Lazy<SysCommonService> SysCommon = new(() => App.GetService<SysCommonService>());
/// <summary> /// <summary>
/// 包含缩写特性的类型属性缓存表 /// 包含缩写特性的类型属性缓存表
/// </summary> /// </summary>
private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindTextAbbrAttribute Attr)>?> _textAbbrPropCache = new(); private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindTextAbbrAttribute Attr)>?> _textAbbrPropCache = new();
/// <summary>
/// 系统通用服务
/// </summary>
private static readonly Lazy<SysCommonService> _lazySysCommonService = new(() => App.GetService<SysCommonService>());
/// <summary> /// <summary>
/// 初始化文本简称数据 /// 初始化文本简称数据
/// </summary> /// </summary>
@ -586,8 +588,9 @@ public static class SqlSugarExtension
if (string.IsNullOrWhiteSpace(value)) return; if (string.IsNullOrWhiteSpace(value)) return;
// 使用线程安全的延迟初始化服务实例获取文本缩写 // 使用线程安全的延迟初始化服务实例获取文本缩写
var abbrValue = _lazySysCommonService.Value var abbrValue = SysCommon.Value
.GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr }) .GetNameAbbr(new() { Text = value, All = attribute.SaveFullAbbr })
.ConfigureAwait(false)
.GetAwaiter() .GetAwaiter()
.GetResult(); .GetResult();
entityInfo.SetValue(abbrValue); entityInfo.SetValue(abbrValue);
@ -597,16 +600,16 @@ public static class SqlSugarExtension
#region #region
/// <summary>
/// 延迟初始化流水号服务实例
/// </summary>
private static readonly Lazy<SysSerialService> SerialService = new(() => App.GetService<SysSerialService>());
/// <summary> /// <summary>
/// 包含流水号特性的类型属性缓存表 /// 包含流水号特性的类型属性缓存表
/// </summary> /// </summary>
private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindSerialAttribute Attr)>?> _serialPropCache = new(); private static readonly ConcurrentDictionary<Type, Dictionary<string, (PropertyInfo Prop, BindSerialAttribute Attr)>?> _serialPropCache = new();
/// <summary>
/// 系统通用服务
/// </summary>
private static readonly Lazy<SysSerialService> _lazySysSerialService = new(() => App.GetService<SysSerialService>());
/// <summary> /// <summary>
/// 自动生成流水号到绑定字段 /// 自动生成流水号到绑定字段
/// </summary> /// </summary>
@ -641,8 +644,9 @@ public static class SqlSugarExtension
var (_, attribute) = propData; var (_, attribute) = propData;
// 使用线程安全的延迟初始化服务实例获取流水号 // 使用线程安全的延迟初始化服务实例获取流水号
var serial = _lazySysSerialService.Value var serial = SerialService.Value
.NextSeqNo(attribute.Type, attribute.IsGlobal) .NextSeqNo(attribute.Type, attribute.IsGlobal)
.ConfigureAwait(false)
.GetAwaiter() .GetAwaiter()
.GetResult(); .GetResult();
entityInfo.SetValue(serial); entityInfo.SetValue(serial);

View File

@ -1,33 +0,0 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 懒加载帮助类
/// </summary>
public class LazyHelper
{
private static readonly ConcurrentDictionary<Type, dynamic> Cache = new();
/// <summary>
/// 获取服务
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Lazy<T> GetService<T>() where T : class
{
try
{
return Cache.GetOrAdd(typeof(T), _ => new Lazy<T>(() => App.GetService<T>()));
}
catch (Exception)
{
Cache.Remove(typeof(T), out _);
return null;
}
}
}

View File

@ -6,13 +6,13 @@
using Renci.SshNet; using Renci.SshNet;
namespace Admin.NET.Core namespace Admin.NET.Core;
/// <summary>
/// SSH/Sftp 工具类
/// </summary>
public class SSHHelper : IDisposable
{ {
/// <summary>
/// SSH/Sftp 工具类
/// </summary>
public class SSHHelper : IDisposable
{
private readonly SftpClient _sftp; private readonly SftpClient _sftp;
public SSHHelper(string host, int port, string user, string password) public SSHHelper(string host, int port, string user, string password)
@ -124,6 +124,7 @@ namespace Admin.NET.Core
if (file.IsRegularFile && filters.Any(f => file.Name.EndsWith(f))) if (file.IsRegularFile && filters.Any(f => file.Name.EndsWith(f)))
files.Add(file.Name); files.Add(file.Name);
} }
return files; return files;
} }
@ -204,5 +205,4 @@ namespace Admin.NET.Core
_sftp.Disconnect(); _sftp.Disconnect();
_sftp.Dispose(); _sftp.Dispose();
} }
}
} }

View File

@ -61,7 +61,7 @@ public class SuperApiAop : DefaultSuperApiAop
var paths = api?.Url?.Split('/'); var paths = api?.Url?.Split('/');
var actionName = paths?[^1]; var actionName = paths?[^1];
var userManager = LazyHelper.GetService<UserManager>(); var userManager = App.GetService<UserManager>();
var apiInfo = new var apiInfo = new
{ {
requestUrl = api?.Url, requestUrl = api?.Url,
@ -79,13 +79,13 @@ public class SuperApiAop : DefaultSuperApiAop
{ {
new new
{ {
type = userManager?.Value.Account, type = userManager?.Account,
value = userManager?.Value.RealName value = userManager?.RealName
}, },
new new
{ {
type = userManager?.Value.RealName, type = userManager?.RealName,
value = userManager?.Value.RealName value = userManager?.RealName
}, },
}, },
exception = aopContext.Exception == null ? null : JSON.Serialize(aopContext.Exception) exception = aopContext.Exception == null ? null : JSON.Serialize(aopContext.Exception)