SequenceNumber.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Fuel.Infrastructure.Payment
  7. {
  8. public class SequenceNumber
  9. {
  10. private const int INIT_VALUE = 1;
  11. private static readonly object obj = new object();
  12. private static int counter = INIT_VALUE;
  13. public static string Next()
  14. {
  15. lock (obj)
  16. {
  17. var now = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  18. var cnt = counter.ToString().PadLeft(11, '0');
  19. if (counter >= int.MaxValue)
  20. counter = INIT_VALUE;
  21. else
  22. counter++;
  23. return now + cnt; // length of 28
  24. }
  25. }
  26. }
  27. public class SequenceNumber20
  28. {
  29. private const int INIT_VALUE = 1;
  30. private const int MAX_VALUE = 999;
  31. private static readonly object obj = new object();
  32. private static int counter = INIT_VALUE;
  33. public static string Next()
  34. {
  35. lock (obj)
  36. {
  37. var now = DateTime.Now.ToString("yyyyMMddHHmmssfff"); // length of 17
  38. var cnt = counter.ToString().PadLeft(3, '0');
  39. if (counter > MAX_VALUE)
  40. counter = INIT_VALUE;
  41. else
  42. counter++;
  43. return now + cnt; // length of 20
  44. }
  45. }
  46. }
  47. }