| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using static ddq.Utility;
- namespace ddq
- {
- public class Utility
- {
- public static readonly Dictionary<int, string> RoleType = new Dictionary<int, string>
- {
- { 1, "Portfolio Manager" },
- { 2, "Researcher"},
- { 3, "Contactor"},
- { 4, "Marketing"},
- { 5, "Risk & Compliance"},
- { 6, "Executive"},
- { 7, "Legal"},
- { 8, "Trading"},
- { 9, "Technology"},
- { 0, "Other"}
- };
- public static readonly Dictionary<int, string> CategoryGroup = new Dictionary<int, string>
- {
- { 0, "All" },
- { 101, "Equity" },
- { 102, "Allocaiton" },
- { 103, "Bond" },
- { 104, "Moeny Market" },
- { 105, "Commodity" },
- { 111, "Convertable" },
- { 112, "Alternative" }
- };
- public static Color COLOR_NORMAL = Color.DarkSlateGray;
- public static Color COLOR_MODIFIED = Color.Red;
- public static Color COLOR_REVIEWED = Color.Green;
- public class DDData
- {
- private object value = null;
- private string userId = null;
- private string updateTime = null;
- public object Value
- {
- get { return this.value; }
- set { this.value = value; }
- }
- public string UserId
- {
- get { return this.userId; }
- set { this.userId = value; }
- }
- public string UpdateTime
- {
- get { return this.updateTime; }
- set { this.updateTime = value; }
- }
- };
- /// <summary>
- /// JsonElment 转为字符串,若输入为空则输出""
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- public static string Json2Text(JsonElement jsonElement, string propertyName)
- {
- string ret = null;
- JsonElement elm;
- if (propertyName == null) return ret;
- bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
- if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
- {
- ret = elm.ToString();
- }
- return ret;
- }
- /// <summary>
- /// JsonElment 转为字符串,若输入为空则输出""
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- public static DDData Json2Text2(JsonElement jsonElement, string propertyName)
- {
- DDData ret = new DDData();
- JsonElement element;
- if (propertyName == null) return ret;
- bool hasProperty = jsonElement.TryGetProperty(propertyName, out element);
- if (hasProperty == true && element.ValueKind == JsonValueKind.Object)
- {
- bool hasData = element.TryGetProperty("v", out JsonElement elmV);
- if (hasData)
- {
- ret.Value = element.GetProperty("v").ToString();
- ret.UserId = element.GetProperty("u").ToString();
- ret.UpdateTime = element.GetProperty("t").ToString();
- }
- }
- return ret;
- }
- public static bool Json2Boolean(JsonElement jsonElement, string propertyName)
- {
- bool ret = false;
- JsonElement elm;
- if (propertyName == null) return ret;
- bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
- if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
- {
- ret = (elm.ValueKind == JsonValueKind.True);
- }
- return ret;
- }
- public static DataTable Json2Table(JsonElement jsonElement)
- {
- if (jsonElement.ValueKind != JsonValueKind.Object) return null;
- bool hasData = jsonElement.TryGetProperty("v", out JsonElement elm);
- DataTable dataTable = new DataTable();
- if (!hasData) return null;
- // 如果解析出的是数组
- if (elm.ValueKind == JsonValueKind.Array)
- {
- // 遍历数组元素
- foreach (JsonElement element in elm.EnumerateArray())
- {
- // 如果是第一次遍历,元素包含列定义信息,需要创建列
- if (dataTable.Columns.Count == 0)
- {
- // 遍历第一个对象的属性来创建列
- foreach (JsonProperty property in element.EnumerateObject())
- {
- dataTable.Columns.Add(property.Name);
- }
- }
- // 创建新行并填充数据
- DataRow newRow = dataTable.NewRow();
- foreach (JsonProperty property in element.EnumerateObject())
- {
- newRow[property.Name] = property.Value.ToString();
- }
- dataTable.Rows.Add(newRow);
- }
- }
- return dataTable;
- }
- public static DDData Json2Table2(JsonElement jsonElement)
- {
- DDData ret = new DDData();
- JsonElement elm = jsonElement.GetProperty("v");
- DataTable dataTable = new DataTable();
- // 如果解析出的是数组
- if (elm.ValueKind == JsonValueKind.Array)
- {
-
- // 遍历数组元素
- foreach (JsonElement element in elm.EnumerateArray())
- {
- // 如果是第一次遍历,元素包含列定义信息,需要创建列
- if (dataTable.Columns.Count == 0)
- {
- // 遍历第一个对象的属性来创建列
- foreach (JsonProperty property in element.EnumerateObject())
- {
- dataTable.Columns.Add(property.Name);
- }
- }
- // 创建新行并填充数据
- DataRow newRow = dataTable.NewRow();
- foreach (JsonProperty property in element.EnumerateObject())
- {
- newRow[property.Name] = property.Value.ToString();
- }
- dataTable.Rows.Add(newRow);
- }
- }
- ret.Value = dataTable;
- ret.UserId = jsonElement.GetProperty("u").ToString();
- ret.UpdateTime = jsonElement.GetProperty("t").ToString();
- return ret;
- }
- public static List<Dictionary<string, object>> DataTable2List(DataTable dataTable)
- {
- if (dataTable == null) return null;
- var records = new List<Dictionary<string, object>>();
- foreach (DataRow row in dataTable.Rows)
- {
- var record = new Dictionary<string, object>();
- foreach (DataColumn col in dataTable.Columns)
- {
- // 处理DBNull值
- record[col.ColumnName] = row[col] == DBNull.Value ? null : row[col];
- }
- records.Add(record);
- }
- return records;
- }
- public static object AddInfo(object input, int userId)
- {
- if (input == null) return null;
- var ret = new
- {
- v = input,
- u = userId,
- t = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
- };
- return ret;
- }
- /// <summary>
- /// 是否在7天内有更新
- /// </summary>
- /// <param name="date"></param>
- /// <returns></returns>
- public static bool IsChangedRecently(string date)
- {
- bool ret = false;
-
- if (date == null) return ret;
- if (DateTime.TryParse(date, out DateTime newDate))
- {
- if (newDate >= DateTime.Now.AddDays(-7)) ret = true;
- }
- return ret;
- }
- /// <summary>
- /// 将Json数据提取放入对应的TextBox及时间戳Label
- /// </summary>
- /// <param name="textBox"></param>
- /// <param name="jsonElement"></param>
- /// <param name="properyName"></param>
- /// <param name="labelUpdateTime"></param>
- public static void LoadTextDataFromJson(TextBox textBox, JsonElement jsonElement, string properyName, Label labelUpdateTime)
- {
- if (textBox == null || labelUpdateTime == null) return;
- bool hasData = jsonElement.TryGetProperty(properyName, out JsonElement element);
- if (hasData)
- {
- DDData dddata = Json2Text2(jsonElement, properyName);
- if (dddata.Value != null)
- {
- textBox.Text = dddata.Value.ToString();
- labelUpdateTime.Text = dddata.UpdateTime;
- labelUpdateTime.ForeColor = IsChangedRecently(dddata.Value.ToString()) ? COLOR_MODIFIED : COLOR_NORMAL;
- }
- }
- }
- /// <summary>
- /// 将Json数据提取放入对应的DateTimePicker及时间戳Label
- /// </summary>
- /// <param name="dateTimePicker"></param>
- /// <param name="jsonElement"></param>
- /// <param name="properyName"></param>
- /// <param name="labelUpdateTime"></param>
- public static void LoadDateDataFromJson(DateTimePicker dateTimePicker, JsonElement jsonElement, string properyName, Label labelUpdateTime)
- {
- if (dateTimePicker == null || labelUpdateTime == null) return;
- bool hasData = jsonElement.TryGetProperty(properyName, out JsonElement element);
- if (hasData)
- {
- DDData dddata = Utility.Json2Text2(jsonElement, properyName);
- if (dddata.Value != null)
- {
- bool isDate = DateTime.TryParse(dddata.Value.ToString(), out DateTime newDate);
- if (isDate)
- {
- dateTimePicker.Value = newDate;
- labelUpdateTime.Text = dddata.UpdateTime;
- labelUpdateTime.ForeColor = IsChangedRecently(dddata.Value.ToString()) ? COLOR_MODIFIED : COLOR_NORMAL;
- }
- }
- }
- }
- }
- }
|