From e280547e4d44bb402ad43ba991e18f75831c3084 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Sat, 26 Aug 2023 15:04:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8DbContext(DbContextOptionsBui?= =?UTF-8?q?lder(SqlConnection))=E8=84=B1=E7=A6=BBABP=E4=BB=A3=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E6=B7=B7=E5=90=88=E4=BD=BF=E7=94=A8ADO.NET=E5=92=8CEF?= =?UTF-8?q?Core=EF=BC=8C=E5=8A=A8=E6=80=81=E5=88=86=E8=A1=A8=E5=B9=B6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=94=AF=E4=B8=80=E7=B4=A2=E5=BC=95=E5=92=8C?= =?UTF-8?q?=E8=81=9A=E9=9B=86=E7=B4=A2=E5=BC=95=EF=BC=8C=E6=8C=89=E5=AD=A3?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E8=A1=A8=E5=92=8C=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entities/BQ/VmiAppService.cs | 58 ++++++++++++------- .../Entities/BQ/VmiAsyncBalanceService.cs | 28 +++++++-- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs index 2e8f1819..428730f0 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Data.SqlClient; +using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; @@ -138,7 +140,7 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen db.Set().Add(snapshot); db.SaveChanges(); db.Database.ExecuteSqlRaw($"select * into {table} from Set_VmiBalance;"); - db.Database.ExecuteSqlRaw($"create clustered index IX_{table} on {table} (BillTime)"); + db.Database.ExecuteSqlRaw($"create clustered index IX_{table}_BillTime on {table} (BillTime)"); snapshot.End = DateTime.Now; transaction.Commit(); return Task.CompletedTask; @@ -217,31 +219,45 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen var start = DateTime.Parse(input.Filters.FirstOrDefault(o => o.Column == "changedTime" && o.Action == EnumFilterAction.BiggerThanOrEqual).Value); var end = DateTime.Parse(input.Filters.FirstOrDefault(o => o.Column == "changedTime" && o.Action == EnumFilterAction.SmallThan).Value); var tables = new List(); - for (var time = start; time <= end; time = time.AddMonths(1)) + + var connectionString = this._serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); + using var connection = new SqlConnection(connectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + try { - var tableName = $"Set_VmiLog_{time.Year}_{(time.Month - 1) / 3 + 1}"; - if (!tables.Contains(tableName)) + for (var time = start; time <= end; time = time.AddMonths(1)) { - tables.Add(tableName); + var tableName = $"Set_VmiLog_{time.Year}_{(time.Month - 1) / 3 + 1}"; + if (!tables.Contains(tableName)) + { + var command = connection.CreateCommand(); + command.Transaction = transaction; + command.CommandText = $"select OBJECT_ID('{tableName}', 'U')"; + var result = command.ExecuteScalar().ToString(); + if (result != string.Empty) + { + tables.Add(tableName); + } + else + { + Debug.WriteLine($"{tableName}不存在"); + //以为代码为测试建表建唯一索引和物理索引 + command.CommandText = $"select * into {tableName} from Set_VmiLog where 1=0;"; + command.CommandText += $"create unique index IX_{tableName}_Id on {tableName} (Id);"; + command.CommandText += $"create clustered index IX_{tableName}_ChangedTime on {tableName} (ChangedTime);"; + command.ExecuteNonQuery(); + } + } } + transaction.Commit(); } - - //不存在则创建table - tables.ForEach(tableName => + catch (Exception ex) { - using var scope = this._serviceProvider.CreateScope(); - var context = scope.ServiceProvider.GetRequiredService(); - using var connection = context.Database.GetDbConnection(); - connection.Open(); - var cmd = connection.CreateCommand(); - cmd.CommandText = $"select OBJECT_ID('{tableName}', 'U')"; - var result = cmd.ExecuteScalar().ToString(); - if (result == string.Empty) - { - cmd.CommandText = $"select * into {tableName} from Set_VmiLog;create clustered index IX_{tableName} on {tableName} (BillTime);"; - cmd.ExecuteNonQuery(); - } - }); + Console.WriteLine(ex.ToString()); + transaction.Rollback(); + throw; + } //生成 union all 的 SQL var sql = $"select * from {tables.First()}"; diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs index e08d27f8..4b0b9a44 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs @@ -1,9 +1,12 @@ using System; +using System.Data.SqlClient; using System.Linq; using System.Linq.Dynamic.Core; using System.Text.Json; using System.Threading.Tasks; using Magicodes.ExporterAndImporter.Core.Extension; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Omu.ValueInjecter; using Volo.Abp.Application.Services; @@ -27,18 +30,35 @@ public class VmiAsyncBalanceService : ApplicationService, IJobService, ITransien public async Task Invoke(IServiceProvider serviceProvider) { - using var scope = _serviceProvider.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - using var transaction = db.Database.BeginTransaction(); + + var connectionString = serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); + using var connection = new SqlConnection(connectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); try { + var options = new DbContextOptionsBuilder().UseSqlServer(connection).Options; + using var db = new SettleAccountDbContext(options); var messages = db.Set().Where(o => !o.isConsumed).OrderBy(o => o.Number).ToList(); var repo = db.Set(); foreach (var message in messages) { var log = JsonSerializer.Deserialize(message.Message); //插入分表 - //var tableName = $"Set_VmiLog_{log.BillTime.Year}_{(time.Month - 1) / 3 + 1}"; + var tableName = $"Set_VmiLog_{log.ChangedTime.Year}_{(log.ChangedTime.Month - 1) / 3 + 1}"; + var command = connection.CreateCommand(); + command.Transaction = transaction; + command.CommandText = $"select OBJECT_ID('{tableName}', 'U')"; + var result = command.ExecuteScalar().ToString(); + if (result == string.Empty) + { + command.CommandText = $"select * into {tableName} from Set_VmiLog where 1=0;"; + command.CommandText += $"create unique index IX_{tableName}_Id on {tableName} (Id);"; + command.CommandText += $"create clustered index IX_{tableName}_ChangedTime on {tableName} (ChangedTime);"; + command.ExecuteNonQuery(); + } + //插入到分表 + command.CommandText = $"insert into {tableName} select * from Set_VmiLog where id ='{log.Id}'"; //插入库存 var balance = db.Set().FirstOrDefault( o => o.DeliverBillType == log.DeliverBillType &&