Browse Source

使用缓存余额表方式优化库存同步

master
wanggang 1 year ago
parent
commit
383e0fb53d
  1. 54
      code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs

54
code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncBalanceService.cs

@ -19,6 +19,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Omu.ValueInjecter; using Omu.ValueInjecter;
using SettleAccount.Job.SignalR; using SettleAccount.Job.SignalR;
using SqlSugar;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -72,7 +73,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
private async Task InvokeInternal(IServiceProvider serviceProvider) private async Task InvokeInternal(IServiceProvider serviceProvider)
{ {
var batchSize = 2500; var batchSize = 10000;
var fetchSize = 0; var fetchSize = 0;
var connectionString = serviceProvider.GetRequiredService<IConfiguration>().GetConnectionString("SettleAccountService"); var connectionString = serviceProvider.GetRequiredService<IConfiguration>().GetConnectionString("SettleAccountService");
for (var i = 0; i < 1000; i++) for (var i = 0; i < 1000; i++)
@ -94,15 +95,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
context.Database.UseTransaction(transaction); context.Database.UseTransaction(transaction);
var vmiMessageRepo = context.Set<VmiMessage>(); var vmiMessageRepo = context.Set<VmiMessage>();
var vmiLogRepo = context.Set<VmiLog>(); var vmiLogRepo = context.Set<VmiLog>();
var vmiBalanceRepo = context.Set<VmiBalance>(); var vmiBalanceRepo = context.Set<VmiBalance>(); //读取可消费消息列表
//sw.Stop();
//this._logger.LogInformation($"缓存开始:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
//var balanceCachedList = vmiBalanceRepo.AsNoTracking().ToList();
//sw.Stop();
//this._logger.LogInformation($"缓存结束:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart();
//读取可消费消息列表
var messages = vmiMessageRepo.AsNoTracking().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(batchSize).ToList(); var messages = vmiMessageRepo.AsNoTracking().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(batchSize).ToList();
//没有可消费消息则返回 //没有可消费消息则返回
if (!messages.Any()) if (!messages.Any())
@ -110,6 +103,15 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
transaction.Commit(); transaction.Commit();
break; break;
} }
//缓存余额表
sw.Stop();
this._logger.LogInformation($"缓存开始:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
sw.Restart();
var balanceCachedList = vmiBalanceRepo.AsNoTracking()
.ToDictionary(o => $"{o.DeliverBillType}.{o.CodeType}.{o.RealPartCode}.{o.VinCode}.{o.ErpToLoc}.{o.OrderNum}.{o.factory}.{o.Configcode}");
sw.Stop();
this._logger.LogInformation($"缓存结束:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
sw.Restart();
//设置数量为实际返回数量 //设置数量为实际返回数量
fetchSize = messages.Count; fetchSize = messages.Count;
//反序列化获取库存事务 //反序列化获取库存事务
@ -162,21 +164,23 @@ namespace Win.Sfs.SettleAccount.Entities.BQ
//数据库查找 //数据库查找
if (balance == null) if (balance == null)
{ {
//sw.Stop(); sw.Stop();
////this._logger.LogInformation($"查询开始:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒"); //this._logger.LogInformation($"查询开始:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
//sw.Restart(); sw.Restart();
balance = vmiBalanceRepo.AsNoTracking().FirstOrDefault(// balanceCachedList.Find( //balance = vmiBalanceRepo.AsNoTracking().FirstOrDefault(
o => o.DeliverBillType == log.DeliverBillType && // // balanceCachedList.Find(
o.CodeType == log.CodeType && // o => o.DeliverBillType == log.DeliverBillType &&
o.RealPartCode == log.RealPartCode && // o.CodeType == log.CodeType &&
o.VinCode == log.VinCode && // o.RealPartCode == log.RealPartCode &&
o.ErpToLoc == log.ErpToLoc && // o.VinCode == log.VinCode &&
o.OrderNum == log.OrderNum && // o.ErpToLoc == log.ErpToLoc &&
o.factory == log.factory && // o.OrderNum == log.OrderNum &&
o.Configcode == log.Configcode); // o.factory == log.factory &&
//sw.Stop(); // o.Configcode == log.Configcode);
//this._logger.LogInformation($"查询结束:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒"); balance = balanceCachedList.GetValueOrDefault($"{log.DeliverBillType}.{log.CodeType}.{log.RealPartCode}.{log.VinCode}.{log.ErpToLoc}.{log.OrderNum}.{log.factory}.{log.Configcode}");
//sw.Restart(); sw.Stop();
this._logger.LogInformation($"查询结束:{fetchSize}条,耗时 {sw.ElapsedMilliseconds / 1000} 秒,{sw.ElapsedMilliseconds} 豪秒");
sw.Restart();
} }
if (balance == null) if (balance == null)
{//不存在库存记录 {//不存在库存记录

Loading…
Cancel
Save