OrderDomainService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using DotNetCore.CAP;
  2. using Hotline.FlowEngine;
  3. using Hotline.Share.Dtos.Order;
  4. using Hotline.Share.Enums.Order;
  5. using Hotline.Share.Mq;
  6. using MapsterMapper;
  7. using Microsoft.Extensions.Logging;
  8. using XF.Domain.Authentications;
  9. using XF.Domain.Cache;
  10. using XF.Domain.Dependency;
  11. using XF.Domain.Entities;
  12. using XF.Domain.Exceptions;
  13. using XF.Domain.Extensions;
  14. namespace Hotline.Orders;
  15. public class OrderDomainService : IOrderDomainService, IScopeDependency
  16. {
  17. private const string OrderNoPrefix = "OrderNo:";
  18. private readonly IOrderRepository _orderRepository;
  19. private readonly ITypedCache<CacheOrderNO> _cacheOrderNo;
  20. private readonly ISessionContext _sessionContext;
  21. private readonly ICapPublisher _capPublisher;
  22. private readonly IMapper _mapper;
  23. private readonly ILogger<OrderDomainService> _logger;
  24. public OrderDomainService(
  25. IOrderRepository orderRepository,
  26. ITypedCache<CacheOrderNO> cacheOrderNo,
  27. ISessionContext sessionContext,
  28. ICapPublisher capPublisher,
  29. IMapper mapper,
  30. ILogger<OrderDomainService> logger)
  31. {
  32. _orderRepository = orderRepository;
  33. _cacheOrderNo = cacheOrderNo;
  34. _sessionContext = sessionContext;
  35. _capPublisher = capPublisher;
  36. _mapper = mapper;
  37. _logger = logger;
  38. }
  39. public async Task<string> AddAsync(Order order, CancellationToken cancellationToken)
  40. {
  41. if (order.OrderType == EOrderType.MarketSupervisionBy12315 && order.AcceptType == EAcceptType.Complain && order.OrderComplain == null)
  42. throw UserFriendlyException.SameMessage("非法投诉参数");
  43. if (order.OrderType == EOrderType.MarketSupervisionBy12315 && order.AcceptType == EAcceptType.Report && order.OrderReport == null)
  44. throw UserFriendlyException.SameMessage("非法举报参数");
  45. order.Init(_sessionContext.RequiredUserId);
  46. order.No = GenerateNewOrderNo();
  47. return await _orderRepository.AddOrderNavAsync(order, cancellationToken);
  48. }
  49. /// <summary>
  50. /// 归档
  51. /// </summary>
  52. public async Task FileAsync(string? orderId, CancellationToken cancellationToken)
  53. {
  54. var order = await GetOrderAsync(orderId, cancellationToken);
  55. if (order.Status is EOrderStatus.Filed) return;
  56. order.Status = EOrderStatus.Filed;
  57. await _orderRepository.UpdateAsync(order, cancellationToken);
  58. }
  59. /// <summary>
  60. /// 接办工单(查看详情视为接办)
  61. /// </summary>
  62. public async Task AcceptAsync(string? orderId, CancellationToken cancellationToken)
  63. {
  64. _logger.LogInformation($"接办工单,orderId: {orderId}");
  65. var order = await GetOrderAsync(orderId, cancellationToken);
  66. CheckOrderIfFiled(order);
  67. order.Accept();
  68. _logger.LogInformation($"接办工单,before order.status: {order.Status}");
  69. await _orderRepository.UpdateAsync(order, cancellationToken);
  70. _logger.LogInformation($"接办工单,after order.status: {order.Status}");
  71. }
  72. /// <summary>
  73. /// 工单办理流程开始(流程开始后触发)
  74. /// </summary>
  75. public async Task ManageFlowStartAsync(FlowAssignMode assignMode, bool isCountersignStart, string? orderId, string workflowId,
  76. DateTime? currentStepTime, string? currentStepName, DateTime expiredTime, CancellationToken cancellationToken)
  77. {
  78. var order = await GetOrderAsync(orderId, cancellationToken);
  79. CheckOrderIfFiled(order);
  80. order.WorkflowId = workflowId;
  81. //更新指派信息
  82. order.Assign(assignMode.FlowAssignType, assignMode.GetHandlers());
  83. //更新流转信息
  84. order.ManageFlow(isCountersignStart, false, currentStepTime, currentStepName, expiredTime);
  85. await _orderRepository.UpdateAsync(order, cancellationToken);
  86. await _capPublisher.PublishAsync(EventNames.HotlineOrderCreated, _mapper.Map<OrderDto>(order),
  87. cancellationToken: cancellationToken);
  88. }
  89. /// <summary>
  90. /// 工单办理(每个节点都会触发)
  91. /// </summary>
  92. public async Task ManageFlowNextAsync(FlowAssignMode assignMode,
  93. bool isCountersignStart, bool isCountersignEnd,
  94. string? orderId, DateTime? currentStepTime, string? currentStepName, DateTime expiredTime, EProcessType processType,
  95. CancellationToken cancellationToken)
  96. {
  97. var order = await GetOrderAsync(orderId, cancellationToken);
  98. CheckOrderIfFiled(order);
  99. //更新指派信息
  100. order.Assign(assignMode.FlowAssignType, assignMode.GetHandlers());
  101. //更新流转信息
  102. order.ManageFlow(isCountersignStart, isCountersignEnd, currentStepTime, currentStepName, expiredTime);
  103. order.ProcessType = processType;
  104. await _orderRepository.UpdateAsync(order, cancellationToken);
  105. await _capPublisher.PublishAsync(EventNames.HotlineOrderFlow, _mapper.Map<OrderDto>(order),
  106. cancellationToken: cancellationToken);
  107. }
  108. /// <summary>
  109. /// 工单最终办理(此完结流程并未结束)
  110. /// </summary>
  111. /// <remarks>
  112. /// 需求:工单实际办理部门办理完成视为工单最终办理完结,
  113. /// 实际办理部门指:1.第一个汇总节点的前一个非汇总节点代表的办理部门 2.汇总以后如果再次开启会签需将工单回退到未办理完结的状态重新等待下一个汇总节点
  114. /// </remarks>
  115. /// <returns></returns>
  116. public async Task FinalManageAsync(string? orderId, CancellationToken cancellationToken)
  117. {
  118. var order = await GetOrderAsync(orderId, cancellationToken);
  119. CheckOrderIfFiled(order);
  120. order.FinalManage();
  121. await _orderRepository.UpdateAsync(order, cancellationToken);
  122. }
  123. /// <summary>
  124. /// 取消最终办理(汇总以后又重新指派到非汇总汇总节点办理的场景)
  125. /// </summary>
  126. /// <returns></returns>
  127. public async Task RecallFinalManageAsync(string? orderId, CancellationToken cancellationToken)
  128. {
  129. var order = await GetOrderAsync(orderId, cancellationToken);
  130. CheckOrderIfFiled(order);
  131. order.RecallFinalManage();
  132. await _orderRepository.UpdateAsync(order, cancellationToken);
  133. }
  134. /// <summary>
  135. /// 工单办理流程流转到结束节点
  136. /// </summary>
  137. public async Task ManageFlowEndAsync(string? orderId, CancellationToken cancellationToken)
  138. {
  139. var order = await GetOrderAsync(orderId, cancellationToken);
  140. CheckOrderIfFiled(order);
  141. order.Filed();
  142. await _orderRepository.UpdateAsync(order, cancellationToken);
  143. await _capPublisher.PublishAsync(EventNames.HotlineOrderFiled, _mapper.Map<OrderDto>(order),
  144. cancellationToken: cancellationToken);
  145. }
  146. /// <summary>
  147. /// 流程跳转
  148. /// </summary>
  149. /// <param name="workflowId"></param>
  150. /// <param name="assignMode"></param>
  151. /// <param name="cancellationToken"></param>
  152. /// <returns></returns>
  153. public async Task ManageFlowJumpAsync(string workflowId, FlowAssignMode assignMode, CancellationToken cancellationToken)
  154. {
  155. var order = await GetOrderByFlowIdAsync(workflowId, cancellationToken);
  156. CheckOrderIfFiled(order);
  157. order.Assign(assignMode.FlowAssignType, assignMode.GetHandlers());
  158. order.Jump();
  159. await _orderRepository.UpdateAsync(order, cancellationToken);
  160. }
  161. /// <summary>
  162. /// 工单办理流程从中心流转至部门
  163. /// </summary>
  164. /// <param name="workflowId"></param>
  165. /// <returns></returns>
  166. public async Task FlowFromCenterToOrgAsync(string workflowId, CancellationToken cancellationToken)
  167. {
  168. var order = await GetOrderByFlowIdAsync(workflowId, cancellationToken);
  169. CheckOrderIfFiled(order);
  170. if (order.ProcessType == EProcessType.Zhiban)
  171. {
  172. order.FlowFromCenterToOrg();
  173. await _orderRepository.UpdateAsync(order, cancellationToken);
  174. await _capPublisher.PublishAsync(EventNames.HotlineOrderCenterToOrg, _mapper.Map<OrderDto>(order),
  175. cancellationToken: cancellationToken);
  176. }
  177. }
  178. #region private
  179. private async Task<Order> GetOrderAsync(string? orderId, CancellationToken cancellationToken)
  180. {
  181. if (string.IsNullOrEmpty(orderId))
  182. throw UserFriendlyException.SameMessage("无效工单编号");
  183. var order = await _orderRepository.Queryable()
  184. .Includes(d => d.Hotspot)
  185. .Includes(d => d.Employee)
  186. .FirstAsync(d => d.Id == orderId);
  187. if (order == null)
  188. throw new UserFriendlyException($"无效工单编号, orderId: {orderId}", "无效工单编号");
  189. return order;
  190. }
  191. private async Task<Order> GetOrderByFlowIdAsync(string workflowId, CancellationToken cancellationToken)
  192. {
  193. if (string.IsNullOrEmpty(workflowId))
  194. throw UserFriendlyException.SameMessage("无效流程编号");
  195. var order = await _orderRepository.Queryable()
  196. .Includes(d => d.Hotspot)
  197. .Includes(d => d.Employee)
  198. .FirstAsync(d => d.WorkflowId == workflowId);
  199. if (order == null)
  200. throw new UserFriendlyException($"无效流程编号, workflowId: {workflowId}", "无效流程编号");
  201. return order;
  202. }
  203. private void CheckOrderIfFiled(Order order)
  204. {
  205. if (order.Status is EOrderStatus.Filed)
  206. throw UserFriendlyException.SameMessage("工单已归档");
  207. }
  208. private string GenerateNewOrderNo()
  209. {
  210. var today = DateTime.Today;
  211. var cacheKey = $"{OrderNoPrefix}{today:yyyyMMdd}";
  212. var cacheOrderNo = _cacheOrderNo.GetOrSet(cacheKey, f =>
  213. {
  214. var todayOrderCount = _orderRepository.Queryable(true)
  215. .CountAsync(d => d.CreationTime.Date == today.Date)
  216. .GetAwaiter()
  217. .GetResult();
  218. return new CacheOrderNO { TotalCount = todayOrderCount };
  219. }, TimeSpan.FromDays(1));
  220. cacheOrderNo!.TotalCount += 1;
  221. var no = GenerateOrderNo(today, cacheOrderNo.TotalCount);
  222. _cacheOrderNo.Set(cacheKey, cacheOrderNo);
  223. return no;
  224. }
  225. private string GenerateOrderNo(DateTime today, int count)
  226. {
  227. return $"{today:yyyyMMdd}{count.ToString("000000")}";
  228. }
  229. #endregion
  230. }
  231. public class CacheOrderNO
  232. {
  233. public int TotalCount { get; set; }
  234. }