diff --git a/ds-wms-service/DS.WMS.Core/Op/Dtos/TaskInteraction/TaskCreationRequest.cs b/ds-wms-service/DS.WMS.Core/Op/Dtos/TaskInteraction/TaskCreationRequest.cs index f8da14c8..ea70dc8e 100644 --- a/ds-wms-service/DS.WMS.Core/Op/Dtos/TaskInteraction/TaskCreationRequest.cs +++ b/ds-wms-service/DS.WMS.Core/Op/Dtos/TaskInteraction/TaskCreationRequest.cs @@ -27,11 +27,6 @@ namespace DS.WMS.Core.Op.Dtos.TaskInteraction /// public string? RejectReason { get; set; } - /// - /// 下一任务类型 - /// - public TaskBaseTypeEnum? NextType { get; set; } - /// /// 下一任务配置ID /// diff --git a/ds-wms-service/DS.WMS.Core/Op/Interface/TaskInteraction/ITaskService.cs b/ds-wms-service/DS.WMS.Core/Op/Interface/TaskInteraction/ITaskService.cs index 91ffb16d..85a8c88e 100644 --- a/ds-wms-service/DS.WMS.Core/Op/Interface/TaskInteraction/ITaskService.cs +++ b/ds-wms-service/DS.WMS.Core/Op/Interface/TaskInteraction/ITaskService.cs @@ -42,7 +42,7 @@ namespace DS.WMS.Core.Op.Interface.TaskInteraction /// /// 是否使用事务 /// - Task> SetTaskStatusAsync(TaskUpdateRequest request, bool useTransaction = true); + Task SetTaskStatusAsync(TaskUpdateRequest request, bool useTransaction = true); /// /// 发起任务审核 diff --git a/ds-wms-service/DS.WMS.Core/Op/Method/TaskInteraction/TaskService.cs b/ds-wms-service/DS.WMS.Core/Op/Method/TaskInteraction/TaskService.cs index 23aed8eb..45a589da 100644 --- a/ds-wms-service/DS.WMS.Core/Op/Method/TaskInteraction/TaskService.cs +++ b/ds-wms-service/DS.WMS.Core/Op/Method/TaskInteraction/TaskService.cs @@ -1,5 +1,4 @@ -using System.Threading.Tasks; -using DS.Module.Core; +using DS.Module.Core; using DS.Module.Core.Data; using DS.Module.Core.Helpers; using DS.WMS.Core.Flow.Dtos; @@ -93,9 +92,9 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction /// /// 获取给定任务的下一任务类型 /// - /// + /// 当前任务对象 /// - public async Task GetNextTypeAsync(BusinessTask current) + public async Task?> GetNextTypeAsync(BusinessTask current) { var order = await ActionService.GetBusinessDataAsync(current.BusinessId, current.BusinessType); TaskFlowRuner flowRuner = new(TenantDb, ServiceProvider); @@ -103,181 +102,7 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction (TaskFlowDataNameConst.Business, order) ), current.NextId); - if (result.HasValue) - { - current.NextId = result.Value.configId; - return result.Value.taskType; - } - - return null; - } - - /// - /// 获取业务的任务信息 - /// - /// 业务ID - /// 业务类型 - /// 任务类型 - /// - public async Task>> GetTasks(long id, BusinessType businessType, params TaskBaseTypeEnum[] types) - { - var list = await TenantDb.Queryable().Where(x => x.BusinessId == id && x.BusinessType == businessType) - .WhereIF(types != null && types.Length > 0, x => types.Contains(x.TaskType)) - .Select().ToListAsync(); - - var result = DataResult>.Success(list); - result.Count = list.Count; - return result; - } - - /// - /// 返回指定类型的任务是否已存在 - /// - /// 业务ID - /// 业务类型 - /// 任务类型 - /// - public async Task> ExistsAsync(long id, BusinessType businessType, TaskBaseTypeEnum type) - { - var value = await TenantDb.Queryable().AnyAsync(x => - x.BusinessId == id && x.BusinessType == businessType && x.TaskType == type); - return DataResult.Success(value); - } - - /// - /// 确保任务交互模块已授权 - /// - /// - protected virtual async Task EnsureModuleAuthorized() - { - long tid = long.Parse(User.TenantId); - var authStr = await Db.Queryable().Where(x => x.PermissionId == PERMISSION_ID && x.TenantId == tid && - SqlFunc.Subqueryable().Where(spt => spt.PermissionId == x.PermissionId).Any()) - .Select(x => x.AuthNum).FirstAsync(); - if (authStr.IsNullOrEmpty()) - return false; - - var appSecret = await Db.Queryable().Where(x => x.Id == tid).Select(x => x.AppSecret).FirstAsync(); - return int.TryParse(EncrypteHelper.DecryptData(authStr, appSecret), out int authNum) && authNum > 0; - } - - /// - /// 发起任务审核 - /// - /// - /// - public async Task SubmitAuditAsync(TaskRequest request) - { - var task = await GetQuery(request.BusinessId, request.BusinessType, request.TaskType).FirstAsync(); - if (task == null) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); - - if (task.TaskStatus == TaskStatusEnum.Complete) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); - - if (task.TaskStatus == TaskStatusEnum.Pending || task.TaskStatus == TaskStatusEnum.Create) - { - await TenantDb.Ado.BeginTranAsync(); - try - { - var req = new TaskUpdateRequest - { - AutoCreateNext = false, - BusinessId = request.BusinessId, - BusinessType = request.BusinessType, - TaskStatus = TaskStatusEnum.Create, - TaskTypeName = request.TaskTypeName - }; - //重置任务为待处理 - var result = await SetTaskStatusAsync(req, false); - if (!result.Succeeded) - return DataResult.Failed(result.Message, result.MultiCode); - - //创建&启动工作流 - var result2 = await CreateAndStartWorkflow(task, false); - if (!result2.Succeeded) - return result2; - - await TenantDb.Updateable().SetColumns(x => x.TaskStatus == TaskStatusEnum.Create) - .Where(x => x.BusinessId == request.BusinessId && x.BusinessType == request.BusinessType && x.TaskType == request.TaskType) - .ExecuteCommandAsync(); - - //记录日志 - await LogService.WriteLogAsync(req, "重新审批"); - await TenantDb.Ado.CommitTranAsync(); - return result2; - } - catch (Exception ex) - { - await TenantDb.Ado.RollbackTranAsync(); - await ex.LogAsync(Db); - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); - } - } - - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskStatusNotSupported)); - } - - /// - /// 撤销审核任务 - /// - /// - /// - public async Task WithdrawAsync(TaskRequest request) - { - var task = await GetQuery(request.BusinessId, request.BusinessType, request.TaskType).Select(x => new - { - x.Id, - x.TaskStatus - }).FirstAsync(); - if (task == null) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); - - if (task.TaskStatus == TaskStatusEnum.Complete) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); - - return await Delete(request.BusinessId, request.BusinessType, "用户撤销审核", request.TaskType); - } - - /// - /// 任务审核 - /// - /// - /// - public async Task AuditAsync(TaskAuditRequest request) - { - long id = request.Ids[0]; - var task = await GetQuery(id, request.BusinessType.GetValueOrDefault(), request.TaskType).FirstAsync(); - if (task == null) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); - - if (task.TaskStatus == TaskStatusEnum.Complete) - { - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); - } - else if (task.TaskStatus != TaskStatusEnum.Create && task.TaskStatus != TaskStatusEnum.Pending) - { - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskAuditStatusError)); - } - - if (task.FlowId == null) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.FlowNotFound)); - - var result = FlowService.AuditFlowInstance(new FlowAuditInfo - { - AuditNote = request.Remark, - Status = request.Result, - Instance = await Db.Queryable().FirstAsync(x => x.Id == task.FlowId.Value) - }); - - var flow = await Db.Queryable().Where(x => x.Id == task.FlowId.Value).Select(x => new FlowInstance - { - FlowStatus = x.FlowStatus, - MakerList = x.MakerList, - }).FirstAsync(); - - result.Data = new { flow.IsCompleted, flow.FlowStatus }; - return result; + return result.HasValue ? new Tuple(result.Value.taskType, result.Value.configId) : null; } /// @@ -375,13 +200,10 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction TaskType = request.TaskType, TaskStatus = TaskStatusEnum.Create, RecvUsers = string.Join(',', info.Main.RecvUserInfoList.Select(x => x.RecvUserId)), + NextId = request.NextId, CreateBy = long.Parse(User.UserId), - CreateTime = DateTime.Now, - NextType = request.NextType, - NextId = request.NextId + CreateTime = DateTime.Now }; - if (!task.NextType.HasValue) - task.NextType = await GetNextTypeAsync(task); await TenantDb.Insertable(task).ExecuteCommandAsync(); @@ -477,10 +299,10 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction /// /// 是否使用事务 /// - public async Task> SetTaskStatusAsync(TaskUpdateRequest request, bool useTransaction = true) + public async Task SetTaskStatusAsync(TaskUpdateRequest request, bool useTransaction = true) { if (!await EnsureModuleAuthorized()) - return DataResult.SuccessMsg(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.ModuleUnauthorized)), MultiLanguageConst.ModuleUnauthorized); + return DataResult.SuccessedWithDesc(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.ModuleUnauthorized))); if (useTransaction) await TenantDb.Ado.BeginTranAsync(); @@ -489,15 +311,15 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction { BusinessTask task = await GetQuery(request.BusinessId, request.BusinessType, request.TaskType).FirstAsync(); if (task == null) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); if (task.TaskStatus == TaskStatusEnum.Complete) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); if (task.TaskStatus == TaskStatusEnum.Cancel) - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCancelled)); + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCancelled)); var result = await ManagerService.SetTaskStatus(request.BusinessId, request.TaskType, request.TaskStatus, DateTime.Now); if (!result.Succeeded) - return DataResult.Failed(result.Message, result.MultiCode); + return DataResult.Failed(result.Message, result.MultiCode); //触发任务状态变更通知 if (task.TaskStatus != request.TaskStatus) @@ -509,27 +331,28 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction if (AuditTaskTypes.Contains(task.TaskType)) task.FlowId = null; - await TenantDb.Updateable(task).UpdateColumns(x => new { x.TaskStatus, x.FlowId, x.RejectReason }).ExecuteCommandAsync(); - - if (task.TaskStatus == TaskStatusEnum.Complete) + if (task.TaskStatus == TaskStatusEnum.Complete && request.AutoCreateNext) { - //若存在下一任务,则继续创建 - if (task.NextType.HasValue && request.AutoCreateNext) + var tuple = await GetNextTypeAsync(task); + if (tuple != null) { + task.NextType = tuple.Item1; var req = new TaskCreationRequest { BusinessId = request.BusinessId, BusinessType = request.BusinessType, - TaskTypeName = task.NextType.ToString(), RecvUserIdList = task.RecvUserIdArray, - NextId = task.NextId + TaskTypeName = tuple.Item1.ToString(), + NextId = tuple.Item2 }; result = await CreateTaskAsync(req, false); if (!result.Succeeded) - return DataResult.Failed("创建下一关联任务时返回错误:" + result.Message, result.MultiCode); + return DataResult.Failed("创建下一关联任务时返回错误:" + result.Message, result.MultiCode); } } + await TenantDb.Updateable(task).UpdateColumns(x => new { x.TaskStatus, x.FlowId, x.RejectReason }).ExecuteCommandAsync(); + //记录日志 await LogService.WriteLogAsync(request); @@ -549,8 +372,8 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction ActionService.TriggerActionAsync(task, additionalData: dic); } - - return DataResult.Success(task.TaskStatus == TaskStatusEnum.Complete ? await GetNextTypeAsync(task) : null); + + return DataResult.Success; } catch (Exception ex) { @@ -558,7 +381,7 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction await TenantDb.Ado.RollbackTranAsync(); await ex.LogAsync(Db); - return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } @@ -572,6 +395,174 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction return Task.CompletedTask; } + /// + /// 获取业务的任务信息 + /// + /// 业务ID + /// 业务类型 + /// 任务类型 + /// + public async Task>> GetTasks(long id, BusinessType businessType, params TaskBaseTypeEnum[] types) + { + var list = await TenantDb.Queryable().Where(x => x.BusinessId == id && x.BusinessType == businessType) + .WhereIF(types != null && types.Length > 0, x => types.Contains(x.TaskType)) + .Select().ToListAsync(); + + var result = DataResult>.Success(list); + result.Count = list.Count; + return result; + } + + /// + /// 返回指定类型的任务是否已存在 + /// + /// 业务ID + /// 业务类型 + /// 任务类型 + /// + public async Task> ExistsAsync(long id, BusinessType businessType, TaskBaseTypeEnum type) + { + var value = await TenantDb.Queryable().AnyAsync(x => + x.BusinessId == id && x.BusinessType == businessType && x.TaskType == type); + return DataResult.Success(value); + } + + /// + /// 确保任务交互模块已授权 + /// + /// + protected virtual async Task EnsureModuleAuthorized() + { + long tid = long.Parse(User.TenantId); + var authStr = await Db.Queryable().Where(x => x.PermissionId == PERMISSION_ID && x.TenantId == tid && + SqlFunc.Subqueryable().Where(spt => spt.PermissionId == x.PermissionId).Any()) + .Select(x => x.AuthNum).FirstAsync(); + if (authStr.IsNullOrEmpty()) + return false; + + var appSecret = await Db.Queryable().Where(x => x.Id == tid).Select(x => x.AppSecret).FirstAsync(); + return int.TryParse(EncrypteHelper.DecryptData(authStr, appSecret), out int authNum) && authNum > 0; + } + + /// + /// 发起任务审核 + /// + /// + /// + public async Task SubmitAuditAsync(TaskRequest request) + { + var task = await GetQuery(request.BusinessId, request.BusinessType, request.TaskType).FirstAsync(); + if (task == null) + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); + + if (task.TaskStatus == TaskStatusEnum.Complete) + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); + + if (task.TaskStatus == TaskStatusEnum.Pending || task.TaskStatus == TaskStatusEnum.Create) + { + await TenantDb.Ado.BeginTranAsync(); + try + { + var req = new TaskUpdateRequest + { + AutoCreateNext = false, + BusinessId = request.BusinessId, + BusinessType = request.BusinessType, + TaskStatus = TaskStatusEnum.Create, + TaskTypeName = request.TaskTypeName + }; + //重置任务为待处理 + var result = await SetTaskStatusAsync(req, false); + if (!result.Succeeded) + return DataResult.Failed(result.Message, result.MultiCode); + + //创建&启动工作流 + var result2 = await CreateAndStartWorkflow(task, false); + if (!result2.Succeeded) + return result2; + + await TenantDb.Updateable().SetColumns(x => x.TaskStatus == TaskStatusEnum.Create) + .Where(x => x.BusinessId == request.BusinessId && x.BusinessType == request.BusinessType && x.TaskType == request.TaskType) + .ExecuteCommandAsync(); + + //记录日志 + await LogService.WriteLogAsync(req, "重新审批"); + await TenantDb.Ado.CommitTranAsync(); + return result2; + } + catch (Exception ex) + { + await TenantDb.Ado.RollbackTranAsync(); + await ex.LogAsync(Db); + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); + } + } + + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskStatusNotSupported)); + } + + /// + /// 撤销审核任务 + /// + /// + /// + public async Task WithdrawAsync(TaskRequest request) + { + var task = await GetQuery(request.BusinessId, request.BusinessType, request.TaskType).Select(x => new + { + x.Id, + x.TaskStatus + }).FirstAsync(); + if (task == null) + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); + + if (task.TaskStatus == TaskStatusEnum.Complete) + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); + + return await Delete(request.BusinessId, request.BusinessType, "用户撤销审核", request.TaskType); + } + + /// + /// 任务审核 + /// + /// + /// + public async Task AuditAsync(TaskAuditRequest request) + { + long id = request.Ids[0]; + var task = await GetQuery(id, request.BusinessType.GetValueOrDefault(), request.TaskType).FirstAsync(); + if (task == null) + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); + + if (task.TaskStatus == TaskStatusEnum.Complete) + { + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskCompleted)); + } + else if (task.TaskStatus != TaskStatusEnum.Create && task.TaskStatus != TaskStatusEnum.Pending) + { + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.TaskAuditStatusError)); + } + + if (task.FlowId == null) + return DataResult.FailedWithDesc(nameof(MultiLanguageConst.FlowNotFound)); + + var result = FlowService.AuditFlowInstance(new FlowAuditInfo + { + AuditNote = request.Remark, + Status = request.Result, + Instance = await Db.Queryable().FirstAsync(x => x.Id == task.FlowId.Value) + }); + + var flow = await Db.Queryable().Where(x => x.Id == task.FlowId.Value).Select(x => new FlowInstance + { + FlowStatus = x.FlowStatus, + MakerList = x.MakerList, + }).FirstAsync(); + + result.Data = new { flow.IsCompleted, flow.FlowStatus }; + return result; + } + /// /// 通知审批执行人变更 /// diff --git a/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportTaskController.cs b/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportTaskController.cs index af625f1f..d26069b9 100644 --- a/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportTaskController.cs +++ b/ds-wms-service/DS.WMS.OpApi/Controllers/SeaExportTaskController.cs @@ -78,10 +78,10 @@ namespace DS.WMS.OpApi.Controllers /// /// [HttpPost, Route("SetTaskStatus")] - public async Task> SetTaskStatusAsync([FromBody] TaskUpdateRequest request) + public async Task SetTaskStatusAsync([FromBody] TaskUpdateRequest request) { if (!ModelState.IsValid) - return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); + return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.SetTaskStatusAsync(request); }