From 4fa854906e67b3739677ebb4458a9ca34ea94601 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 28 Aug 2023 08:51:04 +0800 Subject: [PATCH] up --- .../Entities/BQ/VmiAppService.cs | 55 +++++++------------ ...AccountDbContextModelCreatingExtensions.cs | 9 ++- 2 files changed, 24 insertions(+), 40 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 eca74723..d8624c42 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 @@ -223,49 +223,34 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen var connectionString = this._serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); using var connection = new SqlConnection(connectionString); connection.Open(); - using var transaction = connection.BeginTransaction(); - try + for (var time = start; time <= end; time = time.AddMonths(1)) { - for (var time = start; time <= end; time = time.AddMonths(1)) + var tableName = $"Set_VmiLog_{time.Year}_{(time.Month - 1) / 3 + 1}"; + if (!tables.Contains(tableName)) { - var tableName = $"Set_VmiLog_{time.Year}_{(time.Month - 1) / 3 + 1}"; - if (!tables.Contains(tableName)) + var command = connection.CreateCommand(); + command.CommandText = $"select OBJECT_ID('{tableName}', 'U')"; + var result = command.ExecuteScalar().ToString(); + if (result != string.Empty) { - 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(); - } + tables.Add(tableName); + } + else + { + Debug.WriteLine($"{tableName}不存在"); } } - transaction.Commit(); } - catch (Exception ex) + var options = new DbContextOptionsBuilder().UseSqlServer(connection).Options; + using var db = new SettleAccountDbContext(options); + var sql = string.Empty; + if (tables.Any()) { - Console.WriteLine(ex.ToString()); - transaction.Rollback(); - throw; + //生成 union all 的 SQL使用 FromSqlRaw 查询,如果没有分表则使用原始表 + sql = $"select * from {tables.First()}"; + tables.Skip(1).ForEach(o => sql += $" union all select * from ${o}"); } - - //生成 union all 的 SQL - var sql = $"select * from {tables.First()}"; - tables.Skip(1).ForEach(o => sql += $" union all select * from ${o}"); - //使用 FromSqlRaw 查询 - using var scope = this._serviceProvider.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - var query = db.Set().FromSqlRaw(sql); + var query = string.IsNullOrEmpty(sql) ? db.Set().AsQueryable() : db.Set().FromSqlRaw(sql); var filters = input.Filters.ToLambda(); if (input.Filters.Count > 0) { diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.EntityFrameworkCore/EntityFrameworkCore/SettleAccountDbContextModelCreatingExtensions.cs b/code/src/Modules/SettleAccount/src/SettleAccount.EntityFrameworkCore/EntityFrameworkCore/SettleAccountDbContextModelCreatingExtensions.cs index d84d35f0..e594e2af 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.EntityFrameworkCore/EntityFrameworkCore/SettleAccountDbContextModelCreatingExtensions.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.EntityFrameworkCore/EntityFrameworkCore/SettleAccountDbContextModelCreatingExtensions.cs @@ -1358,14 +1358,13 @@ namespace Win.Sfs.SettleAccount }); //seed - builder.Entity().HasData(new VmiSyncTask("Set_VmiLog".ToGuid()) { Name = "库存事务备份", Number = "Set_VmiLog", LastUpdate = DateTime.Now.AddDays(-5).Date }); - builder.Entity().HasData(new VmiSyncTask("Set_VmiBalance".ToGuid()) { Name = "库存余额备份", Number = "Set_VmiBalance", LastUpdate = DateTime.Now.AddDays(-5).Date }); - builder.Entity().HasData(new JobItem("vmi".ToGuid()) { Name = "库存快照", Cron = "0 0 8 26 *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAppService" }); builder.Entity().HasData(new JobItem("vmi.balance".ToGuid()) { Name = "同步库存", Cron = "0 0/1 * * * ?", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncBalanceService" }); builder.Entity().HasData(new JobItem("vmi.message".ToGuid()) { Name = "消息监控", Cron = "0 0/1 * * * ?", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); - builder.Entity().HasData(new JobItem("vmi.log.sync".ToGuid()) { Name = "库存事务同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); - builder.Entity().HasData(new JobItem("vmi.balance.sync".ToGuid()) { Name = "库存余额同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); + //builder.Entity().HasData(new VmiSyncTask("Set_VmiLog".ToGuid()) { Name = "库存事务备份", Number = "Set_VmiLog", LastUpdate = DateTime.Now.AddDays(-5).Date }); + //builder.Entity().HasData(new VmiSyncTask("Set_VmiBalance".ToGuid()) { Name = "库存余额备份", Number = "Set_VmiBalance", LastUpdate = DateTime.Now.AddDays(-5).Date }); + //builder.Entity().HasData(new JobItem("vmi.log.sync".ToGuid()) { Name = "库存事务同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); + //builder.Entity().HasData(new JobItem("vmi.balance.sync".ToGuid()) { Name = "库存余额同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); builder.Entity().HasData(new JobItem("JisBBACSeSync".ToGuid()) { Name = "JisBBAC发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.JisBBACSeSyncAppService" }); builder.Entity().HasData(new JobItem("JisHBPOSeSync".ToGuid()) { Name = "JisHBPO发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.JisHBPOSeSyncAppService" }); builder.Entity().HasData(new JobItem("MaiDanBBACSeSync".ToGuid()) { Name = "买单件BBAC发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.MaiDanBBACSeSyncAppService" });