AppExtension.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Entity;
  3. using PTMedicalInsurance.Helper;
  4. using PTMedicalInsurance.Variables;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations.Schema;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace PTMedicalInsurance.Common
  14. {
  15. public static class AppExtension
  16. {
  17. /// <summary>
  18. /// 获取交易枚举对于的code信息
  19. /// </summary>
  20. public static string GetCode(this TradeEnum tradeEnum)
  21. {
  22. Type type = tradeEnum.GetType();
  23. FieldInfo fd = type.GetField(tradeEnum.ToString());
  24. return getFieldCode(fd);
  25. }
  26. /// <summary>
  27. /// 获取交易描述信息
  28. /// </summary>
  29. /// <param name="tradeEnum"></param>
  30. /// <returns></returns>
  31. public static string GetName(this TradeEnum tradeEnum)
  32. {
  33. Type type = tradeEnum.GetType();
  34. FieldInfo fd = type.GetField(tradeEnum.ToString());
  35. return getFieldName(fd);
  36. }
  37. public static ModeEnum GetMode(this TradeEnum tradeEnum)
  38. {
  39. Type type = tradeEnum.GetType();
  40. FieldInfo fd = type.GetField(tradeEnum.ToString());
  41. TradeAttribute attribute = getAttribte(fd);
  42. if (attribute != null) return attribute.Mode;
  43. // 默认值
  44. return ModeEnum.REST;
  45. }
  46. public static TradeEnum GetByCode(this TradeEnum tradeEnum,string code)
  47. {
  48. Type type = tradeEnum.GetType();
  49. FieldInfo[] fds = type.GetFields();
  50. foreach (FieldInfo fd in fds)
  51. {
  52. TradeAttribute attribute = getAttribte(fd);
  53. if (code.Equals(attribute?.Code))
  54. {
  55. return (TradeEnum)fd.GetValue(tradeEnum);
  56. }
  57. }
  58. return TradeEnum.DEFAULT;
  59. }
  60. private static TradeAttribute getAttribte(FieldInfo fd)
  61. {
  62. TradeAttribute attribute = null;
  63. if (fd == null)
  64. {
  65. return attribute;
  66. }
  67. var attributes = fd.GetCustomAttributes(typeof(TradeAttribute), false);
  68. if (attributes != null && attributes.Length == 1)
  69. {
  70. attribute = attributes.Single() as TradeAttribute;
  71. return attribute;
  72. }
  73. return attribute;
  74. }
  75. public static string Text(this JToken jtoken)
  76. {
  77. if (jtoken == null) return string.Empty;
  78. return jtoken.ToString();
  79. }
  80. /// <summary>
  81. /// 转换为前端需要的对象字符串
  82. /// </summary>
  83. /// <param name="text"></param>
  84. /// <returns></returns>
  85. public static string ParseReturnObject(this string text)
  86. {
  87. if (string.IsNullOrEmpty(text) || !text.StartsWith("{")) {
  88. return JsonHelper.setIrisReturnValue(-1, text, null).ToString();
  89. }
  90. JObject joRtn = JObject.Parse(text);
  91. // irisReturn格式
  92. if (joRtn.ContainsKey("errorCode"))
  93. {
  94. return text;
  95. }
  96. return JsonHelper.setIrisReturnValue(0, "", joRtn).ToString();
  97. }
  98. public static int Int(this JToken jtoken)
  99. {
  100. if (jtoken == null) return 0;
  101. return int.Parse(jtoken.ToString());
  102. }
  103. private static string getFieldCode(FieldInfo fd)
  104. {
  105. TradeAttribute attribute = getAttribte(fd);
  106. return attribute?.Code;
  107. }
  108. private static string getFieldName(FieldInfo fd)
  109. {
  110. TradeAttribute attribute = getAttribte(fd);
  111. return attribute?.Name;
  112. }
  113. public static void Extend(this JObject jObject,string key,object value)
  114. {
  115. if (jObject != null && !jObject.ContainsKey(key))
  116. {
  117. jObject.Add(key, new JValue(value));
  118. }
  119. }
  120. /// <summary>
  121. /// 将实体类转换为数据库入参(Json)
  122. /// </summary>
  123. public static JObject ConvertToDBJson(this IDbProperty db)
  124. {
  125. Type type = db.GetType();
  126. var properties = type.GetProperties();
  127. JObject joRtn = new JObject();
  128. foreach (var property in properties)
  129. {
  130. var attribute = property.GetCustomAttribute<ColumnAttribute>();
  131. string fieldName = attribute != null ? attribute.Name : "";
  132. // 使用 BindingFlags 来允许访问非公共属性
  133. object value = property.GetValue(db, null);
  134. // 保持原始数据类型
  135. JToken token = value == null ? JValue.CreateNull() : JToken.FromObject(value);
  136. joRtn.Add(fieldName, token);
  137. }
  138. return joRtn;
  139. }
  140. }
  141. }