dep
杨威 6 months ago
parent baace36f1d
commit 542e8e080e

@ -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;
/// <summary>
/// Twitter的分布式自增ID雪花算法
/// </summary>
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<IdWorker> lazy = new Lazy<IdWorker>(() => 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;
}
/// <summary>
/// 产生下一个ID
/// </summary>
/// <returns></returns>
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;
}
}
}

@ -15,8 +15,8 @@ namespace Mesnac.Action.ChemicalWeighing.FreeDb
/// <summary> /// <summary>
/// 主键 /// 主键
/// </summary> /// </summary>
[Column(IsPrimary = true, IsIdentity = true)] [Column(IsPrimary = true, IsIdentity = false)]
public int Id { get; set; } public long Id { get; set; }
public int AlarmId { get; set; } public int AlarmId { get; set; }

@ -917,6 +917,7 @@ namespace Mesnac.Action.ChemicalWeighing.MainDetailControl
string cnInfo = rows["Alarm_Cn_Info"].ToString(); string cnInfo = rows["Alarm_Cn_Info"].ToString();
WarnTable warnTable = new WarnTable(); WarnTable warnTable = new WarnTable();
warnTable.Id = IdWorker.Singleton.nextId();
warnTable.CnInfo = cnInfo; warnTable.CnInfo = cnInfo;
warnTable.AlarmId = Alarm_ID; warnTable.AlarmId = Alarm_ID;
warnTable.OtherInfo = otherInfo; warnTable.OtherInfo = otherInfo;
@ -984,9 +985,7 @@ namespace Mesnac.Action.ChemicalWeighing.MainDetailControl
Sql.Delete<WarnTable>().Where(x => x.CreateTime < DateTime.Now).ExecuteAffrows(); Sql.Delete<WarnTable>().Where(x => x.CreateTime < DateTime.Now).ExecuteAffrows();
} }
//只记录等于1的 Sql.Insert<WarnTable>(list).ExecuteInserted();
var ls=list.Where(x=>x.Status==1);
Sql.Insert<WarnTable>(ls).ExecuteInserted();

@ -286,6 +286,7 @@
<Compile Include="FreeDb\DBEntity\WaterDownloadEntity.cs" /> <Compile Include="FreeDb\DBEntity\WaterDownloadEntity.cs" />
<Compile Include="FreeDb\DryDownloadDetail.cs" /> <Compile Include="FreeDb\DryDownloadDetail.cs" />
<Compile Include="FreeDb\FreeSqlUnit.cs" /> <Compile Include="FreeDb\FreeSqlUnit.cs" />
<Compile Include="FreeDb\IdWorker.cs" />
<Compile Include="FreeDb\WarnTable.cs" /> <Compile Include="FreeDb\WarnTable.cs" />
<Compile Include="InterfaceDocking\DockingEntity\JsonReturnEntity.cs" /> <Compile Include="InterfaceDocking\DockingEntity\JsonReturnEntity.cs" />
<Compile Include="InterfaceDocking\DockingEntity\MaterialPlanSyncEntity.cs" /> <Compile Include="InterfaceDocking\DockingEntity\MaterialPlanSyncEntity.cs" />

Loading…
Cancel
Save