using DS.Module.Core;
using DS.Module.SqlSugar;
using DS.WMS.Core.Flow.Dtos;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core.Flow.Interface;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
namespace DS.WMS.Core.Flow.Method;
///
/// 租户端工作流实例管理
///
public class ClientFlowInstanceService : FlowInstanceService, IClientFlowInstanceService
{
readonly ISaasDbService saasService;
public ClientFlowInstanceService(IServiceProvider serviceProvider) : base(serviceProvider)
{
saasService = serviceProvider.GetRequiredService();
}
protected override FlowInstance BuildInstance(CreateFlowInstanceReq req)
{
if (string.IsNullOrEmpty(req.TemplateId.ToString()))
return null;
FlowTemplateTenant template = db.Queryable().First(x => x.Id == req.TemplateId);
return template == null ? null : new FlowInstance
{
CustomName = template.Name,
TemplateId = template.Id,
BusinessId = req.BusinessId,
BusinessType = req.BusinessType,
PermissionId = template.PermissionId,
ColumnView = template.ColumnView,
Content = template.Content,
CallbackURL = template.CallbackURL,
AuditType = template.AuditType
};
}
protected override string GetForkNodeMakers(FlowRuntime wfruntime, string forkNodeId)
{
string makerList = "";
var nextNode = wfruntime.NextNode;
var nextConditionNodeId = wfruntime.GetClientNextConditionNodeId(nextNode);
var nextConditionNode = wfruntime.ChildNodes.First(x => x.Id == nextConditionNodeId);
if (nextConditionNode.AssigneeType == "role")
{
var users = db.Queryable().Where(x =>
nextConditionNode.Roles.Contains(x.RoleId.ToString()))
.Select(x => x.UserId).Distinct().ToList();
makerList = GenericHelper.ArrayToString(users, makerList);
}
else if (nextConditionNode.AssigneeType == "user")
{
makerList = string.Join(",", nextConditionNode.Users);
}
return makerList;
}
protected override FlowRuntime CreateRuntimeService(FlowInstance instance)
{
return new FlowRuntime(instance, db, saasService, user);
}
///
/// 获取工作流实例信息
///
/// 业务ID
/// 业务类型
///
public DataResult> GetFlowInstances(long businessId, BusinessType? businessType)
{
var query = db.Queryable().Where(x => x.BusinessId == businessId && x.BusinessType == businessType && x.FlowStatus != 1);
if (businessType.HasValue)
query = query.Where(x => x.BusinessType == businessType.Value);
var list = query.OrderBy(x => x.CreateTime).Select(x => new FlowInstance
{
Content = x.Content,
AuditType = x.AuditType,
//CreateBy = x.CreateBy,
//CreateTime = x.CreateTime
}).ToList();
return DataResult>.Success(list.Select(x => x.Adapt()).ToList());
}
///
/// 工作流审批
///
/// 工作流实例
///
public DataResult AuditFlowInstance(FlowAuditInfo info)
{
if (info == null)
return DataResult.Failed("该工作流不存在!", MultiLanguageConst.FlowInstanceNotExist);
if (info.Instance.FlowStatus == FlowStatusEnum.Approve.ToEnumInt())
return DataResult.Failed("该工作流已完成!", MultiLanguageConst.FlowInstanceFinished);
return AuditFlowCore(info.Status, info.AuditNote, info.Instance);
}
}