From 1fdde38a4182e3c84a4018e64f3dd308bdda180d Mon Sep 17 00:00:00 2001 From: mahao Date: Wed, 30 Aug 2023 17:29:30 +0800 Subject: [PATCH 1/2] UP --- .../BQ/Syncs/JisHBPOSeSyncAppService.cs | 11 +- .../BQ/Syncs/JisHBPOSeSyncBaseAppService.cs | 65 +++--- .../Entities/BQ/Syncs/JitSeSyncAppService.cs | 2 +- .../BQ/Syncs/MaiDanHBPOSeSyncAppService.cs | 10 +- .../Entities/BQ/Syncs/SeSyncExtendManager.cs | 98 +++++++- .../SettleAccount.Domain/Bases/EntityBase.cs | 209 +++++++++++++++++- .../Entities/BQ/BBAC_SE_DETAIL.cs | 203 +---------------- .../Entities/BQ/HBPO_SE_DETAIL.cs | 207 +---------------- 8 files changed, 349 insertions(+), 456 deletions(-) diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncAppService.cs index 165fd14f..25484aba 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncAppService.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SettleAccount.Domain.BQ; +using Volo.Abp.Uow; using Win.Sfs.SettleAccount.Entities.BQ.Managers; using Win.Sfs.SettleAccount.Entities.BQ.Vmi; using Win.Sfs.SettleAccount.EntityFrameworkCore; @@ -14,8 +15,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs; /// /// JisHBPO发运同步 /// -[AllowAnonymous] -[Route("api/settleaccount/[controller]/[action]")] +[ApiExplorerSettings(IgnoreApi = true)] public class JisHBPOSeSyncAppService : JisHBPOSeSyncBaseAppService, IJobService { /// @@ -27,8 +27,8 @@ public class JisHBPOSeSyncAppService : JisHBPOSeSyncBaseAppService, IJobService INormalEfCoreRepository syncPositionFlagRepository, INormalEfCoreRepository jisSeDetailRepository, MaterialRelationshipManager materialRelationshipManager, - VmiAppService vmiService - ) : base(wmsBJBMPTContext, settleAccountDbContext, syncPositionFlagRepository, jisSeDetailRepository, materialRelationshipManager, vmiService) + SeSyncExtendManager syncExtendManager + ) : base(wmsBJBMPTContext, settleAccountDbContext, syncPositionFlagRepository, jisSeDetailRepository, materialRelationshipManager, syncExtendManager) { base.SeSyncConfigInfo = new SeSyncConfig() { @@ -43,7 +43,8 @@ public class JisHBPOSeSyncAppService : JisHBPOSeSyncBaseAppService, IJobService }; } - public async Task Invoke(IServiceProvider serviceProvider) + [UnitOfWork(IsDisabled = false)] + public virtual async Task Invoke(IServiceProvider serviceProvider) { await this.Invoke().ConfigureAwait(false); } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncBaseAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncBaseAppService.cs index f2b712eb..078d3753 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncBaseAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JisHBPOSeSyncBaseAppService.cs @@ -2,13 +2,16 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.LinqAsync; using System.Threading.Tasks; using Coravel.Invocable; using EFCore.BulkExtensions; using Microsoft.AspNetCore.Mvc; +using Org.BouncyCastle.Asn1.Cmp; using SettleAccount.Domain.BQ; using Volo.Abp.Application.Services; using Win.Sfs.SettleAccount.Entities.BQ.Managers; +using Win.Sfs.SettleAccount.Entities.MaterialRelationships; using Win.Sfs.SettleAccount.EntityFrameworkCore; using Win.Sfs.SettleAccount.MaterialRelationships; using Win.Sfs.Shared.RepositoryBase; @@ -45,8 +48,10 @@ public class JisHBPOSeSyncBaseAppService : ApplicationService, IInvocable /// 客户零件关系领域 /// private readonly MaterialRelationshipManager _materialRelationshipManager; - - //private readonly IVmiService _vmiService; + /// + /// 发运同步扩展 + /// + private readonly SeSyncExtendManager _syncExtendManager; /// /// 构造 @@ -57,14 +62,14 @@ public class JisHBPOSeSyncBaseAppService : ApplicationService, IInvocable INormalEfCoreRepository syncPositionFlagRepository, INormalEfCoreRepository jisSeDetailRepository, MaterialRelationshipManager materialRelationshipManager, - VmiAppService vmiService) + SeSyncExtendManager syncExtendManager) { _wmsBJBMPTContext = wmsBJBMPTContext; _settleAccountDbContext = settleAccountDbContext; _syncPositionFlagRepository = syncPositionFlagRepository; _jisSeDetailRepository = jisSeDetailRepository; _materialRelationshipManager = materialRelationshipManager; - //_vmiService = vmiService; + _syncExtendManager = syncExtendManager; } /// @@ -80,8 +85,7 @@ public class JisHBPOSeSyncBaseAppService : ApplicationService, IInvocable return; } await SyncJitRecordAsync().ConfigureAwait(false); - await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); - await SyncJisRecordAsync().ConfigureAwait(false); + //await SyncJisRecordAsync().ConfigureAwait(false); } /// @@ -98,15 +102,12 @@ public class JisHBPOSeSyncBaseAppService : ApplicationService, IInvocable //业务类别 var businessType = SeSyncConfigInfo.BusinessType; - Expression> predicate = (t) => t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); - var syncPositionFlag = await _syncPositionFlagRepository.FindAsync(t => t.TableName == syncTableName).ConfigureAwait(false); - if (syncPositionFlag != null) - { - predicate = (t) => t.UID > int.Parse(syncPositionFlag.Position) && t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); - } + var syncPositionFlag = await _settleAccountDbContext.Set().FirstOrDefaultAsync(t => t.TableName == syncTableName).ConfigureAwait(false); + var syncPosition = syncPositionFlag?.Position ?? "0"; + Expression> predicate = (t) => t.UID > int.Parse(syncPositionFlag.Position) && t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); //WMS发运记录 - var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_JIT_RECORD.Where(predicate).OrderBy(b => b.UID).Take(100000).ToList(); + var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_JIT_RECORD.Where(predicate).OrderBy(b => b.UID).Take(100_000).ToList(); var jisSeDetails = ObjectMapper.Map, List>(wmsSeRecords); if (jisSeDetails.Any()) { @@ -115,7 +116,7 @@ public class JisHBPOSeSyncBaseAppService : ApplicationService, IInvocable if (luRePartCodes.Any()) { var materialRelationships = luRePartCodes.Select(t => new MaterialRelationship(GuidGenerator.Create(), t.FactoryPartCode, "", t.LU, businessType)); - await this.AddNewMaterialRelationships(materialRelationships).ConfigureAwait(false); + await _syncExtendManager.AddNewMaterialRelationshipsAsync(materialRelationships).ConfigureAwait(false); } jisSeDetails.ForEach(t => @@ -129,21 +130,35 @@ public class JisHBPOSeSyncBaseAppService : ApplicationService, IInvocable t.Version = int.Parse(t.BillTime?.ToString("yyyyMM")); } }); - await _settleAccountDbContext.BulkInsertAsync(jisSeDetails).ConfigureAwait(false); - if (syncPositionFlag != null) + var syncPositionNew = jisSeDetails.Last().UID.ToString(); + + using var transaction = await _settleAccountDbContext.Database.BeginTransactionAsync().ConfigureAwait(false); + try { - syncPositionFlag.Position = wmsSeRecords.Last().UID.ToString(); - await _syncPositionFlagRepository.UpdateAsync(syncPositionFlag).ConfigureAwait(false); + if (syncPositionFlag != null) + { + syncPositionFlag.Position = syncPositionNew; + _settleAccountDbContext.Update(syncPositionFlag); + } + else + { + syncPositionFlag = new SyncPositionFlag() + { + TableName = syncTableName, + Position = syncPositionNew + }; + _settleAccountDbContext.Add(syncPositionFlag); + } + await _settleAccountDbContext.BulkInsertAsync(jisSeDetails).ConfigureAwait(false); + await _syncExtendManager.JisSeDetailsSaveConsignAsync(jisSeDetails, false).ConfigureAwait(false); + await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); + await transaction.CommitAsync().ConfigureAwait(false); } - else + catch (Exception) { - syncPositionFlag = new SyncPositionFlag() - { - TableName = syncTableName, - Position = wmsSeRecords.Last().UID.ToString() - }; - await _syncPositionFlagRepository.InsertAsync(syncPositionFlag).ConfigureAwait(false); + await transaction.RollbackAsync().ConfigureAwait(false); + throw; } } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JitSeSyncAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JitSeSyncAppService.cs index 54714aa8..061a7505 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JitSeSyncAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/JitSeSyncAppService.cs @@ -145,7 +145,7 @@ public class JitSeSyncAppService : ApplicationService, IInvocable _settleAccountDbContext.Add(syncPositionFlag); } await _settleAccountDbContext.BulkInsertAsync(seDetails).ConfigureAwait(false); - await _syncExtendManager.SaveVmiLogsAsync(seDetails).ConfigureAwait(false); + await _syncExtendManager.PubSeDetailsSaveConsignAsync(seDetails).ConfigureAwait(false); await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); await transaction.CommitAsync().ConfigureAwait(false); } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/MaiDanHBPOSeSyncAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/MaiDanHBPOSeSyncAppService.cs index 570bbfe7..d0cf401f 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/MaiDanHBPOSeSyncAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/MaiDanHBPOSeSyncAppService.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SettleAccount.Domain.BQ; +using Volo.Abp.Uow; using Win.Sfs.SettleAccount.Entities.BQ.Managers; using Win.Sfs.SettleAccount.Entities.BQ.Vmi; using Win.Sfs.SettleAccount.EntityFrameworkCore; @@ -14,8 +14,7 @@ namespace Win.Sfs.SettleAccount.Entities.BQ.Syncs; /// /// 买单件HBPO发运同步 /// -[AllowAnonymous] -[Route("api/settleaccount/[controller]/[action]")] +[ApiExplorerSettings(IgnoreApi = true)] public class MaiDanHBPOSeSyncAppService : JisHBPOSeSyncBaseAppService, IJobService { /// @@ -27,8 +26,8 @@ public class MaiDanHBPOSeSyncAppService : JisHBPOSeSyncBaseAppService, IJobServi INormalEfCoreRepository syncPositionFlagRepository, INormalEfCoreRepository jisSeDetailRepository, MaterialRelationshipManager materialRelationshipManager, - VmiAppService vmiService - ) : base(wmsBJBMPTContext, settleAccountDbContext, syncPositionFlagRepository, jisSeDetailRepository, materialRelationshipManager, vmiService) + SeSyncExtendManager syncExtendManager + ) : base(wmsBJBMPTContext, settleAccountDbContext, syncPositionFlagRepository, jisSeDetailRepository, materialRelationshipManager, syncExtendManager) { base.SeSyncConfigInfo = new SeSyncConfig() { @@ -43,6 +42,7 @@ public class MaiDanHBPOSeSyncAppService : JisHBPOSeSyncBaseAppService, IJobServi }; } + [UnitOfWork(IsDisabled = false)] public async Task Invoke(IServiceProvider serviceProvider) { await this.Invoke().ConfigureAwait(false); diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/SeSyncExtendManager.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/SeSyncExtendManager.cs index 61d236fe..99181705 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/SeSyncExtendManager.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/Syncs/SeSyncExtendManager.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using EFCore.BulkExtensions; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; +using SettleAccount.Bases; using SettleAccount.Domain.BQ; using Volo.Abp.Domain.Services; using Win.Sfs.SettleAccount.Entities.BQ.Vmi; @@ -49,9 +50,9 @@ public class SeSyncExtendManager : DomainService } /// - /// 保存寄售库Log + /// Jis发运数据保存寄售库 /// - public async Task SaveVmiLogsAsync(List seDetails) + public async Task JisSeDetailsSaveConsignAsync(List seDetails, bool isJisTable) where T : JisSeBase { var deliverSeDetails = seDetails.FindAll(t => t.TransType == EnumDelTransType.发货); var returnSeDetails = seDetails.FindAll(t => t.TransType == EnumDelTransType.退货); @@ -67,19 +68,69 @@ public class SeSyncExtendManager : DomainService ChangedTime = dateTimeNow, AssembleData = t.AssembleData, BillTime = t.BillTime, + CodeType = t.CodeType, + Configcode = t.MESConfigCode, CustPartCode = t.CustPartCode, DeliverBillType = t.DeliverBillType, DeliverSubBillType = t.DeliverSubBillType, - ErpToLoc = t.ToErpLocCode, - OrderNum = t.DnBillNum, + ErpToLoc = t.ErpToLoc, + MatchNumber = t.MatchNumber, + OrderNum = isJisTable ? t.OrderNum : t.JISNum, + PjsNum = t.PjsNum, Qty = t.Qty, ReMark = t.Remark, - RealCode = t.PartCode + RealCode = t.RealCode, + RealPartCode = t.PartCode, + Seq = t.Seq, + UniqueCode = isJisTable ? t.UniqueCode : t.DeliverCode, + VinCode = t.VinCode, + factory = t.Factory }).ToList(); var returnVmiLogList = returnSeDetails.Select(t => new VmiLog(Guid.NewGuid()) { LogType = VmiLogType.Type400, ChangedQty = t.Qty, + ChangedType = VmiType.Out, + ChangedBy = "WMS", + ChangedNumber = t.PN, + ChangedTime = dateTimeNow, + AssembleData = t.AssembleData, + BillTime = t.BillTime, + CodeType = t.CodeType, + Configcode = t.MESConfigCode, + CustPartCode = t.CustPartCode, + DeliverBillType = t.DeliverBillType, + DeliverSubBillType = t.DeliverSubBillType, + ErpToLoc = t.ErpToLoc, + MatchNumber = t.MatchNumber, + OrderNum = isJisTable ? t.OrderNum : t.JISNum, + PjsNum = t.PjsNum, + Qty = -t.Qty, + ReMark = t.Remark, + RealCode = t.RealCode, + RealPartCode = t.PartCode, + Seq = t.Seq, + UniqueCode = isJisTable ? t.UniqueCode : t.DeliverCode, + VinCode = t.VinCode, + factory = t.Factory + }).ToList(); + vmiLogList.AddRange(returnVmiLogList); + await SaveVmiLogsAsync(vmiLogList).ConfigureAwait(false); + } + + /// + /// Pub发运数据保存寄售库 + /// + public async Task PubSeDetailsSaveConsignAsync(List seDetails) + { + var deliverSeDetails = seDetails.FindAll(t => t.TransType == EnumDelTransType.发货); + var returnSeDetails = seDetails.FindAll(t => t.TransType == EnumDelTransType.退货); + var dateTimeNow = DateTime.Now; + + var vmiLogList = deliverSeDetails.Select(t => new VmiLog(Guid.NewGuid()) + { + LogType = VmiLogType.Type100, + ChangedQty = t.Qty, ChangedType = VmiType.In, ChangedBy = "WMS", ChangedNumber = t.PN, @@ -90,20 +141,47 @@ public class SeSyncExtendManager : DomainService DeliverBillType = t.DeliverBillType, DeliverSubBillType = t.DeliverSubBillType, ErpToLoc = t.ToErpLocCode, - OrderNum = t.DnBillNum, + OrderNum = t.DeliveryIndex, + Qty = t.Qty, + ReMark = t.Remark, + RealPartCode = t.PartCode + }).ToList(); + var returnVmiLogList = returnSeDetails.Select(t => new VmiLog(Guid.NewGuid()) + { + LogType = VmiLogType.Type400, + ChangedQty = t.Qty, + ChangedType = VmiType.Out, + ChangedBy = "WMS", + ChangedNumber = t.PN, + ChangedTime = dateTimeNow, + AssembleData = t.AssembleData, + BillTime = t.BillTime, + CustPartCode = t.CustPartCode, + DeliverBillType = t.DeliverBillType, + DeliverSubBillType = t.DeliverSubBillType, + ErpToLoc = t.ToErpLocCode, + OrderNum = t.DeliveryIndex, Qty = -t.Qty, ReMark = t.Remark, - RealCode = t.PartCode + RealPartCode = t.PartCode }).ToList(); vmiLogList.AddRange(returnVmiLogList); - await SaveVmiMessagesAsync(vmiLogList).ConfigureAwait(false); - await _settleAccountDbContext.BulkInsertAsync(vmiLogList).ConfigureAwait(false); + await SaveVmiLogsAsync(vmiLogList).ConfigureAwait(false); + } + + /// + /// 保存寄售库Log + /// + private async Task SaveVmiLogsAsync(List vmiLogs) + { + await SaveVmiMessagesAsync(vmiLogs).ConfigureAwait(false); + await _settleAccountDbContext.BulkInsertAsync(vmiLogs).ConfigureAwait(false); } /// /// 保存寄售库Message /// - public async Task SaveVmiMessagesAsync(List vmiLogs) + private async Task SaveVmiMessagesAsync(List vmiLogs) { var vmiMessages = vmiLogs.Select(t => new VmiMessage(Guid.NewGuid()) { diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Bases/EntityBase.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Bases/EntityBase.cs index d412d2a1..86780b82 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Bases/EntityBase.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Bases/EntityBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Security.Policy; @@ -9,6 +10,7 @@ using Volo.Abp.Application.Dtos; using Volo.Abp.Domain.Entities.Auditing; using Volo.Abp.Validation; using Win.Sfs.SettleAccount; +using Win.Sfs.SettleAccount.Entities.BQ.Syncs; using Win.Sfs.SettleAccount.Entities.SettleAccountDomain; using static System.Runtime.CompilerServices.RuntimeHelpers; @@ -678,8 +680,209 @@ namespace SettleAccount.Bases } - - - + public class JisSeBase : SE_BASE + { + /// + /// UID + /// + [Display(Name = "UID")] + [DisplayName("UID")] + public long UID { get; set; } + /// + /// JIT订单号 + /// + [Display(Name = "JIT订单号")] + [DisplayName("JIT订单号")] + public string JISNum { get; set; } + /// + /// JIT排序生产码 + /// + [Display(Name = "JIT排序生产码")] + public string VinCode { get; set; } + /// + /// JIT排序生产码类型 + /// + [Display(Name = "JIT排序生产码类型")] + public string CodeType { get; set; } + /// + /// 订单零件号 + /// + [Display(Name = "订单零件号")] + public string PartCode { get; set; } + /// + /// 订单零件号 + /// + [Display(Name = "订单零件号")] + public string RealPartCode { get; set; } + /// + /// 批次 + /// + [Display(Name = "批次")] + public string Batch { get; set; } + /// + /// 客户零件号 + /// + [Display(Name = "客户零件号")] + public string CustPartCode { get; set; } + /// + /// 订单序号 + /// + [Display(Name = "订单序号")] + public string Seq { get; set; } + /// + /// 订单时间 + /// + [Display(Name = "订单时间")] + public DateTime AssembleData { get; set; } + /// + /// 发货条码 + /// + [Display(Name = "发货条码")] + public string DeliverCode { get; set; } + /// + /// 发货单号 + /// + [Display(Name = "发货单号")] + public string BillNum { get; set; } + /// + /// 发货时间 + /// + [Display(Name = "发货时间")] + public DateTime? BillTime { get; set; } + /// + /// 发货人 + /// + [Display(Name = "发货人")] + public string Oper { get; set; } + /// + /// 客户位置 + /// + [Display(Name = "客户位置")] + public string Position { get; set; } + /// + /// 工厂 + /// + [Display(Name = "工厂")] + public string Factory { get; set; } + /// + /// MES配置码 + /// + [Display(Name = "MES配置码")] + public string MESConfigCode { get; set; } + /// + /// 目标库位 + /// + [Display(Name = "目标库位")] + public string ToLoc { get; set; } + /// + /// 单据类型 + /// + public EnumBillType BillType { get; set; } + /// + /// 子单据类型 + /// + [Display(Name = "子单据类型")] + public EnumSubBillType SubBillType { get; set; } + /// + /// 事务类型 + /// + [Display(Name = "事务类型")] + public EnumDelTransType TransType { get; set; } + /// + /// 发运主类型 + /// + [Display(Name = "发运主类型")] + public EnumDeliverBjBmpBillType DeliverBillType { get; set; } + /// + /// 发运子类型 + /// + [Display(Name = "发运子类型")] + public EnumDeliverSubBillType DeliverSubBillType { get; set; } + /// + /// 单据性质 + /// + [Display(Name = "单据性质")] + public string BillCharacter { get; set; } + /// + /// 原生产码 + /// + [Display(Name = "原生产码")] + public string OrigiCode { get; set; } + /// + /// 描述 + /// + [Display(Name = "描述")] + public string PartDesc { get; set; } + /// + /// 备注 + /// + [Display(Name = "备注")] + public string Remark { get; set; } + /// + /// 业务类型 + /// + [Display(Name = "业务类型")] + public EnumProTpe ProType { get; set; } + /// + /// JIS排序单号 + /// + [Display(Name = "JIS排序单号")] + public string OrderNum { get; set; } + /// + /// JIS实际生产码 + /// + [Display(Name = "JIS实际生产码")] + public string RealCode { get; set; } + /// + /// 来源库位 + /// + [Display(Name = "来源库位")] + public string FromLoc { get; set; } + /// + /// 参照订单生产码 + /// + [Display(Name = "参照订单生产码")] + public string RefVinCode { get; set; } + /// + /// 发货关联单号 + /// + [Display(Name = "发货关联单号")] + public string RefBillNum { get; set; } + /// + /// Erp目标库位 + /// + [Display(Name = "Erp目标库位")] + public string ErpToLoc { get; set; } + /// + /// 塑件唯一码 + /// + [Display(Name = "塑件唯一码")] + public string UniqueCode { get; set; } + /// + /// PJS顺序号 + /// + [Display(Name = "PJS顺序号")] + public string PjsNum { get; set; } + /// + /// 虚拟小总成 + /// + [Display(Name = "虚拟小总成")] + public string MatchNumber { get; set; } + /// + /// 状态 + /// + [Display(Name = "状态")] + public EnumBillState State { get; set; } + /// + /// 创建时间 + /// + [Display(Name = "创建时间")] + public DateTime CreateTime { get; set; } + /// + /// 是否有EDI数据 + /// + [Display(Name = "是否有EDI数据")] + public bool IsHaveEdiData { get; set; } + } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/BBAC_SE_DETAIL.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/BBAC_SE_DETAIL.cs index a5f3d222..16e0faea 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/BBAC_SE_DETAIL.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/BBAC_SE_DETAIL.cs @@ -8,7 +8,7 @@ using Win.Sfs.SettleAccount.Entities.BQ.Syncs; namespace SettleAccount.Domain.BQ; [Display(Name = "BBAC发运单")] -public class BBAC_SE_DETAIL:SE_BASE +public class BBAC_SE_DETAIL : JisSeBase { /// /// 业务分类 @@ -48,207 +48,6 @@ public class BBAC_SE_DETAIL:SE_BASE //[Display(Name = "Wms发货单号")] //public string WmsBillNum { get; set; } = null!; - /// - /// UID - /// - [Display(Name = "UID")] - [DisplayName("UID")] - public long UID { get; set; } - /// - /// JIT订单号 - /// - [Display(Name = "JIT订单号")] - [DisplayName("JIT订单号")] - public string JISNum { get; set; } - /// - /// JIT排序生产码 - /// - [Display(Name = "JIT排序生产码")] - public string VinCode { get; set; } - /// - /// JIT排序生产码类型 - /// - [Display(Name = "JIT排序生产码类型")] - public string CodeType { get; set; } - /// - /// 订单零件号 - /// - [Display(Name = "订单零件号")] - public string PartCode { get; set; } - /// - /// 订单零件号 - /// - [Display(Name = "订单零件号")] - public string RealPartCode { get; set; } - /// - /// 批次 - /// - [Display(Name = "批次")] - public string Batch { get; set; } - /// - /// 客户零件号 - /// - [Display(Name = "客户零件号")] - public string CustPartCode { get; set; } - /// - /// 订单序号 - /// - [Display(Name = "订单序号")] - public string Seq { get; set; } - /// - /// 订单时间 - /// - [Display(Name = "订单时间")] - public DateTime AssembleData { get; set; } - /// - /// 发货条码 - /// - [Display(Name = "发货条码")] - public string DeliverCode { get; set; } - /// - /// 发货单号 - /// - [Display(Name = "发货单号")] - public string BillNum { get; set; } - /// - /// 发货时间 - /// - [Display(Name = "发货时间")] - public DateTime? BillTime { get; set; } - /// - /// 发货人 - /// - [Display(Name = "发货人")] - public string Oper { get; set; } - /// - /// 客户位置 - /// - [Display(Name = "客户位置")] - public string Position { get; set; } - /// - /// 工厂 - /// - [Display(Name = "工厂")] - public string Factory { get; set; } - /// - /// MES配置码 - /// - [Display(Name = "MES配置码")] - public string MESConfigCode { get; set; } - /// - /// 目标库位 - /// - [Display(Name = "目标库位")] - public string ToLoc { get; set; } - /// - /// 单据类型 - /// - public EnumBillType BillType { get; set; } - /// - /// 子单据类型 - /// - [Display(Name = "子单据类型")] - public EnumSubBillType SubBillType { get; set; } - /// - /// 事务类型 - /// - [Display(Name = "事务类型")] - public EnumDelTransType TransType { get; set; } - /// - /// 发运主类型 - /// - [Display(Name = "发运主类型")] - public EnumDeliverBjBmpBillType DeliverBillType { get; set; } - /// - /// 发运子类型 - /// - [Display(Name = "发运子类型")] - public EnumDeliverSubBillType DeliverSubBillType { get; set; } - /// - /// 单据性质 - /// - [Display(Name = "单据性质")] - public string BillCharacter { get; set; } - /// - /// 原生产码 - /// - [Display(Name = "原生产码")] - public string OrigiCode { get; set; } - /// - /// 描述 - /// - [Display(Name = "描述")] - public string PartDesc { get; set; } - /// - /// 备注 - /// - [Display(Name = "备注")] - public string Remark { get; set; } - /// - /// 业务类型 - /// - [Display(Name = "业务类型")] - public EnumProTpe ProType { get; set; } - /// - /// JIS排序单号 - /// - [Display(Name = "JIS排序单号")] - public string OrderNum { get; set; } - /// - /// JIS实际生产码 - /// - [Display(Name = "JIS实际生产码")] - public string RealCode { get; set; } - /// - /// 来源库位 - /// - [Display(Name = "来源库位")] - public string FromLoc { get; set; } - /// - /// 参照订单生产码 - /// - [Display(Name = "参照订单生产码")] - public string RefVinCode { get; set; } - /// - /// 发货关联单号 - /// - [Display(Name = "发货关联单号")] - public string RefBillNum { get; set; } - /// - /// Erp目标库位 - /// - [Display(Name = "Erp目标库位")] - public string ErpToLoc { get; set; } - /// - /// 塑件唯一码 - /// - [Display(Name = "塑件唯一码")] - public string UniqueCode { get; set; } - /// - /// PJS顺序号 - /// - [Display(Name = "PJS顺序号")] - public string PjsNum { get; set; } - /// - /// 虚拟小总成 - /// - [Display(Name = "虚拟小总成")] - public string MatchNumber { get; set; } - /// - /// 状态 - /// - [Display(Name = "状态")] - public EnumBillState State { get; set; } - /// - /// 创建时间 - /// - [Display(Name = "创建时间")] - public DateTime CreateTime { get; set; } - /// - /// 是否有EDI数据 - /// - [Display(Name = "是否有EDI数据")] - public bool IsHaveEdiData { get; set; } public BBAC_SE_DETAIL(Guid guid, string keyCode, int version, string lU, string pN, string seqNumber, string assemblyCode, string injectionCode, decimal qty, DateTime beginDate, DateTime shippingDate, string wmsBillNum) { diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/HBPO_SE_DETAIL.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/HBPO_SE_DETAIL.cs index ff4eea93..a216c0b5 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/HBPO_SE_DETAIL.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Domain/Entities/BQ/HBPO_SE_DETAIL.cs @@ -1,14 +1,12 @@ -using SettleAccount.Bases; using System; -using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using SettleAccount.Bases; using Win.Sfs.SettleAccount; -using Win.Sfs.SettleAccount.Entities.BQ.Syncs; namespace SettleAccount.Domain.BQ; [Display(Name = "HBPO发运数据")] -public class HBPO_SE_DETAIL :SE_BASE +public class HBPO_SE_DETAIL : JisSeBase { /// /// 业务分类 @@ -48,207 +46,6 @@ public class HBPO_SE_DETAIL :SE_BASE //[Display(Name = "Wms发货单号")] //public string WmsBillNum { get; set; } = null!; - /// - /// UID - /// - [Display(Name = "UID")] - [DisplayName("UID")] - public long UID { get; set; } - /// - /// JIT订单号 - /// - [Display(Name = "JIT订单号")] - [DisplayName("JIT订单号")] - public string JISNum { get; set; } - /// - /// JIT排序生产码 - /// - [Display(Name = "JIT排序生产码")] - public string VinCode { get; set; } - /// - /// JIT排序生产码类型 - /// - [Display(Name = "JIT排序生产码类型")] - public string CodeType { get; set; } - /// - /// 订单零件号 - /// - [Display(Name = "订单零件号")] - public string PartCode { get; set; } - /// - /// 订单零件号 - /// - [Display(Name = "订单零件号")] - public string RealPartCode { get; set; } - /// - /// 批次 - /// - [Display(Name = "批次")] - public string Batch { get; set; } - /// - /// 客户零件号 - /// - [Display(Name = "客户零件号")] - public string CustPartCode { get; set; } - /// - /// 订单序号 - /// - [Display(Name = "订单序号")] - public string Seq { get; set; } - /// - /// 订单时间 - /// - [Display(Name = "订单时间")] - public DateTime AssembleData { get; set; } - /// - /// 发货条码 - /// - [Display(Name = "发货条码")] - public string DeliverCode { get; set; } - /// - /// 发货单号 - /// - [Display(Name = "发货单号")] - public string BillNum { get; set; } - /// - /// 发货时间 - /// - [Display(Name = "发货时间")] - public DateTime? BillTime { get; set; } - /// - /// 发货人 - /// - [Display(Name = "发货人")] - public string Oper { get; set; } - /// - /// 客户位置 - /// - [Display(Name = "客户位置")] - public string Position { get; set; } - /// - /// 工厂 - /// - [Display(Name = "工厂")] - public string Factory { get; set; } - /// - /// MES配置码 - /// - [Display(Name = "MES配置码")] - public string MESConfigCode { get; set; } - /// - /// 目标库位 - /// - [Display(Name = "目标库位")] - public string ToLoc { get; set; } - /// - /// 单据类型 - /// - public EnumBillType BillType { get; set; } - /// - /// 子单据类型 - /// - [Display(Name = "子单据类型")] - public EnumSubBillType SubBillType { get; set; } - /// - /// 事务类型 - /// - [Display(Name = "事务类型")] - public EnumDelTransType TransType { get; set; } - /// - /// 发运主类型 - /// - [Display(Name = "发运主类型")] - public EnumDeliverBjBmpBillType DeliverBillType { get; set; } - /// - /// 发运子类型 - /// - [Display(Name = "发运子类型")] - public EnumDeliverSubBillType DeliverSubBillType { get; set; } - /// - /// 单据性质 - /// - [Display(Name = "单据性质")] - public string BillCharacter { get; set; } - /// - /// 原生产码 - /// - [Display(Name = "原生产码")] - public string OrigiCode { get; set; } - /// - /// 描述 - /// - [Display(Name = "描述")] - public string PartDesc { get; set; } - /// - /// 备注 - /// - [Display(Name = "备注")] - public string Remark { get; set; } - /// - /// 业务类型 - /// - [Display(Name = "业务类型")] - public EnumProTpe ProType { get; set; } - /// - /// JIS排序单号 - /// - [Display(Name = "JIS排序单号")] - public string OrderNum { get; set; } - /// - /// JIS实际生产码 - /// - [Display(Name = "JIS实际生产码")] - public string RealCode { get; set; } - /// - /// 来源库位 - /// - [Display(Name = "来源库位")] - public string FromLoc { get; set; } - /// - /// 参照订单生产码 - /// - [Display(Name = "参照订单生产码")] - public string RefVinCode { get; set; } - /// - /// 发货关联单号 - /// - [Display(Name = "发货关联单号")] - public string RefBillNum { get; set; } - /// - /// Erp目标库位 - /// - [Display(Name = "Erp目标库位")] - public string ErpToLoc { get; set; } - /// - /// 塑件唯一码 - /// - [Display(Name = "塑件唯一码")] - public string UniqueCode { get; set; } - /// - /// PJS顺序号 - /// - [Display(Name = "PJS顺序号")] - public string PjsNum { get; set; } - /// - /// 虚拟小总成 - /// - [Display(Name = "虚拟小总成")] - public string MatchNumber { get; set; } - /// - /// 状态 - /// - [Display(Name = "状态")] - public EnumBillState State { get; set; } - /// - /// 创建时间 - /// - [Display(Name = "创建时间")] - public DateTime CreateTime { get; set; } - /// - /// 是否有EDI数据 - /// - [Display(Name = "是否有EDI数据")] - public bool IsHaveEdiData { get; set; } public HBPO_SE_DETAIL(Guid guid, string keyCode, int version, string lU, string pN, string seqNumber, string assemblyCode, string injectionCode, decimal qty, DateTime beginDate, DateTime shippingDate, string wmsBillNum) { From 042950255e9d7d5e6a17001578864a533d2e2e4a Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 30 Aug 2023 17:44:48 +0800 Subject: [PATCH 2/2] up --- .../Entities/BQ/VmiAsyncBalanceService.cs | 212 ++++++++---------- .../Entities/BQ/VmiAsyncMessageService.cs | 173 ++++++++++++++ 2 files changed, 264 insertions(+), 121 deletions(-) create mode 100644 code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncMessageService.cs 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 be57c1c2..4f5f1991 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 @@ -4,15 +4,11 @@ using System.Linq; using System.Linq.Dynamic.Core; using System.Text.Json; using System.Threading.Tasks; -using EFCore.BulkExtensions; using Magicodes.ExporterAndImporter.Core.Extension; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Omu.ValueInjecter; -using SettleAccount.Job.SignalR; using Volo.Abp.Application.Services; using Volo.Abp.DependencyInjection; using Win.Sfs.SettleAccount.Entities.BQ.Vmi; @@ -34,140 +30,114 @@ public class VmiAsyncBalanceService : ApplicationService, IJobService, ITransien public async Task Invoke(IServiceProvider serviceProvider) { - var connectionString = serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); - using var connection = new SqlConnection(connectionString); - connection.Open(); - using var transaction = connection.BeginTransaction(); - try + for (var i = 0; i < 30; i++) { - 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 messages = context.Set().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(1000 * 100).ToList(); - var repo = context.Set(); - foreach (var message in messages) + var connectionString = serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); + using var connection = new SqlConnection(connectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + try { - var log = JsonSerializer.Deserialize(message.Message); - log.SetId(Guid.Parse(JsonSerializer.Deserialize(message.Message).GetProperty("Id").GetString())); - //插入分表 - var table = $"Set_VmiLog_{log.ChangedTime.Year}_{(log.ChangedTime.Month - 1) / 3 + 1}"; - command.CommandText = $"select OBJECT_ID('{table}', 'U')"; - var result = command.ExecuteScalar().ToString(); - if (result == string.Empty) + var command = connection.CreateCommand(); + command.Transaction = transaction; + var options = new DbContextOptionsBuilder().UseSqlServer(connection).Options; + using var context = new SettleAccountDbContext(options); + context.Database.UseTransaction(transaction); + if (!context.Set().Any(o => !o.isConsumed)) { - command.CommandText = $"select * into {table} from Set_VmiLog where 1=0;"; - command.ExecuteNonQuery(); - command.CommandText = $"create clustered index IX_{table}_ChangedTime on {table} (ChangedTime);"; - command.ExecuteNonQuery(); - command.CommandText = $"alter table {table} add constraint PK_{table} primary key (Id);"; - command.ExecuteNonQuery(); + break; } - //插入到分表 - command.CommandText = $"insert into {table} select * from Set_VmiLog where id ='{log.Id}'"; - command.ExecuteNonQuery(); - //插入库存 - var balance = context.Set().FirstOrDefault( - o => o.DeliverBillType == log.DeliverBillType && - o.CodeType == log.CodeType && - o.DeliverBillType == log.DeliverBillType && - o.VinCode == log.VinCode && - o.ErpToLoc == log.ErpToLoc && - o.OrderNum == log.OrderNum && - o.factory == log.factory && - o.Configcode == log.Configcode); - if (balance == null) + var messages = context.Set().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(1000).ToList(); + var repo = context.Set(); + foreach (var message in messages) { - balance = new VmiBalance(GuidGenerator.Create()); - balance.InjectFrom(log); - await repo.AddAsync(balance).ConfigureAwait(false); - log.InjectFrom(balance); - } - else - { - var logType = log.LogType; - - var qty = balance.Qty;// + log.ty - if (logType == VmiLogType.Type100) - { - //发运入库,负库存字段需要更新 - if (balance.Qty < decimal.Zero) - { - balance.InjectFrom(log); - } - } - else if (logType == VmiLogType.Type300) + var log = JsonSerializer.Deserialize(message.Message); + log.SetId(Guid.Parse(JsonSerializer.Deserialize(message.Message).GetProperty("Id").GetString())); + //插入分表 + var table = $"Set_VmiLog_{log.ChangedTime.Year}_{(log.ChangedTime.Month - 1) / 3 + 1}"; + 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 = $"create clustered index IX_{table}_ChangedTime on {table} (ChangedTime);"; + command.ExecuteNonQuery(); + command.CommandText = $"alter table {table} add constraint PK_{table} primary key (Id);"; + command.ExecuteNonQuery(); } - else if (logType == VmiLogType.Type500) + //插入到分表 + command.CommandText = $"insert into {table} select * from Set_VmiLog where id ='{log.Id}'"; + command.ExecuteNonQuery(); + //插入库存 + var balance = context.Set().FirstOrDefault( + o => o.DeliverBillType == log.DeliverBillType && + o.CodeType == log.CodeType && + o.DeliverBillType == log.DeliverBillType && + 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(GuidGenerator.Create()); balance.InjectFrom(log); + await repo.AddAsync(balance).ConfigureAwait(false); + log.InjectFrom(balance); } - // 更新库存 - balance.Qty = qty; - if (balance.Qty == decimal.Zero) - { - //删除0库存 - repo.Remove(balance); - } - if (logType == VmiLogType.Type100 && balance.Qty < decimal.Zero && log.Qty > 0) + else { - //添加负库存补货记录 - var log2 = new VmiReplenished(); - log2.InjectFrom(log); - await context.Set().AddAsync(log2).ConfigureAwait(false); + var logType = log.LogType; + + var qty = 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); + } + // 更新库存 + balance.Qty = qty; + if (balance.Qty == decimal.Zero) + { + //删除0库存 + repo.Remove(balance); + } + if (logType == VmiLogType.Type100 && balance.Qty < decimal.Zero && log.Qty > 0) + { + //添加负库存补货记录 + var log2 = new VmiReplenished(); + log2.InjectFrom(log); + await context.Set().AddAsync(log2).ConfigureAwait(false); + } } + message.isConsumed = true; } - message.isConsumed = true; + context.SaveChanges(); + transaction.Commit(); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + transaction.Rollback(); + throw; } - context.SaveChanges(); - transaction.Commit(); - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - transaction.Rollback(); - throw; } } -} - -/// -/// 消息表定时清理 -/// -public class VmiAsyncMessageService : Controller, IApplicationService, IJobService, ITransientDependency -{ - private readonly IServiceProvider _serviceProvider; - - public VmiAsyncMessageService(IServiceProvider serviceProvider) - { - this._serviceProvider = serviceProvider; - } - - [NonAction] - public Task Invoke(IServiceProvider serviceProvider) - { - using var scope = serviceProvider.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - db.Set().Where(o => o.isConsumed).BatchDelete(); - var count = db.Set().Where(o => !o.isConsumed).Count(); - scope.ServiceProvider.GetService>().Clients.All.ServerToClient("VmiBalance", count.ToString(), ""); - return Task.CompletedTask; - } - /// - /// 未处理消息数量 - /// - /// - [HttpPost] - public int GetMessageCount() + public async Task InvokeInternal(IServiceProvider serviceProvider) { - using var scope = this._serviceProvider.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - var count = db.Set().Where(o => !o.isConsumed).Count(); - return count; } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncMessageService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncMessageService.cs new file mode 100644 index 00000000..be57c1c2 --- /dev/null +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAsyncMessageService.cs @@ -0,0 +1,173 @@ +using System; +using System.Data.SqlClient; +using System.Linq; +using System.Linq.Dynamic.Core; +using System.Text.Json; +using System.Threading.Tasks; +using EFCore.BulkExtensions; +using Magicodes.ExporterAndImporter.Core.Extension; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Omu.ValueInjecter; +using SettleAccount.Job.SignalR; +using Volo.Abp.Application.Services; +using Volo.Abp.DependencyInjection; +using Win.Sfs.SettleAccount.Entities.BQ.Vmi; +using Win.Sfs.Shared.RepositoryBase; + +namespace Win.Sfs.SettleAccount.Entities.BQ; + +/// +/// 异步更新库存 +/// +public class VmiAsyncBalanceService : ApplicationService, IJobService, ITransientDependency +{ + private readonly IServiceProvider _serviceProvider; + + public VmiAsyncBalanceService(IServiceProvider serviceProvider) + { + this._serviceProvider = serviceProvider; + } + + public async Task Invoke(IServiceProvider serviceProvider) + { + var connectionString = serviceProvider.GetRequiredService().GetConnectionString("SettleAccountService"); + using var connection = new 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 messages = context.Set().Where(o => !o.isConsumed).OrderBy(o => o.Number).Take(1000 * 100).ToList(); + var repo = context.Set(); + foreach (var message in messages) + { + var log = JsonSerializer.Deserialize(message.Message); + log.SetId(Guid.Parse(JsonSerializer.Deserialize(message.Message).GetProperty("Id").GetString())); + //插入分表 + var table = $"Set_VmiLog_{log.ChangedTime.Year}_{(log.ChangedTime.Month - 1) / 3 + 1}"; + 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 = $"create clustered index IX_{table}_ChangedTime on {table} (ChangedTime);"; + command.ExecuteNonQuery(); + command.CommandText = $"alter table {table} add constraint PK_{table} primary key (Id);"; + command.ExecuteNonQuery(); + } + //插入到分表 + command.CommandText = $"insert into {table} select * from Set_VmiLog where id ='{log.Id}'"; + command.ExecuteNonQuery(); + //插入库存 + var balance = context.Set().FirstOrDefault( + o => o.DeliverBillType == log.DeliverBillType && + o.CodeType == log.CodeType && + o.DeliverBillType == log.DeliverBillType && + 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(GuidGenerator.Create()); + balance.InjectFrom(log); + await repo.AddAsync(balance).ConfigureAwait(false); + log.InjectFrom(balance); + } + else + { + var logType = log.LogType; + + var qty = 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); + } + // 更新库存 + balance.Qty = qty; + if (balance.Qty == decimal.Zero) + { + //删除0库存 + repo.Remove(balance); + } + if (logType == VmiLogType.Type100 && balance.Qty < decimal.Zero && log.Qty > 0) + { + //添加负库存补货记录 + var log2 = new VmiReplenished(); + log2.InjectFrom(log); + await context.Set().AddAsync(log2).ConfigureAwait(false); + } + } + message.isConsumed = true; + } + context.SaveChanges(); + transaction.Commit(); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + transaction.Rollback(); + throw; + } + } +} + +/// +/// 消息表定时清理 +/// +public class VmiAsyncMessageService : Controller, IApplicationService, IJobService, ITransientDependency +{ + private readonly IServiceProvider _serviceProvider; + + public VmiAsyncMessageService(IServiceProvider serviceProvider) + { + this._serviceProvider = serviceProvider; + } + + [NonAction] + public Task Invoke(IServiceProvider serviceProvider) + { + using var scope = serviceProvider.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.Set().Where(o => o.isConsumed).BatchDelete(); + var count = db.Set().Where(o => !o.isConsumed).Count(); + scope.ServiceProvider.GetService>().Clients.All.ServerToClient("VmiBalance", count.ToString(), ""); + return Task.CompletedTask; + } + + /// + /// 未处理消息数量 + /// + /// + [HttpPost] + public int GetMessageCount() + { + using var scope = this._serviceProvider.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var count = db.Set().Where(o => !o.isConsumed).Count(); + return count; + } +}