using AutoMapper; using Kean.Domain.Task.Events; using Kean.Domain.Task.Repositories; using Kean.Domain.Task.SharedServices.Proxies; using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading; namespace Kean.Domain.Task.EventHandlers { /// /// 任务完成命令成功时,处理后续 /// [EventHandlerIndex(9)] public sealed class CompleteSuccessEventHandler_Follow : EventHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly INotification _notifications; // 总线通知 private readonly IMapper _mapper; // 模型映射 private readonly ITaskRepository _taskRepository; // 任务仓库 private readonly IWarehouseRepository _warehouseRepository; // 库房仓库 private readonly StockProxy _stockProxy; // 仓储域 private readonly ILogger _logger; // 日志 /// /// 依赖注入 /// public CompleteSuccessEventHandler_Follow( ICommandBus commandBus, INotification notifications, IMapper mapper, ITaskRepository taskRepository, IWarehouseRepository warehouseRepository, ILogger logger, StockProxy stockProxy) { _commandBus = commandBus; _notifications = notifications; _mapper = mapper; _taskRepository = taskRepository; _warehouseRepository = warehouseRepository; _stockProxy = stockProxy; _logger = logger; } /// /// 处理程序 /// public override async System.Threading.Tasks.Task Handle(CompleteSuccessEvent @event, CancellationToken cancellationToken) { _logger.LogInformation($"Manage任务({@event.Id})后续任务开始"); foreach (var item in await _taskRepository.GetFollows(@event.Id)) { _logger.LogInformation($"Manage任务({@event.Id})找到后续任务({item.Id})"); //@=@ 新项目时,改为control下发模式 <=这样不行,因为有种近叉先空,再放进时,下达了远叉出库,需要等近叉任务完成后,再避让移开 await _taskRepository.DeleteTask(item.Id); int? cellId = await _stockProxy.GetCell(item.Barcode); int originalCellId = cellId.HasValue ? cellId.Value : item.Original; await _warehouseRepository.ReleaseCell(item.TransitPlace == 0 ? originalCellId : item.TransitPlace, item.Destination); item.Previous = null; item.Warehouse = (await _warehouseRepository.GetCellById(item.Original)) == null? 0: (await _warehouseRepository.GetCellById(item.Original)).Warehouse; if (item.Tag != null) { item.Tag = item.Tag.Contains("isAgv") ? item.Tag : ""; } var command = _mapper.Map(item, typeof(Models.Task), Type.GetType($"Kean.Domain.Task.Commands.OutfeedCommand")) as ICommand; await _commandBus.Execute(command, cancellationToken); if (!_notifications.Any()) { _logger.LogInformation($"Manage任务({@event.Id})生成新的后续任务({Convert.ToInt32(command.GetType().GetProperty("Id").GetValue(command))})"); await _taskRepository.UpdatePrevious(item.Id, Convert.ToInt32(command.GetType().GetProperty("Id").GetValue(command))); } } _logger.LogInformation($"Manage任务({@event.Id})后续任务结束"); } } }