From 77d5c19cb6be3e08b44b81a622af41dab191733d Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Sat, 31 Jul 2021 20:55:11 +0800 Subject: [PATCH] Support snowflake id --- .../InsertableProvider/InsertableProvider.cs | 57 ++++++++++++++++++- .../Snowflake/SnowFlakeSingle.cs | 6 +- Src/Asp.Net/SqlSugar/Interface/Insertable.cs | 4 ++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index ce3df8621..8c7622e8e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -85,7 +85,62 @@ namespace SqlSugar return result; } - + public virtual long ExecuteReturnSnowflakeId() + { + var id = SnowFlakeSingle.instance.getID(); + var entity = this.Context.EntityMaintenance.GetEntityInfo(); + var snowProperty=entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); + Check.Exception(snowProperty==null, "The entity sets the primary key and is long"); + foreach (var item in this.InsertObjs) + { + snowProperty.PropertyInfo.SetValue(item,id); + } + this.ExecuteCommand(); + return id; + } + public List ExecuteReturnSnowflakeIdList() + { + List result = new List(); + var entity = this.Context.EntityMaintenance.GetEntityInfo(); + var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); + Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); + foreach (var item in InsertObjs) + { + var id = SnowFlakeSingle.instance.getID();; + snowProperty.PropertyInfo.SetValue(item, id); + result.Add(id); + } + this.ExecuteCommand(); + return result; + } + public async Task ExecuteReturnSnowflakeIdAsync() + { + var id = SnowFlakeSingle.instance.getID(); + var entity = this.Context.EntityMaintenance.GetEntityInfo(); + var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); + Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); + foreach (var item in this.InsertObjs) + { + snowProperty.PropertyInfo.SetValue(item, id); + } + await this.ExecuteCommandAsync(); + return id; + } + public async Task> ExecuteReturnSnowflakeIdListAsync() + { + List result = new List(); + var entity = this.Context.EntityMaintenance.GetEntityInfo(); + var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); + Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); + foreach (var item in InsertObjs) + { + var id = SnowFlakeSingle.instance.getID(); ; + snowProperty.PropertyInfo.SetValue(item, id); + result.Add(id); + } + await this.ExecuteCommandAsync(); + return result; + } public virtual T ExecuteReturnEntity() { diff --git a/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs b/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs index 84fd706da..93e27c7c5 100644 --- a/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs +++ b/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs @@ -4,21 +4,21 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace SqlSugar.DistributedSystem.Snowflake +namespace SqlSugar { public sealed class SnowFlakeSingle { public static readonly SnowFlakeSingle instance = new SnowFlakeSingle(); private SnowFlakeSingle() { - worker = new Snowflake.IdWorker(1, 1); + worker = new DistributedSystem.Snowflake.IdWorker(1, 1); } static SnowFlakeSingle() { } public static SnowFlakeSingle Instance { get { return instance; } } - private Snowflake.IdWorker worker; + private DistributedSystem.Snowflake.IdWorker worker; public long getID() { return worker.NextId(); diff --git a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs index 11402f753..fec2ee6ce 100644 --- a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs @@ -12,6 +12,10 @@ namespace SqlSugar InsertBuilder InsertBuilder { get; set; } int ExecuteCommand(); Task ExecuteCommandAsync(); + long ExecuteReturnSnowflakeId(); + List ExecuteReturnSnowflakeIdList(); + Task ExecuteReturnSnowflakeIdAsync(); + Task> ExecuteReturnSnowflakeIdListAsync(); int ExecuteReturnIdentity(); Task ExecuteReturnIdentityAsync(); T ExecuteReturnEntity();