😎1、调整启动执行任务模式 2、优化定时任务
This commit is contained in:
parent
d9e0aa6a71
commit
ce57b149b7
@ -1,218 +0,0 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 枚举转字典
|
||||
/// </summary>
|
||||
[JobDetail("job_EnumToDictJob", Description = "枚举转字典", GroupName = "default", Concurrent = false)]
|
||||
[PeriodSeconds(1, TriggerId = "trigger_EnumToDictJob", Description = "枚举转字典", MaxNumberOfRuns = 1, RunOnStart = true)]
|
||||
public class EnumToDictJob : IJob
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private const int OrderOffset = 10;
|
||||
private const string DefaultTagType = "info";
|
||||
|
||||
public EnumToDictJob(IServiceScopeFactory serviceScopeFactory)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||||
{
|
||||
using var serviceScope = _serviceScopeFactory.CreateScope();
|
||||
var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
||||
// 获取数据库连接
|
||||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||
|
||||
// 获取枚举类型列表
|
||||
var enumTypeList = sysEnumService.GetEnumTypeList();
|
||||
var enumCodeList = enumTypeList.Select(u => u.TypeName);
|
||||
// 查询数据库中已存在的枚举类型代码
|
||||
//var exp = Expressionable.Create<SysDictType, SingleColumnEntity<string>>().And((t1, t2) => t1.Code == t2.ColumnName).ToExpression();
|
||||
//var sysDictTypeList = await db.Queryable<SysDictType>().Includes(t1 => t1.Children).BulkListQuery(exp, enumCodeList, stoppingToken);
|
||||
var sysDictTypeList = await db.Queryable<SysDictType>().Includes(u => u.Children)
|
||||
.Where(u => enumCodeList.Contains(u.Code)).ToListAsync(stoppingToken);
|
||||
// 更新的枚举转换字典
|
||||
var updatedEnumCodes = sysDictTypeList.Select(u => u.Code);
|
||||
var updatedEnumType = enumTypeList.Where(u => updatedEnumCodes.Contains(u.TypeName)).ToList();
|
||||
var sysDictTypeDict = sysDictTypeList.ToDictionary(u => u.Code, u => u);
|
||||
var (updatedDictTypes, updatedDictDatas, newSysDictDatas) = GetUpdatedDicts(updatedEnumType, sysDictTypeDict);
|
||||
|
||||
// 新增的枚举转换字典
|
||||
var newEnumType = enumTypeList.Where(u => !updatedEnumCodes.Contains(u.TypeName)).ToList();
|
||||
var (newDictTypes, newDictDatas) = GetNewSysDicts(newEnumType);
|
||||
|
||||
// 执行数据库操作
|
||||
try
|
||||
{
|
||||
await db.BeginTranAsync();
|
||||
|
||||
if (updatedDictTypes.Count > 0)
|
||||
await db.Updateable(updatedDictTypes).ExecuteCommandAsync(stoppingToken);
|
||||
|
||||
if (updatedDictDatas.Count > 0)
|
||||
await db.Updateable(updatedDictDatas).ExecuteCommandAsync(stoppingToken);
|
||||
|
||||
if (newSysDictDatas.Count > 0)
|
||||
{
|
||||
// 达梦:用db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入
|
||||
// 达梦:不支持storageable2.BulkUpdateAsync 功能
|
||||
foreach (var dd in newSysDictDatas)
|
||||
await db.Insertable(dd).ExecuteCommandAsync(stoppingToken);
|
||||
}
|
||||
|
||||
if (newDictTypes.Count > 0)
|
||||
await db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);
|
||||
|
||||
if (newDictDatas.Count > 0)
|
||||
{
|
||||
// 达梦:用db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入
|
||||
// 达梦:不支持storageable2.BulkUpdateAsync 功能
|
||||
foreach (var dd in newDictDatas)
|
||||
await db.Insertable(dd).ExecuteCommandAsync(stoppingToken);
|
||||
}
|
||||
|
||||
await db.CommitTranAsync();
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
await db.RollbackTranAsync();
|
||||
Log.Error($"系统枚举转换字典操作错误:{error.Message}\n堆栈跟踪:{error.StackTrace}", error);
|
||||
throw;
|
||||
}
|
||||
var msg = $"【{DateTime.Now}】系统枚举转换字典。【定时任务】";
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(msg);
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取需要新增的字典列表
|
||||
/// </summary>
|
||||
/// <param name="addEnumType"></param>
|
||||
/// <returns>
|
||||
/// 一个元组,包含以下元素:
|
||||
/// <list type="table">
|
||||
/// <item><term>SysDictTypes</term><description>字典类型列表</description></item>
|
||||
/// <item><term>SysDictDatas</term><description>字典数据列表</description></item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
private (List<SysDictType>, List<SysDictData>) GetNewSysDicts(List<EnumTypeOutput> addEnumType)
|
||||
{
|
||||
var newDictType = new List<SysDictType>();
|
||||
var newDictData = new List<SysDictData>();
|
||||
if (addEnumType.Count <= 0)
|
||||
return (newDictType, newDictData);
|
||||
|
||||
// 新增字典类型
|
||||
newDictType = addEnumType.Select(u => new SysDictType
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
Code = u.TypeName,
|
||||
Name = u.TypeDescribe,
|
||||
Remark = u.TypeRemark,
|
||||
Status = StatusEnum.Enable,
|
||||
SysFlag = YesNoEnum.Y,
|
||||
}).ToList();
|
||||
|
||||
// 新增字典数据
|
||||
newDictData = addEnumType.Join(newDictType, t1 => t1.TypeName, t2 => t2.Code, (t1, t2) => new
|
||||
{
|
||||
Data = t1.EnumEntities.Select(u => new SysDictData
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
DictTypeId = t2.Id,
|
||||
Code = u.Name,
|
||||
Label = u.Describe,
|
||||
Value = u.Value.ToString(),
|
||||
Remark = t2.Remark,
|
||||
OrderNo = u.Value + OrderOffset,
|
||||
TagType = u.Theme != "" ? u.Theme : DefaultTagType,
|
||||
}).ToList()
|
||||
}).SelectMany(x => x.Data).ToList();
|
||||
|
||||
return (newDictType, newDictData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取需要更新的字典列表
|
||||
/// </summary>
|
||||
/// <param name="updatedEnumType"></param>
|
||||
/// <param name="sysDictTypeDict"></param>
|
||||
/// <returns>
|
||||
/// 一个元组,包含以下元素:
|
||||
/// <list type="table">
|
||||
/// <item><term>SysDictTypes</term><description>更新字典类型列表</description>
|
||||
/// </item>
|
||||
/// <item><term>SysDictDatas</term><description>更新字典数据列表</description>
|
||||
/// </item>
|
||||
/// <item><term>SysDictDatas</term><description>新增字典数据列表</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
private (List<SysDictType>, List<SysDictData>, List<SysDictData>) GetUpdatedDicts(List<EnumTypeOutput> updatedEnumType, Dictionary<string, SysDictType> sysDictTypeDict)
|
||||
{
|
||||
var updatedSysDictTypes = new List<SysDictType>();
|
||||
var updatedSysDictData = new List<SysDictData>();
|
||||
var newSysDictData = new List<SysDictData>();
|
||||
foreach (var e in updatedEnumType)
|
||||
{
|
||||
if (!sysDictTypeDict.TryGetValue(e.TypeName, out var value))
|
||||
continue;
|
||||
|
||||
var updatedDictType = value;
|
||||
updatedDictType.Name = e.TypeDescribe;
|
||||
updatedDictType.Remark = e.TypeRemark;
|
||||
updatedSysDictTypes.Add(updatedDictType);
|
||||
var updatedDictData = updatedDictType.Children.Where(u => u.DictTypeId == updatedDictType.Id).ToList();
|
||||
|
||||
// 遍历需要更新的字典数据
|
||||
foreach (var dictData in updatedDictData)
|
||||
{
|
||||
var enumData = e.EnumEntities.FirstOrDefault(u => dictData.Code == u.Name);
|
||||
if (enumData != null)
|
||||
{
|
||||
dictData.Code = enumData.Name;
|
||||
dictData.Label = enumData.Describe;
|
||||
dictData.Value = enumData.Value.ToString();
|
||||
dictData.OrderNo = enumData.Value + OrderOffset;
|
||||
dictData.TagType = enumData.Theme != "" ? enumData.Theme : dictData.TagType != "" ? dictData.TagType : DefaultTagType;
|
||||
updatedSysDictData.Add(dictData);
|
||||
}
|
||||
}
|
||||
|
||||
// 新增的枚举值名称列表
|
||||
var newEnumDataNameList = e.EnumEntities.Select(u => u.Name).Except(updatedDictData.Select(u => u.Code));
|
||||
foreach (var newEnumDataName in newEnumDataNameList)
|
||||
{
|
||||
var enumData = e.EnumEntities.FirstOrDefault(u => newEnumDataName == u.Name);
|
||||
if (enumData != null)
|
||||
{
|
||||
var dictData = new SysDictData
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
DictTypeId = updatedDictType.Id,
|
||||
Code = enumData.Name,
|
||||
Label = enumData.Describe,
|
||||
Value = enumData.Value.ToString(),
|
||||
Remark = updatedDictType.Remark,
|
||||
OrderNo = enumData.Value + OrderOffset,
|
||||
TagType = enumData.Theme != "" ? enumData.Theme : DefaultTagType,
|
||||
};
|
||||
dictData.TagType = enumData.Theme != "" ? enumData.Theme : dictData.TagType != "" ? dictData.TagType : DefaultTagType;
|
||||
newSysDictData.Add(dictData);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除的情况暂不处理
|
||||
}
|
||||
|
||||
return (updatedSysDictTypes, updatedSysDictData, newSysDictData);
|
||||
}
|
||||
}
|
||||
@ -7,20 +7,13 @@
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 清理日志作业任务
|
||||
/// 清理日志作业任务(每天 00:00:00 执行)
|
||||
/// </summary>
|
||||
[JobDetail("job_log", Description = "清理操作日志", GroupName = "default", Concurrent = false)]
|
||||
[Daily(TriggerId = "trigger_log", Description = "清理操作日志")]
|
||||
public class LogJob : IJob
|
||||
public class LogJob(IServiceScopeFactory serviceScopeFactory) : IJob
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public LogJob(IServiceScopeFactory serviceScopeFactory, ILoggerFactory loggerFactory)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_logger = loggerFactory.CreateLogger(CommonConst.SysLogCategoryName);
|
||||
}
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory;
|
||||
|
||||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||||
{
|
||||
@ -29,19 +22,17 @@ public class LogJob : IJob
|
||||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||
var sysConfigService = serviceScope.ServiceProvider.GetRequiredService<SysConfigService>();
|
||||
|
||||
var daysAgo = await sysConfigService.GetConfigValueByCode<int>(ConfigConst.SysLogRetentionDays); // 日志保留天数
|
||||
await db.Deleteable<SysLogVis>().Where(u => u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除访问日志
|
||||
await db.Deleteable<SysLogOp>().Where(u => u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除操作日志
|
||||
await db.Deleteable<SysLogDiff>().Where(u => u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除差异日志
|
||||
await db.Deleteable<SysJobTriggerRecord>().Where(u => u.CreatedTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken); // 删除作业触发器运行记录
|
||||
// 日志保留天数
|
||||
var daysAgo = await sysConfigService.GetConfigValueByCode<int>(ConfigConst.SysLogRetentionDays);
|
||||
// 删除访问日志
|
||||
await db.Deleteable<SysLogVis>().Where(u => u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken);
|
||||
// 删除操作日志
|
||||
await db.Deleteable<SysLogOp>().Where(u => u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken);
|
||||
// 删除差异日志
|
||||
await db.Deleteable<SysLogDiff>().Where(u => u.CreateTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken);
|
||||
// 删除作业触发器运行记录
|
||||
await db.Deleteable<SysJobTriggerRecord>().Where(u => u.CreatedTime < DateTime.Now.AddDays(-daysAgo)).ExecuteCommandAsync(stoppingToken);
|
||||
|
||||
var msg = $"【{DateTime.Now}】清理系统日志成功,删除 {daysAgo} 天前的日志数据。【定时任务】";
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine(msg);
|
||||
Console.ForegroundColor = originColor;
|
||||
|
||||
// 自定义日志
|
||||
_logger.LogInformation(msg);
|
||||
LoggingWriter.LogInformation($"【定时任务】清理系统日志成功,清理 {daysAgo} 天前的日志数据 {DateTime.Now}");
|
||||
}
|
||||
}
|
||||
@ -4,43 +4,23 @@
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
using Furion.Logging.Extensions;
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 清理在线用户作业任务
|
||||
/// 清理在线用户作业任务(每隔 60秒 执行)
|
||||
/// </summary>
|
||||
[JobDetail("job_onlineUser", Description = "清理在线用户", GroupName = "default", Concurrent = false)]
|
||||
[PeriodSeconds(1, TriggerId = "trigger_onlineUser", Description = "清理在线用户", MaxNumberOfRuns = 1, RunOnStart = true)]
|
||||
public class OnlineUserJob : IJob
|
||||
[PeriodSeconds(60, TriggerId = "trigger_onlineUser", Description = "清理在线用户", RunOnStart = true)]
|
||||
public class OnlineUserJob(IServiceScopeFactory serviceScopeFactory) : IJob
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public OnlineUserJob(IServiceScopeFactory serviceScopeFactory, ILoggerFactory loggerFactory)
|
||||
{
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
_logger = loggerFactory.CreateLogger(CommonConst.SysLogCategoryName);
|
||||
}
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory;
|
||||
|
||||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||||
{
|
||||
using var serviceScope = _serviceScopeFactory.CreateScope();
|
||||
|
||||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||
await db.Deleteable<SysOnlineUser>().ExecuteCommandAsync(stoppingToken);
|
||||
|
||||
var msg = $"【{DateTime.Now}】清理系统在线用户。【定时任务】";
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(msg);
|
||||
Console.ForegroundColor = originColor;
|
||||
|
||||
// 自定义日志
|
||||
_logger.LogInformation(msg);
|
||||
|
||||
// 缓存租户列表
|
||||
await serviceScope.ServiceProvider.GetRequiredService<SysTenantService>().CacheTenant();
|
||||
var sysOnlineUserService = serviceScope.ServiceProvider.GetRequiredService<SysOnlineUserService>();
|
||||
await sysOnlineUserService.ClearOnline();
|
||||
LoggingWriter.LogInformation($"【定时任务】清理系统在线用户 {DateTime.Now}");
|
||||
}
|
||||
}
|
||||
42
Admin.NET/Admin.NET.Core/Job/StartHostedService.cs
Normal file
42
Admin.NET/Admin.NET.Core/Job/StartHostedService.cs
Normal file
@ -0,0 +1,42 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 系统启动执行任务
|
||||
/// </summary>
|
||||
public class StartHostedService(IServiceScopeFactory serviceScopeFactory) : IHostedService
|
||||
{
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory;
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
using var serviceScope = _serviceScopeFactory.CreateScope();
|
||||
|
||||
// 枚举转字典
|
||||
var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
||||
await sysEnumService.EnumToDict();
|
||||
LoggingWriter.LogInformation($"【启动任务】系统枚举转换字典 {DateTime.Now}");
|
||||
|
||||
// 缓存租户列表
|
||||
await serviceScope.ServiceProvider.GetRequiredService<SysTenantService>().CacheTenant();
|
||||
LoggingWriter.LogInformation($"【启动任务】缓存系统租户列表 {DateTime.Now}");
|
||||
|
||||
// 清理在线用户
|
||||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||
await db.Deleteable<SysOnlineUser>().ExecuteCommandAsync(cancellationToken);
|
||||
LoggingWriter.LogInformation($"【启动任务】清理系统在线用户 {DateTime.Now}");
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
60
Admin.NET/Admin.NET.Core/Logging/LoggingWriter.cs
Normal file
60
Admin.NET/Admin.NET.Core/Logging/LoggingWriter.cs
Normal file
@ -0,0 +1,60 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core.Service;
|
||||
|
||||
/// <summary>
|
||||
/// 系统日志写入器
|
||||
/// </summary>
|
||||
public static class LoggingWriter
|
||||
{
|
||||
private static readonly ILogger _logger = Log.CreateLoggerFactory().CreateLogger(CommonConst.SysLogCategoryName);
|
||||
|
||||
/// <summary>
|
||||
/// 记录系统消息日志
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void LogInformation(string message)
|
||||
{
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
|
||||
Console.WriteLine(message);
|
||||
_logger.LogInformation(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录系统警告日志
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
|
||||
Console.WriteLine(message);
|
||||
_logger.LogWarning(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录系统错误日志
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public static void LogError(string message)
|
||||
{
|
||||
var originColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
|
||||
Console.WriteLine(message);
|
||||
_logger.LogWarning(message);
|
||||
|
||||
Console.ForegroundColor = originColor;
|
||||
}
|
||||
}
|
||||
@ -12,10 +12,14 @@ namespace Admin.NET.Core.Service;
|
||||
[ApiDescriptionSettings(Order = 275, Description = "系统枚举")]
|
||||
public class SysEnumService : IDynamicApiController, ITransient
|
||||
{
|
||||
private readonly ISqlSugarClient _db;
|
||||
private readonly EnumOptions _enumOptions;
|
||||
private const int OrderOffset = 10;
|
||||
private const string DefaultTagType = "info";
|
||||
|
||||
public SysEnumService(IOptions<EnumOptions> enumOptions)
|
||||
public SysEnumService(ISqlSugarClient db, IOptions<EnumOptions> enumOptions)
|
||||
{
|
||||
_db = db;
|
||||
_enumOptions = enumOptions.Value;
|
||||
}
|
||||
|
||||
@ -92,4 +96,181 @@ public class SysEnumService : IDynamicApiController, ITransient
|
||||
|
||||
return fieldType.EnumToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 枚举转字典
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[DisplayName("枚举转字典")]
|
||||
[UnitOfWork]
|
||||
public async Task EnumToDict()
|
||||
{
|
||||
// 获取枚举类型列表
|
||||
var enumTypeList = GetEnumTypeList();
|
||||
var enumCodeList = enumTypeList.Select(u => u.TypeName);
|
||||
// 查询数据库中已存在的枚举类型代码
|
||||
//var exp = Expressionable.Create<SysDictType, SingleColumnEntity<string>>().And((t1, t2) => t1.Code == t2.ColumnName).ToExpression();
|
||||
//var sysDictTypeList = await db.Queryable<SysDictType>().Includes(t1 => t1.Children).BulkListQuery(exp, enumCodeList, stoppingToken);
|
||||
var sysDictTypeList = await _db.Queryable<SysDictType>().Includes(u => u.Children)
|
||||
.Where(u => enumCodeList.Contains(u.Code)).ToListAsync();
|
||||
// 更新的枚举转换字典
|
||||
var updatedEnumCodes = sysDictTypeList.Select(u => u.Code);
|
||||
var updatedEnumType = enumTypeList.Where(u => updatedEnumCodes.Contains(u.TypeName)).ToList();
|
||||
var sysDictTypeDict = sysDictTypeList.ToDictionary(u => u.Code, u => u);
|
||||
var (updatedDictTypes, updatedDictDatas, newSysDictDatas) = GetUpdatedDicts(updatedEnumType, sysDictTypeDict);
|
||||
|
||||
// 新增的枚举转换字典
|
||||
var newEnumType = enumTypeList.Where(u => !updatedEnumCodes.Contains(u.TypeName)).ToList();
|
||||
var (newDictTypes, newDictDatas) = GetNewSysDicts(newEnumType);
|
||||
|
||||
// 执行数据库操作
|
||||
if (updatedDictTypes.Count > 0)
|
||||
await _db.Updateable(updatedDictTypes).ExecuteCommandAsync();
|
||||
|
||||
if (updatedDictDatas.Count > 0)
|
||||
await _db.Updateable(updatedDictDatas).ExecuteCommandAsync();
|
||||
|
||||
if (newSysDictDatas.Count > 0)
|
||||
{
|
||||
// 达梦:用db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入
|
||||
// 达梦:不支持storageable2.BulkUpdateAsync 功能
|
||||
foreach (var dd in newSysDictDatas)
|
||||
await _db.Insertable(dd).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
if (newDictTypes.Count > 0)
|
||||
await _db.Insertable(newDictTypes).ExecuteCommandAsync();
|
||||
|
||||
if (newDictDatas.Count > 0)
|
||||
{
|
||||
// 达梦:用db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入
|
||||
// 达梦:不支持storageable2.BulkUpdateAsync 功能
|
||||
foreach (var dd in newDictDatas)
|
||||
await _db.Insertable(dd).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取需要新增的字典列表
|
||||
/// </summary>
|
||||
/// <param name="addEnumType"></param>
|
||||
/// <returns>
|
||||
/// 一个元组,包含以下元素:
|
||||
/// <list type="table">
|
||||
/// <item><term>SysDictTypes</term><description>字典类型列表</description></item>
|
||||
/// <item><term>SysDictDatas</term><description>字典数据列表</description></item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
private (List<SysDictType>, List<SysDictData>) GetNewSysDicts(List<EnumTypeOutput> addEnumType)
|
||||
{
|
||||
var newDictType = new List<SysDictType>();
|
||||
var newDictData = new List<SysDictData>();
|
||||
if (addEnumType.Count <= 0)
|
||||
return (newDictType, newDictData);
|
||||
|
||||
// 新增字典类型
|
||||
newDictType = addEnumType.Select(u => new SysDictType
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
Code = u.TypeName,
|
||||
Name = u.TypeDescribe,
|
||||
Remark = u.TypeRemark,
|
||||
Status = StatusEnum.Enable,
|
||||
SysFlag = YesNoEnum.Y,
|
||||
}).ToList();
|
||||
|
||||
// 新增字典数据
|
||||
newDictData = addEnumType.Join(newDictType, t1 => t1.TypeName, t2 => t2.Code, (t1, t2) => new
|
||||
{
|
||||
Data = t1.EnumEntities.Select(u => new SysDictData
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
DictTypeId = t2.Id,
|
||||
Code = u.Name,
|
||||
Label = u.Describe,
|
||||
Value = u.Value.ToString(),
|
||||
Remark = t2.Remark,
|
||||
OrderNo = u.Value + OrderOffset,
|
||||
TagType = u.Theme != "" ? u.Theme : DefaultTagType,
|
||||
}).ToList()
|
||||
}).SelectMany(x => x.Data).ToList();
|
||||
|
||||
return (newDictType, newDictData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取需要更新的字典列表
|
||||
/// </summary>
|
||||
/// <param name="updatedEnumType"></param>
|
||||
/// <param name="sysDictTypeDict"></param>
|
||||
/// <returns>
|
||||
/// 一个元组,包含以下元素:
|
||||
/// <list type="table">
|
||||
/// <item><term>SysDictTypes</term><description>更新字典类型列表</description>
|
||||
/// </item>
|
||||
/// <item><term>SysDictDatas</term><description>更新字典数据列表</description>
|
||||
/// </item>
|
||||
/// <item><term>SysDictDatas</term><description>新增字典数据列表</description>
|
||||
/// </item>
|
||||
/// </list>
|
||||
/// </returns>
|
||||
private (List<SysDictType>, List<SysDictData>, List<SysDictData>) GetUpdatedDicts(List<EnumTypeOutput> updatedEnumType, Dictionary<string, SysDictType> sysDictTypeDict)
|
||||
{
|
||||
var updatedSysDictTypes = new List<SysDictType>();
|
||||
var updatedSysDictData = new List<SysDictData>();
|
||||
var newSysDictData = new List<SysDictData>();
|
||||
foreach (var e in updatedEnumType)
|
||||
{
|
||||
if (!sysDictTypeDict.TryGetValue(e.TypeName, out var value))
|
||||
continue;
|
||||
|
||||
var updatedDictType = value;
|
||||
updatedDictType.Name = e.TypeDescribe;
|
||||
updatedDictType.Remark = e.TypeRemark;
|
||||
updatedSysDictTypes.Add(updatedDictType);
|
||||
var updatedDictData = updatedDictType.Children.Where(u => u.DictTypeId == updatedDictType.Id).ToList();
|
||||
|
||||
// 遍历需要更新的字典数据
|
||||
foreach (var dictData in updatedDictData)
|
||||
{
|
||||
var enumData = e.EnumEntities.FirstOrDefault(u => dictData.Code == u.Name);
|
||||
if (enumData != null)
|
||||
{
|
||||
dictData.Code = enumData.Name;
|
||||
dictData.Label = enumData.Describe;
|
||||
dictData.Value = enumData.Value.ToString();
|
||||
dictData.OrderNo = enumData.Value + OrderOffset;
|
||||
dictData.TagType = enumData.Theme != "" ? enumData.Theme : dictData.TagType != "" ? dictData.TagType : DefaultTagType;
|
||||
updatedSysDictData.Add(dictData);
|
||||
}
|
||||
}
|
||||
|
||||
// 新增的枚举值名称列表
|
||||
var newEnumDataNameList = e.EnumEntities.Select(u => u.Name).Except(updatedDictData.Select(u => u.Code));
|
||||
foreach (var newEnumDataName in newEnumDataNameList)
|
||||
{
|
||||
var enumData = e.EnumEntities.FirstOrDefault(u => newEnumDataName == u.Name);
|
||||
if (enumData != null)
|
||||
{
|
||||
var dictData = new SysDictData
|
||||
{
|
||||
Id = YitIdHelper.NextId(),
|
||||
DictTypeId = updatedDictType.Id,
|
||||
Code = enumData.Name,
|
||||
Label = enumData.Describe,
|
||||
Value = enumData.Value.ToString(),
|
||||
Remark = updatedDictType.Remark,
|
||||
OrderNo = enumData.Value + OrderOffset,
|
||||
TagType = enumData.Theme != "" ? enumData.Theme : DefaultTagType,
|
||||
};
|
||||
dictData.TagType = enumData.Theme != "" ? enumData.Theme : dictData.TagType != "" ? dictData.TagType : DefaultTagType;
|
||||
newSysDictData.Add(dictData);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除的情况暂不处理
|
||||
}
|
||||
|
||||
return (updatedSysDictTypes, updatedSysDictData, newSysDictData);
|
||||
}
|
||||
}
|
||||
@ -107,4 +107,32 @@ public class SysOnlineUserService : IDynamicApiController, ITransient
|
||||
await ForceOffline(user);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理在线用户(开启单设备登录时只留相同账号最后登录的)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[DisplayName("清理在线用户")]
|
||||
public async Task ClearOnline()
|
||||
{
|
||||
if (await _sysConfigService.GetConfigValueByCode<bool>(ConfigConst.SysSingleLogin)) return;
|
||||
|
||||
// 相同账号最后登录的用户Id集合
|
||||
var onlineUserIds = await _sysOnlineUerRep.AsQueryable().GroupBy(u => u.UserId)
|
||||
.Select(u => new
|
||||
{
|
||||
UserId = u.UserId,
|
||||
Count = SqlFunc.AggregateCount(u.UserId),
|
||||
Id = SqlFunc.AggregateMax(u.Id)
|
||||
})
|
||||
.ToListAsync(u => u.Id);
|
||||
|
||||
// 无效登录用户集合
|
||||
var offlineUsers = await _sysOnlineUerRep.AsQueryable().Where(u => !onlineUserIds.Contains(u.Id)).ToListAsync();
|
||||
foreach (var user in offlineUsers)
|
||||
{
|
||||
await ForceOffline(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -247,6 +247,9 @@ public class Startup : AppStartup
|
||||
|
||||
// 注册虚拟文件系统服务
|
||||
services.AddVirtualFileServer();
|
||||
|
||||
// 注册启动执行任务
|
||||
services.AddHostedService<StartHostedService>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user