SoftNumerical.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using HslCommunication.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. namespace HslCommunication.BasicFramework
  8. {
  9. /********************************************************************************************
  10. *
  11. * 一个高度灵活的流水号生成的类,允许根据指定规则加上时间数据进行生成
  12. *
  13. * 根据保存机制进行优化,必须做到好并发量
  14. *
  15. ********************************************************************************************/
  16. /// <summary>
  17. /// 一个用于自动流水号生成的类,必须指定保存的文件,实时保存来确认安全
  18. /// </summary>
  19. public sealed class SoftNumericalOrder : SoftFileSaveBase
  20. {
  21. #region Constructor
  22. /// <summary>
  23. /// 实例化一个流水号生成的对象
  24. /// </summary>
  25. /// <param name="textHead">流水号的头文本</param>
  26. /// <param name="timeFormate">流水号带的时间信息</param>
  27. /// <param name="numberLength">流水号数字的标准长度,不够补0</param>
  28. /// <param name="fileSavePath">流水号存储的文本位置</param>
  29. public SoftNumericalOrder( string textHead, string timeFormate, int numberLength, string fileSavePath )
  30. {
  31. LogHeaderText = "SoftNumericalOrder";
  32. TextHead = textHead;
  33. TimeFormate = timeFormate;
  34. NumberLength = numberLength;
  35. FileSavePath = fileSavePath;
  36. LoadByFile( );
  37. AsyncCoordinator = new HslAsyncCoordinator( ( ) =>
  38. {
  39. if (!string.IsNullOrEmpty( FileSavePath ))
  40. {
  41. using (System.IO.StreamWriter sw = new System.IO.StreamWriter( FileSavePath, false, Encoding.Default ))
  42. {
  43. sw.Write( CurrentIndex );
  44. }
  45. }
  46. } );
  47. }
  48. #endregion
  49. #region Private Member
  50. /// <summary>
  51. /// 当前的生成序列号
  52. /// </summary>
  53. private long CurrentIndex = 0;
  54. /// <summary>
  55. /// 流水号的文本头
  56. /// </summary>
  57. private string TextHead = string.Empty;
  58. /// <summary>
  59. /// 时间格式默认年月日
  60. /// </summary>
  61. private string TimeFormate = "yyyyMMdd";
  62. /// <summary>
  63. /// 流水号数字应该显示的长度
  64. /// </summary>
  65. private int NumberLength = 5;
  66. #endregion
  67. #region Public Method
  68. /// <summary>
  69. /// 获取流水号的值
  70. /// </summary>
  71. /// <returns></returns>
  72. public override string ToSaveString( )
  73. {
  74. return CurrentIndex.ToString( );
  75. }
  76. /// <summary>
  77. /// 加载流水号
  78. /// </summary>
  79. /// <param name="content"></param>
  80. public override void LoadByString( string content )
  81. {
  82. CurrentIndex = Convert.ToInt64( content );
  83. }
  84. /// <summary>
  85. /// 清除流水号计数,进行重新计数
  86. /// </summary>
  87. public void ClearNumericalOrder( )
  88. {
  89. Interlocked.Exchange( ref CurrentIndex, 0 );
  90. AsyncCoordinator.StartOperaterInfomation( );
  91. }
  92. /// <summary>
  93. /// 获取流水号数据
  94. /// </summary>
  95. /// <returns></returns>
  96. public string GetNumericalOrder( )
  97. {
  98. long number = Interlocked.Increment( ref CurrentIndex );
  99. AsyncCoordinator.StartOperaterInfomation( );
  100. if (string.IsNullOrEmpty( TimeFormate ))
  101. {
  102. return TextHead + number.ToString( ).PadLeft( NumberLength, '0' );
  103. }
  104. else
  105. {
  106. return TextHead + DateTime.Now.ToString( TimeFormate ) + number.ToString( ).PadLeft( NumberLength, '0' );
  107. }
  108. }
  109. /// <summary>
  110. /// 获取流水号数据
  111. /// </summary>
  112. /// <param name="textHead">指定一个新的文本头</param>
  113. /// <returns></returns>
  114. public string GetNumericalOrder( string textHead )
  115. {
  116. long number = Interlocked.Increment( ref CurrentIndex );
  117. AsyncCoordinator.StartOperaterInfomation( );
  118. if (string.IsNullOrEmpty( TimeFormate ))
  119. {
  120. return textHead + number.ToString( ).PadLeft( NumberLength, '0' );
  121. }
  122. else
  123. {
  124. return textHead + DateTime.Now.ToString( TimeFormate ) + number.ToString( ).PadLeft( NumberLength, '0' );
  125. }
  126. }
  127. /// <summary>
  128. /// 单纯的获取数字形式的流水号
  129. /// </summary>
  130. /// <returns></returns>
  131. public long GetLongOrder( )
  132. {
  133. long number = Interlocked.Increment( ref CurrentIndex );
  134. AsyncCoordinator.StartOperaterInfomation( );
  135. return number;
  136. }
  137. #endregion
  138. #region 高性能存储块
  139. /// <summary>
  140. /// 高性能存储块
  141. /// </summary>
  142. private HslAsyncCoordinator AsyncCoordinator { get; set; }
  143. #endregion
  144. }
  145. /// <summary>
  146. /// 一个简单的不持久化的序号自增类,采用线程安全实现,并允许指定最大数字,到达后清空从指定数开始
  147. /// </summary>
  148. public sealed class SoftIncrementCount
  149. {
  150. #region Constructor
  151. /// <summary>
  152. /// 实例化一个自增信息的对象,包括最大值
  153. /// </summary>
  154. /// <param name="max">数据的最大值,必须指定</param>
  155. /// <param name="start">数据的起始值,默认为0</param>
  156. public SoftIncrementCount( long max, long start = 0 )
  157. {
  158. this.start = start;
  159. this.max = max;
  160. current = start;
  161. hybirdLock = new SimpleHybirdLock( );
  162. }
  163. #endregion
  164. #region Private Member
  165. private long start = 0;
  166. private long current = 0;
  167. private long max = long.MaxValue;
  168. private SimpleHybirdLock hybirdLock;
  169. #endregion
  170. #region Public Method
  171. /// <summary>
  172. /// 获取自增信息
  173. /// </summary>
  174. /// <returns></returns>
  175. public long GetCurrentValue( )
  176. {
  177. long value = 0;
  178. hybirdLock.Enter( );
  179. value = current;
  180. current++;
  181. if (current > max)
  182. {
  183. current = 0;
  184. }
  185. hybirdLock.Leave( );
  186. return value;
  187. }
  188. #endregion
  189. }
  190. }