using AutoMapper; using Kean.Domain.Order.Commands; using Kean.Domain.Order.Events; using Kean.Domain.Order.Models; using Kean.Domain.Order.Repositories; using System.Threading; using System.Threading.Tasks; namespace Kean.Domain.Order.CommandHandlers { /// /// 修改工作流命令处理程序 /// public sealed class ModifyFlowCommandHandler : CommandHandler { private readonly ICommandBus _commandBus; // 命令总线 private readonly IMapper _mapper; // 模型映射 private readonly IOrderRepository _orderRepository; // 订单仓库 /// /// 依赖注入 /// public ModifyFlowCommandHandler( ICommandBus commandBus, IMapper mapper, IOrderRepository orderRepository) { _commandBus = commandBus; _mapper = mapper; _orderRepository = orderRepository; } /// /// 处理程序 /// public override async Task Handle(ModifyFlowCommand command, CancellationToken cancellationToken) { if (command.ValidationResult.IsValid) { if (!await _orderRepository.IsFlowExist(command.Id)) { await _commandBus.Notify(nameof(command.Id), "工作流不存在", command.Id, nameof(ErrorCode.Gone), cancellationToken: cancellationToken); return; } if (await _orderRepository.IsFlowNameExist(command.Name, command.Id)) { await _commandBus.Notify(nameof(command.Name), "工作流描述已存在", command.Name, nameof(ErrorCode.Conflict), cancellationToken: cancellationToken); return; } if (command.Nodes != null || command.Paths != null) { if (await _orderRepository.IsFlowUsing(command.Id)) { await _commandBus.Notify(nameof(command.Id), "存在未完成实例", command.Id, nameof(ErrorCode.Locked), cancellationToken: cancellationToken); return; } } await _orderRepository.ModifyFlow(_mapper.Map(command)); await _commandBus.Trigger(_mapper.Map(command), cancellationToken); } else { await _commandBus.Notify(command.ValidationResult, cancellationToken: cancellationToken); } } } }