NumberGenerater.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:NumberGenerater
  3. * 作者:xulisong
  4. * 创建时间: 2019/2/13 17:31:00
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace SAGA.DotNetUtils.Data
  13. {
  14. /// <summary>
  15. /// 序号生成器
  16. /// </summary>
  17. public class NumberGenerater
  18. {
  19. private const string DefaultCategory = "D39D59D2-A068-4366-B915-CA651698E30A";
  20. private Dictionary<string, int> m_Seeds;
  21. public NumberGenerater()
  22. {
  23. m_Seeds = new Dictionary<string, int>();
  24. }
  25. public string GetNumber(string category, string format)
  26. {
  27. if (!m_Seeds.TryGetValue(category, out int seed))
  28. {
  29. m_Seeds[category] = seed;
  30. }
  31. return string.Format(format, ++seed);
  32. }
  33. public string GetNumber(string format)
  34. {
  35. return GetNumber(DefaultCategory, format);
  36. }
  37. public string GetNumberWithPrefix(string category, string prefix)
  38. {
  39. if (!m_Seeds.TryGetValue(category, out int seed))
  40. {
  41. m_Seeds[category] = seed;
  42. }
  43. return string.Format("{0}{1}", prefix, ++seed);
  44. }
  45. public string GetNumberWithPrefix(string prefix)
  46. {
  47. return GetNumberWithPrefix(DefaultCategory, prefix);
  48. }
  49. }
  50. }