using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using VberZero.Authorization.Users;
using VberZero.BaseSystem;
using VberZero.DomainService.Notifications;
using VberZero.DomainService.Notifications.Data;
using VberZero.Workflow.DesignInfo;
using VberZero.Workflow.Persistence;
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace VberZero.Workflows;
///
/// 通用审批
///
public class GeneralAuditingStepBody : StepBodyAsync, ITransientDependency
{
private const string ActionName = "UserAuditEvent";
private readonly IAppNotifier _appNotifier;
private readonly VzUserManager _userManager;
private readonly IRepository _auditorRepository;
private readonly IRepository _WorkflowDefinitionRepository;
private readonly IRepository _workflowInfoRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public GeneralAuditingStepBody(IAppNotifier appNotifier, VzUserManager userManager, IRepository auditorRepository, IRepository workflowDefinitionRepository, IRepository workflowInfoRepository, IUnitOfWorkManager unitOfWorkManager)
{
_appNotifier = appNotifier;
_userManager = userManager;
_auditorRepository = auditorRepository;
_WorkflowDefinitionRepository = workflowDefinitionRepository;
_workflowInfoRepository = workflowInfoRepository;
_unitOfWorkManager = unitOfWorkManager;
}
///
/// 审核人
///
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public long UserId { get; set; }
[UnitOfWork]
public override async Task RunAsync(IStepExecutionContext context)
{
WorkflowInfo workflow; WorkflowDefinitionInfo workflowDefinition;
using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant))
{
workflow = await _workflowInfoRepository.FirstOrDefaultAsync(a => a.Id == context.Workflow.Id);
workflowDefinition = await
_WorkflowDefinitionRepository.FirstOrDefaultAsync(a => a.Id == context.Workflow.WorkflowDefinitionId && a.Version ==
context.Workflow.Version);
}
if (workflow == null) return new ExecutionResult() { Proceed = false };
using (_unitOfWorkManager.Current.SetTenantId(workflow.TenantId))
{
if (!context.ExecutionPointer.EventPublished)
{
var user = await _userManager.GetUserByIdAsync(workflow.CreatorUserId ?? 0);
var auditUserInfo = await _userManager.GetUserByIdAsync(UserId);
var recordId = Guid.NewGuid().ToString("N");
await _auditorRepository.InsertAsync(new CommonWorkflowAuditorInfo()
{
Id = recordId,
WorkflowId = workflow.Id,
ExecutionPointerId = context.ExecutionPointer.Id,
Status = VzDefinition.WorkflowAuditStatus.UnAudited,
UserId = UserId,
TenantId = workflow.TenantId,
UserIdentityName = auditUserInfo.FullName,
UserHeadPhoto = auditUserInfo.AvatarPath
});
//通知审批人
await _appNotifier.SendWorkflowAuditNotifyAsync(UserId, workflow.TenantId, new WorkflowAuditNotificationData(workflowDefinition.Title, user.Surname, workflow.Id, recordId));
DateTime effectiveDate = DateTime.MinValue;
return ExecutionResult.WaitForEvent(ActionName, Guid.NewGuid().ToString("N"), effectiveDate);
}
var passCount = await _auditorRepository.CountAsync(u => u.ExecutionPointerId == context.ExecutionPointer.Id && u.UserId == UserId && u.Status == VzDefinition.WorkflowAuditStatus.Pass);
if (passCount > 0)
{
return ExecutionResult.Next();
}
}
//再次开启事件推送
context.ExecutionPointer.EventPublished = false;
return ExecutionResult.WaitForEvent(ActionName, Guid.NewGuid().ToString("N"), DateTime.MinValue);
}
}