using FreeRedis;
using Learn.Archives.Core.Model;
using Learn.Archives.Core.Model.Interface;
using Microsoft.Extensions.DependencyModel;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;
using SqlSugar.IOC;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Runtime.Loader;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UserCenter.Model.Interface;
namespace Learn.Archives.Core.Common
{
///
/// 程序 公共变量
///
public static class AppCommon
{
///
/// 应用有效程序集
///
public static readonly IEnumerable Assemblies;
///
/// 主库数据库表类型
///
public static readonly IEnumerable DbMatserType;
public static readonly IEnumerable KnowsType;
static AppCommon()
{
try
{
Assemblies = ExpandFunction.GetAssemblies();
var assembliesType = Assemblies.Where(s => s.FullName.Contains("VideoAnalysis")).SelectMany(s => s.ExportedTypes
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)));
DbMatserType = assembliesType
.Where(u => u.GetInterfaces().Contains(typeof(IDB)));
}
catch
{
throw;
}
}
///
/// 程序配置
///
public static AppConfig Config = new AppConfig();
///
/// ServiceProvider
///
public static IServiceProvider? Services;
}
///
/// 拓展函数
///
public static class ExpandFunction
{
static Dictionary FormulaData;
static string FormulaDataKey;
///
/// 帧文件名称
///
public static string FrameName = "frame_";
///
/// 对象转化为JSON字符串
///
/// 拓展对象
/// 美化输出?
///
public static string ToJson(this object o, bool WriteIndented = false)
{
var jsonOptions = new JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = WriteIndented // 如果需要美化输出
};
return JsonSerializer.Serialize(o, jsonOptions);
}
///
/// 获取应用有效程序集
///
/// IEnumerable
public static List GetAssemblies()
{
// 获取当前解决方案的所有程序集
var assembliesStr = DependencyContext.Default.RuntimeLibraries
.Where(u => !u.Name.StartsWith(nameof(Microsoft))
&& !u.Name.StartsWith(nameof(System))
&& !u.Name.StartsWith("netstandard")
&& u.Type == "project");
var assemblies = assembliesStr.Select(a => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(a.Name))).ToList();
var assemblies1 = Assembly.GetEntryAssembly().GetReferencedAssemblies().Where(x => x.Name.StartsWith("App.") || x.Name.StartsWith("UserCenter."))
.Select(a => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(a.Name))).ToList();
foreach (var item in assemblies1)
{
if (!assemblies.Contains(item))
assemblies.Add(item);
}
return assemblies;
}
///
/// 获取下一个枚举值
///
///
///
///
///
public static T? NextEnum(this T current) where T : struct, Enum
{
if (!typeof(T).IsEnum)
throw new ArgumentException("传入类型不是枚举");
T[] values = (T[])Enum.GetValues(typeof(T));
int currentIndex = Array.IndexOf(values, current);
if (currentIndex == values.Length - 1)
return null;
int nextIndex = (currentIndex + 1) % values.Length;
return values[nextIndex];
}
///
/// 转化枚举
///
///
///
public static T? ToEnum(this object value) where T : struct, Enum
{
try
{
if (value is null || string.IsNullOrEmpty(value.ToString()))
return null;
if (Enum.TryParse(value.ToString(), true, out var result) && Enum.IsDefined(typeof(T), result))
return result;
return null;
}
catch (Exception)
{
return null;
}
}
}
}