|
@ -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) |
|
|
{ |
|
|
{ |
|
|