删除重复的枚举扩展
添加Theme到字典数据库 更新字典数据库
This commit is contained in:
parent
c556ed05a3
commit
139f000b39
@ -1,45 +1,22 @@
|
|||||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
//
|
//
|
||||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
//
|
//
|
||||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
namespace Admin.NET.Core;
|
namespace Admin.NET.Core;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举拓展主题样式
|
/// 枚举拓展主题样式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SuppressSniffer]
|
[SuppressSniffer]
|
||||||
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
|
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
|
||||||
public class ThemeAttribute : Attribute
|
public class ThemeAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string Theme { get; private set; }
|
public string Theme { get; private set; }
|
||||||
|
|
||||||
public ThemeAttribute(string theme)
|
public ThemeAttribute(string theme)
|
||||||
{
|
{
|
||||||
this.Theme = theme;
|
this.Theme = theme;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static class ThemeExtend
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 扩展方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="enumValue"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string GetTheme(this Enum enumValue)
|
|
||||||
{
|
|
||||||
Type type = enumValue.GetType();
|
|
||||||
FieldInfo field = type.GetField(enumValue.ToString());
|
|
||||||
if (field.IsDefined(typeof(ThemeAttribute), true))
|
|
||||||
{
|
|
||||||
var themeAttribute = (ThemeAttribute)field.GetCustomAttribute(typeof(ThemeAttribute));
|
|
||||||
return themeAttribute.Theme;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,242 +1,242 @@
|
|||||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
//
|
//
|
||||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
//
|
//
|
||||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
namespace Admin.NET.Core;
|
namespace Admin.NET.Core;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举拓展
|
/// 枚举拓展
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class EnumExtension
|
public static class EnumExtension
|
||||||
{
|
{
|
||||||
// 枚举显示字典缓存
|
// 枚举显示字典缓存
|
||||||
private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumDisplayValueDict = new();
|
private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumDisplayValueDict = new();
|
||||||
|
|
||||||
// 枚举值字典缓存
|
// 枚举值字典缓存
|
||||||
private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumNameValueDict = new();
|
private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumNameValueDict = new();
|
||||||
|
|
||||||
// 枚举类型缓存
|
// 枚举类型缓存
|
||||||
private static ConcurrentDictionary<string, Type> _enumTypeDict;
|
private static ConcurrentDictionary<string, Type> _enumTypeDict;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举对象Key与名称的字典(缓存)
|
/// 获取枚举对象Key与名称的字典(缓存)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="enumType"></param>
|
/// <param name="enumType"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static Dictionary<int, string> GetEnumDictionary(this Type enumType)
|
public static Dictionary<int, string> GetEnumDictionary(this Type enumType)
|
||||||
{
|
{
|
||||||
if (!enumType.IsEnum)
|
if (!enumType.IsEnum)
|
||||||
throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
|
throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
|
||||||
|
|
||||||
// 查询缓存
|
// 查询缓存
|
||||||
var enumDic = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary<int, string>();
|
var enumDic = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary<int, string>();
|
||||||
if (enumDic.Count != 0)
|
if (enumDic.Count != 0)
|
||||||
return enumDic;
|
return enumDic;
|
||||||
// 取枚举类型的Key/Value字典集合
|
// 取枚举类型的Key/Value字典集合
|
||||||
enumDic = GetEnumDictionaryItems(enumType);
|
enumDic = GetEnumDictionaryItems(enumType);
|
||||||
|
|
||||||
// 缓存
|
// 缓存
|
||||||
EnumNameValueDict[enumType] = enumDic;
|
EnumNameValueDict[enumType] = enumDic;
|
||||||
|
|
||||||
return enumDic;
|
return enumDic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举对象Key与名称的字典
|
/// 获取枚举对象Key与名称的字典
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="enumType"></param>
|
/// <param name="enumType"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static Dictionary<int, string> GetEnumDictionaryItems(this Type enumType)
|
private static Dictionary<int, string> GetEnumDictionaryItems(this Type enumType)
|
||||||
{
|
{
|
||||||
// 获取类型的字段,初始化一个有限长度的字典
|
// 获取类型的字段,初始化一个有限长度的字典
|
||||||
var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||||
Dictionary<int, string> enumDic = new(enumFields.Length);
|
Dictionary<int, string> enumDic = new(enumFields.Length);
|
||||||
|
|
||||||
// 遍历字段数组获取key和name
|
// 遍历字段数组获取key和name
|
||||||
foreach (var enumField in enumFields)
|
foreach (var enumField in enumFields)
|
||||||
{
|
{
|
||||||
var intValue = (int)enumField.GetValue(enumType);
|
var intValue = (int)enumField.GetValue(enumType);
|
||||||
enumDic[intValue] = enumField.Name;
|
enumDic[intValue] = enumField.Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return enumDic;
|
return enumDic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举类型key与描述的字典(缓存)
|
/// 获取枚举类型key与描述的字典(缓存)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="enumType"></param>
|
/// <param name="enumType"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
public static Dictionary<int, string> GetEnumDescDictionary(this Type enumType)
|
public static Dictionary<int, string> GetEnumDescDictionary(this Type enumType)
|
||||||
{
|
{
|
||||||
if (!enumType.IsEnum)
|
if (!enumType.IsEnum)
|
||||||
throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
|
throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
|
||||||
|
|
||||||
// 查询缓存
|
// 查询缓存
|
||||||
var enumDic = EnumDisplayValueDict.ContainsKey(enumType)
|
var enumDic = EnumDisplayValueDict.ContainsKey(enumType)
|
||||||
? EnumDisplayValueDict[enumType]
|
? EnumDisplayValueDict[enumType]
|
||||||
: new Dictionary<int, string>();
|
: new Dictionary<int, string>();
|
||||||
if (enumDic.Count != 0)
|
if (enumDic.Count != 0)
|
||||||
return enumDic;
|
return enumDic;
|
||||||
// 取枚举类型的Key/Value字典集合
|
// 取枚举类型的Key/Value字典集合
|
||||||
enumDic = GetEnumDescDictionaryItems(enumType);
|
enumDic = GetEnumDescDictionaryItems(enumType);
|
||||||
|
|
||||||
// 缓存
|
// 缓存
|
||||||
EnumDisplayValueDict[enumType] = enumDic;
|
EnumDisplayValueDict[enumType] = enumDic;
|
||||||
|
|
||||||
return enumDic;
|
return enumDic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举类型key与描述的字典(没有描述则获取name)
|
/// 获取枚举类型key与描述的字典(没有描述则获取name)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="enumType"></param>
|
/// <param name="enumType"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
private static Dictionary<int, string> GetEnumDescDictionaryItems(this Type enumType)
|
private static Dictionary<int, string> GetEnumDescDictionaryItems(this Type enumType)
|
||||||
{
|
{
|
||||||
// 获取类型的字段,初始化一个有限长度的字典
|
// 获取类型的字段,初始化一个有限长度的字典
|
||||||
var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||||
Dictionary<int, string> enumDic = new(enumFields.Length);
|
Dictionary<int, string> enumDic = new(enumFields.Length);
|
||||||
|
|
||||||
// 遍历字段数组获取key和name
|
// 遍历字段数组获取key和name
|
||||||
foreach (var enumField in enumFields)
|
foreach (var enumField in enumFields)
|
||||||
{
|
{
|
||||||
var intValue = (int)enumField.GetValue(enumType);
|
var intValue = (int)enumField.GetValue(enumType);
|
||||||
var desc = enumField.GetDescriptionValue<DescriptionAttribute>();
|
var desc = enumField.GetDescriptionValue<DescriptionAttribute>();
|
||||||
enumDic[intValue] = desc != null && !string.IsNullOrEmpty(desc.Description) ? desc.Description : enumField.Name;
|
enumDic[intValue] = desc != null && !string.IsNullOrEmpty(desc.Description) ? desc.Description : enumField.Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return enumDic;
|
return enumDic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从程序集中查找指定枚举类型
|
/// 从程序集中查找指定枚举类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="assembly"></param>
|
/// <param name="assembly"></param>
|
||||||
/// <param name="typeName"></param>
|
/// <param name="typeName"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static Type TryToGetEnumType(Assembly assembly, string typeName)
|
public static Type TryToGetEnumType(Assembly assembly, string typeName)
|
||||||
{
|
{
|
||||||
// 枚举缓存为空则重新加载枚举类型字典
|
// 枚举缓存为空则重新加载枚举类型字典
|
||||||
_enumTypeDict ??= LoadEnumTypeDict(assembly);
|
_enumTypeDict ??= LoadEnumTypeDict(assembly);
|
||||||
|
|
||||||
// 按名称查找
|
// 按名称查找
|
||||||
return _enumTypeDict.ContainsKey(typeName) ? _enumTypeDict[typeName] : null;
|
return _enumTypeDict.ContainsKey(typeName) ? _enumTypeDict[typeName] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从程序集中加载所有枚举类型
|
/// 从程序集中加载所有枚举类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="assembly"></param>
|
/// <param name="assembly"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static ConcurrentDictionary<string, Type> LoadEnumTypeDict(Assembly assembly)
|
private static ConcurrentDictionary<string, Type> LoadEnumTypeDict(Assembly assembly)
|
||||||
{
|
{
|
||||||
// 取程序集中所有类型
|
// 取程序集中所有类型
|
||||||
var typeArray = assembly.GetTypes();
|
var typeArray = assembly.GetTypes();
|
||||||
|
|
||||||
// 过滤非枚举类型,转成字典格式并返回
|
// 过滤非枚举类型,转成字典格式并返回
|
||||||
var dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
|
var dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
|
||||||
ConcurrentDictionary<string, Type> enumTypeDict = new(dict);
|
ConcurrentDictionary<string, Type> enumTypeDict = new(dict);
|
||||||
return enumTypeDict;
|
return enumTypeDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举的Description
|
/// 获取枚举的Description
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetDescription(this System.Enum value)
|
public static string GetDescription(this System.Enum value)
|
||||||
{
|
{
|
||||||
return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()
|
return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()
|
||||||
?.Description;
|
?.Description;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举的Description
|
/// 获取枚举的Description
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetDescription(this object value)
|
public static string GetDescription(this object value)
|
||||||
{
|
{
|
||||||
return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()
|
return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()
|
||||||
?.GetCustomAttribute<DescriptionAttribute>()?.Description;
|
?.GetCustomAttribute<DescriptionAttribute>()?.Description;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举的Theme
|
/// 获取枚举的Theme
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetTheme(this object value)
|
public static string GetTheme(this object value)
|
||||||
{
|
{
|
||||||
return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()
|
return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()
|
||||||
?.GetCustomAttribute<ThemeAttribute>()?.Theme;
|
?.GetCustomAttribute<ThemeAttribute>()?.Theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将枚举转成枚举信息集合
|
/// 将枚举转成枚举信息集合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static List<EnumEntity> EnumToList(this Type type)
|
public static List<EnumEntity> EnumToList(this Type type)
|
||||||
{
|
{
|
||||||
if (!type.IsEnum)
|
if (!type.IsEnum)
|
||||||
throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
|
throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
|
||||||
var arr = System.Enum.GetNames(type);
|
var arr = System.Enum.GetNames(type);
|
||||||
return arr.Select(sl =>
|
return arr.Select(sl =>
|
||||||
{
|
{
|
||||||
var item = System.Enum.Parse(type, sl);
|
var item = System.Enum.Parse(type, sl);
|
||||||
return new EnumEntity
|
return new EnumEntity
|
||||||
{
|
{
|
||||||
Name = item.ToString(),
|
Name = item.ToString(),
|
||||||
Describe = item.GetDescription() ?? item.ToString(),
|
Describe = item.GetDescription() ?? item.ToString(),
|
||||||
Theme = item.GetTheme() ?? string.Empty,
|
Theme = item.GetTheme() ?? string.Empty,
|
||||||
Value = item.GetHashCode()
|
Value = item.GetHashCode()
|
||||||
};
|
};
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举ToList
|
/// 枚举ToList
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static List<T> EnumToList<T>(this Type type)
|
public static List<T> EnumToList<T>(this Type type)
|
||||||
{
|
{
|
||||||
if (!type.IsEnum)
|
if (!type.IsEnum)
|
||||||
throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
|
throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
|
||||||
var arr = System.Enum.GetNames(type);
|
var arr = System.Enum.GetNames(type);
|
||||||
return arr.Select(name => (T)System.Enum.Parse(type, name)).ToList();
|
return arr.Select(name => (T)System.Enum.Parse(type, name)).ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举实体
|
/// 枚举实体
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class EnumEntity
|
public class EnumEntity
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举的描述
|
/// 枚举的描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Describe { set; get; }
|
public string Describe { set; get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举的样式
|
/// 枚举的样式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Theme { set; get; }
|
public string Theme { set; get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举名称
|
/// 枚举名称
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Name { set; get; }
|
public string Name { set; get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举对象的值
|
/// 枚举对象的值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Value { set; get; }
|
public int Value { set; get; }
|
||||||
}
|
}
|
||||||
@ -1,149 +1,150 @@
|
|||||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
//
|
//
|
||||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
//
|
//
|
||||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
namespace Admin.NET.Core;
|
namespace Admin.NET.Core;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 枚举转字典
|
/// 枚举转字典
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JobDetail("job_EnumToDictJob", Description = "枚举转字典", GroupName = "default", Concurrent = false)]
|
[JobDetail("job_EnumToDictJob", Description = "枚举转字典", GroupName = "default", Concurrent = false)]
|
||||||
[PeriodSeconds(1, TriggerId = "trigger_EnumToDictJob", Description = "枚举转字典", MaxNumberOfRuns = 1, RunOnStart = true)]
|
[PeriodSeconds(1, TriggerId = "trigger_EnumToDictJob", Description = "枚举转字典", MaxNumberOfRuns = 1, RunOnStart = true)]
|
||||||
public class EnumToDictJob : IJob
|
public class EnumToDictJob : IJob
|
||||||
{
|
{
|
||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
private readonly IJsonSerializerProvider _jsonSerializer;
|
private readonly IJsonSerializerProvider _jsonSerializer;
|
||||||
|
|
||||||
public EnumToDictJob(IServiceScopeFactory scopeFactory, IJsonSerializerProvider jsonSerializer)
|
public EnumToDictJob(IServiceScopeFactory scopeFactory, IJsonSerializerProvider jsonSerializer)
|
||||||
{
|
{
|
||||||
_scopeFactory = scopeFactory;
|
_scopeFactory = scopeFactory;
|
||||||
_jsonSerializer = jsonSerializer;
|
_jsonSerializer = jsonSerializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
using var serviceScope = _scopeFactory.CreateScope();
|
using var serviceScope = _scopeFactory.CreateScope();
|
||||||
var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
||||||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
|
||||||
|
|
||||||
var enumTypeList = sysEnumService.GetEnumTypeList();
|
var enumTypeList = sysEnumService.GetEnumTypeList();
|
||||||
var enumCodeList = enumTypeList.Select(u => u.TypeName);
|
var enumCodeList = enumTypeList.Select(u => u.TypeName);
|
||||||
var sysDictTypeCodeList = await db.Queryable<SysDictType>().Where(u => enumCodeList.Contains(u.Code)).Select(u => u.Code).ToListAsync(stoppingToken);
|
var sysDictTypeCodeList = await db.Queryable<SysDictType>().Where(u => enumCodeList.Contains(u.Code)).Select(u => u.Code).ToListAsync(stoppingToken);
|
||||||
|
|
||||||
// 更新的枚举转换字典
|
// 更新的枚举转换字典
|
||||||
var uEnumType = enumTypeList.Where(u => sysDictTypeCodeList.Contains(u.TypeName)).ToList();
|
var uEnumType = enumTypeList.Where(u => sysDictTypeCodeList.Contains(u.TypeName)).ToList();
|
||||||
var waitUpdateSysDictType = await db.Queryable<SysDictType>().Where(u => uEnumType.Any(a => a.TypeName == u.Code)).ToListAsync(stoppingToken);
|
var waitUpdateSysDictType = await db.Queryable<SysDictType>().Where(u => uEnumType.Any(a => a.TypeName == u.Code)).ToListAsync(stoppingToken);
|
||||||
var waitUpdateSysDictTypeDict = waitUpdateSysDictType.ToDictionary(u => u.Code, u => u);
|
var waitUpdateSysDictTypeDict = waitUpdateSysDictType.ToDictionary(u => u.Code, u => u);
|
||||||
var waitUpdateSysDictData = await db.Queryable<SysDictData>().Where(u => uEnumType.Any(a => a.TypeName == u.DictType.Code)).ToListAsync(stoppingToken);
|
var waitUpdateSysDictData = await db.Queryable<SysDictData>().Where(u => uEnumType.Any(a => a.TypeName == u.DictType.Code)).ToListAsync(stoppingToken);
|
||||||
var uSysDictType = new List<SysDictType>();
|
var uSysDictType = new List<SysDictType>();
|
||||||
var uSysDictData = new List<SysDictData>();
|
var uSysDictData = new List<SysDictData>();
|
||||||
if (uEnumType.Count > 0)
|
if (uEnumType.Count > 0)
|
||||||
{
|
{
|
||||||
uEnumType.ForEach(e =>
|
uEnumType.ForEach(e =>
|
||||||
{
|
{
|
||||||
if (waitUpdateSysDictTypeDict.TryGetValue(e.TypeName, out SysDictType value))
|
if (waitUpdateSysDictTypeDict.TryGetValue(e.TypeName, out SysDictType value))
|
||||||
{
|
{
|
||||||
var uDictType = value;
|
var uDictType = value;
|
||||||
uDictType.Name = e.TypeDescribe;
|
uDictType.Name = e.TypeDescribe;
|
||||||
uDictType.Remark = e.TypeRemark;
|
uDictType.Remark = e.TypeRemark;
|
||||||
var uDictData = waitUpdateSysDictData.Where(u => u.DictTypeId == uDictType.Id).ToList();
|
var uDictData = waitUpdateSysDictData.Where(u => u.DictTypeId == uDictType.Id).ToList();
|
||||||
if (uDictData.Count > 0)
|
if (uDictData.Count > 0)
|
||||||
{
|
{
|
||||||
uDictData.ForEach(dictData =>
|
uDictData.ForEach(dictData =>
|
||||||
{
|
{
|
||||||
var enumData = e.EnumEntities.Where(u => dictData.Code == u.Name).FirstOrDefault();
|
var enumData = e.EnumEntities.Where(u => dictData.Code == u.Name).FirstOrDefault();
|
||||||
if (enumData != null)
|
if (enumData != null)
|
||||||
{
|
{
|
||||||
dictData.Value = enumData.Value.ToString();
|
dictData.Value = enumData.Value.ToString();
|
||||||
dictData.Code = enumData.Name;
|
dictData.Code = enumData.Name;
|
||||||
dictData.OrderNo = enumData.Value + 10;
|
dictData.OrderNo = enumData.Value + 10;
|
||||||
dictData.Name = enumData.Describe;
|
dictData.Name = enumData.Describe;
|
||||||
uSysDictData.Add(dictData);
|
dictData.TagType = enumData.Theme;
|
||||||
}
|
uSysDictData.Add(dictData);
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
if (!uSysDictType.Any(u => u.Id == uDictType.Id))
|
}
|
||||||
uSysDictType.Add(uDictType);
|
if (!uSysDictType.Any(u => u.Id == uDictType.Id))
|
||||||
}
|
uSysDictType.Add(uDictType);
|
||||||
});
|
}
|
||||||
try
|
});
|
||||||
{
|
try
|
||||||
db.BeginTran();
|
{
|
||||||
|
db.BeginTran();
|
||||||
if (uSysDictType.Count > 0)
|
|
||||||
await db.Updateable(uSysDictType).ExecuteCommandAsync(stoppingToken);
|
if (uSysDictType.Count > 0)
|
||||||
|
await db.Updateable(uSysDictType).ExecuteCommandAsync(stoppingToken);
|
||||||
if (uSysDictData.Count > 0)
|
|
||||||
await db.Updateable(uSysDictData).ExecuteCommandAsync(stoppingToken);
|
if (uSysDictData.Count > 0)
|
||||||
|
await db.Updateable(uSysDictData).ExecuteCommandAsync(stoppingToken);
|
||||||
db.CommitTran();
|
|
||||||
}
|
db.CommitTran();
|
||||||
catch (Exception error)
|
}
|
||||||
{
|
catch (Exception error)
|
||||||
db.RollbackTran();
|
{
|
||||||
Log.Error($"{context.Trigger.Description}更新枚举转换字典入库错误:" + _jsonSerializer.Serialize(error));
|
db.RollbackTran();
|
||||||
throw new Exception($"{context.Trigger.Description}更新枚举转换字典入库错误");
|
Log.Error($"{context.Trigger.Description}更新枚举转换字典入库错误:" + _jsonSerializer.Serialize(error));
|
||||||
}
|
throw new Exception($"{context.Trigger.Description}更新枚举转换字典入库错误");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 新增的枚举转换字典
|
|
||||||
var iEnumType = enumTypeList.Where(u => !sysDictTypeCodeList.Contains(u.TypeName)).ToList();
|
// 新增的枚举转换字典
|
||||||
if (iEnumType.Count > 0)
|
var iEnumType = enumTypeList.Where(u => !sysDictTypeCodeList.Contains(u.TypeName)).ToList();
|
||||||
{
|
if (iEnumType.Count > 0)
|
||||||
// 新增字典类型
|
{
|
||||||
var iDictType = iEnumType.Select(u => new SysDictType
|
// 新增字典类型
|
||||||
{
|
var iDictType = iEnumType.Select(u => new SysDictType
|
||||||
Id = YitIdHelper.NextId(),
|
{
|
||||||
Code = u.TypeName,
|
Id = YitIdHelper.NextId(),
|
||||||
Name = u.TypeDescribe,
|
Code = u.TypeName,
|
||||||
Remark = u.TypeRemark,
|
Name = u.TypeDescribe,
|
||||||
Status = StatusEnum.Enable
|
Remark = u.TypeRemark,
|
||||||
}).ToList();
|
Status = StatusEnum.Enable
|
||||||
// 新增字典数据
|
}).ToList();
|
||||||
var dictData = iEnumType.Join(iDictType, t1 => t1.TypeName, t2 => t2.Code, (t1, t2) => new
|
// 新增字典数据
|
||||||
{
|
var dictData = iEnumType.Join(iDictType, t1 => t1.TypeName, t2 => t2.Code, (t1, t2) => new
|
||||||
data = t1.EnumEntities.Select(u => new SysDictData
|
{
|
||||||
{
|
data = t1.EnumEntities.Select(u => new SysDictData
|
||||||
Id = YitIdHelper.NextId(), // 性能优化,使用BulkCopyAsync必须手动获取Id
|
{
|
||||||
DictTypeId = t2.Id,
|
Id = YitIdHelper.NextId(), // 性能优化,使用BulkCopyAsync必须手动获取Id
|
||||||
Name = u.Describe,
|
DictTypeId = t2.Id,
|
||||||
Value = u.Value.ToString(),
|
Name = u.Describe,
|
||||||
Code = u.Name,
|
Value = u.Value.ToString(),
|
||||||
Remark = t2.Remark,
|
Code = u.Name,
|
||||||
OrderNo = u.Value + 10,
|
Remark = t2.Remark,
|
||||||
TagType = "info"
|
OrderNo = u.Value + 10,
|
||||||
}).ToList()
|
TagType = u.Theme ?? "info"
|
||||||
}).ToList();
|
}).ToList()
|
||||||
var iDictData = new List<SysDictData>();
|
}).ToList();
|
||||||
dictData.ForEach(item =>
|
var iDictData = new List<SysDictData>();
|
||||||
{
|
dictData.ForEach(item =>
|
||||||
iDictData.AddRange(item.data);
|
{
|
||||||
});
|
iDictData.AddRange(item.data);
|
||||||
try
|
});
|
||||||
{
|
try
|
||||||
db.BeginTran();
|
{
|
||||||
|
db.BeginTran();
|
||||||
if (iDictType.Count > 0)
|
|
||||||
await db.Insertable(iDictType).ExecuteCommandAsync(stoppingToken);
|
if (iDictType.Count > 0)
|
||||||
|
await db.Insertable(iDictType).ExecuteCommandAsync(stoppingToken);
|
||||||
if (iDictData.Count > 0)
|
|
||||||
await db.Insertable(iDictData).ExecuteCommandAsync(stoppingToken);
|
if (iDictData.Count > 0)
|
||||||
|
await db.Insertable(iDictData).ExecuteCommandAsync(stoppingToken);
|
||||||
db.CommitTran();
|
|
||||||
}
|
db.CommitTran();
|
||||||
catch (Exception error)
|
}
|
||||||
{
|
catch (Exception error)
|
||||||
db.RollbackTran();
|
{
|
||||||
Log.Error($"{context.Trigger.Description}新增枚举转换字典入库错误:" + _jsonSerializer.Serialize(error));
|
db.RollbackTran();
|
||||||
throw new Exception($"{context.Trigger.Description}新增枚举转换字典入库错误");
|
Log.Error($"{context.Trigger.Description}新增枚举转换字典入库错误:" + _jsonSerializer.Serialize(error));
|
||||||
}
|
throw new Exception($"{context.Trigger.Description}新增枚举转换字典入库错误");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
var originColor = Console.ForegroundColor;
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
var originColor = Console.ForegroundColor;
|
||||||
Console.WriteLine($"【{DateTime.Now}】系统枚举转换字典");
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.ForegroundColor = originColor;
|
Console.WriteLine($"【{DateTime.Now}】系统枚举转换字典");
|
||||||
}
|
Console.ForegroundColor = originColor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user