using ComponentFactory.Krypton.Toolkit; using NXWMS.Client.Model.AppModels.Result; using NXWMS.Client.Model.CoreModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using NXWMS.Client.Code.Extends; using System.Reflection; using System.Data; using System.ComponentModel; namespace NXWMS.Commons { /// /// 通用工具 /// public static class CommonUtil { /// /// 客户端字段排序转换 /// /// /// /// public static Dictionary GetFieldOrderDic(this List clientFieldOrderList) { if (clientFieldOrderList != null) { Dictionary map = new Dictionary(); foreach (var item in clientFieldOrderList) { map.Add(item.FieldName, item.FieldDesc); } return map; } else { return new Dictionary(); } } /// /// 字典追加数据 /// /// /// /// /// public static Dictionary AddKeyValue(this Dictionary keyValues, string key, string value) { if (!keyValues.ContainsKey(key)) { keyValues.Add(key, value); } return keyValues; } /// /// 枚举列表Code FieldValue /// /// /// /// public static List GetFieldValueCodeList(this List fieldValueList) where TEnum : Enum { foreach (int value in Enum.GetValues(typeof(TEnum))) { string strName = Enum.GetName(typeof(TEnum), value); fieldValueList.Add(new FieldValue { Code = strName, Name = ((TEnum)Enum.Parse(typeof(TEnum), strName)).Description() }); } return fieldValueList; } /// /// 枚举列表Id FieldValue /// /// /// /// public static List GetFieldValueIdList(this List fieldValueList) where TEnum : Enum { foreach (int value in Enum.GetValues(typeof(TEnum))) { string strName = Enum.GetName(typeof(TEnum), value); fieldValueList.Add(new FieldValue { Id = value.ToString(), Name = ((TEnum)Enum.Parse(typeof(TEnum), strName)).Description() }); } return fieldValueList; } /// /// 获取obj对象与枚举比对整数结果 /// /// /// /// public static int? GetEnumInt(this object obj) where TEnum : Enum { if (obj == null) { return null; } Type type = typeof(TEnum); foreach (FieldInfo x in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { TEnum item = (TEnum)x.GetValue(null); if (item.Description() == obj.ToString()) { return Convert.ToInt32(x.GetValue(null)); } } return null; } /// /// 获取枚举描述信息 /// /// /// private static string Description(Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs != null && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return en.ToString(); } /// /// 获取obj对象与枚举比对字符串结果 /// /// /// /// public static string GetEnumString(this object obj) where TEnum : Enum { if (obj == null) { return null; } Type type = typeof(TEnum); foreach (FieldInfo x in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { TEnum item = (TEnum)x.GetValue(null); if (item.Description() == obj.ToString()) { return x.GetValue(null).ToString(); } } return null; } /// /// 实体列表code FieldValue /// /// /// /// /// /// /// public static List GetFieldValueCodeList(this List fieldValueList, List entityList, string code, string name) where T : class { foreach (var item in entityList) { var codeInfo = typeof(T).GetProperty(code); var nameInfo = typeof(T).GetProperty(name); fieldValueList.Add(new FieldValue { Code = codeInfo.GetValue(item, null).ToString(), Name = nameInfo.GetValue(item, null).ToString() }); } return fieldValueList; } /// /// 实体列表id FieldValue /// /// /// /// /// /// /// public static List GetFieldValueIdList(this List fieldValueList, List entityList, string id, string name) where T : class { foreach (var item in entityList) { var idInfo = typeof(T).GetProperty(id); var nameInfo = typeof(T).GetProperty(name); fieldValueList.Add(new FieldValue { Id = idInfo.GetValue(item, null).GetObjectToString(), Name = nameInfo.GetValue(item, null).GetObjectToString(), }); } return fieldValueList; } /// /// object 选择返回值 /// /// /// public static string GetObjectToString(this object obj) { if (obj == null) { return string.Empty; } if (obj.ToString() == "") { return string.Empty; } return obj.ToString(); } /// /// object 选择返回值 整数 /// /// /// public static int? GetObjectToInt(this object obj) { if (obj == null) { return (int?)null; } if (obj.ToString() == "") { return (int?)null; } return Convert.ToInt32(obj); } /// /// object 选择返回值 布尔 /// /// /// public static bool GetObjectToBoolean(this object obj) { if (obj == null) { return false; } if (obj.ToString() == "") { return false; } return Convert.ToBoolean(obj); } /// /// object 选择返回值 /// /// /// 对应True的值,其他未false /// public static bool GetObjectToBoolean(this object obj, string trueValue) { if (obj == null) { return false; } if (obj.ToString() == "") { return false; } if (obj.ToString().ToLower() == trueValue.ToLower()) { return true; } return false; } } }