using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Hotline.Application.FlowEngine; using Hotline.DataSharing.Province.Notifications; using Hotline.Orders; using Hotline.Share.Dtos.Order; using Hotline.Share.Enums.Order; using MapsterMapper; using MediatR; using Microsoft.AspNetCore.Http; using XF.Domain.Exceptions; namespace Hotline.Application.Handlers.Order { public class ReceiveOrderNotifyHandler : IRequestHandler { private readonly IOrderRepository _orderRepository; private readonly IOrderDomainService _orderDomainService; private readonly IWorkflowApplication _workflowApplication; private readonly IMapper _mapper; public ReceiveOrderNotifyHandler( IOrderRepository orderRepository, IOrderDomainService orderDomainService, IWorkflowApplication workflowApplication, IMapper mapper) { _orderRepository = orderRepository; _orderDomainService = orderDomainService; _workflowApplication = workflowApplication; _mapper = mapper; } /// Handles a request /// The request /// Cancellation token /// Response from the request public async Task Handle(ReceiveOrderNotify request, CancellationToken cancellationToken) { var dto = request.AddOrderDto; if (string.IsNullOrEmpty(dto.ProvinceNo)) throw new UserFriendlyException("无效省工单编号"); var order = await _orderRepository.GetAsync(d => d.ProvinceNo == dto.ProvinceNo, cancellationToken); if (order is null) { order = _mapper.Map(dto); var orderId = await _orderDomainService.AddAsync(order, cancellationToken); if (order.Source is ESource.ProvinceStraight) { var orderExtension = await _orderDomainService.GetOrderExtensionsAsync(dto.ProvinceNo, cancellationToken); if (orderExtension is not null) { orderExtension.Id = orderId; await _orderDomainService.UpdateExtensionAsync(orderExtension, cancellationToken); } } return new AddOrderResponse { Id = orderId, No = order.No, Password = order.Password }; } else { if (order.Source is ESource.ProvinceStraight) { //todo 特提(撤回至发起) await _workflowApplication.RecallToStartAsync(order.WorkflowId, "省工单重派", cancellationToken); } return _mapper.Map(order); } } } }