12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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<ReceiveOrderNotify, AddOrderResponse>
- {
- 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;
- }
- /// <summary>Handles a request</summary>
- /// <param name="request">The request</param>
- /// <param name="cancellationToken">Cancellation token</param>
- /// <returns>Response from the request</returns>
- public async Task<AddOrderResponse> 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<Hotline.Orders.Order>(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<AddOrderResponse>(order);
- }
- }
- }
- }
|