SerialNumberServices.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.DataLogic.SystemDAL;
  6. namespace EMIS.CommonLogic.SystemServices
  7. {
  8. public class SerialNumberServices : BaseServices, ISerialNumberServices
  9. {
  10. public SerialNumberDAL SerialNumberDAL { get; set; }
  11. /// <summary>
  12. /// 获取下一个序列号带key(可指定序列号位数)
  13. /// </summary>
  14. /// <param name="key"></param>
  15. /// <returns></returns>
  16. public string GetSN(string key, int postfixLength = 2)
  17. {
  18. if (key == null)
  19. {
  20. throw new ArgumentNullException("key");
  21. }
  22. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  23. var result = 1;
  24. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  25. if (entity != null)
  26. {
  27. result = entity.Number + 1;
  28. }
  29. return key + result.ToString().PadLeft(postfixLength, '0');
  30. }
  31. /// <summary>
  32. /// 获取下一个序列号带key同时更新(可指定序列号位数)
  33. /// </summary>
  34. /// <param name="key"></param>
  35. /// <returns></returns>
  36. public string SetSN(string key, int postfixLength = 2)
  37. {
  38. if (key == null)
  39. {
  40. throw new ArgumentNullException("key");
  41. }
  42. postfixLength = postfixLength < 0 ? 0 : postfixLength;
  43. var result = 1;
  44. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  45. if (entity != null)
  46. {
  47. result = entity.Number + 1;
  48. }
  49. else
  50. {
  51. entity = new Entities.Sys_SerialNumber();
  52. entity.ID = key;
  53. UnitOfWork.Add(entity);
  54. }
  55. entity.Number = result;
  56. entity.ResetTime = DateTime.Now;
  57. UnitOfWork.Commit();
  58. return key + result.ToString().PadLeft(postfixLength, '0');
  59. }
  60. /// <summary>
  61. /// 获取下一位序列号不带key
  62. /// </summary>
  63. /// <param name="key"></param>
  64. /// <returns></returns>
  65. public int GetSNValue(string key)
  66. {
  67. if (key == null)
  68. {
  69. throw new ArgumentNullException("key");
  70. }
  71. var result = 1;
  72. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  73. if (entity != null)
  74. {
  75. result = entity.Number + 1;
  76. }
  77. return result;
  78. }
  79. /// <summary>
  80. /// 设置下一个序列号,同时更新(带skip)
  81. /// </summary>
  82. /// <param name="key"></param>
  83. /// <param name="skipCount"></param>
  84. public void SkipSN(string key, int skipCount)
  85. {
  86. if (key == null)
  87. {
  88. throw new ArgumentNullException("key");
  89. }
  90. var entity = SerialNumberDAL.SerialNumberRepository.GetSingle(w => w.ID == key);
  91. var result = 1;
  92. if (entity != null)
  93. {
  94. result = entity.Number;
  95. }
  96. else
  97. {
  98. entity = new Entities.Sys_SerialNumber();
  99. entity.ID = key;
  100. UnitOfWork.Add(entity);
  101. }
  102. entity.Number = result + skipCount;
  103. entity.ResetTime = DateTime.Now;
  104. UnitOfWork.Commit();
  105. }
  106. }
  107. }