From 1085274abe7dafb838df9fc359c9571141064cce Mon Sep 17 00:00:00 2001 From: zhaoxinyu <89237069@qq.com> Date: Mon, 15 Jul 2024 09:11:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entities/BQ/INVOICE_SERVICE.cs | 9 + .../Entities/BQ/VmiAppService.cs | 4 + .../Entities/BQ/VmiAsyncBalanceService.cs | 458 +++++++++--------- 3 files changed, 242 insertions(+), 229 deletions(-) diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/INVOICE_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/INVOICE_SERVICE.cs index 2d437e5f..28eedcb1 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/INVOICE_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/INVOICE_SERVICE.cs @@ -1611,10 +1611,19 @@ namespace Win.Sfs.SettleAccount.Entities.BQ var invgourp=dbcontext.Set().Where(p => p.InvbillNum == p_dto.InvbillNum).ToList(); var detial=dbcontext.Set().Where(p => p.InvbillNum == p_dto.InvbillNum).ToList(); var notsettle=dbcontext.Set().Where(p => p.InvBillNum == p_dto.InvbillNum).ToList(); + var detialext = dbcontext.Set().Where(p => p.InvbillNum == p_dto.InvbillNum).ToList(); + + dbcontext.BulkDelete(invList); dbcontext.BulkDelete(invgourp); dbcontext.BulkDelete(detial); dbcontext.BulkDelete(notsettle); + if (detialext != null && detialext.Count > 0) + { + dbcontext.BulkDelete(detialext); + } + + } return new JsonResult(new { Code = 200, Message = "退回成功" }); ; diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs index ba3e0861..bd4247d2 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs @@ -1271,6 +1271,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ /// private async Task CheckBusinessJISLocation(List p_list, EnumBusinessType enumBusinessType) where T : JisSeBase, new() { + if (p_list.Count == 0) { return "符合条件记录为0,请检查记录中EDI生产码、发运类型等 是否录入正确" ; @@ -1286,6 +1287,9 @@ namespace Win.Sfs.SettleAccount.Entities.BQ p.Value == "MaiDanJianBBAC顺义" ).ToList(); break; + + + case EnumBusinessType.JisHBPO: locList = locList.Where(p => p.Value == "JisHBPO" diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs index 8f2b57dd..daf662eb 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs @@ -79,235 +79,235 @@ namespace Win.Sfs.SettleAccount.Entities.BQ } } - private async Task InvokeInternal(IServiceProvider serviceProvider) - { - var batchSize = 10000; - var fetchSize = 0; - var connectionString = serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); - Dictionary balanceCachedList = null; - for (var i = 0; i < 1000; i++) - { - //1:新建 2:更新 - var balanceList = new List>(); - var vmiReplenishedList = new List(); - var sw = new Stopwatch(); - sw.Start(); - using var connection = new Microsoft.Data.SqlClient.SqlConnection(connectionString); - connection.Open(); - using var transaction = connection.BeginTransaction(); - try - { - var command = connection.CreateCommand(); - command.Transaction = transaction; - var options = new DbContextOptionsBuilder().UseSqlServer(connection).Options; - using var context = new SettleAccountDbContext(options); - context.Database.UseTransaction(transaction); - var vmiMessageRepo = context.Set(); - var vmiLogRepo = context.Set(); - var vmiBalanceRepo = context.Set(); - //读取可消费消息列表 - var messages = vmiMessageRepo.AsNoTracking().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(batchSize).ToList(); - //没有可消费消息则返回 - if (!messages.Any()) - { - transaction.Commit(); - break; - } - //缓存余额表 - var sw2 = new Stopwatch(); - sw2.Start(); - Func keySelector = o => $"{o.DeliverBillType}.{o.CodeType}.{o.RealPartCode}.{o.VinCode}.{o.ErpToLoc}.{o.OrderNum}.{o.factory}.{o.Configcode}"; - //if (balanceCachedList == null) - //{ - balanceCachedList = vmiBalanceRepo.AsNoTracking().ToDictionary(keySelector); - //} - sw2.Stop(); - this._logger.LogInformation($"缓存结束:{balanceCachedList.Count}条,耗时 {sw2.ElapsedMilliseconds / 1000} 秒,{sw2.ElapsedMilliseconds} 豪秒"); - //设置数量为实际返回数量 - fetchSize = messages.Count; - //反序列化获取库存事务 - var list = messages.Select(o => - { - var log = JsonSerializer.Deserialize(o.Message); - var jsonElement = JsonSerializer.Deserialize(o.Message); - var property = jsonElement.GetProperty("Id"); - Console.WriteLine(jsonElement); - var id = Guid.Parse(property.GetString()); - log.SetId(id); - return new LogToBalance { VmiMessage = o, Log = log, Table = $"Set_VmiLog_{log.ChangedTime.Year}_{(log.ChangedTime.Month - 1) / 3 + 1}" }; - }); - //获取分表名称 - var tables = list.Select(o => o.Table).Distinct().ToList(); - //创建分表 - foreach (var table in tables) - { - command.CommandText = $"select OBJECT_ID('{table}', 'U')"; - var result = command.ExecuteScalar().ToString(); - if (result == string.Empty) - { - command.CommandText = $"select * into {table} from Set_VmiLog where 1=0;"; - command.ExecuteNonQuery(); - command.CommandText = $"alter table {table} add constraint PK_{table} primary key (Id);"; - command.ExecuteNonQuery(); - } - } - foreach (var item in list) - { - var message = item.VmiMessage; - message.isConsumed = true; - var log = item.Log; - //本地查找 - var balance = balanceList.Select(o => o.Item1).FirstOrDefault( - o => - o.DeliverBillType == log.DeliverBillType && - o.CodeType == log.CodeType && - o.RealPartCode == log.RealPartCode && - o.VinCode == log.VinCode && - o.ErpToLoc == log.ErpToLoc&& - o.OrderNum == log.OrderNum && - o.factory == log.factory && - o.Configcode == log.Configcode - ); - //数据库查找 - if (balance == null) - { - balance = balanceCachedList.GetValueOrDefault($"{log.DeliverBillType}.{log.CodeType}.{log.RealPartCode}.{log.VinCode}.{log.ErpToLoc}.{log.OrderNum}.{log.factory}.{log.Configcode}"); - //if (balance == null) - //{ - // balance = vmiBalanceRepo.AsNoTracking().FirstOrDefault( - // // balanceCachedList.Find( - // o => o.DeliverBillType == log.DeliverBillType && - // o.CodeType == log.CodeType && - // o.RealPartCode == log.RealPartCode && - // o.VinCode == log.VinCode && - // o.ErpToLoc == log.ErpToLoc && - // o.OrderNum == log.OrderNum && - // o.factory == log.factory && - // o.Configcode == log.Configcode); - //} - } - if (balance == null) - {//不存在库存记录 - //新建库存记录 - balance = new VmiBalance(); - balanceList.Add(new Tuple(balance, 1)); - if (log.LogType == VmiLogType.Type300) - {//反结算入库,重建库存 - var logHistory = vmiLogRepo.AsNoTracking().FirstOrDefault( - o => o.LogType == VmiLogType.Type100 && - o.DeliverBillType == log.DeliverBillType && - o.CodeType == log.CodeType && - o.RealPartCode == log.RealPartCode && - o.VinCode == log.VinCode && - o.ErpToLoc == log.ErpToLoc && - o.OrderNum == log.OrderNum && - o.factory == log.factory && - o.Configcode == log.Configcode); - if (logHistory != null) - { - balance.InjectFrom(logHistory); - } - else - { - balance.InjectFrom(log); - } - } - else - { - balance.InjectFrom(log); - } - balance.Qty = log.ChangedQty; - } - else - {//存在库存记录 - if (!balanceList.Any(o => o.Item1.Id == balance.Id)) - { - balanceList.Add(new Tuple(balance, 2)); - } - var logType = log.LogType; - var currentQty = balance.Qty;// + log.ty - if (logType == VmiLogType.Type100) - { - //发运入库,负库存字段需要更新 - if (balance.Qty < decimal.Zero) - { - balance.InjectFrom(log); - } - } - else if (logType == VmiLogType.Type300) - { - //反结入库,只更新库存 - } - else if (logType == VmiLogType.Type500) - { - //调整入库,更新库存和其他字段 - balance.InjectFrom(log); - } - if (logType == VmiLogType.Type100 && balance.Qty < decimal.Zero) - { - //添加负库存补货记录 - var log2 = new VmiReplenished(); - log2.InjectFrom(log); - vmiReplenishedList.Add(log2); - } - // 更新库存 - balance.Qty = currentQty + log.ChangedQty; - } - } - //更新事务分表 - foreach (var item in tables) - { - var logs = list.Where(o => o.Table == item).Select(o => o.Log).ToList(); - await context.BulkCopyAsync(new LinqToDB.Data.BulkCopyOptions { TableName = item }, logs).ConfigureAwait(false); - } - //批量插入负库存补货记录 - await context.BulkInsertAsync(vmiReplenishedList).ConfigureAwait(false); - //批量插入库存余额 - var addList = balanceList.Where(o => o.Item2 == 1 && o.Item1.Qty != decimal.Zero).Select(o => o.Item1).ToList(); - await context.BulkInsertAsync(addList).ConfigureAwait(false); - //批量更新库存余额 - var editList = balanceList.Where(o => o.Item2 == 2 && o.Item1.Qty != decimal.Zero).Select(o => o.Item1).ToList(); - await context.BulkUpdateAsync(editList).ConfigureAwait(false); - //批量删除库存余额 - var removeList = balanceList.Where(o => o.Item2 == 2 && o.Item1.Qty == decimal.Zero).Select(o => o.Item1).ToList(); - await context.BulkDeleteAsync(removeList).ConfigureAwait(false); - //批量删除消息 - await context.BulkDeleteAsync(messages).ConfigureAwait(false); - transaction.Commit(); - ////更新余额缓存 - //var sw3 = new Stopwatch(); - //sw3.Start(); - //addList.ForEach(o => balanceCachedList.Add(keySelector(o), o)); - //removeList.ForEach(o => balanceCachedList.Remove(keySelector(o))); - //sw3.Stop(); - //this._logger.LogInformation($"更新缓存:{balanceList.Count}条,耗时 {sw3.ElapsedMilliseconds / 1000} 秒,{sw3.ElapsedMilliseconds} 豪秒"); - } - catch (Exception ex) - { - this._logger.LogError(ex.ToString()); - throw new UserFriendlyException(ex.ToString(), "500"); - } - finally - { - sw.Stop(); - this._logger.LogInformation($"处理{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000 / 60}分钟,{sw.ElapsedMilliseconds / 1000}秒"); - } - try - { - using var scope = serviceProvider.CreateScope(); - using var connection2 = new SqlConnection(connectionString); - connection2.Open(); - var command = connection2.CreateCommand(); - command.CommandText = "SELECT count(*) FROM Set_VmiMessage WITH(NOLOCK)"; - var count = Convert.ToInt64(command.ExecuteScalar().ToString()); - scope.ServiceProvider.GetService>().Clients.All.ServerToClient("VmiBalance", count.ToString(), ""); - } - catch (Exception) - { - throw; - } - } - } + //private async Task InvokeInternal(IServiceProvider serviceProvider) + //{ + // var batchSize = 10000; + // var fetchSize = 0; + // var connectionString = serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); + // Dictionary balanceCachedList = null; + // for (var i = 0; i < 1000; i++) + // { + // //1:新建 2:更新 + // var balanceList = new List>(); + // var vmiReplenishedList = new List(); + // var sw = new Stopwatch(); + // sw.Start(); + // using var connection = new Microsoft.Data.SqlClient.SqlConnection(connectionString); + // connection.Open(); + // using var transaction = connection.BeginTransaction(); + // try + // { + // var command = connection.CreateCommand(); + // command.Transaction = transaction; + // var options = new DbContextOptionsBuilder().UseSqlServer(connection).Options; + // using var context = new SettleAccountDbContext(options); + // context.Database.UseTransaction(transaction); + // var vmiMessageRepo = context.Set(); + // var vmiLogRepo = context.Set(); + // var vmiBalanceRepo = context.Set(); + // //读取可消费消息列表 + // var messages = vmiMessageRepo.AsNoTracking().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(batchSize).ToList(); + // //没有可消费消息则返回 + // if (!messages.Any()) + // { + // transaction.Commit(); + // break; + // } + // //缓存余额表 + // var sw2 = new Stopwatch(); + // sw2.Start(); + // Func keySelector = o => $"{o.DeliverBillType}.{o.CodeType}.{o.RealPartCode}.{o.VinCode}.{o.ErpToLoc}.{o.OrderNum}.{o.factory}.{o.Configcode}"; + // //if (balanceCachedList == null) + // //{ + // balanceCachedList = vmiBalanceRepo.AsNoTracking().ToDictionary(keySelector); + // //} + // sw2.Stop(); + // this._logger.LogInformation($"缓存结束:{balanceCachedList.Count}条,耗时 {sw2.ElapsedMilliseconds / 1000} 秒,{sw2.ElapsedMilliseconds} 豪秒"); + // //设置数量为实际返回数量 + // fetchSize = messages.Count; + // //反序列化获取库存事务 + // var list = messages.Select(o => + // { + // var log = JsonSerializer.Deserialize(o.Message); + // var jsonElement = JsonSerializer.Deserialize(o.Message); + // var property = jsonElement.GetProperty("Id"); + // Console.WriteLine(jsonElement); + // var id = Guid.Parse(property.GetString()); + // log.SetId(id); + // return new LogToBalance { VmiMessage = o, Log = log, Table = $"Set_VmiLog_{log.ChangedTime.Year}_{(log.ChangedTime.Month - 1) / 3 + 1}" }; + // }); + // //获取分表名称 + // var tables = list.Select(o => o.Table).Distinct().ToList(); + // //创建分表 + // foreach (var table in tables) + // { + // command.CommandText = $"select OBJECT_ID('{table}', 'U')"; + // var result = command.ExecuteScalar().ToString(); + // if (result == string.Empty) + // { + // command.CommandText = $"select * into {table} from Set_VmiLog where 1=0;"; + // command.ExecuteNonQuery(); + // command.CommandText = $"alter table {table} add constraint PK_{table} primary key (Id);"; + // command.ExecuteNonQuery(); + // } + // } + // foreach (var item in list) + // { + // var message = item.VmiMessage; + // message.isConsumed = true; + // var log = item.Log; + // //本地查找 + // var balance = balanceList.Select(o => o.Item1).FirstOrDefault( + // o => + // o.DeliverBillType == log.DeliverBillType && + // o.CodeType == log.CodeType && + // o.RealPartCode == log.RealPartCode && + // o.VinCode == log.VinCode && + // o.ErpToLoc == log.ErpToLoc&& + // o.OrderNum == log.OrderNum && + // o.factory == log.factory && + // o.Configcode == log.Configcode + // ); + // //数据库查找 + // if (balance == null) + // { + // balance = balanceCachedList.GetValueOrDefault($"{log.DeliverBillType}.{log.CodeType}.{log.RealPartCode}.{log.VinCode}.{log.ErpToLoc}.{log.OrderNum}.{log.factory}.{log.Configcode}"); + // //if (balance == null) + // //{ + // // balance = vmiBalanceRepo.AsNoTracking().FirstOrDefault( + // // // balanceCachedList.Find( + // // o => o.DeliverBillType == log.DeliverBillType && + // // o.CodeType == log.CodeType && + // // o.RealPartCode == log.RealPartCode && + // // o.VinCode == log.VinCode && + // // o.ErpToLoc == log.ErpToLoc && + // // o.OrderNum == log.OrderNum && + // // o.factory == log.factory && + // // o.Configcode == log.Configcode); + // //} + // } + // if (balance == null) + // {//不存在库存记录 + // //新建库存记录 + // balance = new VmiBalance(); + // balanceList.Add(new Tuple(balance, 1)); + // if (log.LogType == VmiLogType.Type300) + // {//反结算入库,重建库存 + // var logHistory = vmiLogRepo.AsNoTracking().FirstOrDefault( + // o => o.LogType == VmiLogType.Type100 && + // o.DeliverBillType == log.DeliverBillType && + // o.CodeType == log.CodeType && + // o.RealPartCode == log.RealPartCode && + // o.VinCode == log.VinCode && + // o.ErpToLoc == log.ErpToLoc && + // o.OrderNum == log.OrderNum && + // o.factory == log.factory && + // o.Configcode == log.Configcode); + // if (logHistory != null) + // { + // balance.InjectFrom(logHistory); + // } + // else + // { + // balance.InjectFrom(log); + // } + // } + // else + // { + // balance.InjectFrom(log); + // } + // balance.Qty = log.ChangedQty; + // } + // else + // {//存在库存记录 + // if (!balanceList.Any(o => o.Item1.Id == balance.Id)) + // { + // balanceList.Add(new Tuple(balance, 2)); + // } + // var logType = log.LogType; + // var currentQty = balance.Qty;// + log.ty + // if (logType == VmiLogType.Type100) + // { + // //发运入库,负库存字段需要更新 + // if (balance.Qty < decimal.Zero) + // { + // balance.InjectFrom(log); + // } + // } + // else if (logType == VmiLogType.Type300) + // { + // //反结入库,只更新库存 + // } + // else if (logType == VmiLogType.Type500) + // { + // //调整入库,更新库存和其他字段 + // balance.InjectFrom(log); + // } + // if (logType == VmiLogType.Type100 && balance.Qty < decimal.Zero) + // { + // //添加负库存补货记录 + // var log2 = new VmiReplenished(); + // log2.InjectFrom(log); + // vmiReplenishedList.Add(log2); + // } + // // 更新库存 + // balance.Qty = currentQty + log.ChangedQty; + // } + // } + // //更新事务分表 + // foreach (var item in tables) + // { + // var logs = list.Where(o => o.Table == item).Select(o => o.Log).ToList(); + // await context.BulkCopyAsync(new LinqToDB.Data.BulkCopyOptions { TableName = item }, logs).ConfigureAwait(false); + // } + // //批量插入负库存补货记录 + // await context.BulkInsertAsync(vmiReplenishedList).ConfigureAwait(false); + // //批量插入库存余额 + // var addList = balanceList.Where(o => o.Item2 == 1 && o.Item1.Qty != decimal.Zero).Select(o => o.Item1).ToList(); + // await context.BulkInsertAsync(addList).ConfigureAwait(false); + // //批量更新库存余额 + // var editList = balanceList.Where(o => o.Item2 == 2 && o.Item1.Qty != decimal.Zero).Select(o => o.Item1).ToList(); + // await context.BulkUpdateAsync(editList).ConfigureAwait(false); + // //批量删除库存余额 + // var removeList = balanceList.Where(o => o.Item2 == 2 && o.Item1.Qty == decimal.Zero).Select(o => o.Item1).ToList(); + // await context.BulkDeleteAsync(removeList).ConfigureAwait(false); + // //批量删除消息 + // await context.BulkDeleteAsync(messages).ConfigureAwait(false); + // transaction.Commit(); + // ////更新余额缓存 + // //var sw3 = new Stopwatch(); + // //sw3.Start(); + // //addList.ForEach(o => balanceCachedList.Add(keySelector(o), o)); + // //removeList.ForEach(o => balanceCachedList.Remove(keySelector(o))); + // //sw3.Stop(); + // //this._logger.LogInformation($"更新缓存:{balanceList.Count}条,耗时 {sw3.ElapsedMilliseconds / 1000} 秒,{sw3.ElapsedMilliseconds} 豪秒"); + // } + // catch (Exception ex) + // { + // this._logger.LogError(ex.ToString()); + // throw new UserFriendlyException(ex.ToString(), "500"); + // } + // finally + // { + // sw.Stop(); + // this._logger.LogInformation($"处理{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000 / 60}分钟,{sw.ElapsedMilliseconds / 1000}秒"); + // } + // try + // { + // using var scope = serviceProvider.CreateScope(); + // using var connection2 = new SqlConnection(connectionString); + // connection2.Open(); + // var command = connection2.CreateCommand(); + // command.CommandText = "SELECT count(*) FROM Set_VmiMessage WITH(NOLOCK)"; + // var count = Convert.ToInt64(command.ExecuteScalar().ToString()); + // scope.ServiceProvider.GetService>().Clients.All.ServerToClient("VmiBalance", count.ToString(), ""); + // } + // catch (Exception) + // { + // throw; + // } + // } + //} } public class LogToBalance