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; private readonly ISessionContext _sessionContext; private readonly ICapPublisher _capPublisher; private readonly IMapper _mapper; private readonly ILogger _logger; public OrderDomainService( IOrderRepository orderRepository, ITypedCache cacheOrderNo, ISessionContext sessionContext, ICapPublisher capPublisher, IMapper mapper, ILogger logger) { _orderRepository = orderRepository; _cacheOrderNo = cacheOrderNo; _sessionContext = sessionContext; _capPublisher = capPublisher; _mapper = mapper; _logger = logger; } public async Task 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); } /// /// 归档 /// 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); } /// /// 接办工单(查看详情视为接办) /// 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}"); } /// /// 工单办理流程开始(流程开始后触发) /// 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(order), cancellationToken: cancellationToken); } /// /// 工单办理(每个节点都会触发) /// 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(order), cancellationToken: cancellationToken); } /// /// 工单最终办理(此完结流程并未结束) /// /// /// 需求:工单实际办理部门办理完成视为工单最终办理完结, /// 实际办理部门指:1.第一个汇总节点的前一个非汇总节点代表的办理部门 2.汇总以后如果再次开启会签需将工单回退到未办理完结的状态重新等待下一个汇总节点 /// /// public async Task FinalManageAsync(string? orderId, CancellationToken cancellationToken) { var order = await GetOrderAsync(orderId, cancellationToken); CheckOrderIfFiled(order); order.FinalManage(); await _orderRepository.UpdateAsync(order, cancellationToken); } /// /// 取消最终办理(汇总以后又重新指派到非汇总汇总节点办理的场景) /// /// public async Task RecallFinalManageAsync(string? orderId, CancellationToken cancellationToken) { var order = await GetOrderAsync(orderId, cancellationToken); CheckOrderIfFiled(order); order.RecallFinalManage(); await _orderRepository.UpdateAsync(order, cancellationToken); } /// /// 工单办理流程流转到结束节点 /// 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(order), cancellationToken: cancellationToken); } /// /// 流程跳转 /// /// /// /// /// 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); } /// /// 工单办理流程从中心流转至部门 /// /// /// 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(order), cancellationToken: cancellationToken); } } #region private private async Task 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 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; } }