xfe 6 місяців тому
батько
коміт
eae002745b

+ 21 - 64
src/Hotline.Api/Controllers/OrderController.cs

@@ -72,6 +72,7 @@ using Order = Hotline.Orders.Order;
 using WorkflowStep = Hotline.FlowEngine.Workflows.WorkflowStep;
 using System.Threading;
 using Hotline.Application.Contracts.Validators.FlowEngine;
+using Hotline.Authentications;
 
 namespace Hotline.Api.Controllers;
 
@@ -3549,8 +3550,6 @@ public class OrderController : BaseController
             startDto.Title = order.Title;
             var startStep = await _workflowDomainService.StartAsync(startDto, order.Id, order.ExpiredTime, HttpContext.RequestAborted);
 
-            
-            
             await HandleOrderAsync(order, startStep, dto.Data, dto.Workflow, HttpContext.RequestAborted);
         }
         catch (Exception e)
@@ -3656,52 +3655,38 @@ public class OrderController : BaseController
                     nextDto.NextHandlers = new List<FlowStepHandler> { handler };
                 }
 
-                await _workflowDomainService.NextAsync(nextDto, order.ExpiredTime, cancellationToken);
+                await _workflowDomainService.NextAsync(_sessionContext, nextDto, order.ExpiredTime, cancellationToken);
                 break;
             case EOrderAssignMode.CrossLevel:
                 if (!orderHandleFlowDto.CrossSteps.Any())
                     throw new UserFriendlyException("跨级指派参数异常");
 
-                //todo 1. 办理startStep 得到nextSteps 2. 利用crossSteps参数逐级办理unhandlSteps
-                
                 orderHandleFlowDto.CrossSteps = orderHandleFlowDto.CrossSteps.OrderBy(d => d.Sort).ToList();
-                for (int i = 0; i < orderHandleFlowDto.CrossSteps.Count; i++)
-                {
-                    var unhandleStep = orderHandleFlowDto.CrossSteps[i];
-                    
-                    await _workflowDomainService.NextAsync(nextflowDto, expiredTime, cancellationToken);
-                }
-                
-                //
-                var orgIds = orderHandleFlowDto.CrossOrgIds;
-                orgIds.Add(workflowDto.NextHandlers.First().OrgId);
-                var orgs = await _organizeRepository.Queryable()
-                    .Where(d => orgIds.Contains(d.Id))
-                    .ToListAsync(HttpContext.RequestAborted);
-                var maxLevel = orgs.MaxBy(d => d.Level).Level;
-
-                //办理开始节点或派单节点至一级部门
-                var levelOneOrgs = orgs.Where(d => d.Level == 1).ToList();
-                var unhandleSteps = await HandleWorkflowStepAsync(startStep, levelOneOrgs, order.ExpiredTime, cancellationToken);
-
-                //依次办理路过节点
-                for (int i = 1; i < maxLevel; i++)
+                var stepCount = orderHandleFlowDto.CrossSteps.Count;
+                var unhandleSteps = new List<WorkflowStep> { startStep };
+                for (int i = 0; i < stepCount; i++)
                 {
+                    var crossStep = orderHandleFlowDto.CrossSteps[i];
                     var tempSteps = new List<WorkflowStep>();
                     foreach (var unhandleStep in unhandleSteps)
                     {
-                        var hasLowerLevel = orgs.Any(d => d.Id.StartsWith(unhandleStep.HandlerOrgId));
-                        if (!hasLowerLevel) continue;
-
-                        var currentLevel = unhandleStep.HandlerOrgId.CalcOrgLevel();
-                        var lowerLevel = currentLevel++;
-                        var handleOrgs = orgs.Where(d => d.Level == lowerLevel).ToList();
-                        if (i != 1)
-                            handleOrgs = handleOrgs.Where(d => d.Id.StartsWith(unhandleStep.HandlerOrgId)).ToList();
-                        var nextSteps = await HandleWorkflowStepAsync(unhandleStep, handleOrgs, order.ExpiredTime, cancellationToken);
+                        var lowerLevelHandlers = crossStep.NextHandlers.Where(d => d.OrgId.StartsWith(unhandleStep.HandlerOrgId)).ToList();
+                        if (!lowerLevelHandlers.Any()) continue;
+                        var nextflowDto = _mapper.Map<NextWorkflowDto>(crossStep);
+                        nextflowDto.WorkflowId = unhandleStep.WorkflowId;
+                        nextflowDto.StepId = unhandleStep.Id;
+                        nextflowDto.IsStartCountersign = lowerLevelHandlers.Count > 1;
+                        nextflowDto.NextHandlers = lowerLevelHandlers;
+                        nextflowDto.Opinion = "跨级派单,自动办理";
+
+                        var operater = new FakeSessionContext
+                        {
+                            OrgId = unhandleStep.HandlerOrgId,
+                            OrgName = unhandleStep.HandlerOrgName,
+                        };
+                        var nextSteps = await _workflowDomainService.NextAsync(operater, nextflowDto, order.ExpiredTime, cancellationToken);
                         tempSteps.AddRange(nextSteps);
                     }
-
                     unhandleSteps = tempSteps;
                 }
 
