123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using DotNetCore.CAP;
- using Hotline.FlowEngine;
- using Hotline.Share.Dtos.Order;
- using Hotline.Share.Enums.Order;
- using Hotline.Share.Mq;
- using MapsterMapper;
- using Microsoft.Extensions.Logging;
- using XF.Domain.Authentications;
- using XF.Domain.Cache;
- using XF.Domain.Dependency;
- using XF.Domain.Entities;
- using XF.Domain.Exceptions;
- using XF.Domain.Extensions;
- namespace Hotline.Orders;
- public class OrderDomainService : IOrderDomainService, IScopeDependency
- {
- private const string OrderNoPrefix = "OrderNo:";
- private readonly IOrderRepository _orderRepository;
- private readonly ITypedCache<CacheOrderNO> _cacheOrderNo;
- private readonly ISessionContext _sessionContext;
- private readonly ICapPublisher _capPublisher;
- private readonly IMapper _mapper;
- private readonly ILogger<OrderDomainService> _logger;
- public OrderDomainService(
- IOrderRepository orderRepository,
- ITypedCache<CacheOrderNO> cacheOrderNo,
- ISessionContext sessionContext,
- ICapPublisher capPublisher,
- IMapper mapper,
- ILogger<OrderDomainService> logger)
- {
- _orderRepository = orderRepository;
- _cacheOrderNo = cacheOrderNo;
- _sessionContext = sessionContext;
- _capPublisher = capPublisher;
- _mapper = mapper;
- _logger = logger;
- }
- public async Task<string> AddAsync(Order order, CancellationToken cancellationToken)
- {
- if (order.OrderType == EOrderType.MarketSupervisionBy12315 && order.AcceptType == EAcceptType.Complain && order.OrderComplain == null)
- throw UserFriendlyException.SameMessage("非法投诉参数");
- if (order.OrderType == EOrderType.MarketSupervisionBy12315 && order.AcceptType == EAcceptType.Report && order.OrderReport == null)
- throw UserFriendlyException.SameMessage("非法举报参数");
- order.Init(_sessionContext.RequiredUserId);
- order.No = GenerateNewOrderNo();
- return await _orderRepository.AddOrderNavAsync(order, cancellationToken);
- }
- /// <summary>
- /// 归档
- /// </summary>
- public async Task FileAsync(string? orderId, CancellationToken cancellationToken)
- {
- var order = await GetOrderAsync(orderId, cancellationToken);
- if (order.Status is EOrderStatus.Filed) return;
- order.Status = EOrderStatus.Filed;
- await _orderRepository.UpdateAsync(order, cancellationToken);
- }
- /// <summary>
- /// 接办工单(查看详情视为接办)
- /// </summary>
- public async Task AcceptAsync(string? orderId, CancellationToken cancellationToken)
- {
- _logger.LogInformation($"接办工单,orderId: {orderId}");
- var order = await GetOrderAsync(orderId, cancellationToken);
- CheckOrderIfFiled(order);
- order.Accept();
- _logger.LogInformation($"接办工单,before order.status: {order.Status}");
- await _orderRepository.UpdateAsync(order, cancellationToken);
- _logger.LogInformation($"接办工单,after order.status: {order.Status}");
- }
- /// <summary>
- /// 工单办理流程开始(流程开始后触发)
- /// </summary>
- public async Task ManageFlowStartAsync(FlowAssignMode assignMode, bool isCountersignStart, string? orderId, string workflowId,
- DateTime? currentStepTime, string? currentStepName, DateTime expiredTime, CancellationToken cancellationToken)
- {
- var order = await GetOrderAsync(orderId, cancellationToken);
- CheckOrderIfFiled(order);
- order.WorkflowId = workflowId;
- //更新指派信息
- order.Assign(assignMode.FlowAssignType, assignMode.GetHandlers());
- //更新流转信息
- order.ManageFlow(isCountersignStart, false, currentStepTime, currentStepName, expiredTime);
- await _orderRepository.UpdateAsync(order, cancellationToken);
- await _capPublisher.PublishAsync(EventNames.HotlineOrderCreated, _mapper.Map<OrderDto>(order),
- cancellationToken: cancellationToken);
- }
- /// <summary>
- /// 工单办理(每个节点都会触发)
- /// </summary>
- public async Task ManageFlowNextAsync(FlowAssignMode assignMode,
- bool isCountersignStart, bool isCountersignEnd,
- string? orderId, DateTime? currentStepTime, string? currentStepName, DateTime expiredTime, EProcessType processType,
- CancellationToken cancellationToken)
- {
- var order = await GetOrderAsync(orderId, cancellationToken);
- CheckOrderIfFiled(order);
- //更新指派信息
- order.Assign(assignMode.FlowAssignType, assignMode.GetHandlers());
- //更新流转信息
- order.ManageFlow(isCountersignStart, isCountersignEnd, currentStepTime, currentStepName, expiredTime);
- order.ProcessType = processType;
- await _orderRepository.UpdateAsync(order, cancellationToken);
- await _capPublisher.PublishAsync(EventNames.HotlineOrderFlow, _mapper.Map<OrderDto>(order),
- cancellationToken: cancellationToken);
- }
- /// <summary>
- /// 工单最终办理(此完结流程并未结束)
- /// </summary>
- /// <remarks>
- /// 需求:工单实际办理部门办理完成视为工单最终办理完结,
- /// 实际办理部门指:1.第一个汇总节点的前一个非汇总节点代表的办理部门 2.汇总以后如果再次开启会签需将工单回退到未办理完结的状态重新等待下一个汇总节点
- /// </remarks>
- /// <returns></returns>
- public async Task FinalManageAsync(string? orderId, CancellationToken cancellationToken)
- {
- var order = await GetOrderAsync(orderId, cancellationToken);
- CheckOrderIfFiled(order);
- order.FinalManage();
- await _orderRepository.UpdateAsync(order, cancellationToken);
- }
- /// <summary>
- /// 取消最终办理(汇总以后又重新指派到非汇总汇总节点办理的场景)
- /// </summary>
- /// <returns></returns>
- public async Task RecallFinalManageAsync(string? orderId, CancellationToken cancellationToken)
- {
- var order = await GetOrderAsync(orderId, cancellationToken);
- CheckOrderIfFiled(order);
- order.RecallFinalManage();
- await _orderRepository.UpdateAsync(order, cancellationToken);
- }
- /// <summary>
- /// 工单办理流程流转到结束节点
- /// </summary>
- public async Task ManageFlowEndAsync(string? orderId, CancellationToken cancellationToken)
- {
- var order = await GetOrderAsync(orderId, cancellationToken);
- CheckOrderIfFiled(order);
- order.Filed();
- await _orderRepository.UpdateAsync(order, cancellationToken);
- await _capPublisher.PublishAsync(EventNames.HotlineOrderFiled, _mapper.Map<OrderDto>(order),
- cancellationToken: cancellationToken);
- }
- /// <summary>
- /// 流程跳转
- /// </summary>
- /// <param name="workflowId"></param>
- /// <param name="assignMode"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task ManageFlowJumpAsync(string workflowId, FlowAssignMode assignMode, CancellationToken cancellationToken)
- {
- var order = await GetOrderByFlowIdAsync(workflowId, cancellationToken);
- CheckOrderIfFiled(order);
- order.Assign(assignMode.FlowAssignType, assignMode.GetHandlers());
- order.Jump();
- await _orderRepository.UpdateAsync(order, cancellationToken);
- }
- /// <summary>
- /// 工单办理流程从中心流转至部门
- /// </summary>
- /// <param name="workflowId"></param>
- /// <returns></returns>
- public async Task FlowFromCenterToOrgAsync(string workflowId, CancellationToken cancellationToken)
- {
- var order = await GetOrderByFlowIdAsync(workflowId, cancellationToken);
- CheckOrderIfFiled(order);
- if (order.ProcessType == EProcessType.Zhiban)
- {
- order.FlowFromCenterToOrg();
- await _orderRepository.UpdateAsync(order, cancellationToken);
- await _capPublisher.PublishAsync(EventNames.HotlineOrderCenterToOrg, _mapper.Map<OrderDto>(order),
- cancellationToken: cancellationToken);
- }
- }
- #region private
- private async Task<Order> GetOrderAsync(string? orderId, CancellationToken cancellationToken)
- {
- if (string.IsNullOrEmpty(orderId))
- throw UserFriendlyException.SameMessage("无效工单编号");
- var order = await _orderRepository.Queryable()
- .Includes(d => d.Hotspot)
- .Includes(d => d.Employee)
- .FirstAsync(d => d.Id == orderId);
- if (order == null)
- throw new UserFriendlyException($"无效工单编号, orderId: {orderId}", "无效工单编号");
- return order;
- }
- private async Task<Order> GetOrderByFlowIdAsync(string workflowId, CancellationToken cancellationToken)
- {
- if (string.IsNullOrEmpty(workflowId))
- throw UserFriendlyException.SameMessage("无效流程编号");
- var order = await _orderRepository.Queryable()
- .Includes(d => d.Hotspot)
- .Includes(d => d.Employee)
- .FirstAsync(d => d.WorkflowId == workflowId);
- if (order == null)
- throw new UserFriendlyException($"无效流程编号, workflowId: {workflowId}", "无效流程编号");
- return order;
- }
- private void CheckOrderIfFiled(Order order)
- {
- if (order.Status is EOrderStatus.Filed)
- throw UserFriendlyException.SameMessage("工单已归档");
- }
- private string GenerateNewOrderNo()
- {
- var today = DateTime.Today;
- var cacheKey = $"{OrderNoPrefix}{today:yyyyMMdd}";
- var cacheOrderNo = _cacheOrderNo.GetOrSet(cacheKey, f =>
- {
- var todayOrderCount = _orderRepository.Queryable(true)
- .CountAsync(d => d.CreationTime.Date == today.Date)
- .GetAwaiter()
- .GetResult();
- return new CacheOrderNO { TotalCount = todayOrderCount };
- }, TimeSpan.FromDays(1));
- cacheOrderNo!.TotalCount += 1;
- var no = GenerateOrderNo(today, cacheOrderNo.TotalCount);
- _cacheOrderNo.Set(cacheKey, cacheOrderNo);
- return no;
- }
- private string GenerateOrderNo(DateTime today, int count)
- {
- return $"{today:yyyyMMdd}{count.ToString("000000")}";
- }
- #endregion
- }
- public class CacheOrderNO
- {
- public int TotalCount { get; set; }
- }
|