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();