@@ -3713,34 +3698,6 @@ public class OrderController : BaseController
         }
     }
 
-    private async Task<List<WorkflowStep>> HandleWorkflowStepAsync(WorkflowStep unhandleStep, List<SystemOrganize> handleOrgs, DateTime? expiredTime, CancellationToken cancellationToken)
-    {
-        var isStartCountersign = handleOrgs.Count > 1;
-        var handlers = handleOrgs.Select(d => new FlowStepHandler
-        {
-            OrgId = d.Id,
-            OrgName = d.Name,
-            Key = d.Id,
-            Value = d.Name
-        }).ToList();
-        var nextflowDto = new NextWorkflowDto
-        {
-            WorkflowId = unhandleStep.WorkflowId,
-            StepId = unhandleStep.Id,
-            NextStepCode = unhandleStep.Code,
-            NextStepName = unhandleStep.Name,
-            FlowDirection = EFlowDirection.CenterToOrg,
-            HandlerType = unhandleStep.HandlerType,
-            StepType = unhandleStep.StepType,
-            NextHandlers = handlers,
-            IsStartCountersign = isStartCountersign,
-            BusinessType = unhandleStep.BusinessType,
-            Opinion = "跨级派单,自动办理"
-        };
-
-        return await _workflowDomainService.NextAsync(nextflowDto, expiredTime, cancellationToken);
-    }
-
     /// <summary>
     /// 跨级指派查询下一步可选节点及办理对象参数
     /// </summary>

+ 48 - 0
src/Hotline/Authentications/FakeSessionContext.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Authentications;
+
+namespace Hotline.Authentications
+{
+    public class FakeSessionContext : ISessionContext
+    {
+        /// <summary>
+        /// Id of current tenant or null for host
+        /// </summary>
+        public string? UserId { get; init; }
+
+        /// <summary>
+        /// Id of current user or throw Exception for guest
+        /// </summary>
+        /// <exception cref="AuthenticationException"></exception>
+        public string RequiredUserId => UserId ?? throw new ArgumentNullException();
+        public string? UserName { get; init; }
+        public string? Phone { get; init; }
+
+        /// <summary>
+        /// Roles
+        /// </summary>
+        public string[] Roles { get; init; }
+        public string? OrgId { get; init; }
+        public string RequiredOrgId => OrgId ?? throw new ArgumentNullException();
+        public string? OrgName { get; init; }
+        public int OrgLevel { get; init; }
+        public string? OrgAreaCode { get; init; }
+        public bool OrgIsCenter { get; init; }
+
+        /// <summary>
+        /// 部门行政区划名称
+        /// </summary>
+        public string? OrgAreaName { get; init; }
+        public string? AreaId { get; init; }
+        public string? ClientId { get; init; }
+
+        /// <summary>
+        /// 工号
+        /// </summary>
+        public string? StaffNo { get; init; }
+    }
+}

+ 1 - 1
src/Hotline/FlowEngine/Workflows/IWorkflowDomainService.cs

@@ -34,7 +34,7 @@ namespace Hotline.FlowEngine.Workflows
         /// <summary>
         /// new
         /// </summary>
