using Hotline.Caching.Interfaces;
using Hotline.FlowEngine;
using Hotline.FlowEngine.Definitions;
using Hotline.FlowEngine.WorkflowModules;
using Hotline.FlowEngine.Workflows;
using Hotline.Identity.Roles;
using Hotline.Orders;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.FlowEngine;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Enums.Identity;
using Hotline.Users;
using MapsterMapper;
using Hotline.File;
using Hotline.Share.Enums.Settings;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Extensions;
using XF.Domain.Repository;
using XF.Utility.EnumExtensions;
using Hotline.Share.Dtos.File;
using Microsoft.Extensions.Logging;
using Hotline.Configurations;
using Hotline.Share.Dtos.Order.Handle;
using Newtonsoft.Json;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using SqlSugar;
using Hotline.SeedData;
using Hotline.Validators.FlowEngine;

namespace Hotline.Application.FlowEngine;

public class WorkflowApplication : IWorkflowApplication, IScopeDependency
{
    private readonly IWorkflowDomainService _workflowDomainService;
    private IRepository<WorkflowStep> _workflowStepRepository;
    private IRepository<WorkflowTrace> _workflowTraceRepository;
    private readonly IRepository<WorkflowCountersign> _workflowCountersignRepository;
    private readonly IRepository<User> _userRepository;
    private readonly ISystemOrganizeRepository _organizeRepository;
    private readonly IRepository<Role> _roleRepository;
    private readonly IWfModuleCacheManager _wfModuleCacheManager;
    private readonly ISessionContextProvider _sessionContextProvider;
    private readonly ISessionContext _sessionContext;
    private readonly IMapper _mapper;
    private readonly IFileRepository _fileRepository;
    private readonly ILogger<WorkflowApplication> _logger;
    private readonly ISystemSettingCacheManager _systemSettingCacheManager;

    public WorkflowApplication(
        IWorkflowDomainService workflowDomainService,
        IRepository<WorkflowStep> workflowStepRepository,
        IRepository<WorkflowTrace> workflowTraceRepository,
        IRepository<WorkflowCountersign> workflowCountersignRepository,
        IRepository<User> userRepository,
        ISystemOrganizeRepository organizeRepository,
        IRepository<Role> roleRepository,
        IWfModuleCacheManager wfModuleCacheManager,
        ISessionContextProvider sessionContextProvider,
        ISessionContext sessionContext,
        IMapper mapper,
        IFileRepository fileRepository,
        ISystemSettingCacheManager systemSettingCacheManager,
        ILogger<WorkflowApplication> logger)
    {
        _workflowDomainService = workflowDomainService;
        _workflowStepRepository = workflowStepRepository;
        _workflowTraceRepository = workflowTraceRepository;
        _workflowCountersignRepository = workflowCountersignRepository;
        _userRepository = userRepository;
        _organizeRepository = organizeRepository;
        _roleRepository = roleRepository;
        _wfModuleCacheManager = wfModuleCacheManager;
        _sessionContextProvider = sessionContextProvider;
        _sessionContext = sessionContext;
        _mapper = mapper;
        _fileRepository = fileRepository;
        _logger = logger;
        _systemSettingCacheManager = systemSettingCacheManager;
    }

    // public async Task<string> StartWorkflowAsync(StartWorkflowDto dto, string externalId,
    //     DateTime? expiredTime, CancellationToken cancellationToken = default)
    // {
    //     //var validator = new StartWorkflowDtoValidator();
    //     //var validResult = await validator.ValidateAsync(dto, cancellationToken);
    //     //if (!validResult.IsValid)
    //     //    throw new UserFriendlyException(
    //     //        $"非法参数, {string.Join(',', validResult.Errors.Select(d => d.ExamErrorMessage))}");
    //
    //     var wfModule = await GetWorkflowModuleAsync(dto.DefinitionModuleCode, cancellationToken);
    //     var definition = wfModule.Definition;
    //     if (definition == null)
    //         throw new UserFriendlyException("无效模板编码");
    //     if (definition.Status is not EDefinitionStatus.Enable)
    //         throw new UserFriendlyException("该模板不可用");
    //
    //     //如果发起会签需检查是否支持发起会签
    //     var startStepDefine = definition.FindStartStepDefine();
    //
    //     //下一节点是否为动态节点
    //     var isNextDynamic = startStepDefine.InstanceMode is EInstanceMode.Dynamic &&
    //                         !_workflowDomainService.DynamicShouldTerminal(startStepDefine, _sessionContext.OrgLevel);
    //     var firstStepDefine = isNextDynamic
    //         ? startStepDefine
    //         : definition.FindStepDefine(dto.NextStepCode);
    //     if (firstStepDefine is null)
    //         throw new UserFriendlyException("未查询到下一步节点配置");
    //
    //     //1. 如果不是按角色指派，handlers必填 2. 如果按角色指派，handlers可以不选
    //     if (firstStepDefine.HandlerType is not EHandlerType.Role && !dto.NextHandlers.Any())
    //         throw UserFriendlyException.SameMessage("未指派办理人");
    //
    //     if (dto.IsStartCountersign)
    //     {
    //         if (!startStepDefine.CanStartCountersign)
    //             throw new UserFriendlyException("当前节点不支持发起会签");
    //         //if (startStepDefine.HandlerType is EHandlerType.Role)
    //         //    throw new UserFriendlyException("当前节点不支持发起会签");
    //         //即使当前节点支持发起会签，但下一节点为信息汇总节点、结束节点时也不可发起会签
    //         if (firstStepDefine.StepType is EStepType.Summary or EStepType.End)
    //             throw new UserFriendlyException("下一节点不允许发起会签");
    //         //下一节点是会签汇总节点也不允许发起会签
    //         if (dto.BackToCountersignEnd)
    //             throw new UserFriendlyException("下一节点不允许发起会签");
    //     }
    //
    //     var workflow = await _workflowDomainService.CreateWorkflowAsync(wfModule, dto.Title,
    //         _sessionContext.RequiredUserId, _sessionContext.RequiredOrgId,
    //         externalId, cancellationToken);
    //
    //     //var startStepHandles = new List<WorkflowStepHandler>{WorkflowStepHandler.Create(workflow.Id, workflow.ExternalId,
    //     //    EFlowAssignType.User, current.RequiredUserId, current.UserName,
    //     //    current.RequiredOrgId, current.OrgName)};
    //
    //     var defineHandler = startStepDefine.HandlerTypeItems.First();
    //
    //     var assigner = new UserInfo(
    //         _sessionContext.UserId,
    //         _sessionContext.UserName,
    //         _sessionContext.OrgId,
    //         _sessionContext.OrgName,
    //         _sessionContext.OrgIsCenter
    //     );
    //
    //     var startStep = _workflowDomainService.CreateStartStep(workflow, startStepDefine, dto, assigner,
    //         new FlowStepHandler
    //         {
    //             Key = _sessionContext.RequiredUserId,
    //             Value = _sessionContext.UserName,
    //             UserId = _sessionContext.UserId,
    //             Username = _sessionContext.UserName,
    //             OrgId = _sessionContext.RequiredOrgId,
    //             OrgName = _sessionContext.OrgName,
    //             RoleId = defineHandler.Key,
    //             RoleName = defineHandler.Value,
    //         }, expiredTime);
    //
    //     var flowAssignInfo =
    //         await GetNextStepFlowAssignInfoAsync(workflow, startStep, dto, firstStepDefine, isNextDynamic, cancellationToken);
    //
    //     var counterSignType = _workflowDomainService.GetCounterSignType(dto.IsStartCountersign, startStep.BusinessType);
    //
    //     //办理开始节点
    //     await _workflowDomainService.HandleStepAsync(startStep, workflow, dto, counterSignType, expiredTime, EHandleMode.Normal, cancellationToken);
    //
    //     if (dto.Files.Any())
    //         startStep.FileJson =
    //             await _fileRepository.AddFileAsync(dto.Files, workflow.ExternalId, startStep.Id, cancellationToken);
    //
    //     await _workflowStepRepository.AddAsync(startStep, cancellationToken);
    //     workflow.Steps.Add(startStep);
    //
    //     //starttrace
    //     var startTrace = _mapper.Map<WorkflowTrace>(startStep);
    //     startTrace.StepId = startStep.Id;
    //     startTrace.TraceType = EWorkflowTraceType.Normal;
    //     //_mapper.Map(dto, startTrace);
    //     await _workflowTraceRepository.AddAsync(startTrace, cancellationToken);
    //     workflow.Traces.Add(startTrace);
    //     startStep.WorkflowTrace = startTrace;
    //
    //     //更新受理人信息
    //     workflow.UpdateAcceptor(
    //         _sessionContext.RequiredUserId,
    //         _sessionContext.UserName,
    //         _sessionContext.StaffNo,
    //         _sessionContext.RequiredOrgId,
    //         _sessionContext.OrgName);
    //
    //     await _workflowDomainService.StartAsync(workflow, startStep, dto, firstStepDefine, isNextDynamic,
    //         flowAssignInfo, counterSignType, expiredTime, cancellationToken);
    //
    //     return workflow.Id;
    // }

