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

Loading…
Cancel
Save