|
|
@ -138,6 +138,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)"); |
|
|
|
snapshot.End = DateTime.Now; |
|
|
|
transaction.Commit(); |
|
|
|
return Task.CompletedTask; |
|
|
@ -212,10 +213,9 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen |
|
|
|
[HttpPost] |
|
|
|
public async Task<PagedResultDto<VmiLog>> Log(LogRequestDto input) |
|
|
|
{ |
|
|
|
using var scope = this._serviceProvider.CreateScope(); |
|
|
|
var db = scope.ServiceProvider.GetRequiredService<SettleAccountDbContext>(); |
|
|
|
var start = DateTime.Parse(input.Filters.FirstOrDefault(o => o.Column == "billTime" && o.Action == EnumFilterAction.BiggerThanOrEqual).Value); |
|
|
|
var end = DateTime.Parse(input.Filters.FirstOrDefault(o => o.Column == "billTime" && o.Action == EnumFilterAction.BiggerThanOrEqual).Value); |
|
|
|
//按季度计算查询需要 union 的表名
|
|
|
|
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>(); |
|
|
|
for (var time = start; time <= end; time = time.AddMonths(1)) |
|
|
|
{ |
|
|
@ -223,18 +223,32 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen |
|
|
|
if (!tables.Contains(tableName)) |
|
|
|
{ |
|
|
|
tables.Add(tableName); |
|
|
|
if (db.Database.ExecuteSqlRaw($"select OBJECT_ID('{tableName}', 'U')") == -1) |
|
|
|
{ |
|
|
|
var tran = db.Database.BeginTransaction(); |
|
|
|
db.Database.ExecuteSqlRaw($"select * into {tableName} from Set_VmiLog"); |
|
|
|
db.Database.ExecuteSqlRaw($"create clustered index IX_{tableName} on {tableName} (BillTime)"); |
|
|
|
tran.Commit(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//不存在则创建table
|
|
|
|
tables.ForEach(tableName => |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
cmd.CommandText = $"select * into {tableName} from Set_VmiLog;create clustered index IX_{tableName} on {tableName} (BillTime);"; |
|
|
|
cmd.ExecuteNonQuery(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
//生成 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 filters = input.Filters.ToLambda<VmiLog>(); |
|
|
|
if (input.Filters.Count > 0) |
|
|
|