    /// <summary>
    /// 开始流程并停留在开始节点（开始节点作为待办节点）
    /// </summary>
    public async Task<string> StartWorkflowToStartStepAsync(StartWorkflowDto dto, string externalId, DateTime? expiredTime = null,
        CancellationToken cancellationToken = default)
    {
        var validator = new StartWorkflowDtoValidator();
        var validResult = validator.Validate(dto);
        if (!validResult.IsValid)
            throw new UserFriendlyException(
                $"非法参数, {string.Join(',', validResult.Errors.Select(d => d.ErrorMessage))}");

        var wfModule = await GetWorkflowModuleAsync(dto.DefinitionModuleCode, cancellationToken);
        var definition = wfModule.Definition;
        if (definition == null)
            throw new UserFriendlyException("无效模板编码");
        if (definition.Status is not EDefinitionStatus.Enable)
            throw new UserFriendlyException("该模板不可用");

        //如果发起会签需检查是否支持发起会签
        var startStepDefine = definition.FindStartStepDefine();

        var workflow = await _workflowDomainService.CreateWorkflowAsync(wfModule, dto.Title,
            _sessionContext.RequiredUserId, _sessionContext.RequiredOrgId,
            externalId, cancellationToken);

        var defineHandler = startStepDefine.HandlerTypeItems.First();

        var assigner = new UserInfo(
            _sessionContext.UserId,
            _sessionContext.UserName,
            _sessionContext.OrgId,
            _sessionContext.OrgName,
            _sessionContext.OrgIsCenter
        );

        //todo 需求：所有坐席都可以办，临时方案，后期重构：依据策略决定指派对象
        var startStep = _workflowDomainService.CreateStartStep(workflow, startStepDefine, dto, assigner,
            new StepAssignInfo
            {
                Key = _sessionContext.RequiredUserId,
                Value = _sessionContext.UserName,
                UserId = _sessionContext.UserId,
                Username = _sessionContext.UserName,
                OrgId = _sessionContext.RequiredOrgId,
                OrgName = _sessionContext.OrgName,
                RoleId = defineHandler.Key,
                RoleName = defineHandler.Value,
                FlowAssignType = EFlowAssignType.Role,
            }, expiredTime);

        if (dto.Files.Any())
            startStep.FileJson = await _fileRepository.AddFileAsync(dto.Files, workflow.ExternalId, startStep.Id, cancellationToken);

        await _workflowStepRepository.AddAsync(startStep, cancellationToken);
        workflow.Steps.Add(startStep);

        //starttrace
        var startTrace = _mapper.Map<WorkflowTrace>(startStep);
        startTrace.StepId = startStep.Id;
        startTrace.TraceType = EWorkflowTraceType.Normal;
        //_mapper.Map(dto, startTrace);
        await _workflowTraceRepository.AddAsync(startTrace, cancellationToken);
        workflow.Traces.Add(startTrace);
        startStep.WorkflowTrace = startTrace;

        //更新受理人信息
        workflow.UpdateAcceptor(
            _sessionContext.RequiredUserId,
            _sessionContext.UserName,
            _sessionContext.StaffNo,
            _sessionContext.RequiredOrgId,
            _sessionContext.OrgName);

        return workflow.Id;
    }

    ///// <summary>
    ///// 流转至下一节点（节点办理）
    ///// </summary>
    //public async Task<Workflow> NextAsync(NextWorkflowDto dto, DateTime? expiredTime = null, CancellationToken cancellationToken = default)
    //{
    //    var validator = new NextWorkflowDtoValidator();
    //    var validResult = await validator.ValidateAsync(dto, cancellationToken);
    //    if (!validResult.IsValid)
    //        throw new UserFriendlyException(
    //            $"非法参数, {string.Join(',', validResult.Errors.Select(d => d.ExamErrorMessage))}");
    //    var workflow = await _workflowDomainService.GetWorkflowAsync(dto.WorkflowId, withDefine: true, withSteps: true,
    //        withTraces: true, withCountersigns: true, cancellationToken: cancellationToken);

    //    //var currentStep = _workflowDomainService.FindCurrentStepWaitForHandle(workflow,
    //    //    current.RequiredUserId, current.RequiredOrgId, current.Roles);
    //    var currentStep = workflow.Steps.FirstOrDefault(d => d.Id == dto.StepId);
    //    if (currentStep == null)
    //        throw new UserFriendlyException(
    //            $"未找到对应节点, workflowId: {dto.WorkflowId}, stepId: {dto.StepId}", "未找到对应节点");
    //    if (currentStep.Status is EWorkflowStepStatus.Handled)
    //        throw new UserFriendlyException("该状态不支持继续办理");

    //    var currentStepDefine = _workflowDomainService.GetStepDefine(workflow.WorkflowDefinition, currentStep.Code);

    //    //下一节点是否为动态节点
    //    var isNextDynamic = currentStepDefine.InstanceMode is EInstanceMode.Dynamic &&
    //                        !_workflowDomainService.DynamicShouldTerminal(currentStepDefine, _sessionContext.OrgLevel);

    //    StepDefine nextStepDefine;
    //    if (isNextDynamic
    //        || (currentStep.IsInCountersign() && !currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
    //        || dto.IsStartCountersign)
    //    {
    //        //下一步配置为当前节点配置
    //        nextStepDefine = currentStepDefine;
    //    }
    //    else
    //    {
    //        //下一步配置为下一步节点配置
    //        nextStepDefine = _workflowDomainService.GetStepDefine(workflow.WorkflowDefinition, dto.NextStepCode);
    //    }

    //    //需求：按角色选择办理人可以不选，表示该角色下所有人都可以办理，同时依据配置：是否本部门人办理显示待选办理人。角色下只要一人办理即可（即:角色下不发起会签）
    //    if (nextStepDefine.HandlerType != EHandlerType.Role && !dto.NextHandlers.Any())
    //        throw new UserFriendlyException("未指定节点处理者");

    //    if (dto.IsStartCountersign)
    //    {
    //        if (!currentStepDefine.CanStartCountersign)
    //            throw new UserFriendlyException("当前节点不支持发起会签");
    //        //if (currentStepDefine.HandlerType is EHandlerType.Role)
    //        //    throw new UserFriendlyException("当前节点不支持发起会签");
    //        //即使当前节点支持发起会签，但下一节点为信息汇总节点、结束节点时也不可发起会签
    //        if (nextStepDefine.StepType is EStepType.Summary or EStepType.End)
    //            throw new UserFriendlyException("下一节点不允许发起会签");
    //        //下一节点是会签汇总节点也不允许发起会签
    //        if (dto.BackToCountersignEnd)
    //            throw new UserFriendlyException("下一节点不允许发起会签");
    //    }

    //    var flowAssignInfo =
    //        await GetNextStepFlowAssignInfoAsync(workflow, currentStep, dto, nextStepDefine, isNextDynamic, cancellationToken);

    //    //var nextStepHandlers = await GetNextStepHandlersAsync(workflow, nextStepDefine, dto, cancellationToken);

    //    await _workflowDomainService.NextAsync(workflow, currentStep, dto, nextStepDefine, isNextDynamic,
    //        flowAssignInfo, expiredTime, cancellationToken);

    //    return workflow;
    //}


    ///// <summary>
    ///// 退回（返回前一节点）
    ///// </summary>
    //public async Task<(Workflow workflow, WorkflowStep currentStep, StepDefine prevDefine,
    //        WorkflowStep prevStep, WorkflowStep newStep, EFlowDirection flowDirection)>
    //    PreviousAsync(PreviousWorkflowDto dto,
    //        EHandleMode handleMode = EHandleMode.Previous,
    //        Action<Workflow, WorkflowStep, StepDefine, WorkflowStep, WorkflowStep>? newStepConfig = null,
    //        CancellationToken cancellationToken = default)
    //{
    //    var workflow = await _workflowDomainService.GetWorkflowAsync(dto.WorkflowId, withDefine: true, withSteps: true,
    //        withTraces: true, withCountersigns: true, cancellationToken: cancellationToken);

    //    return await _workflowDomainService.PreviousAsync(workflow, dto, new OperatorInfo(
    //        _sessionContext.RequiredUserId, _sessionContext.UserName,
    //        _sessionContext.RequiredOrgId, _sessionContext.OrgName,
    //        _sessionContext.OrgAreaCode, _sessionContext.OrgAreaName,
    //        _sessionContext.OrgIsCenter, _sessionContext.Roles,
    //        _sessionContext.OrgLevel), handleMode, newStepConfig, cancellationToken);
    //}

    /// <summary>
    /// 工单退回（返回前一节点）
    /// </summary>
    public async Task<(Workflow workflow, WorkflowStep currentStep, StepDefine prevDefine,
            WorkflowStep prevStep, WorkflowStep newStep, EFlowDirection flowDirection)>
        PreviousAsync(PreviousWorkflowDto dto, string applicantId, string applicantOrgId, string[] applicantRoleIds,
            EHandleMode handleMode = EHandleMode.Previous,
            Action<Workflow, WorkflowStep, StepDefine, WorkflowStep, WorkflowStep>? newStepConfig = null,
            CancellationToken cancellationToken = default)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(dto.WorkflowId, withDefine: true, withSteps: true,
            withTraces: true, withCountersigns: true, cancellationToken: cancellationToken);
        var user = await _userRepository.Queryable()
            .Includes(x => x.Organization)
            .FirstAsync(x => x.Id == applicantId, cancellationToken);

