From 9e7c348b1564170fc2931047490da98c9ac0c791 Mon Sep 17 00:00:00 2001 From: Diego <2248356998@qq.com> Date: Mon, 21 Apr 2025 23:05:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/SugarService/BaseService.cs | 10 ++--- .../SqlSugar/DbContext.cs | 42 +++++++++++++++++++ .../Services/Channel/ChannelService.cs | 4 +- .../Services/Device/DeviceService.cs | 4 +- .../Services/Variable/VariableService.cs | 10 ++--- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/Admin/ThingsGateway.Admin.Application/Services/SugarService/BaseService.cs b/src/Admin/ThingsGateway.Admin.Application/Services/SugarService/BaseService.cs index 606f6b6fa..42c950294 100644 --- a/src/Admin/ThingsGateway.Admin.Application/Services/SugarService/BaseService.cs +++ b/src/Admin/ThingsGateway.Admin.Application/Services/SugarService/BaseService.cs @@ -47,12 +47,10 @@ public class BaseService : IDataService, IDisposable where T : class, new( } /// - public Task DeleteAsync(IEnumerable models) + public async Task DeleteAsync(IEnumerable models) { - if (models.FirstOrDefault() is IPrimaryIdEntity) - return DeleteAsync(models.Select(a => ((IPrimaryIdEntity)a).Id)); - else - return Task.FromResult(false); + using var db = GetDB(); + return await db.Deleteable().In(models.ToList()).ExecuteCommandHasChangeAsync().ConfigureAwait(false); } /// @@ -165,4 +163,6 @@ public class BaseService : IDataService, IDisposable where T : class, new( { return DbContext.Db.GetConnectionScopeWithAttr().CopyNew(); } + + } diff --git a/src/Admin/ThingsGateway.Admin.Application/SqlSugar/DbContext.cs b/src/Admin/ThingsGateway.Admin.Application/SqlSugar/DbContext.cs index c9a844401..006d3d2aa 100644 --- a/src/Admin/ThingsGateway.Admin.Application/SqlSugar/DbContext.cs +++ b/src/Admin/ThingsGateway.Admin.Application/SqlSugar/DbContext.cs @@ -103,4 +103,46 @@ public static class DbContext Console.WriteLine("【Sql执行时间】:" + DateTime.Now.ToDefaultDateTimeFormat()); Console.WriteLine("【Sql语句】:" + msg + Environment.NewLine); } + + + + public static async Task BulkCopyAsync(this SqlSugarClient db, List datas, int size) where TITEM : class, new() + { + switch (db.CurrentConnectionConfig.DbType) + { + case DbType.MySql: + case DbType.SqlServer: + case DbType.Sqlite: + case DbType.Oracle: + case DbType.PostgreSQL: + case DbType.Dm: + case DbType.MySqlConnector: + case DbType.Kdbndp: + await db.Fastest().PageSize(size).BulkCopyAsync(datas).ConfigureAwait(false); + break; + default: + await db.Insertable(datas).PageSize(size).ExecuteCommandAsync().ConfigureAwait(false); + break; + } + + } + public static async Task BulkUpdateAsync(this SqlSugarClient db, List datas, int size) where TITEM : class, new() + { + switch (db.CurrentConnectionConfig.DbType) + { + case DbType.MySql: + case DbType.SqlServer: + case DbType.Sqlite: + case DbType.Oracle: + case DbType.PostgreSQL: + case DbType.Dm: + case DbType.MySqlConnector: + case DbType.Kdbndp: + await db.Fastest().PageSize(size).BulkUpdateAsync(datas).ConfigureAwait(false); + break; + default: + await db.Updateable(datas).PageSize(size).ExecuteCommandAsync().ConfigureAwait(false); + break; + } + } } diff --git a/src/Gateway/ThingsGateway.Gateway.Application/Services/Channel/ChannelService.cs b/src/Gateway/ThingsGateway.Gateway.Application/Services/Channel/ChannelService.cs index 6ae011cd0..7b734e303 100644 --- a/src/Gateway/ThingsGateway.Gateway.Application/Services/Channel/ChannelService.cs +++ b/src/Gateway/ThingsGateway.Gateway.Application/Services/Channel/ChannelService.cs @@ -329,8 +329,8 @@ internal sealed class ChannelService : BaseService, IChannelService ManageHelper.CheckChannelCount(insertData.Count); using var db = GetDB(); - await db.Fastest().PageSize(100000).BulkCopyAsync(insertData).ConfigureAwait(false); - await db.Fastest().PageSize(100000).BulkUpdateAsync(upData).ConfigureAwait(false); + await db.BulkCopyAsync(insertData, 100000).ConfigureAwait(false); + await db.BulkUpdateAsync(upData, 100000).ConfigureAwait(false); DeleteChannelFromCache(); return channels.Select(a => a.Id).ToHashSet(); } diff --git a/src/Gateway/ThingsGateway.Gateway.Application/Services/Device/DeviceService.cs b/src/Gateway/ThingsGateway.Gateway.Application/Services/Device/DeviceService.cs index b7c0e71ae..0a91dce91 100644 --- a/src/Gateway/ThingsGateway.Gateway.Application/Services/Device/DeviceService.cs +++ b/src/Gateway/ThingsGateway.Gateway.Application/Services/Device/DeviceService.cs @@ -351,8 +351,8 @@ internal sealed class DeviceService : BaseService, IDeviceService ManageHelper.CheckDeviceCount(insertData.Count); using var db = GetDB(); - await db.Fastest().PageSize(100000).BulkCopyAsync(insertData).ConfigureAwait(false); - await db.Fastest().PageSize(100000).BulkUpdateAsync(upData).ConfigureAwait(false); + await db.BulkCopyAsync(insertData, 100000).ConfigureAwait(false); + await db.BulkUpdateAsync(upData, 100000).ConfigureAwait(false); DeleteDeviceFromCache(); return devices.Select(a => a.Id).ToHashSet(); } diff --git a/src/Gateway/ThingsGateway.Gateway.Application/Services/Variable/VariableService.cs b/src/Gateway/ThingsGateway.Gateway.Application/Services/Variable/VariableService.cs index df28fb5b9..f0109625d 100644 --- a/src/Gateway/ThingsGateway.Gateway.Application/Services/Variable/VariableService.cs +++ b/src/Gateway/ThingsGateway.Gateway.Application/Services/Variable/VariableService.cs @@ -217,9 +217,9 @@ internal sealed class VariableService : BaseService, IVariableService var result = await db.UseTranAsync(async () => { - await db.Fastest().PageSize(100000).BulkCopyAsync(newChannels).ConfigureAwait(false); - await db.Fastest().PageSize(100000).BulkCopyAsync(newDevices).ConfigureAwait(false); - await db.Fastest().PageSize(100000).BulkCopyAsync(newVariables).ConfigureAwait(false); + await db.BulkCopyAsync(newChannels, 100000).ConfigureAwait(false); + await db.BulkCopyAsync(newDevices, 100000).ConfigureAwait(false); + await db.BulkCopyAsync(newVariables, 100000).ConfigureAwait(false); }).ConfigureAwait(false); if (result.IsSuccess)//如果成功了 { @@ -486,8 +486,8 @@ internal sealed class VariableService : BaseService, IVariableService var insertData = variables.Where(a => !a.IsUp).ToList(); ManageHelper.CheckVariableCount(insertData.Count); using var db = GetDB(); - await db.Fastest().PageSize(100000).BulkCopyAsync(insertData).ConfigureAwait(false); - await db.Fastest().PageSize(100000).BulkUpdateAsync(upData).ConfigureAwait(false); + await db.BulkCopyAsync(insertData, 100000).ConfigureAwait(false); + await db.BulkUpdateAsync(upData, 100000).ConfigureAwait(false); _dispatchService.Dispatch(new()); DeleteVariableCache(); return variables.Select(a => a.Id).ToHashSet();