22
schangxiang@126.com
2025-04-29 3fdd40af6a7c4ca1b9c833fd7fbe17a9bcc9258f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using CMS.Plugin.MesSuite.Abstractions.Events;
using CMS.Plugin.MesSuite.Abstractions.Models;
using CMS.Plugin.PipeLineLems.Domain.MyTestEntityNames;
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.PipeLineLems.EventHandlers
{
    /// <summary>
    /// 流程事件处理程序
    /// </summary>
    public class PipeLineLemsEventHandler : IDistributedEventHandler<ProcessFlowEto>, ITransientDependency
    {
        private readonly ILogger _logger;
        private readonly IServiceProvider _serviceProvider;
 
        /// <summary>
        /// Initializes a new instance of the <see cref="PipeLineLemsEventHandler"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="serviceProvider">The service provider.</param>
        public PipeLineLemsEventHandler(ILogger<PipeLineLemsEventHandler> logger, IServiceProvider serviceProvider)
        {
            this._logger = logger;
            this._serviceProvider = serviceProvider;
        }
 
        /// <summary>
        /// Handler handles the event by implementing this method.
        /// </summary>
        /// <param name="eventData">Event data</param>
        public async Task HandleEventAsync(ProcessFlowEto eventData)
        {
            if (eventData.Activity.Equals("步骤名称"))
            {
                _logger.LogInformation($"MyTestEntityNameEventHandler: 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();
            }
        }
 
        /// <summary>
        /// Processes the asynchronous.
        /// </summary>
        private async Task ProcessAsync()
        {
            using var scope = _serviceProvider.CreateScope();
            var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
            using var uow = unitOfWorkManager.Begin(requiresNew: true);
            var mytestentitynameRepository = scope.ServiceProvider.GetRequiredService<IMyTestEntityNameRepository>();
            var count = await mytestentitynameRepository.GetCountAsync();
 
            // 如果有更新数据库操作,需提交保存
            // await uow.SaveChangesAsync();
 
            _logger.LogInformation($"ProcessAsync,Count={count}");
        }
    }
}