using CellularManagement.Application.Features.AtOperations.Commands.CreateAtOperation; using CellularManagement.Application.Features.AtOperations.Commands.DeleteAtOperation; using CellularManagement.Application.Features.AtOperations.Commands.UpdateAtOperation; using CellularManagement.Application.Features.AtOperations.Queries.GetAtOperationById; using CellularManagement.Application.Features.AtOperations.Queries.GetAtOperations; using CellularManagement.Domain.Common; using CellularManagement.Presentation.Abstractions; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace CellularManagement.Presentation.Controllers { /// /// AT操作管理控制器 /// [Route("api/at-operations")] [ApiController] [Authorize] public class AtOperationsController : ApiController { private readonly ILogger _logger; /// /// 初始化AT操作控制器 /// public AtOperationsController(IMediator mediator, ILogger logger) : base(mediator) { _logger = logger; } /// /// 获取AT操作列表 /// [HttpGet] public async Task> GetAll([FromQuery] GetAtOperationsQuery query) { _logger.LogInformation("开始获取AT操作列表,页码: {PageNumber}, 每页数量: {PageSize}, 搜索关键词: {Keyword}, 设备ID: {DeviceId}", query.PageNumber, query.PageSize, query.Keyword, query.DeviceId); var result = await mediator.Send(query); if (!result.IsSuccess) { _logger.LogWarning("获取AT操作列表失败: {Message}", result.ErrorMessages); return result; } _logger.LogInformation("成功获取AT操作列表,共 {Count} 条记录", result.Data?.TotalCount ?? 0); return result; } /// /// 获取AT操作详情 /// [HttpGet("{id}")] public async Task> GetById(string id) { _logger.LogInformation("开始获取AT操作详情,操作ID: {OperationId}", id); var query = new GetAtOperationByIdQuery { Id = id }; var result = await mediator.Send(query); if (!result.IsSuccess) { _logger.LogWarning("获取AT操作详情失败: {Message}", result.ErrorMessages); return result; } _logger.LogInformation("成功获取AT操作详情,操作ID: {OperationId}", id); return result; } /// /// 创建AT操作 /// [HttpPost] public async Task> Create([FromBody] CreateAtOperationCommand command) { _logger.LogInformation("开始创建AT操作,设备ID: {DeviceId}, 端口: {Port}, 命令: {Command}", command.DeviceId, command.Port, command.Command); var result = await mediator.Send(command); if (!result.IsSuccess) { _logger.LogWarning("创建AT操作失败: {Message}", result.ErrorMessages); return result; } _logger.LogInformation("成功创建AT操作,操作ID: {OperationId}", result.Data?.Id); return result; } /// /// 更新AT操作 /// [HttpPut("{id}")] public async Task> Update(string id, [FromBody] UpdateAtOperationCommand command) { _logger.LogInformation("开始更新AT操作,操作ID: {OperationId}", id); if (id != command.Id) { _logger.LogWarning("AT操作ID不匹配,路径ID: {PathId}, 命令ID: {CommandId}", id, command.Id); return OperationResult.CreateFailure("AT操作ID不匹配"); } var result = await mediator.Send(command); if (!result.IsSuccess) { _logger.LogWarning("更新AT操作失败: {Message}", result.ErrorMessages); return result; } _logger.LogInformation("成功更新AT操作,操作ID: {OperationId}", id); return result; } /// /// 删除AT操作 /// [HttpDelete("{id}")] public async Task> Delete(string id) { _logger.LogInformation("开始删除AT操作,操作ID: {OperationId}", id); var command = new DeleteAtOperationCommand { Id = id }; var result = await mediator.Send(command); if (!result.IsSuccess) { _logger.LogWarning("删除AT操作失败: {Message}", result.ErrorMessages); return result; } _logger.LogInformation("成功删除AT操作,操作ID: {OperationId}", id); return result; } } }