You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
9.0 KiB
C#

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
{
/// <summary>
/// 常用的应用功能
/// </summary>
public class Tools
{
/// <summary>
///server 预警消息 集成钉钉机器人
/// </summary>
public static async Task<ReturnResult<string>> ServerWarnMessage(string Title, string Message) {
Message = Title + Message;
return await ServerWarnMessage(Message);
}
/// <summary>
///server 预警消息 集成钉钉机器人
/// </summary>
public static async Task<ReturnResult<string>> ServerWarnMessage(string Message)
{
return await ServerWarnMessage(new WarnMessageDto {Message=Message });
}
/// <summary>
///server 预警消息 集成钉钉机器人
/// </summary>
public static async Task<ReturnResult<string>> ServerWarnMessageErr(string Message)
{
return await ServerWarnMessage(new WarnMessageDto { Message = Message,DingUrl=sysOptionConfig.Webconfig.ServerWarnApiUrlErr });
}
/// <summary>
/// server 预警消息 集成钉钉机器人
/// </summary>
/// <param name="Dto"></param>
/// <returns></returns>
public static async Task<ReturnResult<string>> ServerWarnMessage(WarnMessageDto Dto)
{
var rs = new ReturnResult<string>();
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;
}
/// <summary>
/// 创建一个编号 如订单编号
/// </summary>
/// <param name="NoHead">开头</param>
/// <param name="type">0长编号 1短编号</param>
/// <returns></returns>
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();
}
/// <summary>
/// 获取一个随机数
/// </summary>
/// <param name="digit">随机数的位数</param>
/// <param name="RandomArray">随机数组</param>
/// <returns></returns>
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<digit; i++)
{
No.Append(slist[r.Next(0,slist.Length)]);
}
return No.ToString();
}
/// <summary>
/// 将对象转换json
/// </summary>
/// <param name="Obj">要转的对象</param>
/// <returns></returns>
public static string ToJson(object Obj)
{
var json = string.Empty;
try { json = JsonConvert.SerializeObject(Obj); }
catch { }
return json;
}
/// <summary>
/// 将Jason反序列化未数据对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T JsonToObject<T>(string json)
{
return (T)JsonConvert.DeserializeObject(json, typeof(T));
}
/// <summary>
/// 对Model快速更新到目标对象 只更新非初始化值对象 自动跳过Model基础数值 "Id", "Guid", "Status", "CreateTimeStamp", "UpTimeStamp", "CreateTime"
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Value">更新来源对象</param>
/// <param name="ToValue">要更新的目标对象</param>
/// <param name="FileList">要跳过更新属性字段默认跳过"Id", "Guid", "Status", "CreateTimeStamp", "UpTimeStamp", "CreateTime"</param>
/// <returns></returns>
public static T UpModel<T>(T Value, T ToValue, List<string> FileList = null)
{
if (FileList == null)
{
FileList = new List<string>();
}
FileList.AddRange(new List<string> { "Id", "Guid", "Status", "CreateTimeStamp", "UpTimeStamp", "CreateTime" });
return UpObject(Value,ToValue,FileList);
}
/// <summary>
/// 将来源数据快速更新到目标对象 只更新非Null对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Value">更新来源对象</param>
/// <param name="ToValue">要更新的目标对象</param>
/// <param name="FilteList">要跳过更新属性</param>
/// <returns></returns>
public static T UpObject<T>(T Value, T ToValue, List<string> FilteList=null)
{
T Rv = ToValue;
try
{
if (FilteList == null)
{
FilteList = new List<string>();
}
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;
}
/// <summary>
/// 测试服务器计算性能
/// </summary>
/// <param name="N">斐波那契数计算次数默认40</param>
/// <returns></returns>
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 + "秒";
}
/// <summary>
/// 斐波那契数列和
/// </summary>
/// <param name="n">次数</param>
/// <returns></returns>
public static int Fib(int n)
{
if (n < 2)
return n;
else
return Fib(n - 2) + Fib(n - 1);
}
}
}