123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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
- {
- /// <summary>
- /// 获取交易枚举对于的code信息
- /// </summary>
- public static string GetCode(this TradeEnum tradeEnum)
- {
- Type type = tradeEnum.GetType();
- FieldInfo fd = type.GetField(tradeEnum.ToString());
- return getFieldCode(fd);
- }
- /// <summary>
- /// 获取交易描述信息
- /// </summary>
- /// <param name="tradeEnum"></param>
- /// <returns></returns>
- 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();
- }
- /// <summary>
- /// 转换为前端需要的对象字符串
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- 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));
- }
- }
- /// <summary>
- /// 将实体类转换为数据库入参(Json)
- /// </summary>
- 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<ColumnAttribute>();
- 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;
- }
- }
- }
|