using iFileProxy.Config; using iFileProxy.Models; using iFileProxy.Services; using Microsoft.AspNetCore.Mvc; using Serilog; using System.Text.Json; using iFileProxy.Attributes; using Serilog.Events; namespace iFileProxy.Controllers { [Authorize(UserMask.Admin,UserMask.SuperAdmin)] [Route("[controller]")] [ApiController] public class ManagementController(TaskManager taskManager, DatabaseGateService dbGateService) : ControllerBase { public TaskManager _taskManager = taskManager; public DatabaseGateService _dbGateService = dbGateService; private readonly Serilog.ILogger _logger = Log.Logger.ForContext(); // 查看任务详情 // 删除任务 // 停止任务 // 查看任务Process信息 // 立即执行任务 // 查看系统配置 // 获取全部任务信息 /// /// 获取任务列表(支持分页) /// /// 页码,从1开始 /// 每页数量 /// 可选的任务状态过滤 /// 分页后的任务列表 [HttpGet("GetTaskList")] public ActionResult GetTaskList([FromQuery] int page = 1, [FromQuery] int pageSize = 10, [FromQuery] TaskState? status = null) { try { var result = _dbGateService.GetPagedTaskList(page, pageSize, status); result.Data.ForEach(task => { if (task.Status == TaskState.Queuing) task.QueuePosition = _taskManager.GetQueuePosition(task.TaskId); }); return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = result }); } catch (Exception ex) { _logger.Error($"获取任务列表失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "获取任务列表失败" }); } } [HttpGet("GetSystemConfig")] public ActionResult GetSystemConfig() { var c = AppConfig.GetCurrConfig(); c.Database.Databases.ForEach(db => { db.Password = "**********"; }); return Ok(new CommonRsp { Retcode = 0 , Data = c,Message = "succ"}); } /// /// 删除指定任务 /// /// 任务ID /// 删除结果 [HttpDelete("DeleteTask/{taskId}")] public ActionResult DeleteTask(string taskId) { try { // 先检查任务是否存在 var taskInfo = JsonSerializer.Deserialize>(_dbGateService.GetTaskInfoByTid(taskId))[0]; if (taskInfo == null) { return Ok(new CommonRsp { Retcode = 1, Message = "任务不存在" }); } // 如果任务正在运行,先尝试中断 if (taskInfo.Status == TaskState.Running && _taskManager.GetRunningTaskInfo(taskId) != null) { _taskManager.TryKillTask(_taskManager.GetRunningTaskInfo(taskId)); } // 删除任务记录 var result = _dbGateService.DeleteTaskByTid(taskId); return Ok(new CommonRsp { Retcode = result ? 0 : 1, Message = result ? "删除成功" : "删除失败" }); } catch (Exception ex) { _logger.Error($"删除任务失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "删除任务失败" }); } } /// /// 获取任务详细信息 /// /// 任务ID /// 任务详细信息 [HttpGet("GetTaskDetail/{taskId}")] public ActionResult GetTaskDetail(string taskId) { try { var taskInfo = _dbGateService.GetTaskDetail(taskId); if (taskInfo == null) { return Ok(new CommonRsp { Retcode = 1, Message = "任务不存在" }); } return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = taskInfo }); } catch (Exception ex) { _logger.Error($"获取任务详情失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "获取任务详情失败" }); } } /// /// 终止正在运行的任务 /// /// 任务ID /// 操作结果 [HttpPost("KillTask/{taskId}")] public ActionResult KillTask(string taskId) { try { // 先获取任务信息 var taskInfo = _taskManager.GetRunningTaskInfo(taskId); if (taskInfo == null) { return Ok(new CommonRsp { Retcode = 1, Message = "任务不存在" }); } // 检查任务是否在运行 if (taskInfo.Status != TaskState.Running) { return Ok(new CommonRsp { Retcode = 1, Message = "任务不在运行状态" }); } // 尝试终止任务 _taskManager.TryKillTask(taskInfo); return Ok(new CommonRsp { Retcode = 0, Message = "任务已终止" }); } catch (Exception ex) { _logger.Error($"终止任务失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "终止任务失败" }); } } /// /// 获取任务状态概览数据 /// /// 各状态任务的数量统计 [HttpGet("GetTaskOverview")] public ActionResult GetTaskOverview() { try { var overview = _dbGateService.GetTaskStatusOverview(); return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = overview }); } catch (Exception ex) { _logger.Error($"获取任务概览失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "获取任务概览失败" }); } } /// /// 获取正在运行任务的进程信息 /// /// 任务ID /// 进程详细信息 [HttpGet("GetProcessInfo/{taskId}")] public ActionResult GetProcessInfo(string taskId) { try { var processInfo = _taskManager.GetProcessInfo(taskId); if (processInfo == null) { return Ok(new CommonRsp { Retcode = 1, Message = "任务不存在或未在运行状态" }); } return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = processInfo }); } catch (Exception ex) { _logger.Error($"获取进程信息失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "获取进程信息失败" }); } } /// /// 重试失败的任务 /// /// 任务ID /// 重试结果 [HttpPost("RetryTask/{taskId}")] public ActionResult RetryTask(string taskId) { try { var result = _taskManager.RetryTask(taskId); return Ok(new CommonRsp { Retcode = result ? 0 : 1, Message = result ? "任务重试已开始" : "任务重试失败" }); } catch (Exception ex) { _logger.Error($"重试任务失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "重试任务失败" }); } } /// /// 将任务移动到等待队列最前面 /// /// 任务ID /// 操作结果 [HttpPost("PrioritizeTask/{taskId}")] public ActionResult PrioritizeTask(string taskId) { try { var result = _taskManager.PrioritizeTask(taskId); return Ok(new CommonRsp { Retcode = result ? 0 : 1, Message = result ? "任务已移至队列首位" : "调整任务优先级失败" }); } catch (Exception ex) { _logger.Error($"调整任务优先级失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "调整任务优先级失败" }); } } /// /// 立即执行指定任务 /// /// 任务ID /// 操作结果 [HttpPost("ExecuteImmediately/{taskId}")] public ActionResult ExecuteImmediately(string taskId) { try { var result = _taskManager.ExecuteImmediately(taskId); return Ok(new CommonRsp { Retcode = result ? 0 : 1, Message = result ? "任务已开始执行" : "立即执行任务失败" }); } catch (Exception ex) { _logger.Error($"立即执行任务失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "立即执行任务失败" }); } } /// /// 获取数据库连接池状态 /// [Authorize(UserMask.SuperAdmin)] [HttpGet("GetConnectionPoolInfo")] public async Task> GetConnectionPoolInfo() { try { var poolInfo = await _dbGateService.GetConnectionPoolInfoAsync(); return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = poolInfo }); } catch (Exception ex) { _logger.Error($"获取连接池信息失败: {ex.Message}"); return Ok(new CommonRsp { Retcode = 1, Message = "获取连接池信息失败" }); } } /// /// 获取系统日志 /// [Authorize(UserMask.SuperAdmin)] [HttpGet("GetSystemLogs")] public async Task> GetSystemLogs( [FromQuery] int page = 1, [FromQuery] int pageSize = 10, [FromQuery] string? level = null, [FromQuery] string? keyword = null, [FromQuery] DateTime? startTime = null, [FromQuery] DateTime? endTime = null) { try { // 解析日志级别 LogEventLevel? logLevel = null; if (!string.IsNullOrEmpty(level) && Enum.TryParse(level, true, out var parsedLevel)) { logLevel = parsedLevel; } var result = await _dbGateService.GetPagedSystemLogsAsync( page, pageSize, logLevel, keyword, startTime, endTime ); return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = result }); } catch (Exception ex) { _logger.Error(ex, "获取系统日志失败"); return Ok(new CommonRsp { Retcode = 1, Message = "获取系统日志失败" }); } } /// /// 获取任务的下载历史 /// [HttpGet("GetDownloadHistory/{taskId}")] public async Task> GetDownloadHistory(string taskId) { try { // 先检查任务是否存在 var taskInfo = JsonSerializer.Deserialize>(_dbGateService.GetTaskInfoByTid(taskId))?[0]; if (taskInfo == null) { return Ok(new CommonRsp { Retcode = 1, Message = "任务不存在" }); } var history = await _dbGateService.GetDownloadHistoryByTaskIdAsync(taskId); return Ok(new CommonRsp { Retcode = 0, Message = "success", Data = history }); } catch (Exception ex) { _logger.Error(ex, "获取下载历史失败"); return Ok(new CommonRsp { Retcode = 1, Message = "获取下载历史失败" }); } } } }