1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- 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>(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<T> InitId<T>(List<T> 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();
- }
- }
- }
|