using AutoMapper; using Kean.Domain.Task.Commands; using Kean.Domain.Task.Enums; using Kean.Domain.Task.Events; using Kean.Domain.Task.Models; using Kean.Domain.Task.Repositories; using Kean.Domain.Task.SharedServices.Proxies; using Kean.Domain.Task.Strategies; using Kean.Infrastructure.Configuration; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Linq; using System.Threading; namespace Kean.Domain.Task.CommandHandlers { /// /// 创建上架命令处理程序 /// public sealed class InfeedCheckCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly INotification _notifications; // 总线通知 private readonly IMapper _mapper; // 模型映射 private readonly ITaskRepository _taskRepository; // 任务仓库 private readonly IWarehouseRepository _warehouseRepository; // 库房仓库 private readonly BarcodeInterpreter _barcodeInterpreter; // 条码解释器 private readonly BarcodeValidator _barcodeValidator; // 条码验证器 private readonly StockProxy _stockProxy; // 仓储域代理 private readonly InfeedContext _infeedContext; // 上架策略 private readonly MaterialProxy _materialProxy; /// /// 依赖注入 /// public InfeedCheckCommandHandler( IServiceProvider serviceProvider, ICommandBus commandBus, INotification notifications, IMapper mapper, ITaskRepository taskRepository, IWarehouseRepository warehouseRepository, BarcodeInterpreter barcodeInterpreter, BarcodeValidator barcodeValidator, StockProxy stockProxy, MaterialProxy materialProxy) { _commandBus = commandBus; _notifications = notifications; _mapper = mapper; _taskRepository = taskRepository; _warehouseRepository = warehouseRepository; _barcodeInterpreter = barcodeInterpreter; _barcodeValidator = barcodeValidator; _stockProxy = stockProxy; _materialProxy = materialProxy; _infeedContext = new(serviceProvider); } /// /// 处理程序 /// public override async System.Threading.Tasks.Task Handle(InfeedCheckCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { // 电气异常报警(检尺异常) if (!string.IsNullOrEmpty(command.Parameters) && command.Parameters.StartsWith("errorMessage:")) { await _commandBus.Notify(null, $"{command.Parameters[13..]}", null, cancellationToken: cancellationToken); return; } // 条码格式校验 if (!await _barcodeValidator.Validate(command.Barcode)) { await _commandBus.Notify(nameof(command.Barcode), "条码格式不正确", command.Barcode, cancellationToken: cancellationToken); return; } // 任务重复 if (await _taskRepository.HasTask(command.Barcode)) { await _commandBus.Notify(nameof(command.Barcode), "托盘正在执行任务", command.Barcode, cancellationToken: cancellationToken); return; } var pallet = await _barcodeInterpreter.Interpret(command.Barcode); // 获取操作位置 var original = command.Original switch { int i => await _warehouseRepository.GetStationById(i), long i => await _warehouseRepository.GetStationById((int)i), string s when s.StartsWith("name:") => await _warehouseRepository.GetStationByName(s[5..], command.Warehouse), string s when s.StartsWith("device:") => await _warehouseRepository.GetStationByDevice(s[7..], command.Warehouse), Station s => s, _ => null }; // 操作位置可用性 if (original?.Warehouse != command.Warehouse || original.AllowIn != true || original.Pallet?.Contains(pallet) == false || original.Spec?.Adaptive(command.Spec) == false) { await _commandBus.Notify(nameof(command.Original), "操作位置不正确", command.Original, cancellationToken: cancellationToken); return; } //校验 托盘规则与申请位置是否匹配 //if ((original.Device.StartsWith("1") || original.Device.StartsWith("2")) && original.Device != "12226" && original.Device != "22226") if (original.Device == "12103" || original.Device == "12105" || original.Device == "12110" || original.Device == "12112" || original.Device == "17101" || original.Device == "22103" || original.Device == "22105" || original.Device == "22110" || original.Device == "22112" || original.Device == "27101") { Models.Warehouse warehouse = await _warehouseRepository.GetWarehouse(original.Warehouse); if (warehouse.Code != command.Barcode.Substring(0, 5)) { await _commandBus.Notify(nameof(command.Barcode), $"托盘({command.Barcode})不能进入({warehouse.Name})库房", command.Barcode, cancellationToken: cancellationToken); return; } } if (!(await _stockProxy.GetCell(command.Barcode)).HasValue) { //@=@ 固定值 if (command.Tag == "3") { int? endDevice = original?.Id; var sGoods = await _materialProxy.GetMaterial(null, (string)pallet, true); Models.Material goods = JsonConvert.DeserializeObject(sGoods); if (goods == null) { await _commandBus.Notify(nameof(command.Original), "空托盘条码解析类型失败", command.Original, cancellationToken: cancellationToken); return; } int? goodsid = goods.Id; if (goodsid.HasValue) { command.Material = goodsid.Value; } await _stockProxy.PalletIn( command.Barcode, endDevice.Value, goodsid.Value, command.Operator, command.Tag ); } else { await _commandBus.Notify(nameof(command.Barcode), "托盘没有组盘入库,请先组盘", command.Barcode, cancellationToken: cancellationToken); return; } } else { command.Material = await _stockProxy.GetStorageMaterial(command.Barcode); bool bPallet = await _taskRepository.IsPallet(command.Material.Value); //if (command.Material == 5 || command.Material == 9 || command.Material == 57 || command.Material == 58 || command.Material == 60 || command.Material == 61 || command.Material == 62) if(bPallet) { await _commandBus.Notify(nameof(command.Barcode), $"托盘({command.Barcode})已经有空托盘库存,无法带物料入库,请检查库存", command.Barcode, cancellationToken: cancellationToken); return; } //@=@ 固定值 if (command.Tag == "3") { //if (command.Material == 5 || command.Material == 9 || command.Material == 57 || command.Material == 58 || command.Material == 60 || command.Material == 61 || command.Material == 62) if (bPallet) { } else { await _commandBus.Notify(nameof(command.Barcode), $"托盘({command.Barcode})已经有库存,无法空托盘入库", command.Barcode, cancellationToken: cancellationToken); return; } } else { //command.Material = await _stockProxy.GetStorageMaterial(command.Barcode); } } if (!command.Material.HasValue) { } // 不允许操作立库中的库存 var cell = await _stockProxy.GetCell(command.Barcode); if (cell.HasValue && await _warehouseRepository.IsCell(cell.Value) == true) { await _commandBus.Notify(nameof(command.Barcode), "不允许操作立库中的库存", command.Barcode, cancellationToken: cancellationToken); return; } //获取库存信息 string storageTag = await _taskRepository.GetBypassTag(command.Barcode); string requestNo = string.Empty; if (!string.IsNullOrEmpty(storageTag)) { try { JObject jObj = JObject.Parse(storageTag); JToken endCell = jObj.SelectToken("Cell"); if (endCell != null) { command.Destination = "device:" + Convert.ToString(jObj["Cell"]); } JToken requestNoFlg = jObj.SelectToken("requestNo"); if (requestNoFlg != null) { requestNo = Convert.ToString(jObj["requestNo"]); } } catch { } } command.Timestamp ??= DateTime.Now; var event0 = _mapper.Map(command); event0.Original = original; await _commandBus.Trigger(event0, cancellationToken); } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }