CheckId.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Snowflake.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace wms.util.Check
  7. {
  8. public static class CheckId
  9. {
  10. public static T InitId<T>(T reqEntity) where T : class
  11. {
  12. Type entityType = reqEntity.GetType();
  13. var idProp = entityType.GetProperty("Id");
  14. if (idProp == null)
  15. {
  16. return reqEntity;
  17. }
  18. object Id = idProp.GetValue(reqEntity);
  19. if (Convert.ToInt64(Id) <= 0)
  20. {
  21. idProp.SetValue(reqEntity, IdFactory.NewId(), null);
  22. }
  23. return reqEntity;
  24. }
  25. public static List<T> InitId<T>(List<T> reqEntity) where T : class
  26. {
  27. if (!reqEntity.Any()) { return reqEntity; }
  28. for (int i = 0; i < reqEntity.Count; i++)
  29. {
  30. reqEntity[i] = InitId(reqEntity[i]);
  31. }
  32. return reqEntity;
  33. }
  34. }
  35. public static class IdFactory
  36. {
  37. private static readonly object locker = new object();
  38. private static IdWorker _idworker;
  39. public static IdWorker GetInstance()
  40. {
  41. if (_idworker == null)
  42. {
  43. lock (locker)
  44. {
  45. if (_idworker == null)
  46. {
  47. _idworker = new IdWorker(1, 1);
  48. }
  49. }
  50. }
  51. return _idworker;
  52. }
  53. public static long NewId()
  54. {
  55. return GetInstance().NextId();
  56. }
  57. }
  58. }