using AutoMapper; using Kean.Application.Command.Interfaces; using Kean.Application.Command.ViewModels; using Kean.Domain; using Kean.Domain.Task.Commands; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Orleans; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; namespace Kean.Application.Command.Implements { /// /// 任务命令服务实现 /// public class TaskService : ITaskService { private readonly ILogger _logger; // 日志 private readonly ICommandBus _bus; // 命令总线 private readonly INotification _notifications; // 总线通知 private readonly IMapper _mapper; // 模型映射 private readonly IClusterClient _cluster; // 筒仓客户端 /// /// 依赖注入 /// public TaskService( ILogger logger, ICommandBus bus, INotification notifications, IMapper mapper, IClusterClient cluster) { _logger = logger; _bus = bus; _notifications = notifications; _mapper = mapper; _cluster = cluster; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Infeed 方法 */ public async Task<(int Id, Failure Failure)> Infeed(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:创建上架任务:条码[{Barcode}],起点[{Original}],终点[{Destination}],标签[{Tag}]。", hash, task.Barcode, task.Original, task.Destination, task.Tag); stopwatch.Start(); //await _bus.Execute(command); //(int Id, Failure Failure) result = (command.Id, _notifications.FirstOrDefault()); var result = await _cluster.GetGrain(Guid.Empty).Infeed(command); // if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:ID[{Id}],耗时{TimeSpan}ms。", hash, result.Id, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Outfeed 方法 */ public async Task<(int Id, Failure Failure)> Outfeed(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:创建下架任务:条码[{Barcode}],终点[{Destination}],标签[{Tag}]。", hash, task.Barcode, task.Destination, task.Tag); stopwatch.Start(); //await _bus.Execute(command); //(int Id, Failure Failure) result = (command.Id, _notifications.FirstOrDefault()); var result = await _cluster.GetGrain(Guid.Empty).Outfeed(command); // if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:ID[{Id}],耗时{TimeSpan}ms。", hash, result.Id, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Transfer 方法 */ public async Task<(int Id, Failure Failure)> Transfer(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:创建移库任务。条码[{Barcode}],终点[{Destination}],标签[{Tag}]。", hash, task.Barcode, task.Destination, task.Tag); stopwatch.Start(); //await _bus.Execute(command); //(int Id, Failure Failure) result = (command.Id, _notifications.FirstOrDefault()); var result = await _cluster.GetGrain(Guid.Empty).Transfer(command); // if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:ID[{Id}],耗时{TimeSpan}ms。", hash, result.Id, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Bypass 方法 */ public async Task<(int Id, Failure Failure)> Bypass(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:创建输送任务。条码[{Barcode}],起点[{Original}],终点[{Destination}],标签[{Tag}]。", hash, task.Barcode, task.Original, task.Destination, task.Tag); stopwatch.Start(); //await _bus.Execute(command); //(int Id, Failure Failure) result = (command.Id, _notifications.FirstOrDefault()); var result = await _cluster.GetGrain(Guid.Empty).Bypass(command); // if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:ID[{Id}],耗时{TimeSpan}ms。", hash, result.Id, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Execute 方法 */ public async Task Execute(int id) { var stopwatch = new Stopwatch(); var command = new ExecuteCommand { Id = id }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:执行任务[{Id}]。", hash, id); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).Execute(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Cancel 方法 */ public async Task Cancel(int id) { var stopwatch = new Stopwatch(); var command = new CancelCommand { Id = id, ManualFlg = 1 }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:取消任务[{Id}]。", hash, id); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).Cancel(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Complete 方法 */ public async Task Complete(int id) { var stopwatch = new Stopwatch(); var command = new CompleteCommand { Id = id, ManualFlg = 1}; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:完成任务[{Id}]。", hash, id); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).Complete(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); if (result.PropertyName == "TaskState" && result.AttemptedValue == "AgvFail") { SetTaskStateCommand errorCommand = new SetTaskStateCommand(); errorCommand.Id = id; errorCommand.State = result.AttemptedValue.ToString(); errorCommand.Message = result.ErrorMessage; errorCommand.Type = "TaskWcs"; await _cluster.GetGrain(Guid.Empty).SetTaskState(errorCommand); } } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.Block 方法 */ public async Task Block(int id, string message) { var stopwatch = new Stopwatch(); var command = new BlockCommand { Id = id, Message = message }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:阻塞任务[{Id}]。原因[{Message}]。", hash, id, message); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).Block(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.Disable 方法 */ public async Task<(IEnumerable States, Failure Failure)> Disable(int warehouse, IEnumerable cells, string celltag) { var stopwatch = new Stopwatch(); var command = new DisableCommand { Warehouse = warehouse, Cells = cells, CellTag = celltag }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:禁用货位[{Cells}]。", hash, cells); stopwatch.Start(); await _bus.Execute(command); (IEnumerable States, Failure Failure) result = (command.States, _notifications.FirstOrDefault()); if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.Enable 方法 */ public async Task<(IEnumerable States, Failure Failure)> Enable(int warehouse, IEnumerable cells) { var stopwatch = new Stopwatch(); var command = new EnableCommand { Warehouse = warehouse, Cells = cells }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:启用货位[{Cells}]。", hash, cells); stopwatch.Start(); await _bus.Execute(command); (IEnumerable States, Failure Failure) result = (command.States, _notifications.FirstOrDefault()); if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } ///////////////////////////////////////////////////////////////////////////// /* * 实现 Kean.Application.Command.Interfaces.ITaskService.ApplyOut 方法 */ public async Task<(int Id, Failure Failure)> ApplyOut(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:物料出库任务:物料[{1}],终点[{Destination}],标签[{Tag}]。", hash, string.Join(",", task.Materials), task.Destination, task.Tag); stopwatch.Start(); //await _bus.Execute(command); //(int Id, Failure Failure) result = (command.Id, _notifications.FirstOrDefault()); var result = await _cluster.GetGrain(Guid.Empty).ApplyOut(command); // if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:ID[{Id}],耗时{TimeSpan}ms。", hash, result.Id, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.PalletOut 方法 */ public async Task<(int Id, Failure Failure)> PalletOut(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:托盘出库任务:条码[{Barcode}],终点[{Destination}],标签[{Tag}]。", hash, task.Barcode, task.Destination, task.Tag); stopwatch.Start(); //await _bus.Execute(command); //(int Id, Failure Failure) result = (command.Id, _notifications.FirstOrDefault()); var result = await _cluster.GetGrain(Guid.Empty).PalletOut(command); // if (result.Failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result.Failure); } _logger.LogInformation("过程[{Hash}]结束:ID[{Id}],耗时{TimeSpan}ms。", hash, result.Id, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.IStockService.AgvApplyOut 方法 */ public async Task<(string barcode, Failure Failure)> AgvApplyOut( string requestNo, string code, Batch batch, string qty, string postUser, string targetNo) { var stopwatch = new Stopwatch(); var grain = _cluster.GetGrain(Guid.Empty); PreApplyOutCommand command = new PreApplyOutCommand(); command.RequestNo = requestNo; command.Batch = _mapper.Map(batch); command.Qty = qty; command.Code = code; command.Target = targetNo; command.Postuser = postUser; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:AgvApplyOut参数-{1}]。", hash, JsonConvert.SerializeObject(new { requestNo = requestNo, code = code, batch = JsonConvert.SerializeObject(batch), qty = qty, targetNo = targetNo, postuser = postUser })); stopwatch.Start(); var result = await grain.PreApplyOut(command); _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms 结果{JsonConvert.SerializeObject(result)}", hash, stopwatch.ElapsedMilliseconds, result); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.ReuploadHiWMS 方法 */ public async Task<(string, Failure)> ReuploadHiWMS(int id) { var stopwatch = new Stopwatch(); var command = new ReuploadTaskCommand { Id = id, ManualFlg = 1};; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:ReuploadHiWMS任务[{Id}]。", hash, id); stopwatch.Start(); //不需要队列执行 await _bus.Execute(command); var result = _notifications.FirstOrDefault(); //var result = await _cluster.GetGrain(Guid.Empty).ReuploadHiWMS(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return (command.Message, result); } /* * 实现 Kean.Application.Command.Interfaces.Emergency 方法 */ public async Task Emergency(string stationCode, string stationModel, int @operator) { var stopwatch = new Stopwatch(); var command = new EmergencyCommand { StationCode = stationCode, StationModel = stationModel }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:Emergency-入参[{1}]。", hash, JsonConvert.SerializeObject(new { stationCode = stationCode, stationModel = stationModel, @operator = @operator })); stopwatch.Start(); //不加入队列,可以并行执行 await _bus.Execute(command); Failure failure = _notifications.FirstOrDefault(); if (failure != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, failure); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return failure; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.SetRollByLocalComplete 方法 */ public async Task<(string, Failure)> SetRollTaskByLocalComplete(int id, int @operator) { var stopwatch = new Stopwatch(); var command = new SetRollTaskCommand { Id = id, Code = "0", Message = "强制完成结果", Operator = @operator }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:SetRollByLocalComplete-入参[{1}]。", hash, JsonConvert.SerializeObject(new { id = id, @operator = @operator })); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).SetRollTask(command); if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return (command.Message, result); } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Recomplete 方法 */ public async Task<(int taskid, Failure failure)> RecompleteForNoAgv(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = new CompleteCommand { Id = task.Id, ManualFlg = 1, IsCancelAgv = task.Tag, Operator = task.Operator }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:RecompleteForNoAgv-入参[{1}]。", hash, JsonConvert.SerializeObject(new { Id = task.Id, ManualFlg = 1, IsCancelAgv = task.Tag, Operator = task.Operator })); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).Complete(command); if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return (task.Id, result); } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.Recomplete 方法 */ public async Task<(int taskid, Failure failure)> RecompleteForNoHiwms(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = new CompleteCommand { Id = task.Id, ManualFlg = 1, IsCancelHiwms = task.Tag, Operator = task.Operator }; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:RecompleteForNoHiwms-入参[{1}]。", hash, JsonConvert.SerializeObject(new { Id = task.Id, ManualFlg = 1, IsCancelHiwms = task.Tag, Operator = task.Operator })); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).Complete(command); if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return (task.Id, result); } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.CallDelete 方法 */ public async Task CallDelete(int id) { var stopwatch = new Stopwatch(); var command = new CallDeleteCommand { Id = id}; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:请求删除任务[{Id}]。", hash, id); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).CallDelete(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.SetPriority 方法 */ public async Task SetPriority(ViewModels.Task task) { var stopwatch = new Stopwatch(); var command = _mapper.Map(task); var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:设置任务优先级[{task.Id}]。", hash, task.Id); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).SetPriority(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.CancelAgvApplyOut 方法 */ public async Task CancelAgvApplyOut(string requestNo, string postUser) { var stopwatch = new Stopwatch(); CancelAgvApplyOutCommand command = new CancelAgvApplyOutCommand(); command.RequestNo = requestNo; command.PostUser = postUser; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:取消出库申请[{requestNo}]。", hash, requestNo); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); var result = await _cluster.GetGrain(Guid.Empty).CancelAgvApplyOut(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); return result; } /* * 实现 Kean.Application.Command.Interfaces.ITaskService.AutoPalletOut 方法 */ public async Task AutoPalletOut() { Failure result = new Failure(); List lsStation = new List(); lsStation.Add("12123"); lsStation.Add("22123"); foreach (string device in lsStation) { //@=@ W1固定值 12123 22123 string infeedDevice = "12226"; int goodsId = 0; switch (device) { case "12123": infeedDevice = "12226"; //W1JPB goodsId = 5; break; case "22123": infeedDevice = "22226"; //W1JPA goodsId = 9; break; } var stopwatch = new Stopwatch(); var command = new AutoPalletOutCommand(); command.Device = device; command.InfeedDevice = infeedDevice; command.GoodsId = goodsId; var hash = command.GetHashCode(); _logger.LogInformation("过程[{Hash}]开始:{device}自动处理空托盘出库。", hash, device); stopwatch.Start(); //await _bus.Execute(command); //var result = _notifications.FirstOrDefault(); result = await _cluster.GetGrain(Guid.Empty).AutoPalletOut(command); // if (result != null) { _logger.LogWarning("过程[{Hash}]中断:{Failure}。", hash, result); } _logger.LogInformation("过程[{Hash}]结束:耗时{TimeSpan}ms。", hash, stopwatch.ElapsedMilliseconds); } return result; } } }