//using Hotline.Share.Dtos;
//using SqlSugar;
//using XF.Domain.Entities;
//using XF.Domain.Exceptions;
//using XF.Domain.Repository;

//namespace Hotline.FlowEngine.Workflows;

//public class WorkflowStepHandler : CreationEntity
//{
//    public string WorkflowId { get; set; }
//    public string ExternalId { get; set; }
//    public string WorkflowStepId { get; set; }

//    /// <summary>
//    /// 流程指派类型
//    /// </summary>
//    public EFlowAssignType FlowAssignType { get; set; }

//    public string? UserId { get; set; }
//    public string? Username { get; set; }
//    public string? OrgId { get; set; }
//    public string? OrgName { get; set; }
//    public string? RoleId { get; set; }
//    public string? RoleName { get; set; }

//    /// <summary>
//    /// 是否为实际办理者
//    /// </summary>
//    public bool IsActualHandler { get; set; }

//    [Navigate(NavigateType.ManyToOne, nameof(WorkflowStepId))]
//    public WorkflowStep WorkflowStep { get; set; }

//    #region method

//    public static WorkflowStepHandler Create(string workflowId, string externalId, EFlowAssignType flowAssignType, string? userId = null, string? username = null,
//        string? orgId = null, string? orgName = null, string? roleId = null, string? roleName = null)
//    {
//        var handler = new WorkflowStepHandler
//        {
//            WorkflowId = workflowId,
//            ExternalId = externalId,
//        };
//        handler.Assign(flowAssignType, userId, username, orgId, orgName, roleId, roleName);
//        return handler;
//    }

//    public void Assign(EFlowAssignType assignType, string? userId, string? username,
//    string? orgId, string? orgName, string? roleId, string? roleName)
//    {
//        FlowAssignType = assignType;
//        switch (assignType)
//        {
//            case EFlowAssignType.Org:
//                if (string.IsNullOrEmpty(orgId) || string.IsNullOrEmpty(orgName))
//                    throw new ArgumentNullException(nameof(orgId));
//                OrgId = orgId;
//                OrgName = orgName;
//                break;
//            case EFlowAssignType.User:
//                if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(username) ||
//                   string.IsNullOrEmpty(orgId) || string.IsNullOrEmpty(orgName))
//                    throw new ArgumentNullException(nameof(userId));
//                UserId = userId;
//                Username = username;
//                OrgId = orgId;
//                OrgName = orgName;
//                break;
//            //case EFlowAssignType.Role:
//            //    if (string.IsNullOrEmpty(roleId) || string.IsNullOrEmpty(roleName))
//            //        throw new ArgumentNullException(nameof(roleId));
//            //    RoleId = roleId;
//            //    RoleName = roleName;
//            //    break;
//            default:
//                throw new ArgumentOutOfRangeException(nameof(assignType), assignType, null);
//        }
//    }

//    public bool IsHandler(ICollection<string> roleIds, string? userId = null, string? orgId = null)
//    {
//        switch (FlowAssignType)
//        {
//            case EFlowAssignType.Org:
//                if (string.IsNullOrEmpty(OrgId))
//                    throw new UserFriendlyException($"数据异常, {nameof(OrgId)}");
//                return OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase);
//            case EFlowAssignType.User:
//                if (string.IsNullOrEmpty(UserId))
//                    throw new UserFriendlyException($"数据异常, {nameof(UserId)}");
//                return UserId.Equals(userId, StringComparison.OrdinalIgnoreCase);
//            //case EFlowAssignType.Role:
//            //    if (string.IsNullOrEmpty(RoleId))
//            //        throw new UserFriendlyException($"数据异常, {nameof(RoleId)}");
//            //    return roleIds.Any() && roleIds.Contains(RoleId, StringComparer.OrdinalIgnoreCase);
//            default:
//                throw new ArgumentOutOfRangeException();
//        }
//    }

//    public Kv GetHandler()
//    {
//        return FlowAssignType switch
//        {
//            EFlowAssignType.Org => new Kv(OrgId, OrgName),
//            EFlowAssignType.User => new Kv(UserId, Username),
//            //EFlowAssignType.Role => new Kv(RoleId, RoleName),
//            _ => throw new ArgumentOutOfRangeException()
//        };
//    }

//    #endregion
//}