using AmtScanner.Api.Data; using AmtScanner.Api.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace AmtScanner.Api.Controllers; [ApiController] [Route("api/amt-devices")] [Authorize] public class AmtDevicesController : ControllerBase { private readonly AppDbContext _db; private readonly ILogger _logger; public AmtDevicesController(AppDbContext db, ILogger logger) { _db = db; _logger = logger; } /// /// 获取已接管设备列表 /// [HttpGet] public async Task GetList([FromQuery] int page = 1, [FromQuery] int pageSize = 20, [FromQuery] string? search = null) { var query = _db.AmtDevices_new.AsQueryable(); if (!string.IsNullOrEmpty(search)) { query = query.Where(d => d.IpAddress.Contains(search) || d.Uuid.Contains(search) || (d.Hostname != null && d.Hostname.Contains(search))); } var total = await query.CountAsync(); var items = await query .OrderByDescending(d => d.CreatedAt) .Skip((page - 1) * pageSize) .Take(pageSize) .Select(d => new { d.Uuid, d.IpAddress, d.SubnetMask, d.Gateway, d.AmtUsername, // 不返回密码 d.AmtVersion, d.Hostname, d.CreatedAt, d.UpdatedAt }) .ToListAsync(); return Ok(ApiResponse.Success(new { items, total, page, pageSize })); } /// /// 获取单个设备详情 /// [HttpGet("{uuid}")] public async Task GetDevice(string uuid) { var device = await _db.AmtDevices_new.FindAsync(uuid); if (device == null) { return NotFound(ApiResponse.Fail(404, "设备不存在")); } return Ok(ApiResponse.Success(new { device.Uuid, device.IpAddress, device.SubnetMask, device.Gateway, device.AmtUsername, device.AmtVersion, device.Hostname, device.CreatedAt, device.UpdatedAt })); } /// /// 更新设备信息 /// [HttpPut("{uuid}")] public async Task UpdateDevice(string uuid, [FromBody] UpdateAmtDeviceRequest request) { var device = await _db.AmtDevices_new.FindAsync(uuid); if (device == null) { return NotFound(ApiResponse.Fail(404, "设备不存在")); } if (!string.IsNullOrEmpty(request.IpAddress)) device.IpAddress = request.IpAddress; if (!string.IsNullOrEmpty(request.SubnetMask)) device.SubnetMask = request.SubnetMask; if (!string.IsNullOrEmpty(request.Gateway)) device.Gateway = request.Gateway; if (!string.IsNullOrEmpty(request.AmtUsername)) device.AmtUsername = request.AmtUsername; if (!string.IsNullOrEmpty(request.AmtPassword)) device.AmtPassword = request.AmtPassword; if (!string.IsNullOrEmpty(request.Hostname)) device.Hostname = request.Hostname; device.UpdatedAt = DateTime.UtcNow; await _db.SaveChangesAsync(); return Ok(ApiResponse.Success(null, "更新成功")); } /// /// 删除设备 /// [HttpDelete("{uuid}")] public async Task DeleteDevice(string uuid) { var device = await _db.AmtDevices_new.FindAsync(uuid); if (device == null) { return NotFound(ApiResponse.Fail(404, "设备不存在")); } _db.AmtDevices_new.Remove(device); await _db.SaveChangesAsync(); return Ok(ApiResponse.Success(null, "删除成功")); } } public class UpdateAmtDeviceRequest { public string? IpAddress { get; set; } public string? SubnetMask { get; set; } public string? Gateway { get; set; } public string? AmtUsername { get; set; } public string? AmtPassword { get; set; } public string? Hostname { get; set; } }