        return await _workflowDomainService.PreviousAsync(workflow, dto, new OperatorInfo(
            applicantId, user.Name,
            applicantOrgId, user.Organization.Name,
            user.Organization.AreaCode, user.Organization.AreaName,
            user.Organization.IsCenter, applicantRoleIds,
            user.Organization.Level), handleMode, newStepConfig, cancellationToken);
    }

    ///// <summary>
    ///// 撤回至之前任意节点
    ///// </summary>
    //public async Task RecallAsync(RecallDto dto, FlowAssignInfo flowAssignInfo, DateTime? expiredTime, bool isOrderFiled, EWorkflowTraceType traceType,
    //    CancellationToken cancellationToken)
    //{
    //    var validator = new RecallDtoValidator();
    //    var validationResult = await validator.ValidateAsync(dto, cancellationToken);
    //    if (!validationResult.IsValid)
    //        throw new UserFriendlyException(string.Join(',', validationResult.Errors));

    //    var workflow = await _workflowDomainService.GetWorkflowAsync(dto.WorkflowId, withDefine: true, withSteps: true,
    //        withTraces: true, withCountersigns: true, cancellationToken: cancellationToken);

    //    //await _orderDomainService.ReadyToRecallAsync(workflow.ExternalId, cancellationToken);

    //    var targetStepDefine = _workflowDomainService.GetStepDefine(workflow.WorkflowDefinition, dto.NextStepCode);
    //    if (targetStepDefine.StepType is EStepType.End)
    //        throw UserFriendlyException.SameMessage("结束节点不支持撤回");
    //    //var isStartCountersign = targetStepDefine.CouldPrevStartCountersign(dto.NextHandlers.Count);
    //    var targetStep = workflow.Steps.FirstOrDefault(d => d.Code == dto.NextStepCode && d.IsOrigin);
    //    if (targetStep is null)
    //        throw UserFriendlyException.SameMessage("该流程尚未流转至该节点");

    //    //退回到派单组 没有下一步办理人 获取之前节点办理人
    //    if (!dto.NextHandlers.Any())
    //    {
    //        dto.NextHandlers.Add(new FlowStepHandler()
    //        {
    //            UserId = targetStep.HandlerId,
    //            Username = targetStep.HandlerName,
    //            OrgId = targetStep.HandlerOrgId,
    //            OrgName = targetStep.HandlerOrgName,
    //            Key = targetStep.HandlerId,
    //            Value = targetStep.HandlerName,
    //            RoleId = targetStep.RoleId,
    //            RoleName = targetStep.RoleName
    //        });
    //    }

    //    //var flowAssignInfo = await _workflowDomainService.GetNextStepFlowAssignInfoByDefineAsync(targetStepDefine, dto.HandlerType, dto.IsStartCountersign,
    //    //    dto.NextHandlers.Select(d => new Kv(d.Key, d.Value)).ToList(), cancellationToken);

    //    //var stepHandlers = await GetNextStepHandlersAsync(workflow, targetStepDefine, dto, cancellationToken);

    //    await _workflowDomainService.RecallAsync(workflow, dto, targetStepDefine, flowAssignInfo, traceType, expiredTime, isOrderFiled,
    //      EHandleMode.Recall, cancellationToken);
    //}

    /// <summary>
    /// 无视流程模板配置直接将当前节点办理至结束节点
    /// </summary>
    public async Task HandleToEndAsync(string workflowId, string opinion, List<FileDto> files, DateTime? expiredTime = null,
        EReviewResult reviewResult = EReviewResult.Unknown, CancellationToken cancellationToken = default)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(workflowId, withDefine: true, withSteps: true,
            cancellationToken: cancellationToken);
        var endStepDefine = workflow.WorkflowDefinition.FindEndStepDefine();
        if (endStepDefine is null)
            throw new UserFriendlyException("未正确配置结束节点");
        var unHandleStep = workflow.Steps.FirstOrDefault(d => d.Status is not EWorkflowStepStatus.Handled);
        if (unHandleStep is null)
            throw new UserFriendlyException($"无待办节点, workflowId: {workflowId}", "无待办节点");
        var dto = new NextWorkflowDto
        {
            WorkflowId = workflowId,
            NextStepCode = endStepDefine.Code,
            NextStepName = endStepDefine.Name,
            FlowDirection = EFlowDirection.OrgToFile,
            BusinessType = endStepDefine.BusinessType,
            ReviewResult = reviewResult,
            Opinion = opinion,
            Files = files,
            StepId = unHandleStep.Id
        };
        //await NextAsync(dto, cancellationToken: cancellationToken);
        await _workflowDomainService.NextAsync(dto, expiredTime: expiredTime,
            cancellationToken: cancellationToken);
    }

    /// <summary>
    /// 查询开始流程的下一步待选节点
    /// </summary>
    public async Task<NextStepsDto<NextStepOption>> GetStartStepsAsync(string moduleCode, CancellationToken cancellationToken)
    {
        var wfModule = await GetWorkflowModuleAsync(moduleCode, cancellationToken);
        var definition = wfModule.Definition;
        if (definition == null)
            throw new UserFriendlyException("无效模板编码");
        if (definition.Status is not EDefinitionStatus.Enable)
            throw new UserFriendlyException("该模板不可用");

        var startStepDefine = definition.FindStartStepDefine();
        if (startStepDefine.InstanceMode is EInstanceMode.Dynamic &&
            !_workflowDomainService.DynamicShouldTerminal(startStepDefine, _sessionContext.OrgLevel))
        {
            var settingHandle = _systemSettingCacheManager.GetSetting(SettingConstants.RoleJingBanRen);
            var settingLead = _systemSettingCacheManager.GetSetting(SettingConstants.RoleLingDao);
            var nextStepOption = await GetDynamicStepAsync(startStepDefine.InstancePolicy.Value,
                startStepDefine.StepType, startStepDefine.BusinessType, settingHandle?.SettingValue[0], settingLead?.SettingValue[0],
                cancellationToken);
            return new NextStepsDto<NextStepOption>
            {
                CanReject = startStepDefine.CanReject,
                CanStartCountersign = startStepDefine.CanStartCountersign,
                CurrentStepType = startStepDefine.StepType,
                CurrentHandlerType = startStepDefine.HandlerType,
                CurrentStepBusinessType = startStepDefine.BusinessType,
                CurrentTag = startStepDefine.Tag ?? string.Empty,
                Steps = new List<NextStepOption> { nextStepOption }
            };
        }

        var firstStepDefines = definition.FindStepDefines(startStepDefine.NextSteps.Select(d => d.Code));
        if (!firstStepDefines.Any())
            throw new UserFriendlyException("未正确配置首个办理节点");

        return new NextStepsDto<NextStepOption>
        {
            CanReject = startStepDefine.CanReject,
            CanStartCountersign = startStepDefine.CanStartCountersign,
            CurrentStepType = startStepDefine.StepType,
            CurrentHandlerType = startStepDefine.HandlerType,
            CurrentStepBusinessType = startStepDefine.BusinessType,
            CurrentTag = startStepDefine.Tag ?? string.Empty,
            TimeTypeOptions = EnumExts.GetDescriptions<ETimeType>().ToList(),
            Steps = await GetConfigStepsAsync(definition.FlowType, startStepDefine.StepType, startStepDefine.BusinessType,
                firstStepDefines, cancellationToken)
        };
    }

    /// <summary>
    /// 查询办理流程的下一步待选节点
    /// </summary>
    public async Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(string workflowId, CancellationToken cancellationToken)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(workflowId, withDefine: true, withSteps: true,
            cancellationToken: cancellationToken);
        var currentStep = _workflowDomainService.FindCurrentStepWaitForHandle(workflow,
            _sessionContext.RequiredUserId, _sessionContext.RequiredOrgId,
            _sessionContext.Roles);
        if (currentStep.StepType is EStepType.End)
            throw new UserFriendlyException("结束节点无需办理");

        return await GetNextStepsAsync(workflow, currentStep, cancellationToken);
    }

    public async Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(string workflowId, string stepId,
        CancellationToken cancellationToken)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(workflowId, withDefine: true, withSteps: true,
            cancellationToken: cancellationToken);
        var currentStep = workflow.Steps.FirstOrDefault(d => d.Id == stepId);
        if (currentStep == null)
            throw new UserFriendlyException($"未查询到选择节点, workflowId: {workflowId}, stepId: {stepId}");
        if (currentStep.StepType is EStepType.End)
            throw new UserFriendlyException("结束节点无需办理");

        return await GetNextStepsAsync(workflow, currentStep, cancellationToken);
    }
    
    public async Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(Workflow workflow, CancellationToken cancellationToken)
    {
        var currentStep = _workflowDomainService.FindCurrentStepWaitForHandle(workflow,
            _sessionContext.RequiredUserId, _sessionContext.RequiredOrgId,
            _sessionContext.Roles);
        if (currentStep.StepType is EStepType.End)
            throw new UserFriendlyException("结束节点无需办理");

        return await GetNextStepsAsync(workflow, currentStep, cancellationToken);
    }

    /// <summary>
    /// 跨级指派查询下一步可选节点及办理对象参数
    /// </summary>
    public async Task<NextStepsDto<NextStepOption>> GetCrossLevelStepsAsync(GetCrossLevelStepsDto dto, CancellationToken cancellationToken)
    {
        WorkflowDefinition definition;
        if (string.IsNullOrEmpty(dto.WorkflowId))
        {
            var wfModule = await GetWorkflowModuleAsync(WorkflowModuleConsts.OrderHandle, cancellationToken);
            definition = wfModule.Definition;
        }
        else
        {
            var workflow = await _workflowDomainService.GetWorkflowAsync(dto.WorkflowId, withDefine: true, cancellationToken: cancellationToken);
            definition = workflow.WorkflowDefinition;
        }

        if (definition == null)
            throw new UserFriendlyException("无效模板编码");
        if (definition.Status is not EDefinitionStatus.Enable)
            throw new UserFriendlyException("该模板不可用");

        var currentStepDefine = definition.FindStepDefine(dto.StepCode);
        var nextStepDefines = definition.FindStepDefines(currentStepDefine.NextSteps.Select(d => d.Code))
            .Where(d => d.StepType == EStepType.Normal && d.BusinessType == EBusinessType.Department)
            .ToList();

        var stepOptions = new List<NextStepOption>();
        foreach (var stepDefine in nextStepDefines)
        {
            var success = int.TryParse(stepDefine.HandlerTypeItems.First().Key, out var level);
            int? orgLevel = success ? level : null;
            var nextStepOption = new NextStepOption
            {
                Key = stepDefine.Code,
                Value = stepDefine.Name,
                StepType = stepDefine.StepType,
                BusinessType = stepDefine.BusinessType,
                HandlerType = stepDefine.HandlerType,
                Tag = stepDefine.Tag ?? string.Empty,
                OrgLevel = stepDefine.HandlerType is EHandlerType.OrgLevel ? orgLevel : null,
            };
            var orgs = await _organizeRepository.Queryable()
                .Where(d => d.IsEnable &&
                            d.Level == orgLevel &&
                            dto.OrgIds.Contains(d.ParentId))
                .ToListAsync(cancellationToken);
            nextStepOption.Items = orgs.Select(d => new StepAssignInfo
            {
                Key = d.Id,
                Value = d.Name,
                OrgId = d.Id,
                OrgName = d.Name,
                FlowAssignType = EFlowAssignType.Org
            })
                .ToList();
            nextStepOption.FlowDirection = _workflowDomainService.GetFlowDirection(dto.BusinessType, stepDefine.BusinessType);
            stepOptions.Add(nextStepOption);
        }

        return new NextStepsDto<NextStepOption>
        {
            CanReject = false,
            CanStartCountersign = true,
            CurrentStepBusinessType = dto.BusinessType,
            Steps = stepOptions
        };
    }

    private async Task<NextStepsWithOpinionDto<NextStepOption>> GetNextStepsAsync(Workflow workflow, WorkflowStep currentStep,
        CancellationToken cancellationToken)
    {
        var dto = new NextStepsWithOpinionDto<NextStepOption>
        {
            CanReject = workflow.IsReviewType() && currentStep.CanReject,
            //ExpiredTime = workflow.ExpiredTime,
            CanStartCountersign = currentStep.CanStartCountersign,
            CurrentStepBusinessType = currentStep.BusinessType,
            CurrentStepType = currentStep.StepType,
            CurrentHandlerType = currentStep.HandlerType,
            CurrentTag = currentStep.Tag ?? string.Empty,
            CurrentIsCountersignEndStep = currentStep.IsCountersignEndStep,
            CurrentIsTopCountersignEndStep = currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId),
            TimeTypeOptions = EnumExts.GetDescriptions<ETimeType>().ToList(),
            IsMainHandlerShow = workflow.WorkflowDefinition.IsMainHandlerShow,
            StepId = currentStep.Id,
            CurrentOrgLevel = string.IsNullOrEmpty(currentStep.HandlerOrgId) ? null : currentStep.HandlerOrgId.CalcOrgLevel(),
            Opinion = currentStep.Opinion,
        };
        
        var currentStepDefine = _workflowDomainService.GetStepDefine(workflow.WorkflowDefinition, currentStep.Code);

        if (currentStep.InstanceMode is EInstanceMode.Dynamic &&
            !_workflowDomainService.DynamicShouldTerminal(currentStepDefine, _sessionContext.OrgLevel))
        {
            var settingHandle = _systemSettingCacheManager.GetSetting(SettingConstants.RoleJingBanRen);
            var settingLead = _systemSettingCacheManager.GetSetting(SettingConstants.RoleLingDao);
            //动态生成下一步
            var nextStepOption = await GetDynamicStepAsync(currentStep.InstancePolicy.Value, currentStep.StepType,
                currentStep.BusinessType, settingHandle?.SettingValue[0], settingLead?.SettingValue[0], cancellationToken);
            dto.Steps = new List<NextStepOption>
            {
                nextStepOption
            };
            return dto;
        }

        if (workflow.IsInCountersign && currentStep.IsInCountersign())
        {
            if (currentStep.IsCountersignEndStep)
            {
                // // 宜宾需求：会签汇总节点展示会签办理节点办理意见
                // var countersignHandleSteps = workflow.Steps.Where(d =>
                //     d.CountersignId == currentStep.CountersignId &&
                //     d.CountersignPosition is ECountersignPosition.Direct or ECountersignPosition.Single).ToList();
                // var sb = new StringBuilder();
                // foreach (var countersignHandleStep in countersignHandleSteps)
                // {
                //     //sb.AppendLine($"{countersignHandleStep.GetActualHandler()?.GetHandler().Value} : {countersignHandleStep.Opinion}");
                //     sb.AppendLine($"{countersignHandleStep.GetHandler().Value} : {countersignHandleStep.Opinion}");
                // }
                //
                // dto.Opinion = sb.ToString();

                //当前待办节点为会签汇总节点时：检查是否为顶级会签汇总节点，t:按配置往下走，f:继续往上汇总，不需要重复往下指派
                if (!currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
                {
                    var startCountersignStep = _workflowDomainService.GetCsLoopStartStep(workflow.Steps, currentStep);

                    ////查找当前节点对应会签开始节点的上级作为下一个cs汇总节点的汇总对象
                    //var startCountersignStep =
                    //    workflow.Steps.FirstOrDefault(d => d.Id == currentStep.CountersignStartStepId);
                    //if (startCountersignStep is null)
                    //    throw new UserFriendlyException(
                    //        $"未查询到会签开始节点，workflowId: {workflow.Id}, startStepId: {currentStep.CountersignStartStepId}",
                    //        "未查询到会签开始节点，数据异常");
                    var countersignEndOption = GetCsEndStepByTargetPrev(workflow.Steps, startCountersignStep);

                    //按会签策略
                    var nextStepOption = await GetCountersignStepAsync(currentStep.CountersignPolicy.Value,
                        EStepType.Normal, currentStep.BusinessType, cancellationToken);

                    dto.Steps = new List<NextStepOption> { nextStepOption };
                    if (countersignEndOption != null)
                        dto.Steps.Add(countersignEndOption);
                    return dto;
                }
            }
            else
            {
                //汇总节点
                var countersignEndOption = GetCsEndStepByTargetPrev(workflow.Steps, currentStep);
                //按会签策略
                var nextStepOption =
                    await GetCountersignStepAsync(currentStep.CountersignPolicy.Value,
                        EStepType.Normal, currentStep.BusinessType, cancellationToken);
                dto.Steps = new List<NextStepOption> { nextStepOption };
                if (countersignEndOption != null)
                    dto.Steps.Add(countersignEndOption);
                return dto;
            }
        }

        var nextDefines = workflow.WorkflowDefinition.FindStepDefines(currentStep.NextSteps.Select(d => d.Code));
        if (!nextDefines.Any())
            throw new UserFriendlyException("未正确配置下一节点");

        dto.Steps = await GetConfigStepsAsync(workflow, currentStep, nextDefines, cancellationToken);

        // 宜宾需求：汇总节点展示前一级节点办理意见
        if (currentStep.StepType is EStepType.Summary &&
            !currentStep.IsCountersignEndStep &&
            string.IsNullOrEmpty(dto.Opinion))
        {
            var prevStep = workflow.Steps.FirstOrDefault(d => d.Id == currentStep.PrevStepId);
            dto.Opinion = $"{prevStep?.GetHandler().Value} : {prevStep?.Opinion}";
        }

        return dto;
    }

    /// <summary>
    /// 查询特提可选节点及办理对象
    /// </summary>
    public async Task<NextStepsDto<RecallStepOption>> GetRecallStepsAsync(string workflowId, CancellationToken cancellationToken)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(workflowId, withDefine: true, withSteps: true,
            cancellationToken: cancellationToken);
        var isEnd = workflow.Steps.Any(x => x.StepType == EStepType.End);
        var currentStep = workflow.Steps.FirstOrDefault(d => d.Id == workflow.ActualHandleStepId) ?? (isEnd
            ? workflow.Steps.Where(d => d.StepType == EStepType.End).MaxBy(d => d.CreationTime)
            : workflow.Steps.Where(d => d.Status != EWorkflowStepStatus.Handled).MaxBy(d => d.CreationTime));
        if (currentStep is null)
            throw new UserFriendlyException("无效当前节点编号");
        var query = workflow.Steps.Where(d =>
            d.StepType != EStepType.End &&
            d.IsOrigin);
        if (!isEnd)
        {
            query = query.Where(d => d.Id != currentStep.Id);
        }

        var originSteps = query.ToList();
        var stepCodes = originSteps.Select(d => d.Code).ToList();
        var stepDefines = workflow.WorkflowDefinition.FindStepDefines(stepCodes);

        var dto = new NextStepsDto<RecallStepOption>
        {
            TimeTypeOptions = EnumExts.GetDescriptions<ETimeType>().ToList()
        };

        var steps = await GetRecallConfigStepsAsync(originSteps, stepDefines, currentStep,
            workflow.FlowType,
            workflow.Status is EWorkflowStatus.Completed,
            cancellationToken);
        dto.Steps = steps;

        return dto;
    }

    private async Task<List<RecallStepOption>> GetRecallConfigStepsAsync(
        List<WorkflowStep> originSteps, List<StepDefine> stepDefines,
        WorkflowStep currentStep, EFlowType flowType, bool isWorkflowFiled, CancellationToken cancellationToken)
    {
        var steps = new List<RecallStepOption>();
        foreach (var originStep in originSteps)
        {
            var stepDefine = stepDefines.First(d => d.Code == originStep.Code);
            var nextStepOption = await GetConfigStepAsync(flowType, stepDefine, cancellationToken);
            var stepOption = _mapper.Map<RecallStepOption>(nextStepOption);
            if (stepDefine.StepType is EStepType.End)
            {
                steps.Add(stepOption);
                continue;
            }

            stepOption.InputRealHandler = false;

            //已归档工单，撤回至中心看作otc,撤回至部门看作cto
            //todo 依据归档节点的busiType判断
            stepOption.FlowDirection = isWorkflowFiled
                ? stepDefine.BusinessType is EBusinessType.Seat or EBusinessType.Send or EBusinessType.CenterMonitor or EBusinessType.CenterLeader
                    ? EFlowDirection.FiledToCenter
                    : stepDefine.BusinessType is EBusinessType.Department or EBusinessType.DepartmentLeader
                        ? EFlowDirection.FiledToOrg
                        : null
                //: CheckFlowDirection(currentStep.BusinessType, stepDefine.BusinessType);
                : _workflowDomainService.GetFlowDirection(currentStep.BusinessType, stepDefine.BusinessType);

            //需求已调整为特提必重算期满时间

            //需求：撤回选择办理对象时，默认选中该节点原办理对象
            //if (originStep.Handlers.Any())
            //    stepOption.Handler = originStep.Handlers.FirstOrDefault(d => d.Key == originStep.HandlerId || d.Key == originStep.HandlerOrgId);
            stepOption.Handler = originStep.GetWorkflowStepHandler();

            //if (originStep.StepHandlers.Any())
            //    stepOption.Handler = originStep.GetActualHandler()?.GetHandler() ?? new();

            steps.Add(stepOption);
        }

        return steps;
    }

    /// <summary>
    /// 否决
    /// </summary>
    public async Task RejectAsync(RejectDto dto, CancellationToken cancellationToken)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(dto.WorkflowId, withDefine: true,
            cancellationToken: cancellationToken);
        //var basicDto = _mapper.Map<BasicWorkflowDto>(dto);
        //basicDto.NextStepCode = string.Empty;
        //basicDto.IsStartCountersign = false;
        //await _workflowDomainService.RejectAsync(workflow, basicDto, cancellationToken);

        var endStepDefine = workflow.WorkflowDefinition.FindEndStepDefine();
        var nextDto = _mapper.Map<NextWorkflowDto>(dto);
        nextDto.ReviewResult = EReviewResult.Failed;
        nextDto.NextStepCode = endStepDefine.Code;
        nextDto.NextStepName = endStepDefine.Name;
        nextDto.FlowDirection = _sessionContext.OrgIsCenter
            ? EFlowDirection.CenterToFile
            : EFlowDirection.OrgToFile;

        //await NextAsync(nextDto, cancellationToken: cancellationToken);
        await _workflowDomainService.NextAsync(nextDto, cancellationToken: cancellationToken);
    }

    //供开启流程调用
    private async Task<List<NextStepOption>> GetConfigStepsAsync(
        EFlowType flowType,
        EStepType currentStepType,
        EBusinessType currentBusinessType,
        List<StepDefine> stepDefines,
        CancellationToken cancellationToken)
    {
        var stepOptions = new List<NextStepOption>();
        foreach (var stepDefine in stepDefines)
        {
            var nextStepOption = await GetConfigStepAsync(flowType, stepDefine, cancellationToken);
            nextStepOption.InputRealHandler = currentStepType == EStepType.Normal &&
                                              stepDefine.StepType is EStepType.Summary or EStepType.End;
            stepOptions.Add(nextStepOption);

            if (stepDefine.StepType is EStepType.End) continue;

            nextStepOption.FlowDirection = _workflowDomainService.GetFlowDirection(currentBusinessType, stepDefine.BusinessType);
            //stepOptions.Add(nextStepOption);
        }

        return stepOptions;
    }

    /// <summary>
    /// 办理流程调用（根据办理过程过滤汇总节点待选办理对象）
    /// </summary>
    private async Task<List<NextStepOption>> GetConfigStepsAsync(
        Workflow workflow,
        WorkflowStep currentStep,
        List<StepDefine> stepDefines,
        CancellationToken cancellationToken)
    {
        var stepOptions = new List<NextStepOption>();
        foreach (var stepDefine in stepDefines)
        {
            NextStepOption nextStepOption;

            //汇总节点只能选择对应节点办理对象
            if (workflow.FlowType is EFlowType.Handle
                && stepDefine.StepType is EStepType.Summary
                /*&& stepDefine.BusinessType is EBusinessType.Seat or EBusinessType.Send*/)
            {
                StepAssignInfo handler;
                if (stepDefine.BusinessType is EBusinessType.Department)
                {
                    //todo 临时方案:解决跨级派单部门节点由中心办理造成的问题
                    var upperOrgId = currentStep.HandlerOrgId.Substring(0, currentStep.HandlerOrgId.CalcOrgLevel() * 3);
                    var org = await _organizeRepository.GetAsync(upperOrgId, cancellationToken);
                    handler = new StepAssignInfo
                    {
                        OrgId = org.Id,
                        OrgName = org.Name,
                        Key = org.Id,
                        Value = org.Name,
                        FlowAssignType = EFlowAssignType.Org
                    };

                    nextStepOption = new NextStepOption
                    {
                        Key = stepDefine.Code,
                        Value = stepDefine.Name,
                        StepType = stepDefine.StepType,
                        BusinessType = stepDefine.BusinessType,
                        HandlerType = stepDefine.HandlerType,
                        //HandlerType = EHandlerType.AssignedUser, //指定办理人(业务需求)
                        Tag = stepDefine.Tag ?? string.Empty,
                        Items = new List<StepAssignInfo> { handler } //handlers
                    };
                }
                else
                {
                    handler = _workflowDomainService.GetSummaryTargetFlowStepHandler(workflow, stepDefine.SummaryTargetCode);

                    nextStepOption = new NextStepOption
                    {
                        Key = stepDefine.Code,
                        Value = stepDefine.Name,
                        StepType = stepDefine.StepType,
                        BusinessType = stepDefine.BusinessType,
                        //HandlerType = stepDefine.HandlerType,
                        HandlerType = EHandlerType.AssignedUser, //指定办理人(业务需求)
                        Tag = stepDefine.Tag ?? string.Empty,
                        Items = new List<StepAssignInfo> { handler } //handlers
                    };
                }

                //nextStepOption = new NextStepOption
                //{
                //    Key = stepDefine.Code,
                //    Value = stepDefine.Name,
                //    StepType = stepDefine.StepType,
                //    BusinessType = stepDefine.BusinessType,
                //    //HandlerType = stepDefine.HandlerType,
                //    HandlerType = EHandlerType.AssignedUser, //指定办理人(业务需求)
                //    Tag = stepDefine.Tag ?? string.Empty,
                //    Items = new List<StepAssignInfo> { handler } //handlers
                //};
            }
            else
            {
                nextStepOption = await GetConfigStepAsync(workflow.FlowType, stepDefine, cancellationToken);
            }

            nextStepOption.InputRealHandler = currentStep.StepType == EStepType.Normal &&
                                              stepDefine.StepType is EStepType.Summary or EStepType.End;
            stepOptions.Add(nextStepOption);

            if (stepDefine.StepType is EStepType.End) continue;

            nextStepOption.FlowDirection = _workflowDomainService.GetFlowDirection(currentStep.BusinessType, stepDefine.BusinessType);
        }

        return stepOptions;
    }

    public async Task<NextStepOption> GetConfigStepAsync(EFlowType flowType, StepDefine stepDefine, CancellationToken cancellationToken)
    {
        var handlers = new List<StepAssignInfo>();
        if (stepDefine.StepType is EStepType.End)
        {
            return new NextStepOption
            {
                Key = stepDefine.Code,
                Value = stepDefine.Name,
                StepType = stepDefine.StepType,
                BusinessType = stepDefine.BusinessType,
                HandlerType = stepDefine.HandlerType,
                Tag = stepDefine.Tag ?? string.Empty,
                Items = handlers
            };
        }

        var orgId = _sessionContext.RequiredOrgId;
        var levelOneOrgId = orgId.GetHigherOrgId();
        var isCenter = levelOneOrgId.IsCenter();
        switch (stepDefine.HandlerType)
        {
            case EHandlerType.AssignedUser:
                handlers = await _userRepository.Queryable()
                    .Includes(d => d.Organization)
                    .Where(d => stepDefine.HandlerTypeItems.Select(x => x.Key).Contains(d.Id))
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        UserId = d.Id,
                        Username = d.Name,
                        OrgId = d.OrgId,
                        OrgName = d.Organization.Name,
                        FlowAssignType = EFlowAssignType.User
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EHandlerType.AssignedOrg:
                handlers = await _organizeRepository.Queryable() //stepDefine.HandlerTypeItems;
                    .Where(d => d.IsEnable && stepDefine.HandlerTypeItems.Select(x => x.Key).Contains(d.Id))
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EHandlerType.Role:
                //当前操作人所属部门的下级部门并且属于配置包含角色
                var roles = await _roleRepository.Queryable()
                    .Includes(d => d.Accounts.Where(x =>
                            !x.IsDeleted && x.Status == EAccountStatus.Normal && x.AccountType == EAccountType.Personal).ToList(),
                        x => x.User, s => s.Organization)
                    .Where(d => stepDefine.HandlerTypeItems.Select(x => x.Key).Contains(d.Name))
                    .ToListAsync(cancellationToken);
                _logger.LogInformation($"角色: {string.Join(",", roles.Select(d => d.Name))}");
                var users1 = roles.SelectMany(d => d.Accounts).Select(d => d.User);

                if (flowType is EFlowType.Handle)
                {
                    //工单办理：除一级部门选择中心汇总(中心会签流程，返回topCountersignStep场景)，其余只能选下级部门
                    if (stepDefine.BusinessType is EBusinessType.Department &&
                        stepDefine.StepType != EStepType.Summary)
                    {
                        users1 = users1.Where(d => d.OrgId.StartsWith(levelOneOrgId));
                    }
                    else if (stepDefine.BusinessType is EBusinessType.DepartmentLeader)
                    {
                        users1 = users1.Where(d => d.OrgId == orgId);
                    }
                }

                var defineTypeItem = stepDefine.HandlerTypeItems.First();

                handlers = users1.Where(d => d != null && !string.IsNullOrEmpty(d.Id))
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        UserId = d.Id,
                        Username = d.Name,
                        OrgId = d.OrgId,
                        OrgName = d.Organization?.Name,
                        RoleId = defineTypeItem.Key,
                        RoleName = defineTypeItem.Value,
                        FlowAssignType = EFlowAssignType.User
                    })
                    .ToList();
                break;
            case EHandlerType.OrgLevel:
                //当前操作人所属部门的垂直部门并且属于配置orgLevel的部门
                var levels = stepDefine.HandlerTypeItems.Select(d => int.Parse(d.Key)).ToList();
                // var orgs1 = await _organizeRepository.Queryable()
                //     .Where(d => d.IsEnable && levels.Contains(d.Level))
                //     .WhereIF(!isCenter, d => d.Id.StartsWith(levelOneOrgId))
                //     .ToListAsync(cancellationToken);
                _logger.LogInformation($"部门等级: {string.Join(",", levels)}");

                var query = _organizeRepository.Queryable()
                    .Where(d => d.IsEnable);
                List<SystemOrganize> orgs1;
                if (isCenter)
                {
                    orgs1 = await query
                        .Where(d => levels.Contains(d.Level) && !d.IsCenter)
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    var upLevels = levels.Where(d => d <= _sessionContext.OrgLevel).ToList();
                    var lowLevels = levels.Where(d => d > _sessionContext.OrgLevel).ToList();
                    orgs1 = await query
                        .Where(d => (upLevels.Contains(d.Level) && d.Id.StartsWith(levelOneOrgId)) ||
                                    (lowLevels.Contains(d.Level) && d.Id.StartsWith(orgId)))
                        .ToListAsync(cancellationToken);
                }

                handlers = orgs1.Select(d => new StepAssignInfo
                {
                    Key = d.Id,
                    Value = d.Name,
                    OrgId = d.Id,
                    OrgName = d.Name,
                    FlowAssignType = EFlowAssignType.Org
                })
                    .ToList();
                break;
            case EHandlerType.OrgType:
                var types = stepDefine.HandlerTypeItems.Select(d => d.Key)
                    .Select(d => Enum.Parse<EOrgType>(d));
                var orgs2 = await _organizeRepository.Queryable()
                    .Where(d => d.IsEnable && types.Contains(d.OrgType))
                    .WhereIF(!isCenter, d => d.Id.StartsWith(levelOneOrgId))
                    .ToListAsync(cancellationToken);

                handlers = orgs2.Select(d => new StepAssignInfo
                {
                    Key = d.Id,
                    Value = d.Name,
                    OrgId = d.Id,
                    OrgName = d.Name,
                    FlowAssignType = EFlowAssignType.Org
                })
                    .ToList();
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }

        var success = int.TryParse(stepDefine.HandlerTypeItems.First().Key, out var level);
        int? orgLevel = success ? level : null;
        return new NextStepOption
        {
            Key = stepDefine.Code,
            Value = stepDefine.Name,
            StepType = stepDefine.StepType,
            BusinessType = stepDefine.BusinessType,
            HandlerType = stepDefine.HandlerType,
            Tag = stepDefine.Tag ?? string.Empty,
            OrgLevel = stepDefine.HandlerType is EHandlerType.OrgLevel ? orgLevel : null,
            Items = handlers
        };
    }

    private NextStepOption? GetCsEndStepByTargetPrev(List<WorkflowStep> steps, WorkflowStep step)
    {
        var prevStep = steps.FirstOrDefault(d => d.Id == step.PrevStepId);
        //归档以后特提发起会签场景
        if (prevStep is null || !prevStep.IsStartCountersign) return null;

        var handlerIsCenter = prevStep.HandlerOrgIsCenter.Value;
        var text = handlerIsCenter
            ? "热线中心会签汇总"
            : $"{prevStep.HandlerOrgId.CalcOrgLevel().ToChinese()}级部门会签汇总";
        var handlers = prevStep.Handlers
            .Where(d => d.Key == prevStep.HandlerId || d.Key == prevStep.HandlerOrgId).ToList();
        //var handler = prevStep.GetActualHandler()?.GetHandler();
        var handler = new StepAssignInfo
        {
            Key = prevStep.HandlerId,
            Value = prevStep.HandlerName,
            UserId = prevStep.HandlerId,
            Username = prevStep.HandlerName,
            OrgId = prevStep.HandlerOrgId,
            OrgName = prevStep.HandlerOrgName,
            FlowAssignType = prevStep.FlowAssignType ?? (handlerIsCenter ? EFlowAssignType.User : EFlowAssignType.Org),
        };

        return new NextStepOption
        {
            Key = prevStep.Code,
            Value = text,
            BackToCountersignEnd = true,
            StepType = EStepType.Summary,
            BusinessType = prevStep.BusinessType,
            //HandlerType = prevStep.HandlerType,
            HandlerType = EHandlerType.AssignedUser, //指定办理人(业务需求)
            Tag = prevStep.Tag ?? string.Empty,
            Items = new List<StepAssignInfo> { handler } //handlers //new List<Kv> { new(prevStep.HandlerId, prevStep.HandlerName) },
        };
    }

    /// <summary>
    /// 动态策略
    /// </summary>
    /// <returns></returns>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    private async Task<NextStepOption> GetDynamicStepAsync(
        EDynamicPolicy policy, EStepType stepType,
        EBusinessType currentBusinessType, string handleRoleCode, string leadRoleCode, CancellationToken cancellationToken)
    {
        int orgLevel;
        List<StepAssignInfo> items;
        string upperOrgId;
        EBusinessType businessType;
        EFlowDirection? flowDirection = null;
        bool isLead = false;
        bool isSkip = false;
        string roleId = string.Empty;
        string roleName = string.Empty;
        string handleRoleName = "经办人";
        string leadRoleName = "领导";
        EHandlerType handlerType = EHandlerType.OrgLevel;

        switch (policy)
        {
            case EDynamicPolicy.OrgUpCenterTop:
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel < 0) orgLevel = 0;

                if (orgLevel == 0)
                {
                    businessType = EBusinessType.Send;
                    if (currentBusinessType == EBusinessType.Department)
                        flowDirection = EFlowDirection.OrgToCenter;

                    items = await _organizeRepository.Queryable()
                        .Where(d => d.IsCenter)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    businessType = EBusinessType.Department;

                    //上级部门Id
                    upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                    items = await _organizeRepository.Queryable()
                        .Where(d => d.Id == upperOrgId)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }

                break;
            case EDynamicPolicy.OrgUp:
                businessType = _sessionContext.OrgIsCenter
                    ? EBusinessType.Send
                    : _sessionContext.RequiredOrgId.CalcOrgLevel() == 1
                        ? EBusinessType.Send
                        : EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel <= 0) orgLevel = 1;
                //上级部门Id
                upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                items = await _organizeRepository.Queryable()
                    .Where(d => d.Id == upperOrgId)
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EDynamicPolicy.OrgUpHandleCenterTop:
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel < 0) orgLevel = 0;

                if (orgLevel == 0)
                {
                    businessType = EBusinessType.Send;
                    if (currentBusinessType == EBusinessType.Department)
                        flowDirection = EFlowDirection.OrgToCenter;

                    items = await _organizeRepository.Queryable()
                        .Where(d => d.IsCenter)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    businessType = EBusinessType.Department;
                    roleName = handleRoleName;
                    //上级部门Id
                    upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                    items = await _organizeRepository.Queryable()
                        .Where(d => d.Id == upperOrgId)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            RoleId = handleRoleCode,
                            RoleName = handleRoleName,
                            FlowAssignType = EFlowAssignType.OrgAndRole
                        })
                        .ToListAsync(cancellationToken);
                }

                break;
            case EDynamicPolicy.OrgUpHandle:
                businessType = _sessionContext.OrgIsCenter
                    ? EBusinessType.Send
                    : _sessionContext.RequiredOrgId.CalcOrgLevel() == 1
                        ? EBusinessType.Send
                        : EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel <= 0) orgLevel = 1;
                roleName = handleRoleName;
                //上级部门Id
                upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                items = await _organizeRepository.Queryable()
                    .Where(d => d.Id == upperOrgId)
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        RoleId = handleRoleCode,
                        RoleName = handleRoleName,
                        FlowAssignType = EFlowAssignType.OrgAndRole
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EDynamicPolicy.OrgUpLeadCenterTop:
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel < 0) orgLevel = 0;
                isLead = _sessionContext.Roles.Any(x => x == leadRoleCode);
                isSkip = await _userRepository.Queryable()
                    .AnyAsync(x => x.OrgId == _sessionContext.RequiredOrgId && x.Roles.Any(r => r.Name == leadRoleCode),
                        cancellationToken);
                if (orgLevel == 0 && (isLead || !isSkip))
                {
                    businessType = EBusinessType.Send;
                    if (currentBusinessType == EBusinessType.Department)
                        flowDirection = EFlowDirection.OrgToCenter;

                    items = await _organizeRepository.Queryable()
                        .Where(d => d.IsCenter)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    businessType = EBusinessType.Department;
                    upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(_sessionContext.OrgLevel);
                    if (!isLead)
                    {
                        if (isSkip)
                        {
                            roleId = leadRoleCode;
                            roleName = leadRoleName;
                        }
                    }

                    if (isLead || !isSkip)
                    {
                        //上级部门Id
                        upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                        roleId = handleRoleCode;
                        roleName = handleRoleName;
                    }
                    else
                    {
                        orgLevel += 1;
                    }

                    items = await _organizeRepository.Queryable()
                        .Where(d => d.Id == upperOrgId)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            RoleId = roleId,
                            RoleName = roleName,
                            FlowAssignType = EFlowAssignType.OrgAndRole
                        })
                        .ToListAsync(cancellationToken);
                }

                break;
            case EDynamicPolicy.OrgUpLead:
                businessType = _sessionContext.OrgIsCenter
                    ? EBusinessType.Send
                    : _sessionContext.RequiredOrgId.CalcOrgLevel() == 1
                        ? EBusinessType.Send
                        : EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel <= 0) orgLevel = 1;
                upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(_sessionContext.OrgLevel);
                isLead = _sessionContext.Roles.Any(x => x == leadRoleCode);
                if (!isLead)
                {
                    isSkip = await _userRepository.Queryable()
                        .AnyAsync(x => x.OrgId == _sessionContext.RequiredOrgId && x.Roles.Any(r => r.Name == leadRoleCode),
                            cancellationToken);
                    if (isSkip)
                    {
                        roleId = leadRoleCode;
                        roleName = leadRoleName;
                    }
                }

                if (isLead || !isSkip)
                {
                    //上级部门Id
                    upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                    roleId = handleRoleCode;
                    roleName = handleRoleName;
                }
                else
                {
                    orgLevel += 1;
                }

                items = await _organizeRepository.Queryable()
                    .Where(d => d.Id == upperOrgId)
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        RoleId = roleId,
                        RoleName = roleName,
                        FlowAssignType = EFlowAssignType.OrgAndRole
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EDynamicPolicy.ArriveCenter:
                businessType = _sessionContext.OrgIsCenter
                    ? EBusinessType.Send
                    : _sessionContext.RequiredOrgId.CalcOrgLevel() == 1
                        ? EBusinessType.Send
                        : EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel <= 0) orgLevel = 1;
                items = await _organizeRepository.Queryable()
                    .Where(d => d.IsCenter)
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EDynamicPolicy.ArriveOneOrg:
                businessType = _sessionContext.OrgIsCenter ? EBusinessType.Send : EBusinessType.Department;
                orgLevel = _sessionContext.OrgIsCenter ? 0 : 1;
                //上级部门Id
                upperOrgId = _sessionContext.OrgIsCenter
                    ? _sessionContext.RequiredOrgId.Substring(0, 3)
                    : _sessionContext.RequiredOrgId.Substring(0, 6);
                items = await _organizeRepository.Queryable()
                    .Where(d => d.Id == upperOrgId)
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            case EDynamicPolicy.OrgDownCenterTop:
                businessType = EBusinessType.Department;
                if (_sessionContext.OrgIsCenter)
                {
                    orgLevel = 1;
                    items = await _organizeRepository.Queryable()
                        .Where(d => !d.IsCenter && d.Level == orgLevel)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    orgLevel = _sessionContext.OrgLevel + 1;
                    items = await _organizeRepository.Queryable()
                        .Where(d => !d.IsCenter && d.Level == orgLevel &&
                                    d.Id.StartsWith(_sessionContext.RequiredOrgId))
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }

                break;
            case EDynamicPolicy.OrgDown:
                businessType = EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel + 1;
                items = await _organizeRepository.Queryable()
                    .Where(d => d.Level == orgLevel && d.Id.StartsWith(_sessionContext.RequiredOrgId))
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(policy), policy, null);
        }

        var centerOrgName = _systemSettingCacheManager.GetSetting(SettingConstants.CenterOrgName)?.SettingValue[0];
        return new NextStepOption
        {
            Key = orgLevel.ToString(),
            Value = orgLevel == 0 ? centerOrgName : $"{orgLevel.ToChinese()}级部门{roleName}",
            FlowDirection = flowDirection,
            StepType = stepType,
            BusinessType = businessType,
            HandlerType = handlerType, //目前所有动态策略均属于部门等级
            Tag = string.Empty,
            Items = items
        };
    }

    /// <summary>
    /// 会签 动态策略
    /// </summary>
    /// <returns></returns>
    private async Task<NextStepOption> GetCountersignStepAsync(
        ECountersignPolicy policy, EStepType stepType,
        EBusinessType currentBusinessType, CancellationToken cancellationToken)
    {
        int orgLevel;
        List<StepAssignInfo> items;
        string upperOrgId;
        EBusinessType businessType;
        EFlowDirection? flowDirection = null;
        switch (policy)
        {
            case ECountersignPolicy.OrgUpCenterTop:
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel < 0) orgLevel = 0;

                if (orgLevel == 0)
                {
                    businessType = EBusinessType.Send;
                    if (currentBusinessType == EBusinessType.Department)
                        flowDirection = EFlowDirection.OrgToCenter;

                    items = await _organizeRepository.Queryable()
                        .Where(d => d.IsCenter)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    businessType = EBusinessType.Department;

                    //上级部门Id
                    upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                    items = await _organizeRepository.Queryable()
                        .Where(d => d.Id == upperOrgId)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }

                break;
            case ECountersignPolicy.OrgUp:
                businessType = _sessionContext.OrgIsCenter
                    ? EBusinessType.Send
                    : _sessionContext.RequiredOrgId.CalcOrgLevel() == 1
                        ? EBusinessType.Send
                        : EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel - 1;
                if (orgLevel <= 0) orgLevel = 1;
                //上级部门Id
                upperOrgId = _sessionContext.RequiredOrgId.GetHigherOrgId(orgLevel);
                items = await _organizeRepository.Queryable()
                    .Where(d => d.Id == upperOrgId)
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            case ECountersignPolicy.OrgDownCenterTop:
                businessType = EBusinessType.Department;
                if (_sessionContext.OrgIsCenter)
                {
                    orgLevel = 1;
                    items = await _organizeRepository.Queryable()
                        .Where(d => !d.IsCenter && d.Level == orgLevel)
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }
                else
                {
                    orgLevel = _sessionContext.OrgLevel + 1;
                    items = await _organizeRepository.Queryable()
                        .Where(d => !d.IsCenter && d.Level == orgLevel &&
                                    d.Id.StartsWith(_sessionContext.RequiredOrgId))
                        .Select(d => new StepAssignInfo
                        {
                            Key = d.Id,
                            Value = d.Name,
                            OrgId = d.Id,
                            OrgName = d.Name,
                            FlowAssignType = EFlowAssignType.Org
                        })
                        .ToListAsync(cancellationToken);
                }

                break;
            case ECountersignPolicy.OrgDown:
                businessType = EBusinessType.Department;
                orgLevel = _sessionContext.OrgLevel + 1;
                items = await _organizeRepository.Queryable()
                    .Where(d => d.Level == orgLevel && d.Id.StartsWith(_sessionContext.RequiredOrgId))
                    .Select(d => new StepAssignInfo
                    {
                        Key = d.Id,
                        Value = d.Name,
                        OrgId = d.Id,
                        OrgName = d.Name,
                        FlowAssignType = EFlowAssignType.Org
                    })
                    .ToListAsync(cancellationToken);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(policy), policy, null);
        }

        var centerOrgName = _systemSettingCacheManager.GetSetting(SettingConstants.CenterOrgName)?.SettingValue[0];
        return new NextStepOption
        {
            Key = orgLevel.ToString(),
            Value = orgLevel == 0 ? centerOrgName : $"{orgLevel.ToChinese()}级部门",
            FlowDirection = flowDirection,
            StepType = stepType,
            BusinessType = businessType,
            HandlerType = EHandlerType.OrgLevel, //目前所有动态策略均属于部门等级
            Tag = string.Empty,
            Items = items
        };
    }

    /// <summary>
    /// 查询下一节点办理对象类型（user or org）及实际办理对象
    /// </summary>
    public async Task<FlowAssignInfo> GetNextStepFlowAssignInfoAsync(Workflow workflow, WorkflowStep currentStep,
        BasicWorkflowDto dto, StepDefine nextStepDefine, bool isNextDynamic, CancellationToken cancellationToken)
    {
        if (nextStepDefine.StepType is EStepType.End) return new();

        var isStartCountersign = dto.IsStartCountersign;
        var handlers = dto.NextHandlers.Select(d => new Kv(d.Key, d.Value)).ToList();

        if (isStartCountersign)
        {
            var assignType = FlowAssignInfo.GetAssignType(dto.HandlerType, dto.NextHandlers.Any());
            //按会签策略判断,目前所有策略为org
            return FlowAssignInfo.Create(assignType, handlers, isStartCountersign);
        }

        //if (currentStep.IsInCountersign() && !currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
        //    return FlowAssignInfo.Create(EFlowAssignType.Org, handlers, isStartCountersign);

        if (currentStep.IsInCountersign())
        {
            if (currentStep.IsCountersignEndStep)
            {
                //汇总节点（非顶级）
                if (!currentStep.IsTopCountersignEndStep(workflow.TopCountersignStepId))
                {
                    if (dto.BackToCountersignEnd)
                    {
                        var csStartStep = _workflowDomainService.GetCsLoopStartStep(workflow.Steps, currentStep);
                        var prevStep = workflow.Steps.FirstOrDefault(d => d.Id == csStartStep.PrevStepId);
                        if (prevStep is null)
                            throw new UserFriendlyException("未查询到目标节点的前一节点");
                        return FlowAssignInfo.Create(prevStep.FlowAssignType.Value, prevStep.Handlers, isStartCountersign);
                    }
                }
            }
            else
            {
                if (dto.BackToCountersignEnd)
                {
                    var prevStep = workflow.Steps.FirstOrDefault(d => d.Id == currentStep.PrevStepId);
                    if (prevStep is null)
                        throw new UserFriendlyException($"未查询到当前节点的上级节点");
                    return FlowAssignInfo.Create(prevStep.FlowAssignType.Value, prevStep.Handlers, isStartCountersign);
                }
                else
                {
                    var assignType = FlowAssignInfo.GetAssignType(dto.HandlerType, dto.NextHandlers.Any());
                    //按会签策略判断,目前所有策略为org
                    return FlowAssignInfo.Create(assignType, handlers, isStartCountersign);
                }
            }
        }

        if (isNextDynamic)
        {
            switch (currentStep.InstancePolicy)
            {
                case EDynamicPolicy.OrgUpCenterTop:
                case EDynamicPolicy.OrgUp:
                case EDynamicPolicy.OrgDownCenterTop:
                case EDynamicPolicy.OrgDown:
                case EDynamicPolicy.ArriveCenter:
                case EDynamicPolicy.ArriveOneOrg:
                    return FlowAssignInfo.Create(EFlowAssignType.Org, handlers, isStartCountersign);
                case EDynamicPolicy.OrgUpHandleCenterTop:
                case EDynamicPolicy.OrgUpHandle:
                case EDynamicPolicy.OrgUpLeadCenterTop:
                case EDynamicPolicy.OrgUpLead:
                    return FlowAssignInfo.Create(EFlowAssignType.OrgAndRole, handlers, isStartCountersign);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        return await _workflowDomainService.GetNextStepFlowAssignInfoByDefineAsync(nextStepDefine, dto.HandlerType, isStartCountersign, handlers,
            cancellationToken);
    }

    public ISugarQueryable<WorkflowCountersign, Workflow, Order> QueryOrderCountersigns(QueryOrderCountersignDto dto, ISessionContext _sessionContext)
    {
        var Role = _sessionContext.Roles;

        var query = _workflowCountersignRepository.Queryable()
            .Includes(x => x.Members)
            .LeftJoin<Workflow>((c, w) => c.WorkflowId == w.Id)
            .InnerJoin<Order>((c, w, o) => w.ExternalId == o.Id)
            .WhereIF(!string.IsNullOrEmpty(dto.Title), (c, w, o) => o.Title.Contains(dto.Title))
            .WhereIF(!string.IsNullOrEmpty(dto.OrderNo), (c, w, o) => o.No == dto.OrderNo)
            .WhereIF(!string.IsNullOrEmpty(dto.AcceptType), (c, w, o) => o.AcceptTypeCode == dto.AcceptType) //受理类型
            .WhereIF(!string.IsNullOrEmpty(dto.Channel), (c, w, o) => o.SourceChannelCode == dto.Channel)    //来源渠道
            .WhereIF(!string.IsNullOrEmpty(dto.Hotspot), (c, w, o) => o.HotspotSpliceName != null && o.HotspotSpliceName.Contains(dto.Hotspot))//热点类型
            .WhereIF(!string.IsNullOrEmpty(dto.OrgId) && dto.OrgId == "001", (c, w, o) => o.ActualHandleOrgCode == dto.OrgId)            //接办部门（中心）
            .WhereIF(!string.IsNullOrEmpty(dto.OrgId) && dto.OrgId != "001", (c, w, o) => o.ActualHandleOrgCode.StartsWith(dto.OrgId))   //接办部门
            .WhereIF(dto.CounterSignType != null, (c, w, o) => c.CounterSignType == dto.CounterSignType)     //会签类型1

            .WhereIF(!string.IsNullOrEmpty(dto.FromPhone), (c, w, o) => o.FromPhone.Contains(dto.FromPhone) || o.Contact.Contains(dto.FromPhone))   //来电号码、联系电话
            .WhereIF(!string.IsNullOrEmpty(dto.StarterName), (c, w, o) => c.StarterName.Contains(dto.StarterName))                //发起人
            .WhereIF(!string.IsNullOrEmpty(dto.StarterOrgName), (c, w, o) => c.StarterOrgName.Contains(dto.StarterOrgName))       //发起部门
            .WhereIF(dto.Status.HasValue, (c, w, o) => o.Status == dto.Status)                                                    //工单状态
            .WhereIF(dto.InitiationTimeStart.HasValue, (c, w, o) => c.CreationTime >= dto.InitiationTimeStart)                    //发起时间开始
            .WhereIF(dto.InitiationTimeEnd.HasValue, (c, w, o) => c.CreationTime <= dto.InitiationTimeEnd)                        //发起时间结束
            .WhereIF(dto.CreationTimeStart.HasValue, (c, w, o) => o.CreationTime >= dto.CreationTimeStart)                        //受理时间开始
            .WhereIF(dto.CreationTimeEnd.HasValue, (c, w, o) => o.CreationTime <= dto.CreationTimeEnd)                            //受理时间结束
            .WhereIF(dto.ExpiredStatus.HasValue && dto.ExpiredStatus == true, (c, w, o) => o.ExpiredTime <= o.FiledTime)          //超期true
            .WhereIF(dto.ExpiredStatus.HasValue && dto.ExpiredStatus == false, (c, w, o) => o.ExpiredTime >= o.FiledTime)         //超期false
        ;

        var rolePaiDan = _systemSettingCacheManager.GetSetting(SettingConstants.RolePaiDan)?.SettingValue[0]; //派单员角色
        var seatsMonitor = _systemSettingCacheManager.GetSetting(SettingConstants.SeatsMonitor)?.SettingValue[0]; //班长角色
        var isAdmin = false;
        var systemAdministrator = _systemSettingCacheManager.GetSetting(SettingConstants.SystemAdministrator)?.SettingValue[0]; //管理员角色
        if (!string.IsNullOrEmpty(systemAdministrator) &&
            (_sessionContext.Roles.Contains(systemAdministrator) || _sessionContext.Roles.Contains(RoleSeedData.AdminRole)))
            isAdmin = true;

        //发起会签：班长角色能看所有会签信件；新增管理员能看到所有会签
        //派单员角色只能看到自己发起的会签信件；
        //承办部门用户能看到自己发起的和同级部门用户发起的会签件
        if (dto.InitiatedCountersignature.HasValue && dto.InitiatedCountersignature == true)
        {
            if (_sessionContext.Roles.Any(p => p == seatsMonitor) || isAdmin)
            {
            }
            else if (_sessionContext.Roles.Any(p => p == rolePaiDan))
            {
                query = query.Where((c, w, o) => c.StarterId == _sessionContext.UserId);
            }
            else
            {
                query = query.Where((c, w, o) => c.StarterOrgId == _sessionContext.RequiredOrgId);
            }
        }

        //会签已办（会签状态为已结束）：
        //班长角色能看所有已结束的会签信件；新增管理员能看到所有会签
        //派单员不会办理会签件只会发起会签件所以这一点派单员角色可以忽略；
        //承办部门用户能看到和同级部门用户已办理过的会签件
        if (dto.HandleCountersignature.HasValue && dto.HandleCountersignature == true)
        {
            if (_sessionContext.Roles.Any(p => p == seatsMonitor) || isAdmin)
            {
                query = query.Where((c, w, o) => c.EndTime.HasValue);
            }
            else if (_sessionContext.Roles.Any(p => p == rolePaiDan))
            {
                query = query.Where((c, w, o) => c.Members.Any(m => m.Key == _sessionContext.RequiredOrgId) && c.EndTime.HasValue);
            }
            else
            {
                query = query.Where((c, w, o) =>
                    c.Members.Any(m => m.Key == _sessionContext.OrgId || m.Key == _sessionContext.RequiredOrgId) && c.EndTime.HasValue);
            }
        }

        var items = query
            .OrderByDescending((c, w, o) => o.ExpiredTime);
        return items;
    }


    #region private

    /// <summary>
    /// 查询流程业务模块
    /// </summary>
    /// <param name="code"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    /// <exception cref="UserFriendlyException"></exception>
    public async Task<WorkflowModule> GetWorkflowModuleAsync(string code, CancellationToken cancellationToken)
    {
        var wfModule = await _wfModuleCacheManager.GetWorkflowModuleAsync(code, cancellationToken);
        if (wfModule == null)
            throw UserFriendlyException.SameMessage("无效流程模块编码");
        if (wfModule.Definition is null)
            throw new UserFriendlyException($"{code} 未配置流程模板", "未配置流程模板");
        return wfModule;
    }

    /// <summary>
    /// 检查退回节点信息
    /// </summary>
    public async Task<(WorkflowStep currentStep, WorkflowStep prevStep, List<WorkflowStep> steps, bool isOrgToCenter, bool isSecondToFirstOrgLevel)>
        GetPreviousInformationAsync(string workflowId, string operatorId, string operatorOrgId, string[] roles,string? assignStepId,
			CancellationToken cancellationToken)
    {
        var workflow = await _workflowDomainService.GetWorkflowAsync(workflowId, withSteps: true,
            withCountersigns: true, cancellationToken: cancellationToken);
        var (currentStep, prevStep, _) = _workflowDomainService.GetPreviousStep(workflow, operatorId, operatorOrgId, roles);
        if (!string.IsNullOrEmpty(assignStepId))
        {
			prevStep = workflow.Steps.FirstOrDefault(d => d.Id == assignStepId);
		}
        var isOrgToCenter = currentStep.BusinessType is EBusinessType.Department &&
                            prevStep.BusinessType is EBusinessType.Seat or EBusinessType.Send;
        var isSecondToFirstOrgLevel = currentStep.HandlerType is EHandlerType.OrgLevel &&
                                      currentStep.Handlers.First().Key.CheckIfOrgLevelIs(2) &&
                                      prevStep.HandlerType is EHandlerType.OrgLevel &&
                                      prevStep.Handlers.First().Key.CheckIfOrgLevelIs(1) &&
                                      !prevStep.Handlers.First().Key.IsCenter();
        return (currentStep, prevStep, workflow.Steps, isOrgToCenter, isSecondToFirstOrgLevel);
    }

    /// <summary>
    /// 更新省平台办理结果节点附件
    /// </summary>
    public async Task UpdateProvinceHandleResultFilesAsync(string workflowId, List<FileDto> files, CancellationToken cancellationToken)
    {
        var setting = _systemSettingCacheManager.GetSetting(SettingConstants.CityBaseConfiguration)?.SettingValue[0];
        CityBaseConfiguration cityBase = JsonConvert.DeserializeObject<CityBaseConfiguration>(setting);
        var workflow = await _workflowDomainService.GetWorkflowAsync(workflowId, withSteps: true, withTraces: true,
            cancellationToken: cancellationToken);
        var step = workflow.Steps.FirstOrDefault(d =>
            //d.StepHandlers.Any(d => d.OrgId == _cityBaseConfiguration.Value.CityProvince.OrgId || d.OrgId == _cityBaseConfiguration.Value.CityProvinceAssign.OrgId));
            d.HandlerOrgId == cityBase.CityProvince.OrgId || d.HandlerOrgId == cityBase.CityProvinceAssign.OrgId);
        if (step is not null)
        {
            step.FileJson = await _fileRepository.AddFileAsync(files, workflow.ExternalId, step.Id, cancellationToken);
            var trace = workflow.Traces.First(d => d.StepId == step.Id);
            trace.FileJson = step.FileJson;
            await _workflowStepRepository.UpdateAsync(step, cancellationToken);
            await _workflowTraceRepository.UpdateAsync(trace, cancellationToken);
        }
    }

    #endregion
}