SNServices.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using YLShipBuildLandMap.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Transactions;
  7. namespace YLShipBuildLandMap.Services.SystemSetting
  8. {
  9. public class SNServices : ISNServices
  10. {
  11. private YLShipBuildLandMapContext DbContext { get; set; }
  12. public SNServices(YLShipBuildLandMapContext dbContext)
  13. {
  14. DbContext = dbContext;
  15. }
  16. /// <summary>
  17. /// 获取下一个序列号带key(可指定序列号位数)
  18. /// </summary>
  19. /// <param name="key"></param>
  20. /// <returns></returns>
  21. public string GetSN(string key, int postfixLength = 2, string text = "")
  22. {
  23. if (key == null)
  24. {
  25. throw new ArgumentNullException("key");
  26. }
  27. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  28. var result = 1;
  29. var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
  30. if (entity != null)
  31. {
  32. result = entity.Number + 1;
  33. }
  34. return key + text + result.ToString().PadLeft(postfixLength, '0');
  35. }
  36. /// <summary>
  37. /// 获取下一个序列号带key同时更新(可指定序列号位数)
  38. /// </summary>
  39. /// <param name="key"></param>
  40. /// <returns></returns>
  41. public string SetSN(string key, int postfixLength = 2, string text = "")
  42. {
  43. if (key == null)
  44. {
  45. throw new ArgumentNullException("key");
  46. }
  47. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  48. var result = 1;
  49. /*var isInTransaction = Transaction.Current != null;
  50. TransactionScope scope = null;
  51. if (!isInTransaction)
  52. {
  53. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  54. }*/
  55. try
  56. {
  57. var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
  58. if (entity != null)
  59. {
  60. result = entity.Number + 1;
  61. }
  62. else
  63. {
  64. entity = new SysSerialNumber();
  65. entity.SerialNumberKey = key;
  66. this.DbContext.SysSerialNumber.Add(entity);
  67. }
  68. entity.Number = result;
  69. entity.ResetTime = DateTime.Now;
  70. this.DbContext.SaveChanges();
  71. /*if (!isInTransaction)
  72. {
  73. scope.Complete();
  74. }*/
  75. }
  76. catch (Exception ex)
  77. {
  78. throw ex;
  79. }
  80. finally
  81. {
  82. /*if (!isInTransaction)
  83. {
  84. scope.Dispose();
  85. }*/
  86. }
  87. return key + text + result.ToString().PadLeft(postfixLength, '0');
  88. }
  89. /// <summary>
  90. /// 获取下一位序列号不带key(可指定序列号位数)
  91. /// </summary>
  92. /// <param name="key"></param>
  93. /// <returns></returns>
  94. public string GetSNValue(string key, int postfixLength = 2)
  95. {
  96. if (key == null)
  97. {
  98. throw new ArgumentNullException("key");
  99. }
  100. var result = 1;
  101. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  102. var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
  103. if (entity != null)
  104. {
  105. result = entity.Number + 1;
  106. }
  107. else
  108. {
  109. entity = new SysSerialNumber();
  110. entity.SerialNumberKey = key;
  111. entity.Number = 0;
  112. entity.ResetTime = DateTime.Now;
  113. DbContext.Add(entity);
  114. DbContext.SaveChanges();
  115. }
  116. return result.ToString().PadLeft(postfixLength, '0');
  117. }
  118. /// <summary>
  119. /// 设置下一个序列号,同时更新(带skip)
  120. /// </summary>
  121. /// <param name="key"></param>
  122. /// <param name="skipCount"></param>
  123. public void SkipSN(string key, int skipCount)
  124. {
  125. if (key == null)
  126. {
  127. throw new ArgumentNullException("key");
  128. }
  129. var isInTransaction = Transaction.Current != null;
  130. TransactionScope scope = null;
  131. if (!isInTransaction)
  132. {
  133. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  134. }
  135. try
  136. {
  137. var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
  138. var result = 1;
  139. if (entity != null)
  140. {
  141. result = entity.Number;
  142. }
  143. else
  144. {
  145. entity = new SysSerialNumber();
  146. entity.SerialNumberKey = key;
  147. DbContext.Add(entity);
  148. }
  149. entity.Number = result + skipCount;
  150. entity.ResetTime = DateTime.Now;
  151. DbContext.SaveChanges();
  152. if (!isInTransaction)
  153. {
  154. scope.Complete();
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. throw ex;
  160. }
  161. finally
  162. {
  163. if (!isInTransaction)
  164. {
  165. scope.Dispose();
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 获取下一个序列号带key同时更新(可指定序列号位数)
  171. /// </summary>
  172. /// <param name="key"></param>
  173. /// <returns></returns>
  174. public string SetDialySN(string key, int postfixLength = 2)
  175. {
  176. if (key == null)
  177. {
  178. throw new ArgumentNullException("key");
  179. }
  180. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  181. var result = 1;
  182. var isInTransaction = Transaction.Current != null;
  183. TransactionScope scope = null;
  184. if (!isInTransaction)
  185. {
  186. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  187. }
  188. try
  189. {
  190. var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
  191. if (entity != null)
  192. {
  193. if (entity.ResetTime.Date != DateTime.Today)
  194. {
  195. result = 1;
  196. }
  197. else
  198. {
  199. result = entity.Number + 1;
  200. }
  201. }
  202. else
  203. {
  204. entity = new SysSerialNumber();
  205. entity.SerialNumberKey = key;
  206. DbContext.Add(entity);
  207. }
  208. entity.Number = result;
  209. entity.ResetTime = DateTime.Now;
  210. DbContext.SaveChanges();
  211. if (!isInTransaction)
  212. {
  213. scope.Complete();
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. throw ex;
  219. }
  220. finally
  221. {
  222. if (!isInTransaction)
  223. {
  224. scope.Dispose();
  225. }
  226. }
  227. return key + DateTime.Now.ToString("yyyyMMdd") + result.ToString().PadLeft(postfixLength, '0');
  228. }
  229. /// <summary>
  230. /// 获取下一个序列号带key同时更新,不带key(可指定序列号位数)
  231. /// </summary>
  232. /// <param name="key"></param>
  233. /// <returns></returns>
  234. public string SetSNValue(string key, int postfixLength = 2, string text = "")
  235. {
  236. if (key == null)
  237. {
  238. throw new ArgumentNullException("key");
  239. }
  240. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  241. var result = 1;
  242. var isInTransaction = Transaction.Current != null;
  243. TransactionScope scope = null;
  244. if (!isInTransaction)
  245. {
  246. scope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable });
  247. }
  248. try
  249. {
  250. var entity = DbContext.SysSerialNumber.FirstOrDefault(w => w.SerialNumberKey == key);
  251. if (entity != null)
  252. {
  253. result = entity.Number + 1;
  254. }
  255. else
  256. {
  257. entity = new SysSerialNumber();
  258. entity.SerialNumberKey = key;
  259. this.DbContext.SysSerialNumber.Add(entity);
  260. }
  261. entity.Number = result;
  262. entity.ResetTime = DateTime.Now;
  263. this.DbContext.SaveChanges();
  264. if (!isInTransaction)
  265. {
  266. scope.Complete();
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. throw ex;
  272. }
  273. finally
  274. {
  275. if (!isInTransaction)
  276. {
  277. scope.Dispose();
  278. }
  279. }
  280. return text + result.ToString().PadLeft(postfixLength, '0');
  281. }
  282. }
  283. }