예제 #1
0
        public void NewId()
        {
            var f  = new Snowflake();
            var id = f.NewId();

            var time = id >> 22;
            var tt   = f.StartTimestamp.AddMilliseconds(time);

            Assert.True(tt <= DateTime.Now);

            var wid = (id >> 12) & 0x3FF;

            Assert.Equal(f.WorkerId, wid);

            var seq = id & 0x0FFF;
            //Assert.Equal(f.Sequence, seq);

            // 时间转编号
            var id2 = f.GetId(tt);

            Assert.Equal(id >> 22, id2 >> 22);

            // 分析
            var rs = f.TryParse(id, out var t, out var w, out var s);

            Assert.True(rs);
            Assert.Equal(tt, t);
            Assert.Equal(wid, w);
            Assert.Equal(seq, s);
        }
예제 #2
0
        private void StuffInstoreInit()
        {
            Snowflake s  = new Snowflake();
            string    id = s.GetId().ToString();

            Send(new SendError("200", id, ""));
        }
예제 #3
0
        /// <summary>时间专用区间函数</summary>
        /// <param name="fi"></param>
        /// <param name="start">起始时间,大于等于</param>
        /// <param name="end">结束时间,小于。如果是日期,则加一天</param>
        /// <param name="snow"></param>
        /// <returns></returns>
        public static Expression Between(this FieldItem fi, DateTime start, DateTime end, Snowflake snow)
        {
            if (fi.Type != typeof(Int64))
            {
                throw new NotSupportedException($"[{nameof(Between)}]函数仅支持Int64字段!");
            }

            var exp = new WhereExpression();

            if (fi == null)
            {
                return(exp);
            }

            if (start <= DateTime.MinValue || start >= DateTime.MaxValue)
            {
                if (end <= DateTime.MinValue || end >= DateTime.MaxValue)
                {
                    return(exp);
                }

                // 如果只有日期,则加一天,表示包含这一天
                if (end == end.Date)
                {
                    end = end.AddDays(1);
                }

                return(fi < snow.GetId(end));
            }
            else
            {
                exp &= fi >= snow.GetId(start);
                if (end <= DateTime.MinValue || end >= DateTime.MaxValue)
                {
                    return(exp);
                }

                // 如果只有日期,则加一天,表示包含这一天
                if (start == start.Date && end == end.Date)
                {
                    end = end.AddDays(1);
                }

                return(exp & fi < snow.GetId(end));
            }
        }
 public string Generate(DbType dbType)
 {
     return(snowflake.GetId().ToString());
 }
 /// <summary>
 /// 生成主键id
 /// </summary>
 /// <returns></returns>
 public static long GetPrimaryKeyId()
 {
     return(snowflake.GetId());
 }