using System.Linq.Expressions; using DS.Module.Core; using DS.Module.Core.Extensions; 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 Masuit.Tools.Systems; namespace DS.WMS.Core.Flow.Method; /// /// 租户端工作流实例管理 /// public class ClientFlowInstanceService : FlowInstanceService, IClientFlowInstanceService { /// /// 初始化 /// /// public ClientFlowInstanceService(IServiceProvider serviceProvider) : base(serviceProvider) { } /// /// 确定工作流是否正在运行中 /// /// 工作流类型 /// 业务类型 /// 业务ID /// public async Task IsRunning(TaskBaseTypeEnum type, BusinessType? businessType = null, params long[] ids) { return await Exists(type, businessType, x => x.FlowStatus == FlowStatusEnum.Ready || x.FlowStatus == FlowStatusEnum.Running, ids); } /// /// 确定工作流是否存在 /// /// 工作流类型 /// 业务类型 /// 自定义查询条件 /// 业务ID /// public async Task Exists(TaskBaseTypeEnum? type = null, BusinessType? businessType = null, Expression>? expression = null, params long[] ids) { if (ids == null || ids.Length == 0) return false; return await Db.Queryable().Where(x => ids.Contains(x.BusinessId)) .WhereIF(type.HasValue, x => x.AuditType == type) .WhereIF(businessType.HasValue, x => x.BusinessType == businessType) .WhereIF(expression != null, expression) .AnyAsync(); } /// /// 根据业务ID获取待处理的工作流 /// /// 工作流类型 /// /// 业务类型 /// 业务ID /// public async Task> GetInstanceByBSIdAsync(TaskBaseTypeEnum type, BusinessType? businessType = null, params long[] ids) { if (ids == null || ids.Length == 0) return []; return await Db.Queryable().Where(x => x.AuditType == type && ids.Contains(x.BusinessId) && (x.FlowStatus == FlowStatusEnum.Ready || x.FlowStatus == FlowStatusEnum.Running)) .WhereIF(businessType.HasValue, x => x.BusinessType == businessType) .OrderByDescending(x => x.CreateTime).Take(1).ToListAsync(); } 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, MarkerNotifyURL = template.MarkerNotifyURL, CallbackURL = template.CallbackURL, AuditType = template.AuditType }; } protected override string GetForkNodeMakers(FlowRuntime wfruntime, string forkNodeId) { string makerList = ""; var nextNode = wfruntime.NextNode; var nextConditionNodeId = wfruntime.GetNextConditionNodeId(nextNode); var nextConditionNode = wfruntime.ChildNodes.Find(x => x.Id == nextConditionNodeId); if (nextConditionNode == null) return makerList; if (nextConditionNode.AssigneeType == "role") { var users = Db.Queryable().Where(x => nextConditionNode.Roles.Contains(x.RoleId.ToString())) .Select(x => x.UserId).Distinct().ToList(); makerList = string.Join(",", users); } else if (nextConditionNode.AssigneeType == "user") { makerList = string.Join(",", nextConditionNode.Users); } return makerList; } protected override FlowRuntime CreateRuntimeService(FlowInstance instance) { return new FlowRuntime(instance, Db, TenantDb); } /// /// 获取工作流实例信息 /// /// 业务ID /// 业务类型 /// 审批类型 /// public DataResult> GetFlowInstances(long businessId, BusinessType? businessType, params TaskBaseTypeEnum[]? types) { var query = Db.Queryable().Where(x => x.BusinessId == businessId && x.FlowStatus != FlowStatusEnum.Ready) .WhereIF(businessType.HasValue, x => x.BusinessType == businessType.Value) .WhereIF(types != null && types.Length > 0, x => types.Contains(x.AuditType.Value)); var list = query.Select(x => new FlowInstance { Id = x.Id, Content = x.Content, AuditType = x.AuditType, FlowStatus = x.FlowStatus }).ToList(); var list2 = list.Select(x => x.Adapt()).ToList(); foreach (var item in list2) { item.AuditTypeName = item.AuditType?.GetDescription(); } return DataResult>.Success(list2); } /// /// 工作流审批 /// /// 工作流实例 /// public DataResult AuditFlowInstance(FlowAuditInfo info) { ArgumentNullException.ThrowIfNull(info, nameof(info)); if (info.Instance == null) return DataResult.Failed("未能获取工作流", MultiLanguageConst.Operation_Failed); if (info.Instance.FlowStatus == FlowStatusEnum.Approve) return DataResult.Failed("该工作流已完成!", MultiLanguageConst.FlowInstanceFinished); if (info.Instance.CallbackURL.IsNullOrEmpty()) { var template = Db.Queryable().Where(x => x.Id == info.Instance.TemplateId) .Select(x => new { x.MarkerNotifyURL, x.CallbackURL }).First(); info.Instance.CallbackURL = template.CallbackURL; info.Instance.MarkerNotifyURL = template.MarkerNotifyURL; } return AuditFlowCore(info.Status, info.AuditNote, info.Instance); } /// /// 撤销审核 /// /// 任务类型 /// 业务ID /// 业务类型 /// 备注 /// public async Task WithdrawAsync(TaskBaseTypeEnum taskType, long[] bsIds, BusinessType? businessType = null, string? note = null) { var instances = await GetInstanceByBSIdAsync(taskType, businessType, bsIds); return await WithdrawCoreAsync(instances, note); } /// /// 撤销审核 /// /// ID /// 备注 /// public async Task WithdrawAsync(long[] ids, string? note = null) { var instances = ids.Select(x => new FlowInstance { Id = x, ActivityId = FlowChild.END, MakerList = string.Empty, FlowStatus = FlowStatusEnum.Draft, Note = note, //Deleted = true, //DeleteBy = userId, //DeleteTime = dt, //DeleteUserName = userInfo.UserName }).ToList(); return await WithdrawCoreAsync(instances, note); } internal async Task WithdrawCoreAsync(List instances, string? note = null) { ArgumentNullException.ThrowIfNull(instances, nameof(instances)); long userId = long.Parse(User.UserId); var userInfo = await Db.Queryable().Where(x => x.Id == userId).Select(x => new { x.Id, x.UserName }).FirstAsync(); //DateTime dt = DateTime.Now; await Db.Ado.BeginTranAsync(); try { await Db.Updateable(instances).UpdateColumns(x => new { x.ActivityId, x.MakerList, x.FlowStatus, x.Note, //x.Deleted, //x.DeleteBy, //x.DeleteTime, //x.DeleteUserName }).ExecuteCommandAsync(); var historys = instances.Select(x => new FlowInstanceHistory { InstanceId = x.Id, Content = $"【撤销】由{userInfo.UserName}撤销,备注:{note}", Result = -1, UserName = userInfo.UserName }).ToList(); await Db.Insertable(historys).ExecuteCommandAsync(); await Db.Ado.CommitTranAsync(); return DataResult.Successed("撤销成功!", MultiLanguageConst.FlowInstanceCancelSuccess); } catch (Exception ex) { await ex.LogAsync(Db); await Db.Ado.RollbackTranAsync(); return DataResult.FailedWithDesc(MultiLanguageConst.Operation_Failed); } } }