using Newtonsoft.Json.Linq; using PTMedicalInsurance.Entity; using PTMedicalInsurance.Helper; using PTMedicalInsurance.Variables; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace PTMedicalInsurance.Common { public static class AppExtension { /// /// 获取交易枚举对于的code信息 /// public static string GetCode(this TradeEnum tradeEnum) { Type type = tradeEnum.GetType(); FieldInfo fd = type.GetField(tradeEnum.ToString()); return getFieldCode(fd); } /// /// 获取交易描述信息 /// /// /// public static string GetName(this TradeEnum tradeEnum) { Type type = tradeEnum.GetType(); FieldInfo fd = type.GetField(tradeEnum.ToString()); return getFieldName(fd); } public static ModeEnum GetMode(this TradeEnum tradeEnum) { Type type = tradeEnum.GetType(); FieldInfo fd = type.GetField(tradeEnum.ToString()); TradeAttribute attribute = getAttribte(fd); if (attribute != null) return attribute.Mode; // 默认值 return ModeEnum.REST; } public static TradeEnum GetByCode(this TradeEnum tradeEnum,string code) { Type type = tradeEnum.GetType(); FieldInfo[] fds = type.GetFields(); foreach (FieldInfo fd in fds) { TradeAttribute attribute = getAttribte(fd); if (code.Equals(attribute?.Code)) { return (TradeEnum)fd.GetValue(tradeEnum); } } return TradeEnum.DEFAULT; } private static TradeAttribute getAttribte(FieldInfo fd) { TradeAttribute attribute = null; if (fd == null) { return attribute; } var attributes = fd.GetCustomAttributes(typeof(TradeAttribute), false); if (attributes != null && attributes.Length == 1) { attribute = attributes.Single() as TradeAttribute; return attribute; } return attribute; } public static string Text(this JToken jtoken) { if (jtoken == null) return string.Empty; return jtoken.ToString(); } /// /// 转换为前端需要的对象字符串 /// /// /// public static string ParseReturnObject(this string text) { if (string.IsNullOrEmpty(text) || !text.StartsWith("{")) { return JsonHelper.setIrisReturnValue(-1, text, null).ToString(); } JObject joRtn = JObject.Parse(text); // irisReturn格式 if (joRtn.ContainsKey("errorCode")) { return text; } return JsonHelper.setIrisReturnValue(0, "", joRtn).ToString(); } public static int Int(this JToken jtoken) { if (jtoken == null) return 0; return int.Parse(jtoken.ToString()); } private static string getFieldCode(FieldInfo fd) { TradeAttribute attribute = getAttribte(fd); return attribute?.Code; } private static string getFieldName(FieldInfo fd) { TradeAttribute attribute = getAttribte(fd); return attribute?.Name; } public static void Extend(this JObject jObject,string key,object value) { if (jObject != null && !jObject.ContainsKey(key)) { jObject.Add(key, new JValue(value)); } } /// /// 将实体类转换为数据库入参(Json) /// public static JObject ConvertToDBJson(this IDbProperty db) { Type type = db.GetType(); var properties = type.GetProperties(); JObject joRtn = new JObject(); foreach (var property in properties) { var attribute = property.GetCustomAttribute(); string fieldName = attribute != null ? attribute.Name : ""; // 使用 BindingFlags 来允许访问非公共属性 object value = property.GetValue(db, null); // 保持原始数据类型 JToken token = value == null ? JValue.CreateNull() : JToken.FromObject(value); joRtn.Add(fieldName, token); } return joRtn; } } }