-        Task<List<WorkflowStep>> NextAsync(NextWorkflowDto dto,
+        Task<List<WorkflowStep>> NextAsync(ISessionContext current, NextWorkflowDto dto,
             DateTime? expiredTime = null, CancellationToken cancellationToken = default);
 
         /// <summary>

+ 14 - 14
src/Hotline/FlowEngine/Workflows/WorkflowDomainService.cs

@@ -246,7 +246,7 @@ namespace Hotline.FlowEngine.Workflows
         /// <summary>
         /// new
         /// </summary>
-        public async Task<List<WorkflowStep>> NextAsync(NextWorkflowDto dto,
+        public async Task<List<WorkflowStep>> NextAsync(ISessionContext current, NextWorkflowDto dto,
             DateTime? expiredTime = null, CancellationToken cancellationToken = default)
         {
             var workflow = await GetWorkflowAsync(dto.WorkflowId, withDefine: true, withSteps: true,
@@ -266,7 +266,7 @@ namespace Hotline.FlowEngine.Workflows
 
             //下一节点是否为动态节点
             var isNextDynamic = currentStepDefine.InstanceMode is EInstanceMode.Dynamic &&
-                                !DynamicShouldTerminal(currentStepDefine, _sessionContext.OrgLevel);
+                                !DynamicShouldTerminal(currentStepDefine, current.OrgLevel);
 
             StepDefine nextStepDefine;
             if (isNextDynamic
@@ -349,9 +349,9 @@ namespace Hotline.FlowEngine.Workflows
 
                     //结束会签
                     currentCountersign.End(currentStep.Id, currentStep.Code, currentStep.BusinessType,
-                        _sessionContext.RequiredUserId, _sessionContext.UserName,
-                        _sessionContext.RequiredOrgId, _sessionContext.OrgName,
-                        _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName);
+                        current.UserId, current.UserName,
+                        current.OrgId, current.OrgName,
+                        current.OrgAreaCode, current.OrgAreaName);
                     await _workflowCountersignRepository.UpdateAsync(currentCountersign, cancellationToken);
                 }
             }
@@ -362,10 +362,10 @@ namespace Hotline.FlowEngine.Workflows
             if (isStartCountersign)
             {
                 var exists = workflow.Countersigns.Any(d =>
-                    !d.IsCompleted() && d.StarterId == _sessionContext.RequiredUserId);
+                    !d.IsCompleted() && d.StarterId == current.UserId);
                 if (exists)
                     throw new UserFriendlyException("该用户在当前流程存在未结束会签");
-                await StartCountersignAsync(_sessionContext, workflow, currentStep, dto, flowAssignInfo.FlowAssignType,
+                await StartCountersignAsync(current, workflow, currentStep, dto, flowAssignInfo.FlowAssignType,
                     counterSignType, expiredTime, cancellationToken);
             }
 
@@ -387,7 +387,7 @@ namespace Hotline.FlowEngine.Workflows
                         //throw new UserFriendlyException(
                         //    $"会签数据异常, workflowId: {currentStep.WorkflowId}, countersignId: {currentStep.CountersignId}",
                         //    "会签数据异常");
-                        countersign.MemberHandled(_sessionContext.RequiredUserId, _sessionContext.RequiredOrgId);
+                        countersign.MemberHandled(current.UserId, current.OrgId);
                         //update cs
                         await _workflowCountersignRepository.UpdateNav(countersign)
                             .Include(d => d.Members)
@@ -427,12 +427,12 @@ namespace Hotline.FlowEngine.Workflows
             if (workflow.ActualHandleStepId == currentStep.Id)
             {
                 //更新实际办理节点信息
-                workflow.UpdateActualStepWhenHandle(currentStep, _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName, _sessionContext.OrgLevel);
+                workflow.UpdateActualStepWhenHandle(currentStep, current.OrgAreaCode, current.OrgAreaName, current.OrgLevel);
             }
 
             if (workflow.CurrentStepId == currentStep.Id)
             {
-                workflow.UpdateCurrentStepWhenHandle(currentStep, _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName, _sessionContext.OrgLevel);
+                workflow.UpdateCurrentStepWhenHandle(currentStep, current.OrgAreaCode, current.OrgAreaName, current.OrgLevel);
             }
 
             //检查是否流转到流程终点
@@ -478,7 +478,7 @@ namespace Hotline.FlowEngine.Workflows
 
             //更新会签实际办理对象信息
             if (currentStep.IsActualHandled)
-                workflow.AddCsActualHandler(_sessionContext.RequiredUserId, _sessionContext.RequiredOrgId);
+                workflow.AddCsActualHandler(current.UserId, current.OrgId);
 
             await _workflowRepository.UpdateAsync(workflow, cancellationToken);
 
@@ -493,7 +493,7 @@ namespace Hotline.FlowEngine.Workflows
             var currentTrace = workflow.Traces.First(d => d.Id == currentStep.Id);
             await _publisher.PublishAsync(
                 new NextStepNotify(workflow, dto, flowAssignInfo, currentTrace, nextStepDefine,
-                    _sessionContext.RequiredOrgId, expiredTime.HasValue), PublishStrategy.ParallelWhenAll,
+                    current.UserId, expiredTime.HasValue), PublishStrategy.ParallelWhenAll,
                 cancellationToken);
 
             return nextSteps;
@@ -2394,9 +2394,9 @@ namespace Hotline.FlowEngine.Workflows
                 StartStepCode = startStep.Code,
                 StartStepBusiType = startStep.BusinessType,
 
-                StarterId = current.RequiredUserId,
+                StarterId = current.UserId,
                 StarterName = current.UserName ?? string.Empty,
-                StarterOrgId = current.RequiredOrgId,
+                StarterOrgId = current.OrgId,
                 StarterOrgName = current.OrgName,
                 StarterOrgAreaCode = current.OrgAreaCode ?? string.Empty,
                 StarterOrgAreaName = current.OrgAreaName ?? string.Empty,