using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace ddq { public class Utility { public static readonly Dictionary RoleType = new Dictionary { { 1, "Portfolio Manager" }, { 2, "Researcher"}, { 3, "Contactor"}, { 4, "Marketing"}, { 5, "Risk & Compliance"}, { 6, "Executive"}, { 7, "Legal"}, { 8, "Trading"}, { 9, "Technology"}, { 0, "Other"} }; public class DDData { public object value { get; set; } public string userId { get; set; } public string updateTime { get; set; } }; /// /// JsonElment 转为字符串,若输入为空则输出"" /// /// /// 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; } /// /// JsonElment 转为字符串,若输入为空则输出"" /// /// /// public static DDData Json2Text2(JsonElement jsonElement, string propertyName) { DDData ret = new DDData(); JsonElement elm; if (propertyName == null) return ret; bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm); if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined) { ret.value = elm.GetProperty("v").ToString(); ret.userId = elm.GetProperty("u").ToString(); ret.updateTime = elm.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) { bool hasData = jsonElement.TryGetProperty("v", out JsonElement elm); if (!hasData) return null; 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); } } 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> DataTable2List(DataTable dataTable) { if (dataTable == null) return null; var records = new List>(); foreach (DataRow row in dataTable.Rows) { var record = new Dictionary(); 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; } /// /// 是否在7天内有更新 /// /// /// 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; } } }