@ -9,7 +9,6 @@ using System.Reflection;
using System.Threading ;
using System.Threading.Tasks ;
using ClosedXML.Excel ;
using LinqToDB.Data ;
using LinqToDB.EntityFrameworkCore ;
using Magicodes.ExporterAndImporter.Core ;
using Magicodes.ExporterAndImporter.Core.Extension ;
@ -18,8 +17,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.SignalR ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore.Infrastructure ;
using Microsoft.EntityFrameworkCore.Storage ;
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.OpenApi.Extensions ;
@ -35,25 +32,15 @@ using Volo.Abp.Validation;
using Win.Sfs.BaseData.ImportExcelCommon ;
using Win.Sfs.SettleAccount.Entities.BQ.Dtos ;
using Win.Sfs.SettleAccount.Entities.BQ.Vmi ;
using Win.Sfs.SettleAccount.EntityFrameworkCore ;
using Win.Sfs.SettleAccount.influxdb ;
using Win.Sfs.Shared.Filter ;
using Win.Sfs.Shared.RepositoryBase ;
namespace Win.Sfs.SettleAccount.Entities.BQ ;
public interface IVmiService : IApplicationService , ITransientDependency , IJobService
{
Task In ( VmiLogType logType , string changedNumber , VmiBalance data ) ;
Task Out ( VmiLogType logType , string changedNumber , VmiLog data ) ;
//Task UnDo(string groupId);
}
[AllowAnonymous]
[Route("api/settleaccount/[controller] / [ action ] ")]
public class VmiAppService : ApplicationService , IVmiService , I JobService , ITransientDependency
public class VmiAppService : ApplicationService , IJobService , ITransientDependency
{
public static SemaphoreSlim backupLock = new SemaphoreSlim ( 1 ) ;
private readonly IConfiguration _ cfg ;
@ -131,60 +118,31 @@ public class VmiAppService : ApplicationService, IVmiService, IJobService, ITran
/// </summary>
[NonAction]
[DisableValidation]
public virtual async Task Invoke ( IServiceProvider serviceProvider )
public virtual Task Invoke ( IServiceProvider serviceProvider )
{
await backupLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
using var scope = serviceProvider . CreateScope ( ) ;
var db = scope . ServiceProvider . GetRequiredService < SettleAccountDbContext > ( ) ;
using var transaction = db . Database . BeginTransaction ( ) ;
try
{
//this._hubContext.Clients.All.ServerToClient("JobItem", "refresh", "");
Directory . CreateDirectory ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "wwwroot/files/vmi" ) ) ;
var date = DateTime . Now . ToString ( "yyyy-MM-dd_HH.mm.ss" ) ;
var connectionString = $"Data Source=wwwroot/files/vmi/{date}.db" ;
using var dbContext = new VmiSqliteContext ( connectionString ) ;
if ( ! dbContext . GetService < IRelationalDatabaseCreator > ( ) . Exists ( ) & & dbContext . Database . EnsureCreated ( ) )
var table = $"Set_VmiBalance_{DateTime.Now.ToString(" yyyy_mm_dd_hh_mm_ss ")}" ;
var snapshot = db . Set < VmiSnapshot > ( ) . Where ( o = > o . Name = = table ) . FirstOrDefault ( ) ;
if ( snapshot = = null )
{
var query = serviceProvider . GetRequiredService < SettleAccountDbContext > ( ) . Set < VmiBalance > ( ) . AsNoTracking ( ) ;
var list = new List < VmiBalance > ( ) ;
using var dc = dbContext . CreateLinqToDBConnection ( ) ;
dc . BeginTransaction ( ) ;
try
{
var now = DateTime . Now ;
var table = "VmiBalance" ;
foreach ( var item in query )
{
item . BackupTime = now ;
list . Add ( item ) ;
if ( list . Count = = 1 0 0 0 0 )
{
//dbContext.Set<VmiBalance>().AddRange(list);
await dc . BulkCopyAsync ( new BulkCopyOptions { TableName = table } , list ) . ConfigureAwait ( false ) ;
list . Clear ( ) ;
}
}
if ( list . Count > 0 )
{
await dc . BulkCopyAsync ( new BulkCopyOptions { TableName = table } , list ) . ConfigureAwait ( false ) ;
}
dc . Transaction . Commit ( ) ;
var snapshot = new VmiSnapshot { Name = date , Path = connectionString } ;
await _ snapshotRepository . InsertAsync ( snapshot ) . ConfigureAwait ( false ) ;
}
catch
{
dc . Transaction . Rollback ( ) ;
throw ;
}
snapshot = new VmiSnapshot { Name = table , Path = table } ;
db . Set < VmiSnapshot > ( ) . Add ( snapshot ) ;
db . SaveChanges ( ) ;
var result = db . Database . ExecuteSqlRaw ( $"select * into {table} from Set_VmiBalance;" ) ;
transaction . Commit ( ) ;
return Task . CompletedTask ;
}
throw new Exception ( "备份程序正在运行" ) ;
}
catch
{
transaction . Rollback ( ) ;
throw ;
}
finally
{
backupLock . Release ( ) ;
}
}
/// <summary>
@ -192,9 +150,26 @@ public class VmiAppService : ApplicationService, IVmiService, IJobService, ITran
/// </summary>
[HttpPost("invoke")]
[DisableValidation]
public virtual async Task VmiBackup ( )
public virtual async Task < IActionResult > VmiBackup ( )
{
await Invoke ( _ serviceProvider ) . ConfigureAwait ( false ) ;
if ( backupLock . CurrentCount = = 1 )
{
return new JsonResult ( new { Code = 5 0 0 , Message = "备份程序正在运行" } ) ;
}
await backupLock . WaitAsync ( ) . ConfigureAwait ( false ) ;
try
{
await Invoke ( _ serviceProvider ) . ConfigureAwait ( false ) ;
return new JsonResult ( new { Code = 2 0 0 , Message = "备份成功" } ) ;
}
catch ( Exception ex )
{
return new JsonResult ( new { Code = 5 0 0 , Message = ex . Message , Data = ex . ToString ( ) } ) ;
}
finally
{
backupLock . Release ( ) ;
}
}
/// <summary>
@ -216,9 +191,9 @@ public class VmiAppService : ApplicationService, IVmiService, IJobService, ITran
var db = scope . ServiceProvider . GetRequiredService < SettleAccountDbContext > ( ) ;
var balance = db . Set < VmiBalance > ( ) . FirstOrDefault (
o = > o . BillType = = data . BillType & &
o = > o . Deliver BillType = = data . Deliver BillType & &
o . CodeType = = data . CodeType & &
o . PartCod e = = data . PartCod e & &
o . DeliverBillTyp e = = data . DeliverBillTyp e & &
o . VinCode = = data . VinCode & &
o . ErpToLoc = = data . ErpToLoc & &
o . OrderNum = = data . OrderNum & &
@ -296,9 +271,9 @@ public class VmiAppService : ApplicationService, IVmiService, IJobService, ITran
using var scope = _ serviceProvider . CreateScope ( ) ;
var db = scope . ServiceProvider . GetRequiredService < SettleAccountDbContext > ( ) ;
var balance = db . Set < VmiBalance > ( ) . FirstOrDefault (
o = > o . BillType = = data . BillType & &
o = > o . Deliver BillType = = data . Deliver BillType & &
o . CodeType = = data . CodeType & &
o . PartCod e = = data . PartCod e & &
o . DeliverBillTyp e = = data . DeliverBillTyp e & &
o . VinCode = = data . VinCode & &
o . ErpToLoc = = data . ErpToLoc & &
o . OrderNum = = data . OrderNum & &
@ -508,11 +483,16 @@ public class VmiAppService : ApplicationService, IVmiService, IJobService, ITran
[HttpPost]
public async Task < PagedResultDto < VmiBalance > > Backup ( BackupListRequest input )
{
var connectionString = $"Data Source=wwwroot/files/vmi/{input.Name}.db" ;
using var dbContext = new VmiSqliteContext ( connectionString ) ;
var repo = dbContext . Set < VmiBalance > ( ) ;
using var scope = this . _ serviceProvider . CreateScope ( ) ;
var db = scope . ServiceProvider . GetRequiredService < SettleAccountDbContext > ( ) ;
var name = input . Name ;
var sql = $"select * from {name}" ;
var query = db . Set < VmiBalance > ( ) . FromSqlRaw ( sql ) ;
var filters = input . Filters . ToLambda < VmiBalance > ( ) ;
var query = ( input . Filters . Count > 0 ? repo . Where ( input . Filters . ToLambda < VmiBalance > ( ) ) : repo ) ;
if ( input . Filters . Count > 0 )
{
query = query . Where ( input . Filters . ToLambda < VmiBalance > ( ) ) ;
}
var totalCount = query . Count ( ) ;
query = string . IsNullOrEmpty ( input . Sorting ) ? query : DynamicQueryableExtensions . OrderBy ( query , input . Sorting ) ;
var entities = await query . PageBy ( input . SkipCount , input . MaxResultCount ) . ToListAsync ( ) . ConfigureAwait ( false ) ;
@ -525,11 +505,17 @@ public class VmiAppService : ApplicationService, IVmiService, IJobService, ITran
[HttpPost]
public async Task < string > BackupExport ( BackupListRequest input )
{
var connectionString = $"Data Source=wwwroot/files/vmi/{input.Name}.db" ;
using var dbContext = new VmiSqliteContext ( connectionString ) ;
var repo = dbContext . Set < VmiBalance > ( ) ;
using var scope = this . _ serviceProvider . CreateScope ( ) ;
var db = scope . ServiceProvider . GetRequiredService < SettleAccountDbContext > ( ) ;
var name = input . Name ;
var sql = $"select * from {name}" ;
var query = db . Set < VmiBalance > ( ) . FromSqlRaw ( sql ) ;
var filters = input . Filters . ToLambda < VmiBalance > ( ) ;
var query = ( input . Filters . Count > 0 ? repo . Where ( input . Filters . ToLambda < VmiBalance > ( ) ) : repo ) ;
if ( input . Filters . Count > 0 )
{
query = query . Where ( input . Filters . ToLambda < VmiBalance > ( ) ) ;
}
var totalCount = query . Count ( ) ;
query = string . IsNullOrEmpty ( input . Sorting ) ? query : DynamicQueryableExtensions . OrderBy ( query , input . Sorting ) ;
var entities = await query . ToListAsync ( ) . ConfigureAwait ( false ) ;
var fileName = $"库存备份_{input.Name}.xlsx" ;