diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/IdWorker.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/IdWorker.cs new file mode 100644 index 0000000..06a6f5c --- /dev/null +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/IdWorker.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Mesnac.Action.ChemicalWeighing.FreeDb +{ + using System; + /// + /// Twitter的分布式自增ID雪花算法 + /// + public class IdWorker + { + //起始的时间戳 + private static long START_STMP = 1480166465631L; + + //每一部分占用的位数 + private static int SEQUENCE_BIT = 12; //序列号占用的位数 + private static int MACHINE_BIT = 5; //机器标识占用的位数 + private static int DATACENTER_BIT = 5;//数据中心占用的位数 + + //每一部分的最大值 + private static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT); + private static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT); + private static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT); + + //每一部分向左的位移 + private static int MACHINE_LEFT = SEQUENCE_BIT; + private static int DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT; + private static int TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT; + + private long datacenterId = 1; //数据中心 + private long machineId = 1; //机器标识 + private long sequence = 0L; //序列号 + private long lastStmp = -1L;//上一次时间戳 + + #region 单例:完全懒汉 + private static readonly Lazy lazy = new Lazy(() => new IdWorker()); + public static IdWorker Singleton { get { return lazy.Value; } } + private IdWorker() { } + #endregion + + public IdWorker(long cid, long mid) + { + if (cid > MAX_DATACENTER_NUM || cid < 0) throw new Exception($"中心Id应在(0,{MAX_DATACENTER_NUM})之间"); + if (mid > MAX_MACHINE_NUM || mid < 0) throw new Exception($"机器Id应在(0,{MAX_MACHINE_NUM})之间"); + datacenterId = cid; + machineId = mid; + } + + /// + /// 产生下一个ID + /// + /// + public long nextId() + { + long currStmp = getNewstmp(); + if (currStmp < lastStmp) throw new Exception("时钟倒退,Id生成失败!"); + + if (currStmp == lastStmp) + { + //相同毫秒内,序列号自增 + sequence = (sequence + 1) & MAX_SEQUENCE; + //同一毫秒的序列数已经达到最大 + if (sequence == 0L) currStmp = getNextMill(); + } + else + { + //不同毫秒内,序列号置为0 + sequence = 0L; + } + + lastStmp = currStmp; + + return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分 + | datacenterId << DATACENTER_LEFT //数据中心部分 + | machineId << MACHINE_LEFT //机器标识部分 + | sequence; //序列号部分 + } + + private long getNextMill() + { + long mill = getNewstmp(); + while (mill <= lastStmp) + { + mill = getNewstmp(); + } + return mill; + } + + private long getNewstmp() + { + return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; + } + } + + + + +} diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/WarnTable.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/WarnTable.cs index f15bfcd..d2d60d2 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/WarnTable.cs +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FreeDb/WarnTable.cs @@ -15,8 +15,8 @@ namespace Mesnac.Action.ChemicalWeighing.FreeDb /// /// 主键 /// - [Column(IsPrimary = true, IsIdentity = true)] - public int Id { get; set; } + [Column(IsPrimary = true, IsIdentity = false)] + public long Id { get; set; } public int AlarmId { get; set; } diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/MainDetailControl/MainDetailControlAction.cs b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/MainDetailControl/MainDetailControlAction.cs index 28d6edb..4808ae4 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/MainDetailControl/MainDetailControlAction.cs +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/MainDetailControl/MainDetailControlAction.cs @@ -917,6 +917,7 @@ namespace Mesnac.Action.ChemicalWeighing.MainDetailControl string cnInfo = rows["Alarm_Cn_Info"].ToString(); WarnTable warnTable = new WarnTable(); + warnTable.Id = IdWorker.Singleton.nextId(); warnTable.CnInfo = cnInfo; warnTable.AlarmId = Alarm_ID; warnTable.OtherInfo = otherInfo; @@ -984,9 +985,7 @@ namespace Mesnac.Action.ChemicalWeighing.MainDetailControl Sql.Delete().Where(x => x.CreateTime < DateTime.Now).ExecuteAffrows(); } - //只记录等于1的 - var ls=list.Where(x=>x.Status==1); - Sql.Insert(ls).ExecuteInserted(); + Sql.Insert(list).ExecuteInserted(); diff --git a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj index 7a87fbc..9b3a33b 100644 --- a/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj +++ b/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Mesnac.Action.ChemicalWeighing.csproj @@ -286,6 +286,7 @@ +