|
|
@ -223,49 +223,34 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen |
|
|
|
var connectionString = this._serviceProvider.GetRequiredService<IConfiguration>().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<SettleAccountDbContext>().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<SettleAccountDbContext>(); |
|
|
|
var query = db.Set<VmiLog>().FromSqlRaw(sql); |
|
|
|
var query = string.IsNullOrEmpty(sql) ? db.Set<VmiLog>().AsQueryable() : db.Set<VmiLog>().FromSqlRaw(sql); |
|
|
|
var filters = input.Filters.ToLambda<VmiLog>(); |
|
|
|
if (input.Filters.Count > 0) |
|
|
|
{ |
|
|
|