using CMS.Plugin.MesSuite.Abstractions.Events; using CMS.Plugin.MesSuite.Abstractions.Models; using CMS.Plugin.HIAWms.Domain.WmsContainers; using CMS.Plugin.OrderManagement.Abstractions.Models; using CMS.Plugin.ProcessManagement.Abstractions.Models; using CMS.Plugin.TraceManagement.Abstractions.Models.Traces; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Distributed; using Volo.Abp.Uow; namespace CMS.Plugin.HIAWms.EventHandlers { /// /// 流程事件处理程序 /// public class HIAWmsEventHandler : IDistributedEventHandler, ITransientDependency { private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; /// /// Initializes a new instance of the class. /// /// The logger. /// The service provider. public HIAWmsEventHandler(ILogger logger, IServiceProvider serviceProvider) { this._logger = logger; this._serviceProvider = serviceProvider; } /// /// Handler handles the event by implementing this method. /// /// Event data public async Task HandleEventAsync(ProcessFlowEto eventData) { if (eventData.Activity.Equals("步骤名称")) { _logger.LogInformation($"WmsContainerEventHandler: Activity={eventData.Activity}"); var serialNumber = eventData?.FlowItems[FlowItemCollection.SerialNumber]?.ToString(); // 工艺模型 var process = eventData?.FlowItems[FlowItemCollection.ApplicationData] as ProcessModel; // 产品模型 var product = eventData?.FlowItems[FlowItemCollection.ProductModel] as AssociationProductModel; // 追溯模型 var trace = eventData?.FlowItems[FlowItemCollection.TraceModel] as TraceModel; // 工单模型 var order = eventData?.FlowItems[FlowItemCollection.OrderModel] as OrderModel; // 业务处理 await ProcessAsync(); } } /// /// Processes the asynchronous. /// private async Task ProcessAsync() { using var scope = _serviceProvider.CreateScope(); var unitOfWorkManager = scope.ServiceProvider.GetRequiredService(); using var uow = unitOfWorkManager.Begin(requiresNew: true); var wmscontainerRepository = scope.ServiceProvider.GetRequiredService(); var count = await wmscontainerRepository.GetCountAsync(); // 如果有更新数据库操作,需提交保存 // await uow.SaveChangesAsync(); _logger.LogInformation($"ProcessAsync,Count={count}"); } } }