wanggang 1 year ago
parent
commit
4fa854906e
  1. 31
      code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs
  2. 9
      code/src/Modules/SettleAccount/src/SettleAccount.EntityFrameworkCore/EntityFrameworkCore/SettleAccountDbContextModelCreatingExtensions.cs

31
code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs

@ -223,16 +223,12 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
var connectionString = this._serviceProvider.GetRequiredService<IConfiguration>().GetConnectionString("SettleAccountService"); var connectionString = this._serviceProvider.GetRequiredService<IConfiguration>().GetConnectionString("SettleAccountService");
using var connection = new SqlConnection(connectionString); using var connection = new SqlConnection(connectionString);
connection.Open(); 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}"; var tableName = $"Set_VmiLog_{time.Year}_{(time.Month - 1) / 3 + 1}";
if (!tables.Contains(tableName)) if (!tables.Contains(tableName))
{ {
var command = connection.CreateCommand(); var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = $"select OBJECT_ID('{tableName}', 'U')"; command.CommandText = $"select OBJECT_ID('{tableName}', 'U')";
var result = command.ExecuteScalar().ToString(); var result = command.ExecuteScalar().ToString();
if (result != string.Empty) if (result != string.Empty)
@ -242,30 +238,19 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
else else
{ {
Debug.WriteLine($"{tableName}不存在"); 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();
} }
catch (Exception ex) var options = new DbContextOptionsBuilder<SettleAccountDbContext>().UseSqlServer(connection).Options;
using var db = new SettleAccountDbContext(options);
var sql = string.Empty;
if (tables.Any())
{ {
Console.WriteLine(ex.ToString()); //生成 union all 的 SQL使用 FromSqlRaw 查询,如果没有分表则使用原始表
transaction.Rollback(); sql = $"select * from {tables.First()}";
throw;
}
//生成 union all 的 SQL
var sql = $"select * from {tables.First()}";
tables.Skip(1).ForEach(o => sql += $" union all select * from ${o}"); tables.Skip(1).ForEach(o => sql += $" union all select * from ${o}");
//使用 FromSqlRaw 查询 }
using var scope = this._serviceProvider.CreateScope(); var query = string.IsNullOrEmpty(sql) ? db.Set<VmiLog>().AsQueryable() : db.Set<VmiLog>().FromSqlRaw(sql);
var db = scope.ServiceProvider.GetRequiredService<SettleAccountDbContext>();
var query = db.Set<VmiLog>().FromSqlRaw(sql);
var filters = input.Filters.ToLambda<VmiLog>(); var filters = input.Filters.ToLambda<VmiLog>();
if (input.Filters.Count > 0) if (input.Filters.Count > 0)
{ {

9
code/src/Modules/SettleAccount/src/SettleAccount.EntityFrameworkCore/EntityFrameworkCore/SettleAccountDbContextModelCreatingExtensions.cs

@ -1358,14 +1358,13 @@ namespace Win.Sfs.SettleAccount
}); });
//seed //seed
builder.Entity<VmiSyncTask>().HasData(new VmiSyncTask("Set_VmiLog".ToGuid()) { Name = "库存事务备份", Number = "Set_VmiLog", LastUpdate = DateTime.Now.AddDays(-5).Date });
builder.Entity<VmiSyncTask>().HasData(new VmiSyncTask("Set_VmiBalance".ToGuid()) { Name = "库存余额备份", Number = "Set_VmiBalance", LastUpdate = DateTime.Now.AddDays(-5).Date });
builder.Entity<JobItem>().HasData(new JobItem("vmi".ToGuid()) { Name = "库存快照", Cron = "0 0 8 26 *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAppService" }); builder.Entity<JobItem>().HasData(new JobItem("vmi".ToGuid()) { Name = "库存快照", Cron = "0 0 8 26 *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAppService" });
builder.Entity<JobItem>().HasData(new JobItem("vmi.balance".ToGuid()) { Name = "同步库存", Cron = "0 0/1 * * * ?", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncBalanceService" }); builder.Entity<JobItem>().HasData(new JobItem("vmi.balance".ToGuid()) { Name = "同步库存", Cron = "0 0/1 * * * ?", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncBalanceService" });
builder.Entity<JobItem>().HasData(new JobItem("vmi.message".ToGuid()) { Name = "消息监控", Cron = "0 0/1 * * * ?", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); builder.Entity<JobItem>().HasData(new JobItem("vmi.message".ToGuid()) { Name = "消息监控", Cron = "0 0/1 * * * ?", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" });
builder.Entity<JobItem>().HasData(new JobItem("vmi.log.sync".ToGuid()) { Name = "库存事务同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); //builder.Entity<VmiSyncTask>().HasData(new VmiSyncTask("Set_VmiLog".ToGuid()) { Name = "库存事务备份", Number = "Set_VmiLog", LastUpdate = DateTime.Now.AddDays(-5).Date });
builder.Entity<JobItem>().HasData(new JobItem("vmi.balance.sync".ToGuid()) { Name = "库存余额同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" }); //builder.Entity<VmiSyncTask>().HasData(new VmiSyncTask("Set_VmiBalance".ToGuid()) { Name = "库存余额备份", Number = "Set_VmiBalance", LastUpdate = DateTime.Now.AddDays(-5).Date });
//builder.Entity<JobItem>().HasData(new JobItem("vmi.log.sync".ToGuid()) { Name = "库存事务同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" });
//builder.Entity<JobItem>().HasData(new JobItem("vmi.balance.sync".ToGuid()) { Name = "库存余额同步", Cron = "0 0/5 * * * ? *", Service = "Win.Sfs.SettleAccount.Entities.BQ.VmiAsyncMessageService" });
builder.Entity<JobItem>().HasData(new JobItem("JisBBACSeSync".ToGuid()) { Name = "JisBBAC发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.JisBBACSeSyncAppService" }); builder.Entity<JobItem>().HasData(new JobItem("JisBBACSeSync".ToGuid()) { Name = "JisBBAC发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.JisBBACSeSyncAppService" });
builder.Entity<JobItem>().HasData(new JobItem("JisHBPOSeSync".ToGuid()) { Name = "JisHBPO发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.JisHBPOSeSyncAppService" }); builder.Entity<JobItem>().HasData(new JobItem("JisHBPOSeSync".ToGuid()) { Name = "JisHBPO发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.JisHBPOSeSyncAppService" });
builder.Entity<JobItem>().HasData(new JobItem("MaiDanBBACSeSync".ToGuid()) { Name = "买单件BBAC发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.MaiDanBBACSeSyncAppService" }); builder.Entity<JobItem>().HasData(new JobItem("MaiDanBBACSeSync".ToGuid()) { Name = "买单件BBAC发运数据同步", Cron = "0 0/30 * * * ? ", Service = "Win.Sfs.SettleAccount.Entities.BQ.Syncs.MaiDanBBACSeSyncAppService" });

Loading…
Cancel
Save