Utility.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. namespace ddq
  9. {
  10. public class Utility
  11. {
  12. public static readonly Dictionary<int, string> RoleType = new Dictionary<int, string>
  13. {
  14. { 1, "Portfolio Manager" },
  15. { 2, "Researcher"},
  16. { 3, "Contactor"},
  17. { 4, "Marketing"},
  18. { 5, "Risk & Compliance"},
  19. { 6, "Executive"},
  20. { 7, "Legal"},
  21. { 8, "Trading"},
  22. { 9, "Technology"},
  23. { 0, "Other"}
  24. };
  25. public class DDData
  26. {
  27. public object value { get; set; }
  28. public string userId { get; set; }
  29. public string updateTime { get; set; }
  30. };
  31. /// <summary>
  32. /// JsonElment 转为字符串,若输入为空则输出""
  33. /// </summary>
  34. /// <param name="jsonElement"></param>
  35. /// <returns></returns>
  36. public static string Json2Text(JsonElement jsonElement, string propertyName)
  37. {
  38. string ret = null;
  39. JsonElement elm;
  40. if (propertyName == null) return ret;
  41. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  42. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  43. {
  44. ret = elm.ToString();
  45. }
  46. return ret;
  47. }
  48. /// <summary>
  49. /// JsonElment 转为字符串,若输入为空则输出""
  50. /// </summary>
  51. /// <param name="jsonElement"></param>
  52. /// <returns></returns>
  53. public static DDData Json2Text2(JsonElement jsonElement, string propertyName)
  54. {
  55. DDData ret = new DDData();
  56. JsonElement elm;
  57. if (propertyName == null) return ret;
  58. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  59. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  60. {
  61. ret.value = elm.GetProperty("v").ToString();
  62. ret.userId = elm.GetProperty("u").ToString();
  63. ret.updateTime = elm.GetProperty("t").ToString();
  64. }
  65. return ret;
  66. }
  67. public static bool Json2Boolean(JsonElement jsonElement, string propertyName)
  68. {
  69. bool ret = false;
  70. JsonElement elm;
  71. if (propertyName == null) return ret;
  72. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  73. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  74. {
  75. ret = (elm.ValueKind == JsonValueKind.True);
  76. }
  77. return ret;
  78. }
  79. public static DataTable Json2Table(JsonElement jsonElement)
  80. {
  81. bool hasData = jsonElement.TryGetProperty("v", out JsonElement elm);
  82. if (!hasData) return null;
  83. DataTable dataTable = new DataTable();
  84. // 如果解析出的是数组
  85. if (elm.ValueKind == JsonValueKind.Array)
  86. {
  87. // 遍历数组元素
  88. foreach (JsonElement element in elm.EnumerateArray())
  89. {
  90. // 如果是第一次遍历,元素包含列定义信息,需要创建列
  91. if (dataTable.Columns.Count == 0)
  92. {
  93. // 遍历第一个对象的属性来创建列
  94. foreach (JsonProperty property in element.EnumerateObject())
  95. {
  96. dataTable.Columns.Add(property.Name);
  97. }
  98. }
  99. // 创建新行并填充数据
  100. DataRow newRow = dataTable.NewRow();
  101. foreach (JsonProperty property in element.EnumerateObject())
  102. {
  103. newRow[property.Name] = property.Value.ToString();
  104. }
  105. dataTable.Rows.Add(newRow);
  106. }
  107. }
  108. return dataTable;
  109. }
  110. public static DDData Json2Table2(JsonElement jsonElement)
  111. {
  112. DDData ret = new DDData();
  113. JsonElement elm = jsonElement.GetProperty("v");
  114. DataTable dataTable = new DataTable();
  115. // 如果解析出的是数组
  116. if (elm.ValueKind == JsonValueKind.Array)
  117. {
  118. // 遍历数组元素
  119. foreach (JsonElement element in elm.EnumerateArray())
  120. {
  121. // 如果是第一次遍历,元素包含列定义信息,需要创建列
  122. if (dataTable.Columns.Count == 0)
  123. {
  124. // 遍历第一个对象的属性来创建列
  125. foreach (JsonProperty property in element.EnumerateObject())
  126. {
  127. dataTable.Columns.Add(property.Name);
  128. }
  129. }
  130. // 创建新行并填充数据
  131. DataRow newRow = dataTable.NewRow();
  132. foreach (JsonProperty property in element.EnumerateObject())
  133. {
  134. newRow[property.Name] = property.Value.ToString();
  135. }
  136. dataTable.Rows.Add(newRow);
  137. }
  138. }
  139. ret.value = dataTable;
  140. ret.userId = jsonElement.GetProperty("u").ToString();
  141. ret.updateTime = jsonElement.GetProperty("t").ToString();
  142. return ret;
  143. }
  144. public static List<Dictionary<string, object>> DataTable2List(DataTable dataTable)
  145. {
  146. if (dataTable == null) return null;
  147. var records = new List<Dictionary<string, object>>();
  148. foreach (DataRow row in dataTable.Rows)
  149. {
  150. var record = new Dictionary<string, object>();
  151. foreach (DataColumn col in dataTable.Columns)
  152. {
  153. // 处理DBNull值
  154. record[col.ColumnName] = row[col] == DBNull.Value ? null : row[col];
  155. }
  156. records.Add(record);
  157. }
  158. return records;
  159. }
  160. public static object AddInfo(object input, int userId)
  161. {
  162. if (input == null) return null;
  163. var ret = new
  164. {
  165. v = input,
  166. u = userId,
  167. t = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  168. };
  169. return ret;
  170. }
  171. /// <summary>
  172. /// 是否在7天内有更新
  173. /// </summary>
  174. /// <param name="date"></param>
  175. /// <returns></returns>
  176. public static bool IsChangedRecently(string date)
  177. {
  178. bool ret = false;
  179. if (date == null) return ret;
  180. if (DateTime.TryParse(date, out DateTime newDate))
  181. {
  182. if (newDate >= DateTime.Now.AddDays(-7)) ret = true;
  183. }
  184. return ret;
  185. }
  186. }
  187. }