首页C# 正文

Net 数据 模型扩展EntityExtension

时间: 2022年2月15日 浏览 109

 public static class EntityExtension
    {
        public static TSource ToCreate<TSource>(this TSource source, HttpContext context = null)
        {
            var types = source.GetType();

            //var worker = new IdWorker(1, 1);
            //if (types.GetProperty("ID") != null)
            //{
            //    long id = worker.NextId();

            //    types.GetProperty("ID").SetValue(source, id.ToString(), null);
            //}

            if (types.GetProperty("CreateTime") != null)
            {
                types.GetProperty("CreateTime").SetValue(source, DateTime.Now, null);
            }
            if (types.GetProperty("AddTime") != null)
            {
                types.GetProperty("AddTime").SetValue(source, DateTime.Now, null);
            }
            if (types.GetProperty("UpdateTime") != null)
            {
                types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null);
            }

            if (types.GetProperty("Create_by") != null && context != null)
            {
                types.GetProperty("Create_by").SetValue(source, context.GetName(), null);

                // types.GetProperty("CreateName").SetValue(source, userSession.UserName, null);
            }
            if (types.GetProperty("UserId") != null && context != null)
            {
                types.GetProperty("UserId").SetValue(source, context.GetUId(), null);
            }
            //if (types.GetProperty("UpdateID") != null)
            //{
            //    types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null);

            //    types.GetProperty("UpdateName").SetValue(source, userSession.UserName, null);
            //}


            return source;
        }

        public static TSource ToUpdate<TSource>(this TSource source, HttpContext context = null)
        {
            var types = source.GetType();

            if (types.GetProperty("UpdateTime") != null)
            {
                types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null);
            }
            if (types.GetProperty("Update_time") != null)
            {
                types.GetProperty("Update_time").SetValue(source, DateTime.Now, null);
            }
            //if (types.GetProperty("UpdateID") != null)
            //{
            //    types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null);
            //}

            if (types.GetProperty("UpdateBy") != null)
            {
                types.GetProperty("UpdateBy").SetValue(source,context.GetName(), null);
            }
            if (types.GetProperty("Update_by") != null)
            {
                types.GetProperty("Update_by").SetValue(source, context.GetName(), null);
            }

            return source;
        }

ConfigureOption

    /// <summary>
    /// 标识Id生成
    /// </summary>
    public class IdGenerator : ConfigureOption
    {
        /// <summary>
        /// 
        /// </summary>
        public static IIdGenerator IdGenInstance => _IdGenInstance;

        /// <summary>
        /// 生成新的Id
        /// 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。
        /// 否则将会初始化一个WorkerId为1的对象。
        /// </summary>
        /// <returns></returns>
        private static long NextId()
        {
            if (_IdGenInstance == null)
            {
                _IdGenInstance = new DefaultIdGenerator(new IdGeneratorOptions() { WorkerId = 0 });
            }
            return _IdGenInstance.NewLong();
        }

        #region 自动生成编号
        private static int _sn = 0;
        private static readonly object Locker = new object();
        /// <summary>
        /// 自动生成编号  201008251145409865
        /// </summary>
        /// <returns></returns>
        public static string No()
        {
            lock (Locker)  //lock 关键字可确保当一个线程位于代码的临界区时,另一个线程不会进入该临界区。
            {
                if (_sn == int.MaxValue) _sn = 0; else _sn++;
                Thread.Sleep(10);
                Random random = new Random();
                string strRandom = random.Next(1000, 10000).ToString(); //生成编号
                string code = DateTime.Now.ToString("MMHHmmyyyyssdd") + strRandom;//形如
                return code;
            }
        }
        #endregion

        /// <summary>
        /// 生成 数值型 Id
        /// </summary>
        /// <returns></returns>
        public static long Id
        {
            get
            {
                return NextId();
            }
        }

        /// <summary>
        /// 生成 字符串 Id
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static string IdStr(string prefix = "")
        {
            return prefix + NextId();
        }
    }
 public class ConfigureOption
    {
        internal static IIdGenerator _IdGenInstance = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="options"></param>
        public static void Configure(IdGeneratorOptions options)
        {
            _IdGenInstance = new DefaultIdGenerator(options);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workerId">机器码 必须由外部设定,最大值 2^6-1 </param>
        /// <param name="seqBitLength">机器码 必须由外部设定,最大值 2^6-1 </param>
        public static void Configure(ushort workerId, byte seqBitLength)
        {
            _IdGenInstance = new DefaultIdGenerator(new IdGeneratorOptions() { WorkerId = workerId, SeqBitLength = seqBitLength });
        }
        /// <summary>
        /// 每增加 1位 WorkerIdBitLength 或 SeqBitLength,生成的ID数字值将会乘以2(基础长度可参考前一节“ID示例”),反之则除以2。
        /// WorkerIdBitLength,机器码位长,决定 WorkerId 的最大值,默认值6,取值范围 [1, 19],实际上有些语言采用 无符号 ushort (uint16) 类型接收该参数,所以最大值是16,如果是采用 有符号 short (int16),则最大值为15。
        /// WorkerIdBitLength,机器码位长,决定 WorkerId 的最大值,默认值6,取值范围 [1, 19],实际上有些语言采用 无符号 ushort (uint16) 类型接收该参数,所以最大值是16,如果是采用 有符号 short (int16),则最大值为15。
        /// 
        /// 特别提示:如果一台服务器部署多个独立服务,需要为每个服务指定不同的 WorkerId。
        /// 
        /// SeqBitLength,序列数位长,默认值6,取值范围 [3, 21](建议不小于4),决定每毫秒基础生成的ID个数。规则要求:WorkerIdBitLength + SeqBitLength 不超过 22。
        /// MinSeqNumber,最小序列数,默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位。
        /// MaxSeqNumber,最大序列数,设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,真实最大序列数取最大值(2^SeqBitLength-1),不为0时,取其为真实最大序列数,一般无需设置,除非多机共享WorkerId分段生成ID(此时还要正确设置最小序列数)。
        /// </summary>
        /// <param name="configuration"></param>
        public static void Configure(IConfiguration configuration)
        {
            var generator = configuration.GetSection("IdGenerator");
            ushort workerId = 0;
            bool hasGet = ushort.TryParse(generator.GetSection("WorkerId").Value, out workerId);
            byte seqBitLength = 6;
            bool hasSeq = byte.TryParse(generator.GetSection("SeqBitLength").Value, out seqBitLength);
            if (!hasGet) { workerId = 0; }
            if (!hasSeq) { workerId = 6; }
            _IdGenInstance = new DefaultIdGenerator(new IdGeneratorOptions() { WorkerId = workerId, SeqBitLength = seqBitLength });
        }
    }