using System.Net;
using DS.Module.Core;
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 Mapster;
using Microsoft.AspNetCore.Mvc;
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(
[FromQuery] long businessId, [FromQuery] BusinessType businessType, [FromQuery] params string[] taskTypes)
{
TaskBaseTypeEnum[]? types = null;
if (taskTypes?.Length > 0)
types = taskTypes.Select(Enum.Parse).ToArray();
return await taskService.GetTasks(businessId, businessType, types);
}
///
/// 获取指定业务的任务日志
///
///
/// 业务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, request.TaskType);
if (result.Data?.Count > 0)
return await actionService.TriggerActionAsync(result.Data[0].Adapt(), false);
return DataResult.FailedWithDesc(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);
}
///
/// 设置任务状态
///
///
///
[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("SubmitAudit")]
public async Task SubmitAuditAsync(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("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.Delete(id, businessType, "API请求删除任务", taskType.HasValue ? [taskType.Value] : []);
}
}
}