|
|
@ -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<VmiSnapshot>().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<string>(); |
|
|
|
|
|
|
|
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)) |
|
|
|
{ |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//不存在则创建table
|
|
|
|
tables.ForEach(tableName => |
|
|
|
else |
|
|
|
{ |
|
|
|
using var scope = this._serviceProvider.CreateScope(); |
|
|
|
var context = scope.ServiceProvider.GetRequiredService<SettleAccountDbContext>(); |
|
|
|
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) |
|
|
|
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) |
|
|
|
{ |
|
|
|
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()}"; |
|
|
|