using Hotline.File;
using Hotline.FlowEngine.Definitions;
using Hotline.FlowEngine.Notifications;
using Hotline.FlowEngine.WorkflowModules;
using Hotline.SeedData;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Dtos.FlowEngine.Definition;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Users;
using MapsterMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Entities;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.FlowEngine.Workflows
{
    public class WorkflowDomainService : IWorkflowDomainService, IScopeDependency
    {
        private readonly IWorkflowRepository _workflowRepository;
        private readonly IRepository<WorkflowStep> _workflowStepRepository;
        private readonly IRepository<WorkflowTrace> _workflowTraceRepository;
        private readonly IRepository<WorkflowSupplement> _workflowSupplementRepository;
        private readonly IRepository<WorkflowCountersign> _workflowCountersignRepository;
        //private readonly IRepository<WorkflowStepHandler> _workflowStepHandlerRepository;
        private readonly ISessionContext _sessionContext;
        private readonly IMapper _mapper;
        private readonly IMediator _mediator;
        private readonly ILogger<WorkflowDomainService> _logger;
        private readonly IFileRepository _fileRepository;
        private readonly IRepository<User> _userRepository;

        public WorkflowDomainService(
            IWorkflowRepository workflowRepository,
            IRepository<WorkflowStep> workflowStepRepository,
            IRepository<WorkflowTrace> workflowTraceRepository,
            IRepository<WorkflowSupplement> workflowSupplementRepository,
            IRepository<WorkflowCountersign> workflowCountersignRepository,
            //IRepository<WorkflowStepHandler> workflowStepHandlerRepository,
            ISessionContext sessionContext,
            IMapper mapper,
            IMediator mediator,
            ILogger<WorkflowDomainService> logger,
            IFileRepository fileRepository)
        {
            _workflowRepository = workflowRepository;
            _workflowStepRepository = workflowStepRepository;
            _workflowTraceRepository = workflowTraceRepository;
            _workflowSupplementRepository = workflowSupplementRepository;
            _workflowCountersignRepository = workflowCountersignRepository;
            //_workflowStepHandlerRepository = workflowStepHandlerRepository;
            _sessionContext = sessionContext;
            _mapper = mapper;
            _mediator = mediator;
            _logger = logger;
            _fileRepository = fileRepository;
        }

        public async Task<Workflow> CreateWorkflowAsync(WorkflowModule wfModule, string title, string userId,
            string orgId, string? externalId = null, CancellationToken cancellationToken = default)
        {
            var definition = wfModule.Definition;
            if (definition is null)
                throw new UserFriendlyException("无效流程模板");
            var workflow = new Workflow
            {
                Title = title,
                ModuleId = wfModule.Id,
                ModuleName = wfModule.Name,
                ModuleCode = wfModule.Code,
                DefinitionId = definition.Id,
                Status = EWorkflowStatus.Runnable,
                Steps = new(),
                Traces = new(),
                WorkflowDefinition = definition,
                ExternalId = externalId ?? string.Empty,
                //FlowedOrgIds = new List<string> { orgId },
                FlowedUserIds = new List<string> { userId },
                FlowType = definition.FlowType,
            };

            await _workflowRepository.AddAsync(workflow, cancellationToken);

            return workflow;
        }

        /// <summary>
        /// 流程开始
        /// </summary>
        public async Task StartAsync(Workflow workflow, WorkflowStep startStep, BasicWorkflowDto dto,
            StepDefine firstStepDefine, bool isNextDynamic, FlowAssignInfo flowAssignInfo,
            ECounterSignType? counterSignType, DateTime? expiredTime,
            ISessionContext current, CancellationToken cancellationToken)
        {
            //1. 创建first节点 （和trace）2.办理开始节点 

            if (firstStepDefine.StepType is EStepType.End)
            {
                //firstStep是否为end，t: 实际办理节点为startStep, 并且handlerId赋值 f: 实际办理节点为firstStep, handlerId未赋值
                workflow.UpdateActualStepWhenHandle(startStep,
                    current.RequiredUserId, current.UserName,
                    current.RequiredOrgId, current.OrgName,
                    current.OrgAreaCode, current.OrgAreaName,
                    current.OrgLevel);

                var endTrace = await EndAsync(workflow, dto, firstStepDefine, startStep, current, expiredTime, cancellationToken);
                return;
            }

            //firststeps
            var firstSteps = await CreateNextStepsAsync(workflow, startStep, dto, firstStepDefine,
                isNextDynamic, flowAssignInfo, expiredTime, dto.IsStartCountersign, cancellationToken);
            if (firstSteps.Any())
                workflow.Steps.AddRange(firstSteps);

            //var counterSignType = GetCounterSignType(startStep.BusinessType);
            ////办理开始节点
            //await HandleStepAsync(startStep, workflow, dto, flowAssignInfo.FlowAssignType, counterSignType,
            //    cancellationToken);

            ////赋值当前节点的下级办理节点
            //if (dto.IsStartCountersign)
            //    startStep.CreateCountersignSteps(firstSteps);

            await _workflowStepRepository.UpdateAsync(startStep, cancellationToken);

            //handle trace
            var trace = await NextTraceAsync(workflow, dto, startStep, cancellationToken);

            //todo 计算办理工作时长

            ////更新当前办理节点信息
            //workflow.UpdateWorkflowCurrentStepInfo(dto.IsStartCountersign,
            //    current.RequiredUserId, current.UserName,
            //    current.RequiredOrgId, current.OrgName,
            //    current.OrgAreaCode, current.OrgAreaName,
            //    startStep, firstSteps.First());

            //指派实际办理节点
            UpdateActualStep(workflow, dto, firstStepDefine, firstSteps);


            //发起会签时记录顶层会签节点（必须在update currentStep之后）
            if (dto.IsStartCountersign && !workflow.IsInCountersign)
                workflow.StartCountersign(startStep.Id, counterSignType);

            ////更新实际办理节点信息
            //workflow.UpdateWorkflowActualHandleInfo(startStep,
            //    current.RequiredUserId, current.UserName,
            //    current.RequiredOrgId, current.OrgName,
            //    current.OrgAreaCode, current.OrgAreaName,
            //    current.OrgLevel);

            workflow.UpdateHandlers(current.RequiredUserId, current.RequiredOrgId,
                flowAssignInfo.FlowAssignType, flowAssignInfo.HandlerObjects, true);

            //更新指派信息
            workflow.Assign(flowAssignInfo.FlowAssignType, flowAssignInfo.GetHandlerIds());

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            //publish
            await _mediator.Publish(new StartWorkflowNotify(workflow, dto, flowAssignInfo, trace), cancellationToken);
        }

        public async Task<Workflow> GetWorkflowAsync(string workflowId,
            bool withDefine = false, bool withSteps = false,
            bool withTraces = false, bool withTracesTree = false,
            bool withSupplements = false, bool withCountersigns = false,
            CancellationToken cancellationToken = default)
        {
            if (withTraces && withTracesTree)
                throw new UserFriendlyException("traces只能在集合与树形集合结构中二选一");
            var query = _workflowRepository.Queryable().Where(d => d.Id == workflowId);
            if (withDefine)
                query = query.Includes(d => d.WorkflowDefinition);
            if (withSupplements)
                query = query.Includes(d => d.Supplements);
            //if (withAssigns)
            //    query = query.Includes(d => d.Assigns);
            if (withCountersigns)
                query = query.Includes(d => d.Countersigns, x => x.Members);
            if (withSteps)
                query = query.Includes(d => d.Steps);
            //if (withTraces)
            //    query = query.Includes(d => d.Traces);

            var workflow = await query.FirstAsync(cancellationToken);
            if (workflow is null)
                throw new UserFriendlyException("无效workflowId");

            //if (withSteps)
            //{
            //    var steps = await _workflowStepRepository.Queryable()
            //        .Where(d => d.WorkflowId == workflow.Id)
            //        .OrderBy(d => d.CreationTime)
            //        .ToTreeAsync(d => d.Steps, d => d.ParentId, null);
            //    workflow.Steps = steps;
            //}

            if (withTracesTree)
            {
                workflow.Traces = await _workflowTraceRepository.Queryable()
                    .Where(d => d.WorkflowId == workflow.Id)
                    .OrderBy(d => d.CreationTime)
                    .ToTreeAsync(d => d.Traces, d => d.ParentId, null);
            }

            if (withTraces)
            {
                workflow.Traces = await _workflowTraceRepository.Queryable()
                    .Where(d => d.WorkflowId == workflow.Id)
                    .OrderBy(d => d.CreationTime)
                    .ToListAsync(cancellationToken);
            }

            return workflow;
        }

        /// <summary>
        /// 查询工作流包含当前用户结束会签权限（是否可结束）
        /// </summary>
        public async Task<(Workflow Workflow, string? CountersignId, bool CanHandle, bool CanPrevious)> GetWorkflowHandlePermissionAsync(
            string workflowId, string userId, string orgId, string[] roleIds, CancellationToken cancellationToken = default)
        {
            var workflow = await GetWorkflowAsync(workflowId, withSteps: true, withCountersigns: true,
                cancellationToken: cancellationToken);

            var canHandle = workflow.IsCanHandle(userId, orgId, roleIds);
            var canPrevious = false;
            if (canHandle)
            {
                var currentStep = FindCurrentStepWaitForHandle(workflow, userId, orgId, roleIds);
                if (currentStep.Status is not EWorkflowStepStatus.Handled)
                {
                    canPrevious = !(currentStep.IsInCountersign() &&
                                    !currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId));
                }
            }

            var unCompletedCountersign = workflow.Countersigns
                .FirstOrDefault(d => !d.IsCompleted() && d.StarterId == userId);
            if (unCompletedCountersign is null) return (workflow, null, canHandle, canPrevious);

            //var existCountersignEndStep = workflow.Steps.Exists(d =>
            //    d.IsCountersignEndStep && d.CountersignStartStepId == unCompletedCountersign.StartStepId);
            //return (workflow, existCountersignEndStep ? null : unCompletedCountersign.Id, canPrevious);
            return (workflow, unCompletedCountersign.Id, canHandle, canPrevious);
        }

        /// <summary>
        /// 受理(接办)
        /// </summary>
        public async Task AcceptAsync(Workflow workflow,
            string userId, string? userName,
            string orgId, string? orgName,
            string? orgAreaCode, string? orgAreaName,
            CancellationToken cancellationToken)
        {
            if (!workflow.IsCanHandle(_sessionContext.RequiredUserId, _sessionContext.RequiredOrgId, _sessionContext.Roles)) return;
            //工单完成以后查看的场景
            if (workflow.Status != EWorkflowStatus.Runnable) return;

            var currentStep = GetUnHandleStep(workflow.Steps, _sessionContext.RequiredOrgId,
                _sessionContext.RequiredUserId, _sessionContext.Roles);
            if (currentStep.Status is not EWorkflowStepStatus.WaitForAccept) return;

            if (currentStep.Handlers.All(d => d.Key != orgId && d.Key != userId)) return;

            if (currentStep.StepType is EStepType.End)
                throw new UserFriendlyException("当前流程已流转到最终步骤");

            //var changedSteps = new List<WorkflowStep> { currentStep };
            currentStep.Accept(userId, userName,
                orgId, orgName,
                orgAreaCode, orgAreaName);

            ////接办时非会签并且有多个接办部门时需更新接办部门
            //if (!workflow.IsInCountersign())
            //{
            //    var assigns = await _workflowAssignRepository.QueryAsync(d => d.WorkflowId == workflow.Id);
            //    if (assigns.Count > 1)
            //    {
            //        await _workflowAssignRepository.RemoveRangeAsync(assigns, cancellationToken);

            //        var assign = WorkflowAssign.Create(workflow.Id, orgId, orgName);
            //        await _workflowAssignRepository.AddAsync(assign, cancellationToken);
            //    }
            //}

            //await _workflowStepRepository.UpdateRangeAsync(changedSteps, cancellationToken);

            await _workflowStepRepository.UpdateAsync(currentStep, cancellationToken);

            await AcceptTraceAsync(workflow, currentStep, cancellationToken);

            await _mediator.Publish(new AcceptWorkflowNotify(workflow), cancellationToken);
        }

        /// <summary>
        /// 办理（流转至下一节点）
        /// </summary>
        public async Task NextAsync(Workflow workflow, WorkflowStep currentStep, NextWorkflowDto dto,
            StepDefine nextStepDefine, bool isNextDynamic, FlowAssignInfo flowAssignInfo,
            DateTime? expiredTime, ISessionContext current, CancellationToken cancellationToken)
        {
            ValidatePermission(workflow, current.RequiredOrgId, current.RequiredUserId, current.Roles);
            //CheckWhetherRunnable(workflow.Status);

            #region 办理当前节点

            if (dto.Files != null && dto.Files.Any())
                currentStep.FileJson = await _fileRepository.AddFileAsync(dto.Files, workflow.ExternalId,
                    currentStep.Id, cancellationToken);

            //(currentStep.IsInCountersign() && !dto.BackToCountersignEnd) || dto.IsStartCountersign;
            var isStartCountersign = currentStep.CountersignPosition switch
            {
                ECountersignPosition.None => dto.IsStartCountersign,
                ECountersignPosition.Multi => !dto.BackToCountersignEnd,
                ECountersignPosition.Single => !dto.BackToCountersignEnd,
                ECountersignPosition.End => dto.IsStartCountersign,
                _ => throw new ArgumentOutOfRangeException()
            };

            var counterSignType = GetCounterSignType(currentStep.BusinessType);

            var updateSteps = new List<WorkflowStep> { currentStep };

            //结束当前会签流程
            if (currentStep.IsCountersignEndStep)
            {
                var countersignStartStep =
                    workflow.Steps.FirstOrDefault(d => d.Id == currentStep.CountersignStartStepId);
                if (countersignStartStep is null)
                    throw new UserFriendlyException(
                        $"未查询到会签开始step, workflowId: {workflow.Id}, currentStepId: {currentStep.Id}",
                        "未查询到会签开始节点");

                if (countersignStartStep.IsStartCountersign)
                {
                    var currentCountersign =
                        workflow.Countersigns.FirstOrDefault(d => d.Id == countersignStartStep.StartCountersignId);
                    if (currentCountersign is null)
                        throw new UserFriendlyException(
                            $"未查询到对应会签信息，workflowId:{workflow.Id}, countersignId:{currentStep.CountersignId}",
                            "无效会签编号");

                    //结束step会签信息
                    countersignStartStep.CountersignEnd();
                    updateSteps.Add(countersignStartStep);

                    //结束会签
                    currentCountersign.End(currentStep.Id, currentStep.Code, currentStep.BusinessType,
                        current.RequiredUserId, current.UserName,
                        current.RequiredOrgId, current.OrgName,
                        current.OrgAreaCode, current.OrgAreaName);
                    await _workflowCountersignRepository.UpdateAsync(currentCountersign, cancellationToken);
                }
            }

            await HandleStepAsync(current, currentStep, workflow, dto, flowAssignInfo.FlowAssignType,
                counterSignType, expiredTime, cancellationToken);

            //创建会签数据
            if (isStartCountersign)
            {
                var exists = workflow.Countersigns.Any(d =>
                    !d.IsCompleted() && d.StarterId == current.RequiredUserId);
                if (exists)
                    throw new UserFriendlyException("该用户在当前流程存在未结束会签");
                await StartCountersignAsync(current, workflow, currentStep, dto, flowAssignInfo.FlowAssignType,
                    counterSignType, expiredTime, cancellationToken);
            }

            currentStep.IsActualHandled = CheckIsActualHandle(workflow, currentStep, nextStepDefine, dto);

            _mapper.Map(dto, workflow);

            ////操作为回到会签汇总时，更新开始会签节点的会签办理状态
            //if (currentStep.IsInCountersign() && dto.BackToCountersignEnd)
            //{
            //    if (currentStep.IsCountersignEndStep)
            //    {
            //        if (!currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
            //        {
            //            //汇总节点（非顶级）
            //            var csStartStep =
            //                workflow.Steps.FirstOrDefault(d => d.Id == currentStep.CountersignStartStepId);
            //            if (csStartStep is null)
            //                throw new UserFriendlyException("未查询到会签开始节点");
            //            PrevStepCsHandled(workflow, csStartStep, ref updateSteps);
            //        }
            //    }
            //    else if (currentStep.CountersignPosition is ECountersignPosition.Inner)
            //    {
            //        PrevStepCsHandled(workflow, currentStep, ref updateSteps);
            //    }
            //}

            //会签办理节点办理时更新会签members字段
            if (currentStep.CountersignPosition is ECountersignPosition.Multi or ECountersignPosition.Single)
            {
                //会签中正常办理节点，更新会签members办理状态
                var countersign =
                    workflow.Countersigns.FirstOrDefault(d => !d.IsCompleted() && d.Id == currentStep.CountersignId);
                if (countersign is null)
                    throw new UserFriendlyException(
                        $"会签数据异常, workflowId: {currentStep.WorkflowId}, countersignId: {currentStep.CountersignId}",
                        "会签数据异常");
                countersign.MemberHandled(current.RequiredUserId, current.RequiredOrgId);
                //update cs
                await _workflowCountersignRepository.UpdateNav(countersign)
                    .Include(d => d.Members)
                    .ExecuteCommandAsync();
            }

            await _workflowStepRepository.UpdateRangeAsync(updateSteps, cancellationToken);
            //await _workflowStepRepository.UpdateNav(updateSteps)
            //    .Include(d => d.StepHandlers)
            //    .ExecuteCommandAsync();

            await NextTraceAsync(workflow, dto, currentStep, cancellationToken);

            #endregion

            #region 处理流程

            //检查会签是否结束，并更新当前会签节点字段
            var isCountersignOver = false;
            if (workflow.IsInCountersign && currentStep.IsCountersignEndStep)
            {
                isCountersignOver = workflow.CheckIfCountersignOver();
                if (isCountersignOver)
                    workflow.EndCountersign();
            }

            if (workflow.ActualHandleStepId == currentStep.Id)
            {
                //更新实际办理节点信息
                workflow.UpdateActualStepWhenHandle(currentStep,
                    current.RequiredUserId, current.UserName,
                    current.RequiredOrgId, current.OrgName,
                    current.OrgAreaCode, current.OrgAreaName,
                    current.OrgLevel);
            }

            //检查是否流转到流程终点
            if (nextStepDefine.StepType is EStepType.End)
            {
                var endTrace = await EndAsync(workflow, dto, nextStepDefine, currentStep, current, expiredTime, cancellationToken);
                return;
            }

            //创建下一/N个节点(会签汇总节点:会签未全部办理时不创建，最后一个会签办理节点创建会签汇总节点)
            var nextSteps = await CreateNextStepsAsync(workflow, currentStep, dto,
                nextStepDefine, isNextDynamic, flowAssignInfo, expiredTime, isStartCountersign,
                cancellationToken);

            ////赋值当前节点的下级办理节点
            //if (dto.IsStartCountersign
            //   //|| (currentStep.IsInCountersign() &&
            //   //    !currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
            //   )
            //{
            //    currentStep.CreateCountersignSteps(nextSteps);
            //    await _workflowStepRepository.UpdateAsync(currentStep, cancellationToken);
            //}

            //更新办理对象(nextSteps无元素表示当前节点为会签办理节点且当前会签没有全部办理完成)
            workflow.UpdateHandlers(current.RequiredUserId, current.RequiredOrgId,
                flowAssignInfo.FlowAssignType, flowAssignInfo.HandlerObjects, nextSteps.Any());

            //todo 计算办理工作时长

            //指派实际办理节点
            UpdateActualStep(workflow, dto, nextStepDefine, nextSteps);

            //发起会签时记录顶层会签节点
            if (dto.IsStartCountersign && !workflow.IsInCountersign)
                workflow.StartCountersign(currentStep.Id, counterSignType);

            //更新指派信息
            workflow.Assign(flowAssignInfo.FlowAssignType, flowAssignInfo.GetHandlerIds());

            //更新会签实际办理对象信息
            if (currentStep.IsActualHandled)
                workflow.AddCsActualHandler(current.RequiredUserId, current.RequiredOrgId);

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            #endregion

            #region 流转记录

            var trace = await NextTraceAsync(workflow, dto, currentStep, cancellationToken);

            #endregion

            await _mediator.Publish(
                new NextStepNotify(workflow, dto, flowAssignInfo, trace, nextStepDefine,
                    current.RequiredOrgId, expiredTime.HasValue),
                cancellationToken);
        }

        /// <summary>
        /// 退回（返回前一节点）
        /// </summary>
        /// <returns></returns>
        public async Task<EFlowDirection> PreviousAsync(Workflow workflow, PreviousWorkflowDto dto,
            string applicantId, string applicantOrgId, string[] applicantRoleIds,
            ISessionContext current, CancellationToken cancellationToken)
        {
            //ValidatePermission(workflow, operater.OrgId, operater.Id);

            var (currentStep, prevStep, countersignStartStep) = GetPreviousStep(workflow, applicantId, applicantOrgId, applicantRoleIds);

            //保存附件
            if (dto.Files.Any())
                currentStep.FileJson = await _fileRepository.AddFileAsync(
                    dto.Files, workflow.ExternalId, currentStep.Id, cancellationToken);

            // add prev current to remove list
            var removeSteps = new List<WorkflowStep> { currentStep, prevStep };

            if (countersignStartStep is not null)
            {
                //add cs steps to remove list
                SearchCountersignSteps(countersignStartStep, workflow.Steps, ref removeSteps);

                //end cs
                var currentCountersign =
                    workflow.Countersigns.FirstOrDefault(d => d.Id == countersignStartStep.StartCountersignId);
                if (currentCountersign is null)
                    throw new UserFriendlyException(
                        $"未查询到对应会签信息，workflowId:{workflow.Id}, countersignId:{currentStep.CountersignId}",
                        "无效会签编号");

                //结束step会签信息
                countersignStartStep.CountersignEnd();
                await _workflowStepRepository.UpdateAsync(countersignStartStep, cancellationToken);
                //updateSteps.Add(countersignStartStep);

                //结束会签
                currentCountersign.End(currentStep.Id, currentStep.Code, currentStep.BusinessType,
                    current.RequiredUserId, current.UserName,
                    current.RequiredOrgId, current.OrgName,
                    current.OrgAreaCode, current.OrgAreaName);
                await _workflowCountersignRepository.UpdateAsync(currentCountersign, cancellationToken);

                //update workflow cs status
                if (workflow.CheckIfCountersignOver())
                    workflow.EndCountersign();
            }

            //update trace
            var trace = await PreviousTraceAsync(workflow.Id, dto, currentStep, current, cancellationToken);

            //复制上一个节点为待接办
            var newPrevStep = await DuplicateStepWithTraceAsync(workflow, prevStep, EWorkflowTraceType.Previous, cancellationToken);

            //remove workflow.steps
            await _workflowStepRepository.RemoveRangeAsync(removeSteps, cancellationToken);
            //await _workflowStepRepository.RemoveNav(removeSteps)
            //    .Include(d => d.StepHandlers)
            //    .ExecuteCommandAsync();

            if (workflow.Status is EWorkflowStatus.Completed)
                workflow.SetStatusRunnable();

            //更新实际办理节点信息
            workflow.UpdateActualStepWhenAssign(newPrevStep, prevStep.HandlerOrgName, prevStep.HandlerOrgId);

            //更新流程可办理对象
            workflow.UpdatePreviousHandlers(applicantId, applicantOrgId, prevStep);

            //orgToCenter会触发重新计算期满时间，1.无需审核按当前时间进行计算 2.需审核按审核通过时间计算
            var isOrgToCenter = prevStep.BusinessType is EBusinessType.Send && prevStep.IsOrigin;

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            await _mediator.Publish(new PreviousNotify(workflow, newPrevStep, dto, isOrgToCenter), cancellationToken);

            return GetFlowDirection(currentStep.BusinessType, prevStep.BusinessType);
        }

        /// <summary>
        /// 查询退回节点信息
        /// </summary>
        public (WorkflowStep currentStep, WorkflowStep prevStep, WorkflowStep? countersignStartStep) GetPreviousStep(
            Workflow workflow, string operaterId, string operaterOrgId, string[] roleIds)
        {
            var currentStep = GetUnHandleStep(workflow.Steps, operaterOrgId, operaterId, roleIds);
            var isCurrentTopCountersignEndStep = workflow.IsInCountersign &&
                                                 currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId);
            if (currentStep.IsInCountersign() && !isCurrentTopCountersignEndStep)
                throw UserFriendlyException.SameMessage("会签节点不支持退回");

            if (workflow.FlowType is EFlowType.Review && currentStep.StepType is EStepType.Start && currentStep.IsOrigin)
                throw UserFriendlyException.SameMessage("当前流程已退回到开始节点");

            //当退回操作遇到会签时，删除所有会签节点直达topCsStep
            //find prevStep, update handler
            WorkflowStep? prevStep, countersignStartStep = null;
            if (isCurrentTopCountersignEndStep)
            {
                //prev is topstart's prev
                countersignStartStep = workflow.Steps.FirstOrDefault(d => d.Id == currentStep.CountersignStartStepId);
                if (countersignStartStep is null)
                    throw new UserFriendlyException("未查询到对应会签开始节点");
                prevStep = workflow.Steps.FirstOrDefault(d => d.Id == countersignStartStep.PrevStepId);
            }
            else
            {
                prevStep = workflow.Steps.FirstOrDefault(d => d.Id == currentStep.PrevStepId);
            }

            if (prevStep == null)
                throw UserFriendlyException.SameMessage("未查询到前一节点");

            if (prevStep.IsCountersignEndStep)
            {
                countersignStartStep = workflow.Steps.FirstOrDefault(d => d.Id == prevStep.CountersignStartStepId);
                prevStep = countersignStartStep ?? throw new UserFriendlyException("未查询到对应会签开始节点");
                if (prevStep == null)
                    throw UserFriendlyException.SameMessage("未查询到前一节点");
            }

            return (currentStep, prevStep, countersignStartStep);
        }

        /// <summary>
        /// 查询派单池中流程节点id
        /// </summary>
        public async Task<IReadOnlyList<string>> GetUnhandleStepIdsFromSendPoolAsync(string sendPoolId, CancellationToken cancellationToken)
        {
            return await _workflowStepRepository.Queryable()
                .Where(d => SqlFunc.JsonListObjectAny(d.Handlers, "Key", sendPoolId))
                .Select(d => d.Id)
                .ToListAsync(cancellationToken);
        }

        /// <summary>
        /// 查询归属某用户的所有流程节点
        /// </summary>
        public async Task<List<WorkflowStep>> GetStepsBelongsToAsync(string userId, CancellationToken cancellationToken)
        {
            return await _workflowStepRepository.Queryable()
                .Includes(d => d.WorkflowTrace)
                .Where(d => d.HandlerId == userId)
                .OrderBy(d => d.CreationTime)
                .ToListAsync(cancellationToken);
        }

        ///// <summary>
        ///// 批量改变办理对象
        ///// </summary>
        //public async Task<ICollection<string>> ChangeHandlerRangeAsync(string sendPoolId,
        //    IReadOnlyList<(string userId, string username, string orgId, string orgName, IReadOnlyList<string> stepIds)> handlers,
        //    CancellationToken cancellationToken)
        //{
        //    var stepsIds = handlers.SelectMany(d => d.stepIds).ToList();
        //    var steps = await _workflowStepRepository.Queryable()
        //        .Includes(d => d.Workflow)
        //        .Includes(d => d.WorkflowTrace)
        //        //.Includes(d => d.StepHandlers)
        //        .Where(d => stepsIds.Contains(d.Id))
        //        .ToListAsync(cancellationToken);
        //    foreach (var handler in handlers)
        //    {
        //        var thisHandlers = new List<Kv> { new(handler.userId, handler.username) };
        //        var thisHandlerGroup = new HandlerGroupItem
        //        {
        //            GroupId = Guid.NewGuid().ToString(),
        //            Key = handler.userId,
        //            Value = handler.username,
        //        };
        //        var thisSteps = steps.Where(d => handler.stepIds.Contains(d.Id)).ToList();
        //        foreach (var thisStep in thisSteps)
        //        {
        //            //var stepHandler = WorkflowStepHandler.Create(thisStep.Workflow.Id, thisStep.Workflow.ExternalId,
        //            //    thisStep.FlowAssignType ?? EFlowAssignType.User, handler.userId, handler.username, handler.orgId, handler.orgName);
        //            //thisStep.StepHandlers.Clear();
        //            //thisStep.StepHandlers.Add(stepHandler);

        //            thisStep.Handlers = thisHandlers;

        //            //update trace
        //            thisStep.WorkflowTrace.Handlers = thisStep.Handlers;

        //            // update workflow
        //            thisStep.Workflow.FlowedUserIds.Remove(sendPoolId);
        //            thisStep.Workflow.FlowedUserIds.Add(handler.userId);
        //            thisStep.Workflow.UpdateHandlers(sendPoolId, null, EFlowAssignType.User,
        //                new List<HandlerGroupItem> { thisHandlerGroup }, true);

        //            var handlerUser = thisStep.Workflow.HandlerUsers.FirstOrDefault(d => d.Key == sendPoolId);
        //            if (handlerUser == null) continue;
        //            handlerUser.Key = handler.userId;
        //            handlerUser.Value = handler.username;
        //        }
        //    }

        //    await _workflowStepRepository.UpdateNav(steps)
        //        .Include(d => d.WorkflowTrace)
        //        .Include(d => d.Workflow)
        //        //.Include(d => d.StepHandlers)
        //        .ExecuteCommandAsync();

        //    return steps.Select(d => d.WorkflowId).ToList();
        //}

        /// <summary>
        /// 批量修改工单办理对象
        /// </summary>
        public async Task ChangeHandlerBatchAsync(
            IReadOnlyList<(string userId, string username, string orgId, string orgName, ICollection<WorkflowStep> steps)> handlers,
            CancellationToken cancellationToken)
        {
            foreach (var handler in handlers)
            {
                foreach (var step in handler.steps)
                {
                    step.FlowAssignType = EFlowAssignType.User;
                    step.Assign(handler.userId, handler.username,
                        handler.orgId, handler.orgName);
                    if (step.WorkflowTrace is null)
                        throw new UserFriendlyException("未查询节点对应快照信息");
                    step.WorkflowTrace.Assign(handler.userId, handler.username,
                        handler.orgId, handler.orgName);
                }
            }

            var steps = handlers.SelectMany(d => d.steps).ToList();
            //await _workflowStepRepository.UpdateRangeAsync(steps, cancellationToken);
            await _workflowStepRepository.UpdateNav(steps)
                .Include(d => d.WorkflowTrace)
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 查询工单办理中的一级部门
        /// </summary>
        public async Task<ICollection<Kv>> GetLevelOneOrgsAsync(string workflowId, CancellationToken cancellation)
        {
            var traces = await _workflowTraceRepository.Queryable()
                 .LeftJoin<SystemOrganize>((t, o) => t.HandlerOrgId == o.Id)
                 .Where((t, o) => t.WorkflowId == workflowId &&
                                  !string.IsNullOrEmpty(t.HandlerOrgId) &&
                                  o.Level == 1)
                 .ToListAsync(cancellation);

            //var handlers = await _workflowStepHandlerRepository.Queryable()
            //    .InnerJoin<WorkflowTrace>((wsh, wt) => wsh.WorkflowStepId == wt.StepId)
            //    .LeftJoin<SystemOrganize>((wsh, wt, o) => wsh.OrgId == o.Id)
            //    .Where((wsh, wt, o) => wsh.WorkflowId == workflowId &&
            //                        //wt.BusinessType == EBusinessType.Department &&
            //                        //wt.HandlerType == EHandlerType.OrgLevel &&
            //                        !string.IsNullOrEmpty(wsh.OrgId) &&
            //                        o.Level == 1)
            //    .ToListAsync(cancellation);

            //var orgs = handlers.Select(d => new Kv(d.OrgId, d.OrgName))
            //    .DistinctBy(d => d.Key)
            //    .ToList();

            var orgs = traces
                .DistinctBy(d => d.HandlerOrgId)
                .Select(d => new Kv(d.HandlerOrgId, d.HandlerOrgName))
                .ToList();


            return orgs;
            //var workflow = await GetWorkflowAsync(workflowId, withSteps: true, cancellationToken: cancellation);
            //var list = workflow.Steps.Distinct().Where(d => d.BusinessType == EBusinessType.Department &&
            //                                 d.HandlerType == EHandlerType.OrgLevel &&
            //                                 d.StepHandlers.Any(d =>
            //                                     !string.IsNullOrEmpty(d.OrgId) && d.OrgId.CheckIfOrgLevelIs(1)))
            //    .Select(d => new Kv(d.StepHandlers.First().OrgId, d.StepHandlers.First().OrgName))
            //    .ToList();
            //return list.Where((x, i) => list.FindIndex(z => z.Key == x.Key) == i).ToList();
        }

        /// <summary>
        /// 更新未办理节点的期满时间
        /// </summary>
        public async Task UpdateUnhandleExpiredTimeAsync(string workflowId, DateTime expiredTime, CancellationToken cancellation)
        {
            var steps = await _workflowStepRepository.Queryable()
                .Includes(d => d.WorkflowTrace)
                .Where(d => d.WorkflowId == workflowId &&
                            d.Status < EWorkflowStepStatus.Handled)
                .ToListAsync(cancellation);
            foreach (var step in steps)
            {
                step.StepExpiredTime = expiredTime;
                step.WorkflowTrace.StepExpiredTime = expiredTime;
            }

            await _workflowStepRepository.UpdateNav(steps)
                .Include(d => d.WorkflowTrace)
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 查询该部门最后办理节点
        /// </summary>
        /// <returns></returns>
        public async Task<WorkflowStep> FindLastHandleStepAsync(string workflowId, string orgId, CancellationToken cancellation)
        {
            return await _workflowStepRepository.Queryable()
                .Where(d => d.WorkflowId == workflowId && d.HandlerOrgId == orgId && d.StepType != EStepType.End)
                //.Where(d => d.StepHandlers.Any(sh => sh.OrgId == orgId) && d.WorkflowId == workflowId)
                .OrderByDescending(d => d.HandleTime)
                .FirstAsync(cancellation);
        }

        /// <summary>
        /// 查询流转方向
        /// </summary>
        public EFlowDirection GetFlowDirection(EBusinessType sourceStepBusinessType, EBusinessType directionStepBusinessType)
        {
            switch (sourceStepBusinessType)
            {
                case EBusinessType.Center:
                case EBusinessType.Send:
                    return directionStepBusinessType switch
                    {
                        EBusinessType.Center => EFlowDirection.CenterToCenter,
                        EBusinessType.Send => EFlowDirection.CenterToCenter,
                        EBusinessType.Department => EFlowDirection.CenterToOrg,
                        EBusinessType.File => EFlowDirection.CenterToFile,
                        _ => throw new ArgumentOutOfRangeException(nameof(directionStepBusinessType),
                            directionStepBusinessType, null)
                    };
                case EBusinessType.Department:
                case EBusinessType.File:
                    return directionStepBusinessType switch
                    {
                        EBusinessType.Center => EFlowDirection.OrgToCenter,
                        EBusinessType.Send => EFlowDirection.OrgToCenter,
                        EBusinessType.Department => EFlowDirection.OrgToOrg,
                        EBusinessType.File => EFlowDirection.OrgToFile,
                        _ => throw new ArgumentOutOfRangeException(nameof(directionStepBusinessType),
                            directionStepBusinessType, null)
                    };
                default:
                    throw new ArgumentOutOfRangeException(nameof(sourceStepBusinessType), sourceStepBusinessType, null);
            }
        }

        /// <summary>
        /// 流程被签收至某个用户（更新流转对象，办理对象，节点办理对象以及stepHandlers）
        /// </summary>
        public async Task<Workflow> SignToSomebodyAsync(string workflowId, string userId, string username, string orgId, string orgName, CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(workflowId, withSteps: true, withTraces: true, cancellationToken: cancellationToken);
            workflow.Assign(EFlowAssignType.User, _sessionContext.RequiredUserId);

            workflow.HandlerOrgs = new();
            workflow.HandlerUsers = new List<HandlerGroupItem>
            {
                new()
                {
                    GroupId = Guid.NewGuid().ToString(),
                    Key = userId,
                    Value = username
                }
            };

            var startStep = workflow.Steps.First(d => d.StepType == EStepType.Start && d.IsOrigin);
            startStep.Handlers = new List<Kv> { new(userId, username) };
            startStep.AcceptorId = userId;
            startStep.AcceptorName = username;
            startStep.AcceptTime = DateTime.Now;
            startStep.AcceptorOrgId = orgId;
            startStep.AcceptorOrgName = orgName;

            startStep.FlowAssignType = EFlowAssignType.User;
            startStep.Assign(userId, username, orgId, orgName);

            //var stepHandler = startStep.StepHandlers.First();
            //startStep.StepHandlers.RemoveAll(d => d.Id != stepHandler.Id);
            //stepHandler.UserId = userId;
            //stepHandler.Username = username;
            //stepHandler.OrgId = orgId;
            //stepHandler.OrgName = orgName;

            startStep.WorkflowTrace = workflow.Traces.First(d => d.Id == startStep.Id);
            _mapper.Map(startStep, startStep.WorkflowTrace);

            await _workflowStepRepository.UpdateNav(startStep)
                //.Include(d => d.StepHandlers)
                .Include(d => d.WorkflowTrace)
                .ExecuteCommandAsync();

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            return workflow;
        }

        /// <summary>
        /// 查找当前会签内所有节点(含start,end)
        /// </summary>
        private void SearchCountersignSteps(WorkflowStep startStep, List<WorkflowStep> steps, ref List<WorkflowStep> csSteps)
        {
            if (startStep.IsStartCountersign)
            {
                var countersignSteps = steps.Where(d => d.CountersignId == startStep.StartCountersignId).ToList();
                if (countersignSteps.Any())
                {
                    foreach (var countersignStep in countersignSteps)
                    {
                        SearchCountersignSteps(countersignStep, steps, ref csSteps);
                    }
                }
            }

            csSteps.Add(startStep);
        }

        /// <summary>
        /// 撤回（返回到之前任意节点）
        /// </summary>
        public async Task RecallAsync(Workflow workflow, RecallDto dto, StepDefine targetStepDefine,
            FlowAssignInfo flowAssignInfo, DateTime? expiredTime,
            ISessionContext current, CancellationToken cancellationToken)
        {
            var targetStep = workflow.Steps.FirstOrDefault(d => d.Code == dto.NextStepCode && d.IsOrigin);
            if (targetStep is null)
                throw UserFriendlyException.SameMessage("该流程尚未流转至该节点");

            //update uncompleted traces
            await RecallTraceAsync(workflow.Id, dto.Opinion, current, cancellationToken);

            var isOrgToCenter = await RecallAsync(workflow, dto, flowAssignInfo, targetStepDefine, targetStep,
                EWorkflowTraceType.Recall, expiredTime, cancellationToken);

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            await _mediator.Publish(new RecallNotify(workflow, targetStep, dto, isOrgToCenter), cancellationToken);
        }

        /// <summary>
        /// 撤回至开始节点
        /// </summary>
        public async Task RecallToStartStepAsync(string workflowId, string opinion, ISessionContext current, CancellationToken cancellationToken)
        {
            //todo 1.当前待办节点删掉 2.当前待办trace更新(status, opinion) 3.复制startStep为待办 4.更新workflow(status, csStatus, handlers) 5.publish event
            var workflow = await GetWorkflowAsync(workflowId, withDefine: true, withSteps: true,
                cancellationToken: cancellationToken);
            //update uncompleted traces
            await RecallTraceAsync(workflow.Id, opinion, current, cancellationToken);

            var startStep = workflow.Steps.First(d => d.StepType == EStepType.Start);
            workflow.Steps.RemoveAll(d => true);
            await _workflowStepRepository.RemoveRangeAsync(workflow.Steps, cancellationToken);

            workflow.EndCountersign();
            workflow.ResetOption();
            if (workflow.Status is EWorkflowStatus.Completed)
                workflow.SetStatusRunnable();

            var newStartStep =
                await DuplicateStepWithTraceAsync(workflow, startStep, EWorkflowTraceType.Recall, cancellationToken);

            //更新当前办理节点信息
            //workflow.UpdateWorkflowCurrentStepInfo(false,
            //    _sessionContext.RequiredUserId, _sessionContext.UserName,
            //    _sessionContext.RequiredOrgId, _sessionContext.OrgName,
            //    _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName,
            //    nextStep: newStartStep);
            workflow.UpdateActualStepWhenAssign(startStep, startStep.HandlerOrgName, startStep.HandlerOrgId);

            var isOrgToCenter = CheckIfFlowFromOrgToCenter(workflow, startStep);

            var flowAssignInfo = FlowAssignInfo.Create(startStep.FlowAssignType.Value, startStep.Handlers);
            workflow.ResetHandlers(flowAssignInfo.FlowAssignType, flowAssignInfo.HandlerObjects);

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            var dto = _mapper.Map<RecallDto>(startStep);
            dto.WorkflowId = workflowId;
            await _mediator.Publish(new RecallNotify(workflow, startStep, dto, isOrgToCenter), cancellationToken);
        }

        ///// <summary>
        ///// 跳转（直接将流程跳转至任意节点）
        ///// </summary>
        //public async Task JumpAsync(Workflow workflow, RecallDto dto, StepDefine targetStepDefine,
        //    FlowAssignInfo flowAssignInfo, CancellationToken cancellationToken)
        //{
        //    //todo 跳转至结束节点，（自动办理）
        //    //if (targetStepDefine.StepType is EStepType.Start or EStepType.End)
        //    //    throw UserFriendlyException.SameMessage("开始/结束节点不支持跳转");

        //    //update uncompleted traces
        //    await JumpTraceAsync(workflow.Id, dto, cancellationToken);

        //    bool isOrgToCenter = false, isCenterToOrg = false;
        //    var targetStep = workflow.Steps.FirstOrDefault(d => d.Code == dto.NextStepCode && d.IsOrigin);
        //    if (targetStep == null)
        //    {
        //        //向后跳转

        //        //此场景并非按配置流转，默认最靠后的节点做为targetStep的prevStep
        //        var lastStep = workflow.Steps.Where(d => d.IsOrigin).MaxBy(d => d.CreationTime);
        //        if (lastStep is null || lastStep.StepType is EStepType.End)
        //            throw new UserFriendlyException($"流程流转数据异常，未结束流程出现endStep, flowId: {workflow.Id}", "流程流转数据异常");

        //        var targetSteps = await CreateConfigStepsAsync(workflow, targetStepDefine, lastStep, dto,
        //            flowAssignInfo, EWorkflowTraceStatus.Jump, cancellationToken);
        //        targetStep = targetSteps.First();

        //        workflow.EndCountersign();
        //        workflow.ResetOption();

        //        ////更新当前办理节点信息
        //        //workflow.UpdateWorkflowCurrentStepInfo(dto.IsStartCountersign,
        //        //    _sessionContext.RequiredUserId, _sessionContext.UserName,
        //        //    _sessionContext.RequiredOrgId, _sessionContext.OrgName,
        //        //    _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName,
        //        //    nextStep: targetStep);

        //        //calc workflow expired time
        //        isCenterToOrg = CheckIfFlowFromCenterToOrg(workflow, targetStep);
        //        //if (isCenterToOrg)
        //        //    workflow.ExpiredTime = CalculateExpiredTime("");//todo calc expiredTime

        //        #region 补充中间节点处理方案（暂不需要）

        //        //var completeStepCodes = workflow.StepBoxes.Select(d => d.Code);
        //        //var uncompleteStepDefines = workflow.Definition.Steps.Where(d => !completeStepCodes.Contains(d.Code));
        //        //创建当前节点与目标节点中间节点
        //        //var jumpDto = new BasicWorkflowDto
        //        //{
        //        //    Opinion = "跳转补充"
        //        //};

        //        //foreach (var stepDefine in uncompleteStepDefines)
        //        //{
        //        //    var previousStepId = lastStepBox.Steps.Count > 1 ? lastStepBox.Id : lastStepBox.Steps.First().Id;
        //        //    if (dto.TargetStepCode == stepDefine.Code)
        //        //    {
        //        //        await CreateStepAsync(workflow, stepDefine, dto, lastStepBox.Id, previousStepId, cancellationToken);
        //        //        break;
        //        //    }

        //        //    //jump业务下，如果当前节点为会签节点，第一个补充节点的subStep.PreviousId无法确定从哪个子节点跳转过来，统一处理为当前节点的stepBox.Id
        //        //    lastStepBox = await CreateStepAsync(workflow, stepDefine, dto, lastStepBox.Id, previousStepId, cancellationToken);
        //        //} 

        //        #endregion
        //    }
        //    else
        //    {
        //        //返回之前节点
        //        isOrgToCenter = await RecallAsync(workflow, dto, flowAssignInfo, targetStepDefine, targetStep,
        //            EWorkflowTraceStatus.Jump, cancellationToken);
        //    }

        //    workflow.ResetHandlers(flowAssignInfo.FlowAssignType, flowAssignInfo.HandlerObjects);
        //    await _workflowRepository.UpdateAsync(workflow, cancellationToken);

        //    await _mediator.Publish(
        //        new JumpNotify(workflow, targetStep, dto, flowAssignInfo, isCenterToOrg, isOrgToCenter),
        //        cancellationToken);
        //}

        ///// <summary>
        ///// 重办
        ///// </summary>
        //public async Task RedoAsync(Workflow workflow, RecallDto dto, StepDefine targetStepDefine,
        //    FlowAssignInfo flowAssignInfo, CancellationToken cancellationToken)
        //{
        //    if (targetStepDefine.StepType is EStepType.Start or EStepType.End)
        //        throw UserFriendlyException.SameMessage("开始/结束节点不支持重办");

        //    var targetStepBox = workflow.Steps.FirstOrDefault(d => d.Code == dto.NextStepCode);
        //    if (targetStepBox is null)
        //        throw UserFriendlyException.SameMessage("未找到该节点配置");

        //    var isOrgToCenter = await RecallAsync(workflow, dto, flowAssignInfo, targetStepDefine, targetStepBox,
        //        EWorkflowTraceStatus.Redo, cancellationToken);

        //    workflow.Redo();
        //    workflow.ResetHandlers(flowAssignInfo.FlowAssignType, flowAssignInfo.HandlerObjects);

        //    //todo calc expiredTime
        //    //dto.Extension.TimeLimitCount


        //    await _workflowRepository.UpdateAsync(workflow, cancellationToken);

        //    await _mediator.Publish(new RedoNotify(workflow, dto, isOrgToCenter), cancellationToken);
        //}

        ///// <summary>
        ///// 否决（审批流程不通过）
        ///// </summary>
        ///// <returns></returns>
        //public async Task RejectAsync(Workflow workflow, BasicWorkflowDto dto, CancellationToken cancellationToken)
        //{
        //    var currentStep = GetUnHandleStep(workflow.Steps, _sessionContext.RequiredOrgId,
        //        _sessionContext.RequiredUserId);
        //    await HandleStepAsync(currentStep, workflow, dto, null,
        //        null, null, cancellationToken);
        //    await _workflowStepRepository.UpdateAsync(currentStep, cancellationToken);

        //    workflow.UpdateActualStepWhenHandle(currentStep,
        //        _sessionContext.RequiredUserId, _sessionContext.UserName,
        //        _sessionContext.RequiredOrgId, _sessionContext.OrgName,
        //        _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName,
        //        _sessionContext.OrgLevel);

        //    var endStepDefine = workflow.WorkflowDefinition.FindEndStepDefine();
        //    var endTrace = await EndAsync(workflow, dto, endStepDefine, currentStep, cancellationToken);

        //    //await _mediator.Publish(new RejectNotify(workflow, dto), cancellationToken);
        //}

        /// <summary>
        /// 补充
        /// </summary>
        /// <returns></returns>
        public async Task SupplementAsync(Workflow workflow, EndWorkflowDto dto, CancellationToken cancellationToken)
        {
            CheckWhetherRunnable(workflow.Status);
            //todo 检查当前办理人是否为该流程中的办理人

            var supplement = _mapper.Map<WorkflowSupplement>(dto);
            await _workflowSupplementRepository.AddAsync(supplement, cancellationToken);
        }

        /// <summary>
        /// 终止流程
        /// </summary>
        public async Task TerminateAsync(TerminateDto dto, CancellationToken cancellationToken)
        {
            var workflow = await _workflowRepository.GetAsync(dto.WorkflowId, cancellationToken);
            if (workflow == null)
                throw UserFriendlyException.SameMessage("无效的流程编号");
            workflow.Terminate(dto.Opinion);
            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            await _mediator.Publish(new TerminalWorkflowNotify(workflow), cancellationToken);
        }

        /// <summary>
        /// 根据stepCode查询流程配置中对应的节点
        /// </summary>
        public StepDefine GetStepDefine(WorkflowDefinition workflowDefinition, string stepCode)
        {
            if (workflowDefinition == null) throw new ArgumentNullException(nameof(workflowDefinition));
            if (string.IsNullOrEmpty(stepCode)) throw new ArgumentNullException(nameof(stepCode));
            var stepDefine = workflowDefinition.FindStepDefine(stepCode);
            if (stepDefine == null)
                throw new UserFriendlyException(
                    $"未找到流程中对应的节点，DefineCode: {workflowDefinition.Code}, stepCode: {stepCode}",
                    "未查询到对应节点");
            return stepDefine;
        }

        /// <summary>
        /// 查询当前待办理节点
        /// </summary>
        public WorkflowStep FindCurrentStepWaitForHandle(Workflow workflow, string userId, string orgId, string[] roleIds) =>
            GetUnHandleStep(workflow.Steps, orgId, userId, roleIds);

        /// <summary>
        /// 查询当前节点中最后一个节点
        /// </summary>
        public async Task<WorkflowStep?> FindLastStepAsync(string workflowId, CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(workflowId, withSteps: true, cancellationToken: cancellationToken);
            return workflow.Steps.MaxBy(d => d.CreationTime);
        }

        /// <summary>
        /// 查询待回访部门
        /// </summary>
        /// <returns></returns>
        public async Task<(Kv, IReadOnlyList<Kv>)> GetUnvisitOrgsAsync(string workflowId,
            CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(workflowId, withSteps: true, cancellationToken: cancellationToken);
            if (workflow.CounterSignType is not ECounterSignType.Center)
                return new(new Kv(workflow.ActualHandleOrgCode, workflow.ActualHandleOrgName), new List<Kv>());
            var steps = workflow.Steps
                .Where(d => d.StepType is EStepType.Normal)
                .ToList();
            var items = steps.Select(d => new Kv(d.HandlerOrgId, d.HandlerOrgName))
                .DistinctBy(d => d.Key).ToList();
            return (new Kv(workflow.ActualHandleOrgCode, workflow.ActualHandleOrgName), items);
        }

        /// <summary>
        /// 依据配置过滤下一节点
        /// </summary>
        public List<StepDefine> NextStepDefineFilter(EPathPolicy pathPolicy, List<StepDefine> nextStepDefines)
        {
            switch (pathPolicy)
            {
                case EPathPolicy.DirectUpper:
                    break;
                case EPathPolicy.DirectUpperCenterIsTop:
                    var currentOrgLevel = _sessionContext.RequiredOrgId.CalcOrgLevel();
                    if (currentOrgLevel == 1)
                    {
                        nextStepDefines = nextStepDefines.Where(d => d.IsCenter()).ToList();
                    }
                    else
                    {
                        var upperLevel = (--currentOrgLevel).ToString();
                        nextStepDefines = nextStepDefines
                            .Where(d => d.HandlerType is EHandlerType.OrgLevel &&
                                        d.HandlerTypeItems.Any(x => x.Key == upperLevel))
                            .ToList();
                    }

                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return nextStepDefines;
        }

        /// <summary>
        /// 撤销流程
        /// </summary>
        public async Task CancelAsync(CancelDto dto, DateTime? expiredTime, ISessionContext current, CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(dto.WorkflowId, withDefine: true, withSteps: true,
                cancellationToken: cancellationToken);

            var currentStep = GetUnHandleStep(workflow.Steps, _sessionContext.RequiredOrgId,
                _sessionContext.RequiredUserId, _sessionContext.Roles);
            //var (currentStepBox, currentStep) = GetUnCompleteStep(workflow.Steps, _sessionContext.RequiredOrgId, _sessionContext.RequiredUserId);

            var endStepDefine = workflow.WorkflowDefinition.FindEndStepDefine();

            var basicDto = _mapper.Map<BasicWorkflowDto>(dto);
            var endTrace = await EndAsync(workflow, basicDto, endStepDefine, currentStep, current,
               expiredTime, cancellationToken: cancellationToken);

            await _mediator.Publish(new CancelWorkflowNotify(workflow), cancellationToken);
        }

        /// <summary>
        /// 新增流程流转记录
        /// </summary>
        public async Task AddTracesAsync(string workflowId, List<WorkflowTrace> traces,
            CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(workflowId, cancellationToken: cancellationToken);
            if (workflow is null)
                throw new UserFriendlyException("找不到该流程");

            await _workflowTraceRepository.AddRangeAsync(traces, cancellationToken);
        }

        /// <summary>
        /// 创建开始节点
        /// </summary>
        public WorkflowStep CreateStartStep(Workflow workflow, StepDefine startStepDefine,
            BasicWorkflowDto dto, FlowStepHandler handler, DateTime? expiredTime)
        {
            //startstep
            var nextSteps = _mapper.Map<List<StepSimple>>(startStepDefine.NextSteps);
            if (startStepDefine.InstanceMode is EInstanceMode.Config)
            {
                var selectedStep = nextSteps.FirstOrDefault(d => d.Code == dto.NextStepCode);
                if (selectedStep is not null)
                    selectedStep.Selected = true;
            }

            var startStep = _mapper.Map<WorkflowStep>(startStepDefine);
            _mapper.Map(workflow, startStep);
            startStep.FlowAssignType = EFlowAssignType.User;
            startStep.Handlers = new List<Kv> { new(handler.Key, handler.Value) };
            //startStep.StepHandlers = stepHandlers;
            startStep.NextSteps = nextSteps;
            startStep.IsMain = true;
            startStep.IsOrigin = true;
            startStep.Status = EWorkflowStepStatus.WaitForAccept;
            startStep.PrevChosenStepCode = null;
            startStep.StepExpiredTime = expiredTime;

            startStep.Assign(handler.UserId, handler.Username,
                handler.OrgId, handler.OrgName,
                handler.RoleId, handler.RoleName);

            startStep.InitId();
            return startStep;
        }

        public async Task<Kv> FindActualHandlerAsync(string workflowId, CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(workflowId, cancellationToken: cancellationToken);
            return new Kv(workflow.ActualHandleOrgCode, workflow.ActualHandleOrgName);
        }

        /// <summary>
        /// 流程结束
        /// </summary>
        public async Task<WorkflowTrace> EndAsync(Workflow workflow, BasicWorkflowDto dto,
            StepDefine endStepDefine, WorkflowStep currentStep, ISessionContext current,
            DateTime? expiredTime, CancellationToken cancellationToken)
        {
            //var endStepHandles = new List<WorkflowStepHandler>
            //{
            //    WorkflowStepHandler.Create(workflow.Id, workflow.ExternalId, EFlowAssignType.User,
            //        current.RequiredUserId, current.UserName, current.RequiredOrgId, current.OrgName)
            //};

            //create endStep
            var endStep = await CreateEndStepAsync(current, workflow, endStepDefine, currentStep, expiredTime, cancellationToken);
            workflow.Steps.Add(endStep);

            //update endTrace
            var endTrace = await NextTraceAsync(workflow, dto, endStep, cancellationToken);

            workflow.Complete(dto.ReviewResult);

            await _workflowRepository.UpdateAsync(workflow, cancellationToken);

            await _mediator.Publish(new EndWorkflowNotify(workflow, endTrace, dto.External), cancellationToken);

            return endTrace;
        }

        /// <summary>
        /// 判断会签类型(中心会签或部门会签)
        /// </summary>
        /// <param name="businessType"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public ECounterSignType? GetCounterSignType(EBusinessType businessType) =>
            businessType switch
            {
                EBusinessType.Center => ECounterSignType.Center,
                EBusinessType.Send => ECounterSignType.Center,
                EBusinessType.Department => ECounterSignType.Department,
                EBusinessType.File => null,
                _ => throw new ArgumentOutOfRangeException(nameof(businessType), businessType, null)
            };

        /// <summary>
        /// 办理节点
        /// </summary>
        public async Task HandleStepAsync(ISessionContext current, WorkflowStep step, Workflow workflow,
            BasicWorkflowDto dto, EFlowAssignType? flowAssignType, ECounterSignType? counterSignType,
            DateTime? expiredTime, CancellationToken cancellationToken)
        {
            if (step.Status is EWorkflowStepStatus.Handled)
                throw UserFriendlyException.SameMessage("当前节点状态已办理");

            if (step.StepType is EStepType.End)
                throw new UserFriendlyException("当前流程已流转到最终步骤");

            if (dto.IsStartCountersign && !counterSignType.HasValue)
                throw new UserFriendlyException("缺少会签类型参数");

            //办理参数
            //_mapper.Map(dto, step);
            step.NextHandlers = dto.NextHandlers;
            step.NextMainHandler = dto.NextMainHandler;
            step.NextStepCode = dto.NextStepCode;
            step.IsSms = dto.IsSms;
            step.Opinion = dto.Opinion;

            //step办理状态
            HandleStep(current, step, dto.Opinion, dto.NextStepCode);
        }


        #region private method

        private static void UpdateActualStep(Workflow workflow, BasicWorkflowDto dto, StepDefine nextStepDefine,
            List<WorkflowStep> nextSteps)
        {
            if (dto.IsStartCountersign) return;
            if (workflow.IsInCountersign) return;
            if (nextStepDefine.StepType is EStepType.Summary or EStepType.End) return;

            if (nextStepDefine.BusinessType is EBusinessType.Center or EBusinessType.Send)
            {
                workflow.UpdateActualStepWhenAssign(nextSteps.First(), actualHandleOrgCode: OrgSeedData.CenterId,
                    actualHandleOrgName: "热线中心");
            }
            else
            {
                var nextHandler = dto.NextHandlers.First();
                workflow.UpdateActualStepWhenAssign(nextSteps.First(),
                    actualHandleOrgCode: nextHandler.Key, actualHandleOrgName: nextHandler.Value);
            }

            //if ( /*workflow.FlowType is EFlowType.Handle &&*/
            //    !workflow.IsInCountersign &&
            //    nextStepDefine.StepType != EStepType.Summary &&
            //    nextStepDefine.StepType != EStepType.End)
            //{
            //    if (nextStepDefine.BusinessType is EBusinessType.Center or EBusinessType.Send)
            //    {
            //        workflow.UpdateActualStepWhenAssign(firstSteps.First(), actualHandleOrgCode: OrgSeedData.CenterId,
            //            actualHandleOrgName: "热线中心");
            //    }
            //    else
            //    {
            //        var nextHandler = dto.NextHandlers.First();
            //        workflow.UpdateActualStepWhenAssign(firstSteps.First(),
            //            actualHandleOrgCode: nextHandler.Key, actualHandleOrgName: nextHandler.Value);
            //    }
            //}
        }

        private async Task<WorkflowStep> CreateStartStepAsync(Workflow workflow, StepDefine startStepDefine,
            BasicWorkflowDto dto, FlowStepHandler handler, EWorkflowTraceType traceType,
            DateTime? expiredTime, CancellationToken cancellationToken)
        {
            var startStep = CreateStartStep(workflow, startStepDefine, dto, handler, expiredTime);
            await _workflowStepRepository.AddAsync(startStep, cancellationToken);
            //await _workflowStepRepository.AddNav(startStep)
            //    .Include(d => d.StepHandlers)
            //    .ExecuteCommandAsync();
            await CreateTraceAsync(workflow, startStep, traceType, cancellationToken);
            return startStep;
        }

        ////更新目标节点前一节点的会签办理完成状态
        //private void PrevStepCsHandled(Workflow workflow, WorkflowStep targetStep, ref List<WorkflowStep> updateSteps)
        //{
        //    var prevStep = workflow.Steps.FirstOrDefault(d => d.Id == targetStep.PrevStepId);
        //    if (prevStep is null)
        //        throw new UserFriendlyException("未查询到目标节点的前一节点");

        //    var csStep = prevStep.CountersignSteps.FirstOrDefault(d => d.StepId == targetStep.Id);
        //    if (csStep is null)
        //        throw new UserFriendlyException("未查询到当前待办节点");
        //    csStep.Completed = true;
        //    updateSteps.Add(prevStep);
        //}

        /// <summary>
        /// 创建下1/N个节点
        /// </summary>
        private async Task<List<WorkflowStep>> CreateNextStepsAsync(Workflow workflow, WorkflowStep currentStep,
            BasicWorkflowDto dto, StepDefine nextStepDefine, bool isNextDynamic, FlowAssignInfo flowAssignInfo,
            DateTime? expiredTime, bool isStartCountersign,
            CancellationToken cancellationToken)
        {
            List<WorkflowStep> nextSteps = new();
            if (currentStep.IsInCountersign())
            {
                if (currentStep.IsCountersignEndStep)
                {
                    // check if current is topend f: csStartStep.prev
                    // t: check if dto.StartCs t: csconfig f: config
                    if (currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
                    {
                        if (isStartCountersign)
                        {
                            //依据会签策略创建会签下一级节点
                            nextSteps = await CreateCountersignStepsAsync(workflow, nextStepDefine, currentStep, dto,
                                flowAssignInfo.FlowAssignType, expiredTime, isStartCountersign, cancellationToken);
                        }
                        else
                        {
                            //创建普通节点（根据配置）
                            nextSteps = await CreateConfigStepsAsync(workflow, nextStepDefine, currentStep, dto,
                                flowAssignInfo, EWorkflowTraceType.Normal, expiredTime, cancellationToken);
                        }
                    }
                    else
                    {
                        if (dto.BackToCountersignEnd)
                        {
                            // csStartStep.prev
                            var csStartStep = workflow.Steps.FirstOrDefault(d => d.Id == currentStep.CountersignStartStepId);
                            if (csStartStep is null)
                                throw new UserFriendlyException("未查询到会签节点");

                            nextSteps = await CreateCsEndStepsByTargetPrevAsync(workflow, csStartStep, dto, expiredTime, cancellationToken);
                        }
                        else
                        {
                            //依据会签策略创建会签下一级节点
                            nextSteps = await CreateCountersignStepsAsync(workflow, nextStepDefine, currentStep, dto,
                                flowAssignInfo.FlowAssignType, expiredTime, isStartCountersign, cancellationToken);
                        }
                    }
                }
                else
                {
                    if (dto.BackToCountersignEnd)
                    {
                        // check if cs all complete, create next
                        nextSteps = await CreateCsEndStepsByTargetPrevAsync(workflow, currentStep, dto,
                            expiredTime, cancellationToken);
                    }
                    else
                    {
                        //依据会签策略创建会签下一级节点
                        nextSteps = await CreateCountersignStepsAsync(workflow, nextStepDefine, currentStep, dto,
                            flowAssignInfo.FlowAssignType, expiredTime, isStartCountersign, cancellationToken);
                    }
                }
            }
            else if (isStartCountersign) //top
            {
                //依据会签策略创建会签下一级节点
                nextSteps = await CreateCountersignStepsAsync(workflow, nextStepDefine, currentStep, dto,
                    flowAssignInfo.FlowAssignType, expiredTime, isStartCountersign, cancellationToken);
            }
            else if (isNextDynamic)
            {
                //创建动态下一级节点
                nextSteps = await CreateDynamicStepsAsync(workflow, nextStepDefine, currentStep, dto, flowAssignInfo,
                    expiredTime, cancellationToken);
            }
            else
            {
                //创建普通节点（根据配置）
                nextSteps = await CreateConfigStepsAsync(workflow, nextStepDefine, currentStep, dto, flowAssignInfo,
                    EWorkflowTraceType.Normal, expiredTime, cancellationToken);
            }

            return nextSteps;
        }

        private async Task<List<WorkflowStep>> CreateDynamicStepsAsync(
            Workflow workflow,
            StepDefine nextStepDefine,
            WorkflowStep prevStep,
            BasicWorkflowDto dto,
            FlowAssignInfo flowAssignInfo,
            DateTime? expiredTime,
            CancellationToken cancellationToken)
        {
            var handlerType = nextStepDefine.InstancePolicy switch
            {
                EDynamicPolicy.OrgUpCenterTop => EHandlerType.OrgLevel,
                EDynamicPolicy.OrgUp => EHandlerType.OrgLevel,
                EDynamicPolicy.OrgDownCenterTop => EHandlerType.OrgLevel,
                EDynamicPolicy.OrgDown => EHandlerType.OrgLevel,
                null => throw new ArgumentOutOfRangeException(),
                _ => throw new ArgumentOutOfRangeException()
            };

            return await CreateStepsAsync(workflow, nextStepDefine, prevStep, dto,
                flowAssignInfo.FlowAssignType, dto.NextHandlers, null, EWorkflowStepStatus.WaitForAccept,
                ECountersignPosition.None, false, EWorkflowTraceType.Normal, handlerType, expiredTime,
                cancellationToken: cancellationToken);
        }

        private Task<List<WorkflowStep>> CreateCountersignStepsAsync(
            Workflow workflow,
            StepDefine stepDefine,
            WorkflowStep prevStep,
            BasicWorkflowDto dto,
            EFlowAssignType flowAssignType,
            DateTime? expiredTime,
            bool isStartCountersign,
            CancellationToken cancellationToken = default
        )
        {
            //var countersignId = dto.IsStartCountersign ? prevStep.StartCountersignId : prevStep.CountersignId;
            var countersignId = prevStep.StartCountersignId;

            var handlerType = stepDefine.CountersignPolicy switch
            {
                EDynamicPolicy.OrgUpCenterTop => EHandlerType.OrgLevel,
                EDynamicPolicy.OrgUp => EHandlerType.OrgLevel,
                EDynamicPolicy.OrgDownCenterTop => EHandlerType.OrgLevel,
                EDynamicPolicy.OrgDown => EHandlerType.OrgLevel,
                null => throw new ArgumentOutOfRangeException(),
                _ => throw new ArgumentOutOfRangeException()
            };

            var nextStepCountersignPosition = dto.NextHandlers.Count > 1
                ? ECountersignPosition.Multi
                : ECountersignPosition.Single;

            return CreateStepsAsync(workflow, stepDefine, prevStep, dto, flowAssignType, dto.NextHandlers,
                countersignId, EWorkflowStepStatus.WaitForAccept, nextStepCountersignPosition,
                false, EWorkflowTraceType.Normal, handlerType, expiredTime, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// 根据传入节点的上一节点创建会签汇总节点（汇总传入节点的前一节点）
        /// </summary>
        private async Task<List<WorkflowStep>> CreateCsEndStepsByTargetPrevAsync(Workflow workflow, WorkflowStep step,
            BasicWorkflowDto dto, DateTime? expiredTime, CancellationToken cancellationToken)
        {
            var countersignStartStep = workflow.Steps.FirstOrDefault(d => d.Id == step.PrevStepId);
            if (countersignStartStep is null)
                throw new UserFriendlyException("未查询到当前节点上级节点");
            var nextSteps = new List<WorkflowStep>();
            //会签未全部办理则不创建汇总节点

            var csInnerSteps = workflow.Steps.Where(d => d.PrevStepId == countersignStartStep.Id).ToList();
            if (csInnerSteps.Any(d => d.Status != EWorkflowStepStatus.Handled || (d.IsStartCountersign && !d.IsStartedCountersignEnd)))
                return nextSteps;
            //if (csInnerSteps.All(d => d.Status == EWorkflowStepStatus.Handled))
            //{
            // 创建会签汇总节点
            var countersignEndStep =
                await CreateCountersignEndStepAsync(countersignStartStep, dto, expiredTime, cancellationToken);
            nextSteps = new List<WorkflowStep> { countersignEndStep };

            //create trace
            await CreateTraceAsync(workflow, countersignEndStep, EWorkflowTraceType.Normal, cancellationToken);

            await _mediator.Publish(new CountersignEndAssigned(workflow), cancellationToken);
            //}

            return nextSteps;
        }

        private async Task<WorkflowStep> CreateCountersignEndStepAsync(WorkflowStep countersignStartStep,
            BasicWorkflowDto dto, DateTime? expiredTime,
            CancellationToken cancellationToken = default)
        {
            var csEndStep = _mapper.Map<WorkflowStep>(countersignStartStep);
            csEndStep.Status = EWorkflowStepStatus.WaitForAccept;
            csEndStep.PrevStepId = null;
            csEndStep.PrevStepCode = null;
            csEndStep.IsOrigin = false;
            csEndStep.CountersignId = countersignStartStep.StartCountersignId;
            csEndStep.CountersignPosition = ECountersignPosition.End;
            //csEndStep.CountersignSteps = new();
            csEndStep.IsCountersignEndStep = true;
            csEndStep.CountersignStartStepId = countersignStartStep.Id;
            csEndStep.Name = dto.NextStepName;
            //csEndStep.TimeLimit = GetTimeLimit("");
            csEndStep.StepExpiredTime = expiredTime;
            csEndStep.BusinessType = dto.BusinessType;
            csEndStep.Handlers = countersignStartStep.Handlers
                .Where(d => d.Key == countersignStartStep.HandlerId || d.Key == countersignStartStep.HandlerOrgId)
                .ToList();
            //csEndStep.StepHandlers = stepHandlers;

            csEndStep.Reset();
            csEndStep.ResetParameters();

            await _workflowStepRepository.AddAsync(csEndStep, cancellationToken);
            //await _workflowStepRepository.AddNav(csEndStep)
            //    .Include(d => d.StepHandlers)
            //    .ExecuteCommandAsync();
            return csEndStep;
        }


        private bool CheckIsActualHandle(Workflow workflow, WorkflowStep step, StepDefine nextStepDefine,
            BasicWorkflowDto dto)
        {
            //1. workflow是否为办理类型 2. 非会签：当前是否为普通节点and下一节点是否为汇总 or endStep 3. 会签：当前操作为汇总还是继续往下办理？thk: 汇总以后但未回到top又往下办理的场景,前面实际办理部门也算作办理部门
            if (workflow.FlowType is not EFlowType.Handle) return false;

            if (workflow.IsInCountersign)
            {
                return !step.IsCountersignEndStep && dto.BackToCountersignEnd;
            }
            else
            {
                return step.StepType is EStepType.Normal &&
                       nextStepDefine.StepType is EStepType.Summary or EStepType.End;
            }
        }

        /// <summary>
        /// 办理节点(赋值节点的办理对象信息)
        /// </summary>
        private void HandleStep(ISessionContext current, WorkflowStep step, string opinion, string nextStepCode)
        {
            step.Handle(current.RequiredUserId, current.UserName,
                 current.RequiredOrgId, current.OrgName,
                 current.OrgAreaCode, current.OrgAreaName,
                 current.OrgIsCenter, opinion, nextStepCode);

            //var handler = step.FindActualHandler(current.Roles, current.RequiredUserId, current.RequiredOrgId);
            //if (handler is not null)
            //    handler.IsActualHandler = true;
        }


        /// <summary>
        /// 开始会签（创建会签数据，更新currentStep会签数据）
        /// </summary>
        private async Task StartCountersignAsync(ISessionContext current, Workflow workflow, WorkflowStep startStep, BasicWorkflowDto dto,
            EFlowAssignType? flowAssignType, ECounterSignType? counterSignType, DateTime? expiredTime,
            CancellationToken cancellationToken)
        {
            var countersign = await CreateCountersignAsync(current, workflow, startStep,
                dto.NextHandlers.Select(d => new Kv(d.Key, d.Value)).ToList(), flowAssignType,
                counterSignType, expiredTime, startStep.CountersignId, cancellationToken);
            startStep.StartCountersign(countersign.Id);
        }

        /// <summary>
        /// 检查是否从中心流转至部门
        /// </summary>
        private bool CheckIfFlowFromCenterToOrg(WorkflowStep sourceStep, StepDefine targetStepBoxDefine)
        {
            var isFromCenter = sourceStep.IsCenter();
            if (!isFromCenter) return false;

            var isToOrg = targetStepBoxDefine.IsOrg();
            return isFromCenter && isToOrg;
        }

        /// <summary>
        /// 检查是否从中心流转至部门
        /// </summary>
        private bool CheckIfFlowFromCenterToOrg(Workflow workflow, WorkflowStep targetStepBox)
        {
            var isToOrg = targetStepBox.IsOrg();
            if (!isToOrg) return false;

            var isFromCenter = workflow.Steps.All(d => d.BusinessType is not EBusinessType.Department);
            return isFromCenter && isToOrg;
        }

        /// <summary>
        /// 检查是否从部门流转至中心
        /// </summary>
        private bool CheckIfFlowFromOrgToCenter(WorkflowStep sourceStepBox, StepDefine targetStepBoxDefine)
        {
            var isFromOrg = sourceStepBox.IsOrg();
            if (!isFromOrg) return false;

            var isToCenter = targetStepBoxDefine.IsCenter();
            return isFromOrg && isToCenter;
        }

        /// <summary>
        /// 检查是否从部门流转至中心
        /// </summary>
        private bool CheckIfFlowFromOrgToCenter(WorkflowStep sourceStepBox, WorkflowStep targetStep)
        {
            var isFromOrg = sourceStepBox.IsOrg();
            if (!isFromOrg) return false;

            var isToCenter = targetStep.IsCenter();
            return isFromOrg && isToCenter;
        }

        /// <summary>
        /// 检查是否从部门流转至中心
        /// </summary>
        private bool CheckIfFlowFromOrgToCenter(Workflow workflow, WorkflowStep targetStep)
        {
            var isToCenter = targetStep.IsCenter();
            if (!isToCenter) return false;

            var isFromOrg = workflow.Steps.Any(d => d.BusinessType is EBusinessType.Department);
            return isFromOrg && isToCenter;
        }

        /// <summary>
        /// 复制一个节点为待接办
        /// </summary>
        private async Task<WorkflowStep> DuplicateStepWithTraceAsync(Workflow workflow, WorkflowStep step,
            EWorkflowTraceType traceType, CancellationToken cancellationToken)
        {
            var newStep = _mapper.Map<WorkflowStep>(step);
            newStep.Reset();
            newStep.Status = EWorkflowStepStatus.WaitForAccept;
            newStep.PrevStepId = step.PrevStepId;
            newStep.IsMain = step.IsMain;
            newStep.IsOrigin = step.IsOrigin;
            //newStep.ParentId = step.ParentId;
            newStep.Handlers = step.Handlers;
            //newStep.StepHandlers = _mapper.Map<List<WorkflowStepHandler>>(step.StepHandlers);
            newStep.StartCountersignId = step.StartCountersignId;
            newStep.CountersignId = step.CountersignId;
            newStep.IsStartedCountersignEnd = step.IsStartedCountersignEnd;

            //退回场景：指派给原办理人，其余场景：按照原节点原始指派方式复制
            if (traceType is EWorkflowTraceType.Previous)
            {
                newStep.FlowAssignType = EFlowAssignType.User;
                newStep.Assign(step.HandlerId, step.HandlerName, step.HandlerOrgId, step.HandlerOrgName);
            }

            await _workflowStepRepository.AddAsync(newStep, cancellationToken);
            //await _workflowStepRepository.AddNav(newStep)
            //    .Include(d => d.StepHandlers)
            //    .ExecuteCommandAsync();



            await CreateTraceAsync(workflow, newStep, traceType, cancellationToken);

            return newStep;
        }

        private async Task<WorkflowCountersign> CreateCountersignAsync(
            ISessionContext current, Workflow workflow, WorkflowStep startStep, List<Kv> handlers,
            EFlowAssignType? flowAssignType, ECounterSignType? counterSignType, DateTime? expiredTime,
            string? parentId = null, CancellationToken cancellationToken = default)
        {
            var members = handlers.Select(d => new WorkflowCountersignMember
            {
                Key = d.Key,
                Value = d.Value,
                FlowAssignType = flowAssignType
            }).ToList();

            var countersign = new WorkflowCountersign
            {
                WorkflowId = workflow.Id,
                StartStepId = startStep.Id,
                StartStepCode = startStep.Code,
                StartStepBusiType = startStep.BusinessType,

                StarterId = current.RequiredUserId,
                StarterName = current.UserName ?? string.Empty,
                StarterOrgId = current.RequiredOrgId,
                StarterOrgName = current.OrgName,
                StarterOrgAreaCode = current.OrgAreaCode ?? string.Empty,
                StarterOrgAreaName = current.OrgAreaName ?? string.Empty,

                ParentId = parentId,
                Members = members,
                FlowAssignType = flowAssignType,
                CounterSignType = counterSignType,
                ExpiredTime = expiredTime,
                //ExternalId = workflow.ExternalId,
            };
            //await _workflowCountersignRepository.AddAsync(countersign, cancellationToken);

            await _workflowCountersignRepository.AddNav(countersign)
                .Include(d => d.Members)
                .ExecuteCommandAsync();
            return countersign;
        }

        //private async Task JumpTraceAsync(string workflowId, RecallDto dto, CancellationToken cancellationToken)
        //{
        //    //未办理的traces
        //    var uncompleteTraces =
        //        await _workflowTraceRepository.QueryAsync(d =>
        //            d.WorkflowId == workflowId && string.IsNullOrEmpty(d.HandlerId));
        //    foreach (var trace in uncompleteTraces)
        //    {
        //        HandleTrace(trace, dto.Opinion);
        //    }

        //    await _workflowTraceRepository.UpdateRangeAsync(uncompleteTraces, cancellationToken);
        //}

        private void HandleTrace(WorkflowTrace trace, string opinion, ISessionContext current)
        {
            trace.Handle(
                current.RequiredUserId, current.UserName,
                current.RequiredOrgId, current.OrgName,
                current.OrgAreaCode, current.OrgAreaName,
                current.OrgIsCenter, opinion);
        }

        private async Task RecallTraceAsync(string workflowId, string opinion, ISessionContext current, CancellationToken cancellationToken)
        {
            //未办理的traces
            var uncompleteTraces =
                await _workflowTraceRepository.QueryAsync(d =>
                    d.WorkflowId == workflowId && string.IsNullOrEmpty(d.HandlerId));

            if (uncompleteTraces.Any())
            {
                foreach (var trace in uncompleteTraces)
                {
                    HandleTrace(trace, opinion, current);
                }

                await _workflowTraceRepository.UpdateRangeAsync(uncompleteTraces, cancellationToken);
            }
        }

        private async Task<WorkflowTrace> PreviousTraceAsync(string workflowId, PreviousWorkflowDto dto,
            WorkflowStep step, ISessionContext current, CancellationToken cancellationToken)
        {
            var trace = await GetWorkflowTraceAsync(workflowId, step.Id, cancellationToken);
            _mapper.Map(dto, trace);
            HandleTrace(trace, dto.Opinion, current);
            await _workflowTraceRepository.UpdateAsync(trace, cancellationToken);
            return trace;
        }

        //private async Task EndTraceAsync(Workflow workflow, BasicWorkflowDto dto, WorkflowStep step, CancellationToken cancellationToken)
        //{
        //    var trace = _mapper.Map<WorkflowTrace>(step);
        //    trace.Status = EWorkflowTraceStatus.Normal;
        //    trace.ExpiredTime = workflow.ExpiredTime;
        //    trace.TimeLimit = workflow.TimeLimit;
        //    await _workflowTraceRepository.AddAsync(trace, cancellationToken);
        //}

        private async Task<WorkflowTrace> NextTraceAsync(Workflow workflow, BasicWorkflowDto dto, WorkflowStep step,
            CancellationToken cancellationToken)
        {
            var trace = await GetWorkflowTraceAsync(workflow.Id, step.Id, cancellationToken);
            _mapper.Map(dto, trace);
            _mapper.Map(step, trace);
            await _workflowTraceRepository.UpdateAsync(trace, cancellationToken);

            return trace;
        }

        private async Task AcceptTraceAsync(Workflow workflow, WorkflowStep step, CancellationToken cancellationToken)
        {
            var trace = await GetWorkflowTraceAsync(workflow.Id, step.Id, cancellationToken);
            _mapper.Map(step, trace);
            await _workflowTraceRepository.UpdateAsync(trace, cancellationToken);
        }

        private async Task CreateTraceAsync(Workflow workflow, WorkflowStep step,
            EWorkflowTraceType traceType = EWorkflowTraceType.Normal,
            CancellationToken cancellationToken = default)
        {
            var sendHandleTimes = 0;
            if (step.BusinessType == EBusinessType.Send)
            {
                var sendHandleCount = workflow.Traces.Count(d => d.StepType == EStepType.Normal &&
                                                                 d.BusinessType == EBusinessType.Send);
                sendHandleTimes = sendHandleCount + 1;
            }


            var trace = _mapper.Map<WorkflowTrace>(step);
            trace.TraceType = traceType;
            trace.SendHandleTimes = sendHandleTimes;

            if (step.IsInCountersign())
            {
                if (step.IsCountersignEndStep)
                {
                    var startTrace =
                        await GetWorkflowTraceAsync(workflow.Id, step.CountersignStartStepId, cancellationToken);
                    trace.ParentId = startTrace.ParentId;
                }
                else
                {
                    //if (step.CountersignPosition is ECountersignPosition.Multi)
                    //{
                    //    var prevTrace = await GetWorkflowTraceAsync(workflow.Id, step.PrevStepId, cancellationToken);
                    //    trace.ParentId = prevTrace.Id;
                    //}
                    //else if (step.CountersignPosition is ECountersignPosition.Single)
                    //{
                    //    var prevTrace = await GetWorkflowTraceAsync(workflow.Id, step.PrevStepId, cancellationToken);
                    //    trace.ParentId = prevTrace.ParentId;
                    //}
                    var prevTrace = await GetWorkflowTraceAsync(workflow.Id, step.PrevStepId, cancellationToken);
                    trace.ParentId = prevTrace.Id;
                }
            }

            await _workflowTraceRepository.AddAsync(trace, cancellationToken);
        }

        private async Task<WorkflowTrace> GetWorkflowTraceAsync(string workflowId, string stepId,
            CancellationToken cancellationToken)
        {
            var parentTrace = await _workflowTraceRepository.GetAsync(d =>
                d.WorkflowId == workflowId && d.StepId == stepId, cancellationToken);
            if (parentTrace == null)
                throw new UserFriendlyException($"未找到对应trace, workflowId: {workflowId}, stepId: {stepId}");
            return parentTrace;
        }

        private async Task<bool> RecallAsync(Workflow workflow, BasicWorkflowDto dto, FlowAssignInfo flowAssignInfo,
            StepDefine targetStepDefine, WorkflowStep targetStep, EWorkflowTraceType traceType,
            DateTime? expiredTime, CancellationToken cancellationToken)
        {
            var targetIsStartStep = targetStepDefine.StepType is EStepType.Start;

            //get targetStep's previous
            WorkflowStep? targetPrevStep = null;
            if (!targetIsStartStep)
            {
                targetPrevStep = workflow.Steps.FirstOrDefault(d => d.Id == targetStep.PrevStepId);
                if (targetPrevStep == null)
                    throw new UserFriendlyException($"{nameof(RecallAsync)}, 未找到目标节点的前一节点, flowId: {workflow.Id}");
            }

            //查询所有目标节点之后的节点,然后删掉（包括目标节点）
            var removeSteps = GetStepsBehindTargetStep(workflow.Steps, targetStep);
            if (removeSteps.Any())
            {
                await _workflowStepRepository.RemoveRangeAsync(removeSteps, cancellationToken);
                //await _workflowStepRepository.RemoveNav(removeSteps)
                //    .Include(d => d.StepHandlers)
                //    .ExecuteCommandAsync();
                workflow.Steps.RemoveAll(d => removeSteps.Contains(d));
            }

            workflow.EndCountersign();
            workflow.ResetOption();
            if (workflow.Status is EWorkflowStatus.Completed)
                workflow.SetStatusRunnable();

            var targetStepNew = targetIsStartStep
                ? await CreateStartStepAsync(workflow, targetStepDefine, dto,
                    dto.NextHandlers.First(), traceType, expiredTime, cancellationToken)
                : (await CreateStepsAsync(workflow, targetStepDefine, targetPrevStep, dto,
                    flowAssignInfo.FlowAssignType, dto.NextHandlers,
                    null, EWorkflowStepStatus.WaitForAccept, ECountersignPosition.None, true, traceType,
                    null, expiredTime, cancellationToken: cancellationToken)).First();

            //更新实际办理节点信息
            workflow.UpdateActualStepWhenAssign(targetStepNew, targetStep.HandlerOrgName, targetStep.HandlerOrgId);

            workflow.Assign(flowAssignInfo.FlowAssignType, flowAssignInfo.GetHandlerIds());
            workflow.ResetHandlers(flowAssignInfo.FlowAssignType, flowAssignInfo.HandlerObjects);

            //calc workflow expired time
            var isOrgToCenter = CheckIfFlowFromOrgToCenter(workflow, targetStep);

            return isOrgToCenter;
        }

        private List<WorkflowStep> GetStepsBehindTargetStep(List<WorkflowStep> steps, WorkflowStep targetStep)
        {
            var behindSteps = new List<WorkflowStep> { targetStep };
            if (!steps.Any()) return behindSteps;

            var nextSteps = targetStep.IsStartCountersign
                ? steps.Where(d => d.CountersignId == targetStep.StartCountersignId).ToList()
                : steps.Where(d => d.PrevStepId == targetStep.Id).ToList();

            //var nextSteps = steps.Where(d => d.PrevStepId == targetStep.Id).ToList();
            if (!nextSteps.Any())
                return behindSteps;
            foreach (var nextStep in nextSteps)
            {
                var leftSteps = steps.Except(behindSteps).ToList();
                behindSteps.AddRange(GetStepsBehindTargetStep(leftSteps, nextStep));
            }

            return behindSteps;
        }

        private static void CheckWhetherRunnable(EWorkflowStatus status)
        {
            if (status != EWorkflowStatus.Runnable)
                throw UserFriendlyException.SameMessage("当前流程状态不可继续流转");
        }

        private void ValidatePermission(Workflow workflow, string OrgId, string UserId, string[] roleIds)
        {
            if (!workflow.IsCanHandle(UserId, OrgId, roleIds))
                throw UserFriendlyException.SameMessage("无办理权限");
        }

        private async Task<WorkflowStep> CreateEndStepAsync(
            ISessionContext current,
            Workflow workflow,
            StepDefine endStepDefine,
            WorkflowStep prevStep,
            DateTime? expiredTime,
            CancellationToken cancellationToken)
        {
            if (workflow.Steps.Any(d => d.StepType == EStepType.End))
                throw UserFriendlyException.SameMessage("无法重复创建结束节点");

            var handler = new FlowStepHandler
            {
                Key = current.UserId,
                Value = current.UserName,
                UserId = current.RequiredUserId,
                Username = current.UserName,
                OrgId = current.OrgId,
                OrgName = current.OrgName,
            };

            var step = CreateStep(workflow, endStepDefine, prevStep, null, handler,
                 null, null, EWorkflowStepStatus.WaitForAccept,
                ECountersignPosition.None, expiredTime, endStepDefine.Name, true);

            //step.Accept(_sessionContext.RequiredUserId, _sessionContext.UserName,
            //    _sessionContext.RequiredOrgId, _sessionContext.OrgName,
            //    _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName);

            HandleStep(current, step, "流程归档", string.Empty);

            await _workflowStepRepository.AddAsync(step, cancellationToken);

            //end trace
            await CreateTraceAsync(workflow, step, cancellationToken: cancellationToken);

            return step;
        }

        public async Task<List<WorkflowStep>> CreateConfigStepsAsync(
            Workflow workflow,
            StepDefine stepDefine,
            WorkflowStep prevStep,
            BasicWorkflowDto dto,
            FlowAssignInfo flowAssignInfo,
            EWorkflowTraceType traceType,
            DateTime? expiredTime,
            CancellationToken cancellationToken)
        {
            //List<Kv> handlers;
            //if (stepDefine.HandlerType is EHandlerType.AssignedUser or EHandlerType.AssignedOrg)
            //{
            //    handlers = stepDefine.HandlerTypeItems;
            //}
            //else
            //{
            List<FlowStepHandler> handlers;
            if (stepDefine.HandlerType != EHandlerType.Role && !dto.NextHandlers.Any())
                throw new UserFriendlyException("未指定节点处理者");
            if (stepDefine.HandlerType == EHandlerType.Role && !dto.NextHandlers.Any())
            {
                var handler = stepDefine.HandlerTypeItems.First();
                handlers = new List<FlowStepHandler>
                {
                    new(){Key = handler.Key, Value = handler.Value, RoleId = handler.Key, RoleName = handler.Value}
                }; //flowAssignInfo.GetHandlers();
            }
            else
            {
                handlers = dto.NextHandlers;
            }
            //}

            return await CreateStepsAsync(workflow, stepDefine, prevStep, dto, /*dto.IsStartCountersign,*/
                flowAssignInfo.FlowAssignType, handlers, null,
                EWorkflowStepStatus.WaitForAccept, ECountersignPosition.None,
                true, traceType, null, expiredTime, cancellationToken);
        }

        private async Task<List<WorkflowStep>> CreateStepsAsync(
            Workflow workflow,
            StepDefine stepDefine,
            WorkflowStep prevStep,
            BasicWorkflowDto dto,
            //bool isStartCountersign,
            EFlowAssignType? flowAssignType,
            List<FlowStepHandler> handlers,
            string? countersignId,
            EWorkflowStepStatus stepStatus,
            ECountersignPosition csPosition,
            bool isOrigin,
            EWorkflowTraceType traceType,
            EHandlerType? handlerType = null,
            DateTime? expiredTime = null,
            CancellationToken cancellationToken = default
        )
        {
            List<WorkflowStep> steps = new();
            //if (isStartCountersign)
            //{

            foreach (var handler in handlers)
            {
                var isMain = handlers.Count == 1 || (handlers.Count > 1 && handler.Key == dto.NextMainHandler);
                var step = CreateStep(workflow, stepDefine, prevStep, flowAssignType,
                    handler, dto.NextStepCode, countersignId, stepStatus, csPosition, expiredTime,
                    dto.NextStepName, isOrigin, isMain, handlerType, dto.BusinessType);

                //var stepHandler = stepHandlers.First(d => d.GetHandler().Key == handler.Key);
                //step.StepHandlers = new List<WorkflowStepHandler> { stepHandler };

                steps.Add(step);
            }
            //}
            //else
            //{
            //    var step = CreateStep(workflow, stepDefine, prevStep, flowAssignType, handlers,
            //        stepHandlers, dto.NextStepCode, dto.NextMainHandler, countersignId, stepStatus,
            //        csPosition, expiredTime, dto.NextStepName, isOrigin, handlerType, dto.BusinessType);

            //    steps.Add(step);
            //}

            await _workflowStepRepository.AddRangeAsync(steps, cancellationToken);
            //await _workflowStepRepository.AddNav(steps)
            //    .Include(d => d.StepHandlers)
            //    .ExecuteCommandAsync();

            //create traces todo add range traces
            foreach (var step in steps)
            {
                await CreateTraceAsync(workflow, step, traceType, cancellationToken);
            }

            return steps;
        }

        /// <summary>
        /// 查询未完成节点
        /// </summary>
        public WorkflowStep GetUnHandleStep(List<WorkflowStep> steps, string orgId, string userId, string[] roleIds)
        {
            //var step = GetStep(steps, orgCode, userId, d => d != EWorkflowStepStatus.Handled);
            var step = steps.FirstOrDefault(d => d.IsCanHandle(userId, orgId, roleIds));
            if (step == null)
                throw new UserFriendlyException(
                    $"未找到对应节点, workflowId: {steps.FirstOrDefault()?.WorkflowId} orgCode:{orgId}, userId: {userId}, roleIds: {string.Join(',', roleIds)}",
                    "未找到对应节点");
            return step;
        }

        /// <summary>
        /// 检查当前办理节点是否为开始节点
        /// </summary>
        /// <param name="workflowId"></param>
        /// <param name="userId">当前办理人Id</param>
        /// <param name="orgId">当前办理人orgId</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<bool> CheckCurrentIsStartStepAsync(string workflowId, string userId, string orgId,
            CancellationToken cancellationToken)
        {
            var workflow = await GetWorkflowAsync(workflowId, withSteps: true, cancellationToken: cancellationToken);
            var currentStep = GetStep(workflow.Steps, orgId, userId, d => d != EWorkflowStepStatus.Handled);
            if (currentStep is null) return false;
            return workflow.Steps.Count == 1 && currentStep.StepType is EStepType.Start && currentStep.IsOrigin;
        }

        /// <summary>
        /// 检查动态节点是否该终止
        /// </summary>
        public bool DynamicShouldTerminal(StepDefine currentStepDefine, int currentOrgLevel)
        {
            if (currentStepDefine.InstanceMode is not EInstanceMode.Dynamic)
                throw new UserFriendlyException("非动态节点");
            switch (currentStepDefine.InstancePolicy)
            {
                case EDynamicPolicy.OrgUpCenterTop:
                case EDynamicPolicy.OrgUp:
                    if (!int.TryParse(currentStepDefine.TerminalDynamicMark, out var tMark))
                        throw new UserFriendlyException(
                            $"TerminalDynamicMark parse to int failed, tMark: {currentStepDefine.TerminalDynamicMark}");
                    return currentOrgLevel <= tMark;
                case EDynamicPolicy.OrgDownCenterTop:
                case EDynamicPolicy.OrgDown:
                    if (!int.TryParse(currentStepDefine.TerminalDynamicMark, out var tMark1))
                        throw new UserFriendlyException(
                            $"TerminalDynamicMark parse to int failed, tMark: {currentStepDefine.TerminalDynamicMark}");
                    return currentOrgLevel >= tMark1;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        /// <summary>
        /// 终止会签
        /// </summary>
        /// <param name="countersignId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Workflow> TerminalCountersignAsync(string countersignId, CancellationToken cancellationToken)
        {
            var countersign = await _workflowCountersignRepository.GetAsync(countersignId, cancellationToken);
            if (countersign is null)
                throw new UserFriendlyException("无效会签编号");

            //1. 检查会签是否已结束 t: return 2.检查是否有嵌套会签 t: 一起结束 3.结束会签 4.trace 5.检查workflow会签状态，如果会签全结束需更新状态 6.cp会签发起节点变为待办节点
            if (countersign.IsCompleted())
                throw new UserFriendlyException("该会签已结束");

            var workflow = await GetWorkflowAsync(countersign.WorkflowId, withSteps: true, withTraces: true,
                withCountersigns: true, cancellationToken: cancellationToken);
            if (!workflow.IsInCountersign)
                throw new UserFriendlyException("该流程未处于会签中");
            countersign = workflow.Countersigns.First(d => d.Id == countersignId);

            var startCountersignStep = workflow.Steps.Find(d => d.StartCountersignId == countersignId);
            if (startCountersignStep is null)
                throw new UserFriendlyException("未查询到发起会签节点");
            if (startCountersignStep.IsStartedCountersignEnd)
                throw new UserFriendlyException("该会签已汇总");

            var updateCountersigns = new List<WorkflowCountersign>();
            EndCountersignWithCascade(countersign, workflow.Countersigns, startCountersignStep.BusinessType,
                ref updateCountersigns);

            if (updateCountersigns.Any())
            {
                var updateSteps = new List<WorkflowStep>();
                var updateTraces = new List<WorkflowTrace>();

                HandleStepsByTerminalCs(startCountersignStep, workflow.Steps, workflow.Traces, ref updateSteps, ref updateTraces);
                if (updateSteps.Any())
                    await _workflowStepRepository.RemoveRangeAsync(updateSteps, cancellationToken);
                //await _workflowStepRepository.RemoveNav(updateSteps)
                //    .Include(d => d.StepHandlers)
                //    .ExecuteCommandAsync();
                if (updateTraces.Any())
                    await _workflowTraceRepository.UpdateRangeAsync(updateTraces, cancellationToken);

                await _workflowCountersignRepository.UpdateRangeAsync(updateCountersigns, cancellationToken);

                //cp会签发起节点变为待办节点
                //1. create terminal trace 2. 撤回至startStep
                var newStep = await DuplicateStepWithTraceAsync(workflow, startCountersignStep, EWorkflowTraceType.Normal,
                    cancellationToken);

                //当topcsStep结束cs时，实际办理节点应该更新为newStep
                if (startCountersignStep.Id == workflow.TopCountersignStepId)
                    workflow.UpdateActualStepWhenAssign(newStep, startCountersignStep.HandlerOrgName,
                        startCountersignStep.HandlerOrgId);

                //csEndStep又开启了cs，在结束会签时，如果该节点是topcs的end节点， workflow.topcsStep应该更新为前一cs开启stepId
                if (startCountersignStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
                    workflow.TopCountersignStepId = startCountersignStep.CountersignStartStepId;

                if (workflow.CheckIfCountersignOver())
                    workflow.EndCountersign();

                var removeHandlers = updateSteps.SelectMany(d => d.Handlers).Select(d => d.Key).ToList();

                var handlerObjs = newStep.Handlers.Select(d => new HandlerGroupItem
                {
                    GroupId = Guid.NewGuid().ToString(),
                    Key = d.Key,
                    Value = d.Value
                }).ToList();
                workflow.UpdateHandlers(removeHandlers, newStep.FlowAssignType.Value, handlerObjs);

                await _workflowRepository.UpdateAsync(workflow, cancellationToken);
            }

            return workflow;
        }

        private void HandleStepsByTerminalCs(WorkflowStep step, List<WorkflowStep> steps, List<WorkflowTrace> traces,
            ref List<WorkflowStep> updateSteps, ref List<WorkflowTrace> updateTraces)
        {
            if (step.IsStartCountersign)
            {
                var countersignSteps = steps.Where(d => d.CountersignId == step.StartCountersignId).ToList();
                if (countersignSteps.Any())
                {
                    foreach (var countersignStep in countersignSteps)
                    {
                        HandleStepsByTerminalCs(countersignStep, steps, traces, ref updateSteps, ref updateTraces);
                    }
                }
            }

            EndStepByTerminalCs(step, traces, ref updateSteps, ref updateTraces);
        }

        private void EndStepByTerminalCs(WorkflowStep step, List<WorkflowTrace> traces,
            ref List<WorkflowStep> updateSteps, ref List<WorkflowTrace> updateTraces)
        {
            var isHandled = step.Status is EWorkflowStepStatus.Handled;
            var opinion = $"会签未办理完成，由 {_sessionContext.OrgName} 的 {_sessionContext.UserName} 终止办理";
            if (step.IsStartCountersign)
                step.CountersignEnd();

            if (step.Status is not EWorkflowStepStatus.Handled)
            {
                step.Handle(_sessionContext.RequiredUserId, _sessionContext.UserName,
                    _sessionContext.RequiredOrgId, _sessionContext.OrgName,
                    _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName,
                    _sessionContext.OrgIsCenter, opinion);
            }

            updateSteps.Add(step);

            if (isHandled) return;

            var trace = traces.FirstOrDefault(d => d.StepId == step.Id);
            if (trace != null)
            {
                _mapper.Map(step, trace);
                updateTraces.Add(trace);
            }
        }

        /// <summary>
        /// 结束会签（包含子项）
        /// </summary>
        /// <param name="countersign"></param>
        /// <param name="updateCountersigns"></param>
        private void EndCountersignWithCascade(WorkflowCountersign countersign, List<WorkflowCountersign> countersigns,
            EBusinessType businessType, ref List<WorkflowCountersign> updateCountersigns)
        {
            if (countersign is null) return;

            var childCountersigns = countersigns.Where(d => d.ParentId == countersign.Id).ToList();
            if (childCountersigns.Any())
            {
                foreach (var childCountersign in childCountersigns)
                {
                    EndCountersignWithCascade(childCountersign, countersigns, businessType, ref updateCountersigns);
                }
            }

            EndCountersign(countersign, countersigns, businessType, ref updateCountersigns);
        }

        private void EndCountersign(WorkflowCountersign countersign, List<WorkflowCountersign> countersigns,
            EBusinessType businessType, ref List<WorkflowCountersign> updateCountersigns)
        {
            //todo 1. trace? 先确定展现形式 2. end cs

            countersign.End(null, null, businessType,
                _sessionContext.RequiredUserId, _sessionContext.UserName,
                _sessionContext.RequiredOrgId, _sessionContext.OrgName,
                _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName);

            /*
            * //结束step会签信息
                   countersignStartStep.CountersignEnd();
                   updateSteps.Add(countersignStartStep);

                   //结束会签
                   currentCountersign.End(currentStep.Id, currentStep.Code, currentStep.BusinessType,
                       _sessionContext.RequiredUserId, _sessionContext.UserName,
                       _sessionContext.RequiredOrgId, _sessionContext.OrgName,
                       _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName);
                   await _workflowCountersignRepository.UpdateAsync(currentCountersign, cancellationToken);
            */

            updateCountersigns.Add(countersign);
        }

        private WorkflowStep? GetStep(List<WorkflowStep> steps, string orgCode, string userId,
            Func<EWorkflowStepStatus, bool> predicate) =>
            steps.FirstOrDefault(d =>
                predicate(d.Status) && d.Handlers.Any(x => x.Key == orgCode || x.Key == userId));

        private WorkflowStep CreateStep(
            Workflow workflow,
            StepDefine stepDefine,
            WorkflowStep prevStep,
            EFlowAssignType? flowAssignType,
            FlowStepHandler handler,
            string nextStepCode,
            string? countersignId,
            EWorkflowStepStatus stepStatus,
            ECountersignPosition countersignPosition,
            DateTime? expiredTime,
            string stepName,
            bool isOrigin,
            bool isMainHandler = false,
            EHandlerType? handlerType = null, //动态节点依据动态策略判断
            EBusinessType? businessType = null
        )
        {
            //if (!handlers.Any())
            //    throw new UserFriendlyException($"非法参数, handlers为空, method: {nameof(CreateStep)}");
            var step = _mapper.Map<WorkflowStep>(stepDefine);
            _mapper.Map(workflow, step);
            step.FlowAssignType = flowAssignType;
            step.Handlers = new List<Kv> { new(handler.Key, handler.Value) };
            //step.StepHandlers = stepHandlers;
            step.NextStepCode = step.StepType is EStepType.End ? string.Empty : nextStepCode;
            step.IsMain = isMainHandler;
            step.PrevStepId = prevStep.Id;
            step.PrevStepCode = prevStep.Code;
            step.CountersignId = countersignId;
            step.Status = stepStatus;
            step.CountersignPosition = countersignPosition;
            step.StepExpiredTime = expiredTime;
            //step.TimeLimit = GetTimeLimit("");
            step.IsOrigin = isOrigin;
            step.Name = stepName;

            step.Assign(handler.UserId, handler.Username,
                handler.OrgId, handler.OrgName,
                handler.RoleId, handler.RoleName);

            if (handlerType.HasValue)
                step.HandlerType = handlerType.Value;
            if (businessType.HasValue)
                step.BusinessType = businessType.Value;

            return step;
        }

        ///// <summary>
        ///// 依据配置生成过期时间
        ///// </summary>
        ///// <returns></returns>
        //private DateTime CalculateExpiredTime(string defineCode, DateTime? time = null)
        //{
        //    time ??= DateTime.Now;
        //    var config = GetConfig(defineCode);
        //    return time.Value.AddDays(config.Days);
        //}

        //private string GetTimeLimit(string defineCode)
        //{
        //    return GetConfig(defineCode).Description;
        //}

        //private ConfigIncludeDescriptionAndTime GetConfig(string defineCode)
        //{
        //    return new ConfigIncludeDescriptionAndTime
        //    {
        //        Days = 7,
        //        Description = "7个工作日"//todo 依据配置生成, Think about 工作日
        //    };
        //}

        #endregion
    }

    public class ConfigIncludeDescriptionAndTime
    {
        public int Days { get; set; }
        public string Description { get; set; }
    }
}