@ -8,6 +8,7 @@ using System.IO;
using System.Linq ;
using System.Linq.Dynamic.Core ;
using System.Reflection ;
using System.Text.Json ;
using System.Threading.Tasks ;
using ClosedXML.Excel ;
using LinqToDB.Data ;
@ -48,7 +49,6 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
private readonly IServiceProvider _ serviceProvider ;
private readonly INormalEfCoreRepository < VmiBalance , Guid > _ balanceRepository ;
private readonly INormalEfCoreRepository < VmiLog , Guid > _l ogRepository ;
private readonly INormalEfCoreRepository < VmiSnapshot , Guid > _ snapshotRepository ;
private readonly IBlobContainer < MyFileContainer > _f ileContainer ;
private readonly IHubContext < PageHub > _ hubContext ;
private readonly ICurrentUser _ currentUser ;
@ -57,7 +57,6 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
IServiceProvider serviceProvider ,
INormalEfCoreRepository < VmiBalance , Guid > balanceRepository ,
INormalEfCoreRepository < VmiLog , Guid > logRepository ,
INormalEfCoreRepository < VmiSnapshot , Guid > snapshotRepository ,
IBlobContainer < MyFileContainer > fileContainer ,
IHubContext < PageHub > hubContext ,
ICurrentUser currentUser )
@ -66,7 +65,6 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
this . _ serviceProvider = serviceProvider ;
this . _ balanceRepository = balanceRepository ;
this . _l ogRepository = logRepository ;
this . _ snapshotRepository = snapshotRepository ;
this . _f ileContainer = fileContainer ;
this . _ hubContext = hubContext ;
this . _ currentUser = currentUser ;
@ -140,6 +138,7 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
db . SaveChanges ( ) ;
db . Database . ExecuteSqlRaw ( $"select * into {table} from Set_VmiBalance;" ) ;
db . Database . ExecuteSqlRaw ( $"create clustered index IX_{table}_BillTime on {table} (BillTime)" ) ;
db . Database . ExecuteSqlRaw ( $"alter table {table} add constraint PK_{table} primary key (Id);" ) ;
snapshot . End = DateTime . Now ;
transaction . Commit ( ) ;
return Task . CompletedTask ;
@ -188,7 +187,7 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
}
/// <summary>
/// 库存余额导出
/// 1.1 库存余额导出
/// </summary>
[HttpPost]
public async Task < string > BalanceExport ( RequestDto input )
@ -201,7 +200,69 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
}
/// <summary>
/// 2.库存事务查询
/// 快照列表
/// </summary>
[HttpPost]
public async Task < List < VmiSnapshot > > Snapshot ( )
{
var connectionString = this . _ serviceProvider . GetRequiredService < IConfiguration > ( ) . GetConnectionString ( "SettleAccountService" ) ;
using var connection = new SqlConnection ( connectionString ) ;
connection . Open ( ) ;
var options = new DbContextOptionsBuilder < SettleAccountDbContext > ( ) . UseSqlServer ( connection ) . Options ;
using var db = new SettleAccountDbContext ( options ) ;
var list = db . Set < VmiSnapshot > ( ) . AsNoTracking ( ) . OrderByDescending ( o = > o . Start ) . ToList ( ) ;
return list ;
}
/// <summary>
/// 2.时点库存查询
/// </summary>
[HttpPost]
public async Task < PagedResultDto < VmiBalance > > Backup ( BackupListRequest input )
{
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 > ( ) ;
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 ) ;
return new PagedResultDto < VmiBalance > ( totalCount , entities ) ;
}
/// <summary>
/// 2.1时点库存导出
/// </summary>
[HttpPost]
public async Task < string > BackupExport ( BackupListRequest input )
{
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 > ( ) ;
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" ;
var content = this . GetContent ( entities , "库存快照" ) ;
await _f ileContainer . SaveAsync ( fileName , content , true ) . ConfigureAwait ( false ) ;
return fileName ;
}
/// <summary>
/// 3.库存事务查询
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
@ -256,12 +317,12 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
}
/// <summary>
/// 库存事务导出
/// 3.1 库存事务导出
/// </summary>
[HttpPost]
public async Task < string > LogExport ( RequestDto input )
{
var entities = await _l ogRepository . GetListByFilterAsync ( input . Filters ) . ConfigureAwait ( false ) ;
var entities = await _l ogRepository . GetListByFilterAsync ( input . Filters , input . Sorting , input . MaxResultCount , input . SkipCount ) . ConfigureAwait ( false ) ;
var fileName = $"库存事务_{DateTime.Now.ToString(" yyyy - MM - dd_HH : mm : ss ")}.xlsx" ;
var content = this . GetContent ( entities , "库存事务_" ) ;
await _f ileContainer . SaveAsync ( fileName , content , true ) . ConfigureAwait ( false ) ;
@ -288,22 +349,42 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
/// <param name="log"></param>
/// <returns></returns>
[HttpPost]
public async Task EditBalance ( VmiLog log )
public IActionResult EditBalance ( VmiLog log )
{
log . SetId ( GuidGenerator . Create ( ) ) ;
if ( log . ChangedQty > = decimal . Zero )
var connectionString = this . _ serviceProvider . GetRequiredService < IConfiguration > ( ) . GetConnectionString ( "SettleAccountService" ) ;
using var connection = new SqlConnection ( connectionString ) ;
connection . Open ( ) ;
using var transaction = connection . BeginTransaction ( ) ;
try
{
log . Qty = log . ChangedQty ;
log . LogType = VmiLogType . Type500 ;
log . ChangedType = VmiType . In ;
var options = new DbContextOptionsBuilder < SettleAccountDbContext > ( ) . UseSqlServer ( connection ) . Options ;
using var db = new SettleAccountDbContext ( options ) ;
log . SetId ( GuidGenerator . Create ( ) ) ;
if ( log . ChangedQty > = decimal . Zero )
{
log . Qty = log . ChangedQty ;
log . LogType = VmiLogType . Type500 ;
log . ChangedType = VmiType . In ;
}
else
{
log . Qty = - log . Qty ;
log . LogType = VmiLogType . Type600 ;
log . ChangedType = VmiType . Out ;
}
log . ChangedBy = this . _ currentUser . UserName ;
log . ChangedTime = DateTime . Now ;
db . Set < VmiLog > ( ) . Add ( log ) ;
db . Set < VmiMessage > ( ) . Add ( new VmiMessage ( GuidGenerator . Create ( ) ) { Message = JsonSerializer . Serialize ( log ) } ) ;
transaction . Commit ( ) ;
return new OkResult ( ) ;
}
else
catch ( Exc eption ex )
{
log . Qty = - log . Qty ;
log . LogType = VmiLogType . Type600 ;
log . ChangedType = VmiType . Out ;
transaction . Rollback ( ) ;
Console . WriteLine ( ex . ToString ( ) ) ;
return new JsonResult ( new { code = 4 0 0 , data = ex . ToString ( ) , message = ex . Message } ) ; ;
}
log . ChangedBy = this . _ currentUser . UserName ;
}
/// <summary>
@ -318,67 +399,10 @@ public class VmiAppService : ApplicationService, IJobService, ITransientDependen
var list = this . ImportInternal < VmiLog > ( ms . ToArray ( ) ) ;
foreach ( var file in list )
{
await EditBalance ( file ) . ConfigureAwait ( fals e ) ;
EditBalance ( file ) ;
}
}
/// <summary>
/// 快照列表
/// </summary>
[HttpPost]
public async Task < ListResultDto < VmiSnapshot > > Snapshot ( )
{
var list = await _ snapshotRepository . GetListAsync ( ) . ConfigureAwait ( false ) ;
return new ListResultDto < VmiSnapshot > ( list ) ;
}
/// <summary>
/// 3.时点库存查询
/// </summary>
[HttpPost]
public async Task < PagedResultDto < VmiBalance > > Backup ( BackupListRequest input )
{
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 > ( ) ;
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 ) ;
return new PagedResultDto < VmiBalance > ( totalCount , entities ) ;
}
/// <summary>
/// 时点库存导出
/// </summary>
[HttpPost]
public async Task < string > BackupExport ( BackupListRequest input )
{
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 > ( ) ;
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" ;
var content = this . GetContent ( entities , "库存备份" ) ;
await _f ileContainer . SaveAsync ( fileName , content , true ) . ConfigureAwait ( false ) ;
return fileName ;
}
private List < T > ImportInternal < T > ( byte [ ] data )
{
var list = new List < T > ( ) ;