颐和api

CommonHelper.cs 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using NPOI.XSSF.UserModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Text;
  8. using System.Threading;
  9. namespace MadRunFabric.Common.Helpers
  10. {
  11. public class CommonHelper
  12. {
  13. private static Random rnd = new Random();
  14. private static readonly int __staticMachine = ((0x00ffffff & Environment.MachineName.GetHashCode()) +
  15. #if NETSTANDARD1_5 || NETSTANDARD1_6
  16. 1
  17. #else
  18. AppDomain.CurrentDomain.Id
  19. #endif
  20. ) & 0x00ffffff;
  21. private static readonly int __staticPid = Process.GetCurrentProcess().Id;
  22. private static int __staticIncrement = rnd.Next();
  23. /// <summary>
  24. /// 生成类似Mongodb的ObjectId有序、不重复Guid
  25. /// </summary>
  26. /// <returns></returns>
  27. public static Guid NewMongodbId()
  28. {
  29. var now = DateTime.Now;
  30. var uninxtime = (int)now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
  31. var increment = Interlocked.Increment(ref __staticIncrement) & 0x00ffffff;
  32. var rand = rnd.Next(0, int.MaxValue);
  33. var guid =
  34. $"{uninxtime.ToString("x8").PadLeft(8, '0')}{__staticMachine.ToString("x8").PadLeft(8, '0').Substring(2, 6)}{__staticPid.ToString("x8").PadLeft(8, '0').Substring(6, 2)}{increment.ToString("x8").PadLeft(8, '0')}{rand.ToString("x8").PadLeft(8, '0')}";
  35. return Guid.Parse(guid);
  36. }
  37. /// <summary>
  38. /// 获取日期差
  39. /// </summary>
  40. /// <param name="src"></param>
  41. /// <returns></returns>
  42. public static string GetDateDiff(DateTime src)
  43. {
  44. string result = null;
  45. var currentSecond = (long)(DateTime.Now - src).TotalSeconds;
  46. long minSecond = 60; //60s = 1min
  47. var hourSecond = minSecond * 60; //60*60s = 1 hour
  48. var daySecond = hourSecond * 24; //60*60*24s = 1 day
  49. var weekSecond = daySecond * 7; //60*60*24*7s = 1 week
  50. var monthSecond = daySecond * 30; //60*60*24*30s = 1 month
  51. var yearSecond = daySecond * 365; //60*60*24*365s = 1 year
  52. if (currentSecond >= yearSecond)
  53. {
  54. var year = (int)(currentSecond / yearSecond);
  55. result = $"{year}年前";
  56. }
  57. else if (currentSecond < yearSecond && currentSecond >= monthSecond)
  58. {
  59. var month = (int)(currentSecond / monthSecond);
  60. result = $"{month}个月前";
  61. }
  62. else if (currentSecond < monthSecond && currentSecond >= weekSecond)
  63. {
  64. var week = (int)(currentSecond / weekSecond);
  65. result = $"{week}周前";
  66. }
  67. else if (currentSecond < weekSecond && currentSecond >= daySecond)
  68. {
  69. var day = (int)(currentSecond / daySecond);
  70. result = $"{day}天前";
  71. }
  72. else if (currentSecond < daySecond && currentSecond >= hourSecond)
  73. {
  74. var hour = (int)(currentSecond / hourSecond);
  75. result = $"{hour}小时前";
  76. }
  77. else if (currentSecond < hourSecond && currentSecond >= minSecond)
  78. {
  79. var min = (int)(currentSecond / minSecond);
  80. result = $"{min}分钟前";
  81. }
  82. else if (currentSecond < minSecond && currentSecond >= 0)
  83. {
  84. result = "刚刚";
  85. }
  86. else
  87. {
  88. result = src.ToString("yyyy/MM/dd HH:mm:ss");
  89. }
  90. return result;
  91. }
  92. /// <summary>
  93. /// 获得类型
  94. /// </summary>
  95. /// <param name="type"></param>
  96. /// <returns></returns>
  97. public static Type GetTypeByString(string type)
  98. {
  99. switch (type.ToLower())
  100. {
  101. case "system.boolean":
  102. return Type.GetType("System.Boolean", true, true);
  103. case "system.byte":
  104. return Type.GetType("System.Byte", true, true);
  105. case "system.sbyte":
  106. return Type.GetType("System.SByte", true, true);
  107. case "system.char":
  108. return Type.GetType("System.Char", true, true);
  109. case "system.decimal":
  110. return Type.GetType("System.Decimal", true, true);
  111. case "system.double":
  112. return Type.GetType("System.Double", true, true);
  113. case "system.single":
  114. return Type.GetType("System.Single", true, true);
  115. case "system.int32":
  116. return Type.GetType("System.Int32", true, true);
  117. case "system.uint32":
  118. return Type.GetType("System.UInt32", true, true);
  119. case "system.int64":
  120. return Type.GetType("System.Int64", true, true);
  121. case "system.uint64":
  122. return Type.GetType("System.UInt64", true, true);
  123. case "system.object":
  124. return Type.GetType("System.Object", true, true);
  125. case "system.int16":
  126. return Type.GetType("System.Int16", true, true);
  127. case "system.uint16":
  128. return Type.GetType("System.UInt16", true, true);
  129. case "system.string":
  130. return Type.GetType("System.String", true, true);
  131. case "system.datetime":
  132. case "datetime":
  133. return Type.GetType("System.DateTime", true, true);
  134. case "system.guid":
  135. return Type.GetType("System.Guid", true, true);
  136. default:
  137. return Type.GetType(type, true, true);
  138. }
  139. }
  140. }
  141. }