SerialNumberServices.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Transactions;
  6. using EMIS.DataLogic.SystemDAL;
  7. namespace EMIS.CommonLogic.SystemServices
  8. {
  9. public class SerialNumberServices : BaseServices, ISerialNumberServices
  10. {
  11. public SerialNumberDAL SerialNumberDAL { get; set; }
  12. /// <summary>
  13. /// 获取下一个序列号带key(可指定序列号位数)
  14. /// </summary>
  15. /// <param name="key"></param>
  16. /// <returns></returns>
  17. public string GetSN(string key, int postfixLength = 2, string text = "")
  18. {
  19. if (key == null)
  20. {
  21. throw new ArgumentNullException("key");
  22. }
  23. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  24. var result = 1;
  25. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  26. if (entity != null)
  27. {
  28. result = entity.Number + 1;
  29. }
  30. return key + text + result.ToString().PadLeft(postfixLength, '0');
  31. }
  32. /// <summary>
  33. /// 获取下一个序列号带key同时更新(可指定序列号位数)
  34. /// </summary>
  35. /// <param name="key"></param>
  36. /// <returns></returns>
  37. public string SetSN(string key, int postfixLength = 2)
  38. {
  39. if (key == null)
  40. {
  41. throw new ArgumentNullException("key");
  42. }
  43. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  44. var result = 1;
  45. var isInTransaction = Transaction.Current != null;
  46. TransactionScope scope = null;
  47. if (!isInTransaction)
  48. {
  49. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  50. }
  51. try
  52. {
  53. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  54. if (entity != null)
  55. {
  56. result = entity.Number + 1;
  57. }
  58. else
  59. {
  60. entity = new Entities.Sys_SerialNumber();
  61. entity.ID = key;
  62. UnitOfWork.Add(entity);
  63. }
  64. entity.Number = result;
  65. entity.ResetTime = DateTime.Now;
  66. UnitOfWork.Commit();
  67. }
  68. catch (Exception ex)
  69. {
  70. throw ex;
  71. }
  72. finally
  73. {
  74. if (!isInTransaction)
  75. {
  76. scope.Dispose();
  77. }
  78. }
  79. return key + result.ToString().PadLeft(postfixLength, '0');
  80. }
  81. /// <summary>
  82. /// 获取下一位序列号不带key
  83. /// </summary>
  84. /// <param name="key"></param>
  85. /// <returns></returns>
  86. public int GetSNValue(string key)
  87. {
  88. if (key == null)
  89. {
  90. throw new ArgumentNullException("key");
  91. }
  92. var result = 1;
  93. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  94. if (entity != null)
  95. {
  96. result = entity.Number + 1;
  97. }
  98. return result;
  99. }
  100. /// <summary>
  101. /// 设置下一个序列号,同时更新(带skip)
  102. /// </summary>
  103. /// <param name="key"></param>
  104. /// <param name="skipCount"></param>
  105. public void SkipSN(string key, int skipCount)
  106. {
  107. if (key == null)
  108. {
  109. throw new ArgumentNullException("key");
  110. }
  111. var isInTransaction = Transaction.Current != null;
  112. TransactionScope scope = null;
  113. if (!isInTransaction)
  114. {
  115. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  116. }
  117. try
  118. {
  119. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  120. var result = 1;
  121. if (entity != null)
  122. {
  123. result = entity.Number;
  124. }
  125. else
  126. {
  127. entity = new Entities.Sys_SerialNumber();
  128. entity.ID = key;
  129. UnitOfWork.Add(entity);
  130. }
  131. entity.Number = result + skipCount;
  132. entity.ResetTime = DateTime.Now;
  133. UnitOfWork.Commit();
  134. if (!isInTransaction)
  135. {
  136. scope.Complete();
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. throw ex;
  142. }
  143. finally
  144. {
  145. if (!isInTransaction)
  146. {
  147. scope.Dispose();
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// 获取下一个序列号带key同时更新(可指定序列号位数)
  153. /// </summary>
  154. /// <param name="key"></param>
  155. /// <returns></returns>
  156. public string SetDialySN(string key, int postfixLength = 2)
  157. {
  158. if (key == null)
  159. {
  160. throw new ArgumentNullException("key");
  161. }
  162. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  163. var result = 1;
  164. var isInTransaction = Transaction.Current != null;
  165. TransactionScope scope = null;
  166. if (!isInTransaction)
  167. {
  168. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  169. }
  170. try
  171. {
  172. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  173. if (entity != null)
  174. {
  175. if (entity.ResetTime.Date != DateTime.Today)
  176. {
  177. result = 1;
  178. }
  179. else
  180. {
  181. result = entity.Number + 1;
  182. }
  183. }
  184. else
  185. {
  186. entity = new Entities.Sys_SerialNumber();
  187. entity.ID = key;
  188. UnitOfWork.Add(entity);
  189. }
  190. entity.Number = result;
  191. entity.ResetTime = DateTime.Now;
  192. UnitOfWork.Commit();
  193. if (!isInTransaction)
  194. {
  195. scope.Complete();
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. throw ex;
  201. }
  202. finally
  203. {
  204. if (!isInTransaction)
  205. {
  206. scope.Dispose();
  207. }
  208. }
  209. return key + result.ToString().PadLeft(postfixLength, '0');
  210. }
  211. }
  212. }