using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using Common.Entity; using Common.Extensions; using Newtonsoft.Json; namespace Common.Tools { /// /// 常用的应用功能 /// public class Tools { /// ///server 预警消息 集成钉钉机器人 /// public static async Task> ServerWarnMessage(string Title, string Message) { Message = Title + Message; return await ServerWarnMessage(Message); } /// ///server 预警消息 集成钉钉机器人 /// public static async Task> ServerWarnMessage(string Message) { return await ServerWarnMessage(new WarnMessageDto {Message=Message }); } /// ///server 预警消息 集成钉钉机器人 /// public static async Task> ServerWarnMessageErr(string Message) { return await ServerWarnMessage(new WarnMessageDto { Message = Message,DingUrl=sysOptionConfig.Webconfig.ServerWarnApiUrlErr }); } /// /// server 预警消息 集成钉钉机器人 /// /// /// public static async Task> ServerWarnMessage(WarnMessageDto Dto) { var rs = new ReturnResult(); try { if (Dto == null) { rs.Not("空值!"); return rs; } if (Dto.DingUrl.IsNull()) { Dto.DingUrl = sysOptionConfig.Webconfig.ServerWarnApiUrl; } if (Dto.KeyTag.IsNull()) { Dto.KeyTag= "[" + sysOptionConfig.Webconfig.ServerWarnKey + "]"; } if (Dto.Title.IsNull()) { Dto.Title = sysOptionConfig.Webconfig.WebName; } if (Dto.DingUrl.IsNotNull()) { var postdata = new { text = new { content = "["+Dto.Title+"]"+Dto.Message + Dto.KeyTag }, msgtype = "text" }; var urllist = Dto.DingUrl.Split(",").ToList(); urllist.ForEach(Url=> { HttpHelp.Post(postdata, Url, PsotType.Json); }); } else { rs.Not("没有 机器人url配置"); return rs; } rs.OK("OK"); } catch (Exception ex) { rs.Not(ex.Message); } return rs; } /// /// 创建一个编号 如订单编号 /// /// 开头 /// 0长编号 1短编号 /// public static string GetNewNo(string NoHead=null,int type=0,Random random=null) { var No =new StringBuilder(); if (type == 0) { No.Append(NoHead); No.Append(DateTime.Now.ToString("yyMMddHHmmss")); No.Append(GetRandom(3, random: random)); } else if (type == 1) { No.Append(NoHead); No.Append(DateTime.Now.ToString("yyMMdd")); No.Append(GetRandom(4,random:random)); } return No.ToString(); } /// /// 获取一个随机数 /// /// 随机数的位数 /// 随机数组 /// public static string GetRandom(int digit=1, string[]RandomArray=null,Random random=null) { var slist = new string[] {"0","1","2","3","4", "5", "6", "7", "8", "9"}; slist = RandomArray == null ? slist : RandomArray; var No = new StringBuilder(); Random r = new Random(); if (random != null) { r = random; } for (int i= 0; i /// 将对象转换json /// /// 要转的对象 /// public static string ToJson(object Obj) { var json = string.Empty; try { json = JsonConvert.SerializeObject(Obj); } catch { } return json; } /// /// 将Jason反序列化未数据对象 /// /// /// /// public static T JsonToObject(string json) { return (T)JsonConvert.DeserializeObject(json, typeof(T)); } /// /// 对Model快速更新到目标对象 只更新非初始化值对象 自动跳过Model基础数值 "Id", "Guid", "Status", "CreateTimeStamp", "UpTimeStamp", "CreateTime" /// /// /// 更新来源对象 /// 要更新的目标对象 /// 要跳过更新属性字段默认跳过"Id", "Guid", "Status", "CreateTimeStamp", "UpTimeStamp", "CreateTime" /// public static T UpModel(T Value, T ToValue, List FileList = null) { if (FileList == null) { FileList = new List(); } FileList.AddRange(new List { "Id", "Guid", "Status", "CreateTimeStamp", "UpTimeStamp", "CreateTime" }); return UpObject(Value,ToValue,FileList); } /// /// 将来源数据快速更新到目标对象 只更新非Null对象 /// /// /// 更新来源对象 /// 要更新的目标对象 /// 要跳过更新属性 /// public static T UpObject(T Value, T ToValue, List FilteList=null) { T Rv = ToValue; try { if (FilteList == null) { FilteList = new List(); } if (Value.GetType() != ToValue.GetType()) { return ToValue; } Type type = ToValue.GetType(); var NewT = Activator.CreateInstance(type); PropertyInfo[] PropertyList = type.GetProperties(); foreach (PropertyInfo item in PropertyList) { var rname = FilteList.Find(o => o.ToLower() == item.Name.ToLower()); if (rname==null) { var getv = item.GetValue(Value); var tov = item.GetValue(ToValue); var defaultvalue = item.GetValue(NewT); if (getv != null) { if (!getv.Equals(defaultvalue)&&getv!=tov) { item.SetValue(Rv, getv); } } } } } catch(Exception ex) { var message = ex.Message; Rv = ToValue; } return Rv; } /// /// 测试服务器计算性能 /// /// 斐波那契数计算次数默认40 /// public static object GetServerRunPer(int N=40) { Stopwatch time = new Stopwatch(); time.Start(); Console.WriteLine("计算结果:" + Fib(N)); time.Stop(); var t = time.Elapsed.TotalMilliseconds / 1000; return "计算"+N+"个斐波那契数列和耗时:" + t + "秒"; } /// /// 斐波那契数列和 /// /// 次数 /// public static int Fib(int n) { if (n < 2) return n; else return Fib(n - 2) + Fib(n - 1); } } }