ReceiveOrderNotifyHandler.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Hotline.Application.FlowEngine;
  7. using Hotline.DataSharing.Province.Notifications;
  8. using Hotline.Orders;
  9. using Hotline.Share.Dtos.Order;
  10. using Hotline.Share.Enums.Order;
  11. using MapsterMapper;
  12. using MediatR;
  13. using Microsoft.AspNetCore.Http;
  14. using XF.Domain.Exceptions;
  15. namespace Hotline.Application.Handlers.Order
  16. {
  17. public class ReceiveOrderNotifyHandler : IRequestHandler<ReceiveOrderNotify, AddOrderResponse>
  18. {
  19. private readonly IOrderRepository _orderRepository;
  20. private readonly IOrderDomainService _orderDomainService;
  21. private readonly IWorkflowApplication _workflowApplication;
  22. private readonly IMapper _mapper;
  23. public ReceiveOrderNotifyHandler(
  24. IOrderRepository orderRepository,
  25. IOrderDomainService orderDomainService,
  26. IWorkflowApplication workflowApplication,
  27. IMapper mapper)
  28. {
  29. _orderRepository = orderRepository;
  30. _orderDomainService = orderDomainService;
  31. _workflowApplication = workflowApplication;
  32. _mapper = mapper;
  33. }
  34. /// <summary>Handles a request</summary>
  35. /// <param name="request">The request</param>
  36. /// <param name="cancellationToken">Cancellation token</param>
  37. /// <returns>Response from the request</returns>
  38. public async Task<AddOrderResponse> Handle(ReceiveOrderNotify request, CancellationToken cancellationToken)
  39. {
  40. var dto = request.AddOrderDto;
  41. if (string.IsNullOrEmpty(dto.ProvinceNo))
  42. throw new UserFriendlyException("无效省工单编号");
  43. var order = await _orderRepository.GetAsync(d => d.ProvinceNo == dto.ProvinceNo, cancellationToken);
  44. if (order is null)
  45. {
  46. order = _mapper.Map<Hotline.Orders.Order>(dto);
  47. var orderId = await _orderDomainService.AddAsync(order, cancellationToken);
  48. if (order.Source is ESource.ProvinceStraight)
  49. {
  50. var orderExtension = await _orderDomainService.GetOrderExtensionsAsync(dto.ProvinceNo, cancellationToken);
  51. if (orderExtension is not null)
  52. {
  53. orderExtension.Id = orderId;
  54. await _orderDomainService.UpdateExtensionAsync(orderExtension, cancellationToken);
  55. }
  56. }
  57. return new AddOrderResponse
  58. {
  59. Id = orderId,
  60. No = order.No,
  61. Password = order.Password
  62. };
  63. }
  64. else
  65. {
  66. if (order.Source is ESource.ProvinceStraight)
  67. {
  68. //todo 特提(撤回至发起)
  69. await _workflowApplication.RecallToStartAsync(order.WorkflowId, "省工单重派", cancellationToken);
  70. }
  71. return _mapper.Map<AddOrderResponse>(order);
  72. }
  73. }
  74. }
  75. }