Utility.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using static ddq.Utility;
  11. namespace ddq
  12. {
  13. public class Utility
  14. {
  15. public static readonly Dictionary<int, string> RoleType = new Dictionary<int, string>
  16. {
  17. { 1, "Portfolio Manager" },
  18. { 2, "Researcher"},
  19. { 3, "Contactor"},
  20. { 4, "Marketing"},
  21. { 5, "Risk & Compliance"},
  22. { 6, "Executive"},
  23. { 7, "Legal"},
  24. { 8, "Trading"},
  25. { 9, "Technology"},
  26. { 0, "Other"}
  27. };
  28. public static readonly Dictionary<int, string> CategoryGroup = new Dictionary<int, string>
  29. {
  30. { 0, "All" },
  31. { 101, "Equity" },
  32. { 102, "Allocaiton" },
  33. { 103, "Bond" },
  34. { 104, "Moeny Market" },
  35. { 105, "Commodity" },
  36. { 111, "Convertable" },
  37. { 112, "Alternative" }
  38. };
  39. public static Color COLOR_NORMAL = Color.DarkSlateGray;
  40. public static Color COLOR_MODIFIED = Color.Red;
  41. public static Color COLOR_REVIEWED = Color.Green;
  42. public class DDData
  43. {
  44. private object value = null;
  45. private string userId = null;
  46. private string updateTime = null;
  47. public object Value
  48. {
  49. get { return this.value; }
  50. set { this.value = value; }
  51. }
  52. public string UserId
  53. {
  54. get { return this.userId; }
  55. set { this.userId = value; }
  56. }
  57. public string UpdateTime
  58. {
  59. get { return this.updateTime; }
  60. set { this.updateTime = value; }
  61. }
  62. };
  63. /// <summary>
  64. /// JsonElment 转为字符串,若输入为空则输出""
  65. /// </summary>
  66. /// <param name="jsonElement"></param>
  67. /// <returns></returns>
  68. public static string Json2Text(JsonElement jsonElement, string propertyName)
  69. {
  70. string ret = null;
  71. JsonElement elm;
  72. if (propertyName == null) return ret;
  73. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  74. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  75. {
  76. ret = elm.ToString();
  77. }
  78. return ret;
  79. }
  80. /// <summary>
  81. /// JsonElment 转为字符串,若输入为空则输出""
  82. /// </summary>
  83. /// <param name="jsonElement"></param>
  84. /// <returns></returns>
  85. public static DDData Json2Text2(JsonElement jsonElement, string propertyName)
  86. {
  87. DDData ret = new DDData();
  88. JsonElement element;
  89. if (propertyName == null) return ret;
  90. bool hasProperty = jsonElement.TryGetProperty(propertyName, out element);
  91. if (hasProperty == true && element.ValueKind == JsonValueKind.Object)
  92. {
  93. bool hasData = element.TryGetProperty("v", out JsonElement elmV);
  94. if (hasData)
  95. {
  96. ret.Value = element.GetProperty("v").ToString();
  97. ret.UserId = element.GetProperty("u").ToString();
  98. ret.UpdateTime = element.GetProperty("t").ToString();
  99. }
  100. }
  101. return ret;
  102. }
  103. public static bool Json2Boolean(JsonElement jsonElement, string propertyName)
  104. {
  105. bool ret = false;
  106. JsonElement elm;
  107. if (propertyName == null) return ret;
  108. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  109. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  110. {
  111. ret = (elm.ValueKind == JsonValueKind.True);
  112. }
  113. return ret;
  114. }
  115. public static DataTable Json2Table(JsonElement jsonElement)
  116. {
  117. if (jsonElement.ValueKind != JsonValueKind.Object) return null;
  118. bool hasData = jsonElement.TryGetProperty("v", out JsonElement elm);
  119. DataTable dataTable = new DataTable();
  120. if (!hasData) return null;
  121. // 如果解析出的是数组
  122. if (elm.ValueKind == JsonValueKind.Array)
  123. {
  124. // 遍历数组元素
  125. foreach (JsonElement element in elm.EnumerateArray())
  126. {
  127. // 如果是第一次遍历,元素包含列定义信息,需要创建列
  128. if (dataTable.Columns.Count == 0)
  129. {
  130. // 遍历第一个对象的属性来创建列
  131. foreach (JsonProperty property in element.EnumerateObject())
  132. {
  133. dataTable.Columns.Add(property.Name);
  134. }
  135. }
  136. // 创建新行并填充数据
  137. DataRow newRow = dataTable.NewRow();
  138. foreach (JsonProperty property in element.EnumerateObject())
  139. {
  140. newRow[property.Name] = property.Value.ToString();
  141. }
  142. dataTable.Rows.Add(newRow);
  143. }
  144. }
  145. return dataTable;
  146. }
  147. public static DDData Json2Table2(JsonElement jsonElement)
  148. {
  149. DDData ret = new DDData();
  150. JsonElement elm = jsonElement.GetProperty("v");
  151. DataTable dataTable = new DataTable();
  152. // 如果解析出的是数组
  153. if (elm.ValueKind == JsonValueKind.Array)
  154. {
  155. // 遍历数组元素
  156. foreach (JsonElement element in elm.EnumerateArray())
  157. {
  158. // 如果是第一次遍历,元素包含列定义信息,需要创建列
  159. if (dataTable.Columns.Count == 0)
  160. {
  161. // 遍历第一个对象的属性来创建列
  162. foreach (JsonProperty property in element.EnumerateObject())
  163. {
  164. dataTable.Columns.Add(property.Name);
  165. }
  166. }
  167. // 创建新行并填充数据
  168. DataRow newRow = dataTable.NewRow();
  169. foreach (JsonProperty property in element.EnumerateObject())
  170. {
  171. newRow[property.Name] = property.Value.ToString();
  172. }
  173. dataTable.Rows.Add(newRow);
  174. }
  175. }
  176. ret.Value = dataTable;
  177. ret.UserId = jsonElement.GetProperty("u").ToString();
  178. ret.UpdateTime = jsonElement.GetProperty("t").ToString();
  179. return ret;
  180. }
  181. public static List<Dictionary<string, object>> DataTable2List(DataTable dataTable)
  182. {
  183. if (dataTable == null) return null;
  184. var records = new List<Dictionary<string, object>>();
  185. foreach (DataRow row in dataTable.Rows)
  186. {
  187. var record = new Dictionary<string, object>();
  188. foreach (DataColumn col in dataTable.Columns)
  189. {
  190. // 处理DBNull值
  191. record[col.ColumnName] = row[col] == DBNull.Value ? null : row[col];
  192. }
  193. records.Add(record);
  194. }
  195. return records;
  196. }
  197. public static object AddInfo(object input, int userId)
  198. {
  199. if (input == null) return null;
  200. var ret = new
  201. {
  202. v = input,
  203. u = userId,
  204. t = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  205. };
  206. return ret;
  207. }
  208. /// <summary>
  209. /// 是否在7天内有更新
  210. /// </summary>
  211. /// <param name="date"></param>
  212. /// <returns></returns>
  213. public static bool IsChangedRecently(string date)
  214. {
  215. bool ret = false;
  216. if (date == null) return ret;
  217. if (DateTime.TryParse(date, out DateTime newDate))
  218. {
  219. if (newDate >= DateTime.Now.AddDays(-7)) ret = true;
  220. }
  221. return ret;
  222. }
  223. /// <summary>
  224. /// 将Json数据提取放入对应的TextBox及时间戳Label
  225. /// </summary>
  226. /// <param name="textBox"></param>
  227. /// <param name="jsonElement"></param>
  228. /// <param name="properyName"></param>
  229. /// <param name="labelUpdateTime"></param>
  230. public static void LoadTextDataFromJson(TextBox textBox, JsonElement jsonElement, string properyName, Label labelUpdateTime)
  231. {
  232. if (textBox == null || labelUpdateTime == null) return;
  233. bool hasData = jsonElement.TryGetProperty(properyName, out JsonElement element);
  234. if (hasData)
  235. {
  236. DDData dddata = Json2Text2(jsonElement, properyName);
  237. if (dddata.Value != null)
  238. {
  239. textBox.Text = dddata.Value.ToString();
  240. labelUpdateTime.Text = dddata.UpdateTime;
  241. labelUpdateTime.ForeColor = IsChangedRecently(dddata.Value.ToString()) ? COLOR_MODIFIED : COLOR_NORMAL;
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// 将Json数据提取放入对应的DateTimePicker及时间戳Label
  247. /// </summary>
  248. /// <param name="dateTimePicker"></param>
  249. /// <param name="jsonElement"></param>
  250. /// <param name="properyName"></param>
  251. /// <param name="labelUpdateTime"></param>
  252. public static void LoadDateDataFromJson(DateTimePicker dateTimePicker, JsonElement jsonElement, string properyName, Label labelUpdateTime)
  253. {
  254. if (dateTimePicker == null || labelUpdateTime == null) return;
  255. bool hasData = jsonElement.TryGetProperty(properyName, out JsonElement element);
  256. if (hasData)
  257. {
  258. DDData dddata = Utility.Json2Text2(jsonElement, properyName);
  259. if (dddata.Value != null)
  260. {
  261. bool isDate = DateTime.TryParse(dddata.Value.ToString(), out DateTime newDate);
  262. if (isDate)
  263. {
  264. dateTimePicker.Value = newDate;
  265. labelUpdateTime.Text = dddata.UpdateTime;
  266. labelUpdateTime.ForeColor = IsChangedRecently(dddata.Value.ToString()) ? COLOR_MODIFIED : COLOR_NORMAL;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }