using System.Net; using DS.Module.Core; using DS.WMS.Core.Flow.Dtos; using DS.WMS.Core.Op.Entity; using DS.WMS.Core.TaskInteraction.Dtos; using DS.WMS.Core.TaskInteraction.Entity; using DS.WMS.Core.TaskInteraction.Interface; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; namespace DS.WMS.OpApi.Controllers { /// /// 海运出口任务API /// public class SeaExportTaskController : ApiController { readonly ISeaExportTaskService taskService; /// /// 构造函数 /// /// public SeaExportTaskController(ISeaExportTaskService service) { taskService = service; } /// /// 获取任务列表 /// /// 业务ID /// 业务类型(可选参数) /// 是否获取子任务,默认为否 /// 任务类型名称(支持多个,可选参数) /// /// /GetTasks?taskTypes=任务类型1&taskTypes=任务类型2 [HttpGet, Route("GetTasks")] public async Task>> GetTasksAsync(long businessId, BusinessType? businessType, bool withSubTask = false, params string[] taskTypes) { TaskBaseTypeEnum[]? types = null; if (taskTypes?.Length > 0) types = taskTypes.Select(Enum.Parse).ToArray(); return await taskService.GetTasks(businessId, businessType, withSubTask, types); } /// /// 获取提单确认信息 /// /// 业务ID /// 业务类型(可选参数) /// [HttpGet, Route("GetBLConfirmation")] public async Task> GetBLConfirmationAsync(long businessId, BusinessType businessType) { return await taskService.GetBLConfirmationAsync(businessId, businessType); } /// /// 获取指定业务的任务日志 /// /// /// 业务ID /// 业务类型(可选参数) /// [HttpGet, Route("GetLogs")] public async Task>> GetLogsAsync([FromServices] ITaskLogService logService, [FromQuery] long businessId, [FromQuery] BusinessType? businessType) { return await logService.ReadLogAsync(businessId, businessType); } /// /// 触发自动订舱 /// /// /// 业务ID /// 业务类型(可空,默认=海运出口) /// [HttpPost, Route("AutomaticBooking")] public async Task AutomaticBookingAsync([FromServices] IActionManagerService actionService, [FromQuery] long businessId, [FromQuery] BusinessType businessType = BusinessType.OceanShippingExport) { return await actionService.AutomaticBooking(businessId, businessType); } /// /// 运行任务动作 /// /// /// 任务信息 /// [HttpPost, Route("RunTaskAction")] public async Task RunTaskActionAsync([FromServices] IActionManagerService actionService, [FromBody] TaskRequest request) { var result = await taskService.GetTasks(request.BusinessId, request.BusinessType, false, request.TaskType); if (result.Data?.Count > 0) { var task = result.Data[0]; task.BusinessId = request.BusinessId; task.BusinessType = request.BusinessType; Dictionary? dic = null; if (request.ExtraData is JObject jobject) { dic = []; foreach (var item in jobject) dic.Add(item.Key, item.Value?.ToString()); } return await actionService.TriggerActionAsync(task, false, request.ExtraData == null ? null : dic); } return DataResult.FailedWithDesc(nameof(MultiLanguageConst.EmptyData)); } /// /// 创建服务项目任务 /// /// 业务ID /// 业务类型(可空,默认=海运出口) /// [HttpPost, Route("CreateSubTask")] public async Task CreateSubTaskAsync([FromQuery] long businessId, [FromQuery] BusinessType businessType = BusinessType.OceanShippingExport) { HttpStatusCode statusCode = HttpStatusCode.NoContent; try { await taskService.CreateSubTaskAsync(new BusinessTask { BusinessId = businessId, BusinessType = businessType }); } catch { statusCode = HttpStatusCode.InternalServerError; } return new StatusCodeResult((int)statusCode); } /// /// 创建关联任务 /// /// /// [HttpPost, Route("CreateTask")] public async Task CreateTaskAsync([FromBody] TaskCreationRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.CreateTaskAsync(request); } /// /// 批量创建关联任务 /// /// /// /// 使用【Ids】字段指定多个业务ID [HttpPost, Route("CreateMultipleTask")] public async Task CreateMultipleTaskAsync([FromBody] TaskCreationRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.CreateMultipleTaskAsync(request); } /// /// 设置任务状态 /// /// /// [HttpPost, Route("SetTaskStatus")] public async Task SetTaskStatusAsync([FromBody] TaskUpdateRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.SetTaskStatusAsync(request); } /// /// 设置任务步骤 /// /// /// [HttpPost, Route("SetSteps")] public async Task SetStepsAsync([FromBody] TaskRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.SetStepsAsync(request); } /// /// 手动发起任务审核(用于二次审核) /// /// /// [HttpPost, Route("SubmitAudit")] public async Task SubmitAuditAsync([FromBody] TaskRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.SubmitAuditAsync(request); } /// /// 任务审核 /// /// /// [HttpPost, Route("Audit")] public async virtual Task AuditAsync([FromBody] TaskAuditRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.AuditAsync(request); } /// /// 撤销审核任务(目前仅支持单票) /// /// /// [HttpPost, Route("Withdraw")] public async virtual Task AuditAsync([FromBody] TaskRequest request) { if (!ModelState.IsValid) return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest); return await taskService.WithdrawAsync(request); } /// /// 审批完成回调更新(无需客户端手动调用) /// /// 回调信息 /// [HttpPost, Route("UpdateBusiness")] public async Task UpdateBusinessAsync([FromBody] FlowCallback callback) { await taskService.UpdateBusinessAsync(callback); return StatusCode((int)HttpStatusCode.NoContent); } /// /// 通知审批执行人变更(无需客户端手动调用) /// /// 回调信息 /// [HttpPost, Route("MarkerChanged")] public async Task MarkerChangedAsync([FromBody] MarkerChangedCallback callback) { await taskService.MarkerChangedAsync(callback); return StatusCode((int)HttpStatusCode.NoContent); } /// /// 删除任务(内部调试用) /// /// 业务ID /// 业务类型 /// 任务类型(为空时删除全部任务) /// [HttpPost, Route("Delete")] public async Task Delete(long id, BusinessType? businessType, TaskBaseTypeEnum? taskType = null) { return await taskService.DeleteAsync(id, businessType, "API请求删除任务", taskType.HasValue ? [taskType.Value] : []); } } }