From febb2f360cf6e08f1c8aade4551772852817bd5a Mon Sep 17 00:00:00 2001 From: FunCoder Date: Wed, 16 Apr 2025 09:43:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E8=BD=AC=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=AF=B9sqlite=E3=80=81pg=E3=80=81mysql?= =?UTF-8?q?=E3=80=81sqlserver=E9=87=87=E7=94=A8=E6=89=B9=E9=87=8F=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Enum/SysEnumService.cs | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/Service/Enum/SysEnumService.cs b/Admin.NET/Admin.NET.Core/Service/Enum/SysEnumService.cs index 0a76b4a3..a541826c 100644 --- a/Admin.NET/Admin.NET.Core/Service/Enum/SysEnumService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Enum/SysEnumService.cs @@ -123,30 +123,54 @@ public class SysEnumService : IDynamicApiController, ITransient var newEnumType = enumTypeList.Where(u => !updatedEnumCodes.Contains(u.TypeName)).ToList(); var (newDictTypes, newDictDatas) = GetNewSysDicts(newEnumType); + // 若是sqlite、pg、mysql、sqlserver则采用批量处理 + bool useBulk = _db.CurrentConnectionConfig.DbType == SqlSugar.DbType.Sqlite + || _db.CurrentConnectionConfig.DbType == SqlSugar.DbType.PostgreSQL + || _db.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql + || _db.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySqlConnector + || _db.CurrentConnectionConfig.DbType == SqlSugar.DbType.SqlServer; + // 执行数据库操作 if (updatedDictTypes.Count > 0) - await _db.Updateable(updatedDictTypes).ExecuteCommandAsync(); + { + if (useBulk) await _db.Fastest().PageSize(300).BulkMergeAsync(updatedDictTypes); + else await _db.Updateable(updatedDictTypes).ExecuteCommandAsync(); + } if (updatedDictDatas.Count > 0) - await _db.Updateable(updatedDictDatas).ExecuteCommandAsync(); + { + if (useBulk) await _db.Fastest().PageSize(300).BulkMergeAsync(updatedDictDatas); + else await _db.Updateable(updatedDictDatas).ExecuteCommandAsync(); + } if (newSysDictDatas.Count > 0) { - // 达梦:用db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入 - // 达梦:不支持storageable2.BulkUpdateAsync 功能 - foreach (var dd in newSysDictDatas) - await _db.Insertable(dd).ExecuteCommandAsync(); + if (useBulk) await _db.Fastest().PageSize(300).BulkMergeAsync(newSysDictDatas); + else + { + // 达梦:用db.Insertable(newSysDictDatas).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入 + // 达梦:不支持storageable2.BulkUpdateAsync 功能 + foreach (var dd in newSysDictDatas) + await _db.Insertable(dd).ExecuteCommandAsync(); + } } if (newDictTypes.Count > 0) - await _db.Insertable(newDictTypes).ExecuteCommandAsync(); + { + if (useBulk) await _db.Fastest().PageSize(300).BulkMergeAsync(newDictTypes); + else await _db.Insertable(newDictTypes).ExecuteCommandAsync(); + } if (newDictDatas.Count > 0) { - // 达梦:用db.Insertable(newDictTypes).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入 - // 达梦:不支持storageable2.BulkUpdateAsync 功能 - foreach (var dd in newDictDatas) - await _db.Insertable(dd).ExecuteCommandAsync(); + if (useBulk) await _db.Fastest().PageSize(300).BulkMergeAsync(newDictDatas); + else + { + // 达梦:用db.Insertable(newDictDatas).ExecuteCommandAsync(stoppingToken);插入400条以上会内容溢出错误,所以改用逐条插入 + // 达梦:不支持storageable2.BulkUpdateAsync 功能 + foreach (var dd in newDictDatas) + await _db.Insertable(dd).ExecuteCommandAsync(); + } } }