using Snowflake.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wms.util.Check { public static class CheckId { public static T InitId(T reqEntity) where T : class { Type entityType = reqEntity.GetType(); var idProp = entityType.GetProperty("Id"); if (idProp == null) { return reqEntity; } object Id = idProp.GetValue(reqEntity); if (Convert.ToInt64(Id) <= 0) { idProp.SetValue(reqEntity, IdFactory.NewId(), null); } return reqEntity; } public static List InitId(List reqEntity) where T : class { if (!reqEntity.Any()) { return reqEntity; } for (int i = 0; i < reqEntity.Count; i++) { reqEntity[i] = InitId(reqEntity[i]); } return reqEntity; } } public static class IdFactory { private static readonly object locker = new object(); private static IdWorker _idworker; public static IdWorker GetInstance() { if (_idworker == null) { lock (locker) { if (_idworker == null) { _idworker = new IdWorker(1, 1); } } } return _idworker; } public static long NewId() { return GetInstance().NextId(); } } }