using DS.Module.Core; using DS.Module.Core.Extensions; using DS.WMS.Core.Fee.Method; using DS.WMS.Core.Flow.Dtos; using DS.WMS.Core.Op.Dtos.TaskInteraction; using DS.WMS.Core.Op.Entity; using DS.WMS.Core.Op.Entity.TaskInteraction; using DS.WMS.Core.Op.Interface.TaskInteraction; using DS.WMS.Core.Sys.Entity; using DS.WMS.Core.TaskPlat.Dtos; using DS.WMS.Core.TaskPlat.Interface; using Masuit.Tools.Systems; using Microsoft.Extensions.DependencyInjection; using SqlSugar; namespace DS.WMS.Core.Op.Method.TaskInteraction { /// /// 任务交互服务 /// public class TaskService : FeeServiceBase, ITaskService { /// /// 任务管理服务 /// protected ITaskManageService ManagerService { get; private set; } /// /// 初始化 /// /// protected TaskService(IServiceProvider provider) : base(provider) { ManagerService = provider.GetRequiredService(); } /// /// 创建关联任务 /// /// /// 是否使用事务 /// public async Task CreateTaskAsync(TaskCreationRequest request, bool useTransaction = true) { var task = await GetTaskAsync(request.BusinessId, request.BusinessType, request.TaskType); if (task != null) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Task_Exists)); long tenatId = long.Parse(User.TenantId); string tenatName = Db.Queryable().Where(x => x.Id == tenatId).Select(x => x.Name).First(); var info = new TaskManageOrderMessageInfo { Head = new TaskManageOrderMessageHeadInfo { GID = Guid.NewGuid().ToString(), BSNO = request.BusinessId, MessageType = "WORK_FLOW_TASK", SenderId = "WorkFlow", SenderName = "工作流平台", ReceiverId = "TaskManage", ReceiverName = "任务管理平台", Version = "1.0", RequestDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), RequestAction = "Add" }, Main = new TaskManageOrderMessageMainInfo { TaskType = request.TaskType, TaskSource = TaskSourceEnum.WORK_FLOW, TaskTitle = request.TaskTitle, TaskDesp = request.TaskDescription, TaskUserId = User.UserId, TaskUserName = User.UserName, RecvUserId = User.UserId, RecvUserName = User.UserName, TaskTenatId = tenatId, TaskTenatName = tenatName } }; if (info.Main.TaskTitle.IsNullOrEmpty()) { var biz = await TenantDb.Queryable().Select(x => new { x.Id, x.MBLNO, x.Vessel, x.Voyno, x.ETD, }).FirstAsync(x => x.Id == request.BusinessId); info.Main.TaskDesp = info.Main.TaskTitle = $"{request.TaskType.GetDescription()} {biz.Vessel} {biz.Voyno} ETD:{biz.ETD?.ToString("yyyy-MM-dd")} BLNo:{biz.MBLNO}"; } if (useTransaction) await TenantDb.Ado.BeginTranAsync(); try { var result = await ManagerService.InitTaskJob(info); if (!result.Succeeded) return result; task = new BusinessTask { BusinessId = request.BusinessId, BusinessType = request.BusinessType, TaskType = request.TaskType, TaskStatus = TaskStatusEnum.Create, CreateBy = long.Parse(User.UserId), CreateTime = DateTime.Now }; task.NextType = GetNextType(task); await TenantDb.Insertable(task).ExecuteCommandAsync(); result = await OnTaskCreated(task); if (!result.Succeeded) return result; if (useTransaction) await TenantDb.Ado.CommitTranAsync(); return result; } catch (Exception ex) { if (useTransaction) await TenantDb.Ado.RollbackTranAsync(); await ex.LogAsync(Db); return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } /// /// 当任务创建时调用 /// /// /// protected virtual Task OnTaskCreated(BusinessTask task) { return Task.FromResult(DataResult.Success); } /// /// 设置任务状态 /// /// /// public async Task SetTaskStatusAsync(TaskUpdateRequest request) { await TenantDb.Ado.BeginTranAsync(); try { var result = await ManagerService.SetTaskStatus(request.BusinessId, request.TaskType, request.TaskStatus, DateTime.Now); if (!result.Succeeded) return result; //更新当前任务状态 BusinessTask task = await GetTaskAsync(request.BusinessId, request.BusinessType, request.TaskType); if (task == null) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); if (task.TaskStatus!= request.TaskStatus) await OnTaskStatusChanged(request); task.TaskStatus = request.TaskStatus; await TenantDb.Updateable(task).UpdateColumns(x => x.TaskStatus).ExecuteCommandAsync(); if (task.TaskStatus == TaskStatusEnum.Complete) { //若存在下一任务,则继续创建 if (task.NextType.HasValue) { var req = new TaskCreationRequest { BusinessId = request.BusinessId, BusinessType = request.BusinessType, TaskType = task.NextType.Value }; await CreateTaskAsync(req, false); } } await TenantDb.Ado.CommitTranAsync(); return DataResult.Success; } catch (Exception ex) { await TenantDb.Ado.RollbackTranAsync(); await ex.LogAsync(Db); return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } } /// /// 当任务状态发生变化时调用 /// /// /// protected virtual Task OnTaskStatusChanged(TaskUpdateRequest request) { return Task.CompletedTask; } /// /// 审批完成回调更新 /// /// 回调信息 /// /// 为null时引发 public virtual async Task UpdateBusinessAsync(FlowCallback callback) { ArgumentNullException.ThrowIfNull(callback, nameof(callback)); //更新任务状态为完成 await SetTaskStatusAsync(new TaskUpdateRequest { BusinessId = callback.BusinessId, BusinessType = callback.BusinessType.GetValueOrDefault(), TaskType = TaskBaseTypeEnum.WAIT_ORDER_AUDIT, TaskStatus = TaskStatusEnum.Complete }); } protected internal async Task GetTaskAsync(long id, BusinessType businessType, TaskBaseTypeEnum taskType) { return await TenantDb.Queryable().FirstAsync(x => x.BusinessId == id && x.BusinessType == businessType && x.TaskType == taskType); } /// /// 获取给定任务的下一任务类型 /// /// 任务信息 /// public static TaskBaseTypeEnum? GetNextType(BusinessTask current) { if (current.TaskType == TaskBaseTypeEnum.WAIT_CHECKOUT_BILL) //流程的最后一步 return null; int currentTypeVal = (int)current.TaskType; if (currentTypeVal >= 300) //300开始的枚举值为可选服务项目,不存在前后关联性 return null; return (TaskBaseTypeEnum)currentTypeVal++; } } }