using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AdminService.Admin; using Common; using CommonInfrastructure; using Domain.Admin.Admin; using Domain.Admin.Admin.ENum; using Domain.Admin.Admin.Param; using Microsoft.AspNetCore.Mvc; using MongoRepository; namespace AdminApi.Controllers { [Route("adminapi/[controller]")] [ApiController] public class OperationLogController : BaseController { private readonly IAdminOperationLogService _operationLogService; private readonly IMongoBaseRepository _systemUserRepo; private readonly IMongoBaseRepository _logRepo; public OperationLogController(IAdminOperationLogService operationLogService, IMongoBaseRepository systemUserRepo, IMongoBaseRepository logRepo) { _operationLogService = operationLogService; _systemUserRepo = systemUserRepo; _logRepo = logRepo; } [HttpPost("list")] public async Task> GetList([FromBody] OperationLogQueryParam param) { var (list, count) = await _operationLogService.QueryPage(param); if (list == null) return this.ToPageResult(new List(), 0); var dtos = list.Select(MapToDto); return this.ToPageResult(dtos, (int)count); } [HttpGet("admins")] public async Task>> GetAdmins() { var users = await _systemUserRepo.FilterByAsync(x => x.State == 1); var result = users.Select(x => new AdminOptionDto { Value = x.Id, Label = x.DisplayName }).ToList(); return this.ToResult(result); } [HttpGet("modules")] public BaseResponse> GetModules() { var modules = Enum.GetValues() .Select(x => new ModuleOptionDto { Value = (int)x, Label = ((int)x).ToString() }) .ToList(); return this.ToResult(modules); } [HttpPost("Delete")] public async Task> Delete(string id) { var result = await _logRepo.DeleteByIdAsync(id); if (result.ok) return this.ToResult("删除成功"); else return this.ToFailResult("删除失败"); } [HttpPost("BatchDelete")] public async Task> BatchDelete([FromBody] List ids) { int success = 0, fail = 0; foreach (var id in ids) { var result = await _logRepo.DeleteByIdAsync(id); if (result.ok) success++; else fail++; } if (fail == 0) return this.ToResult($"成功删除 {success} 条"); return this.ToResult($"删除完成:成功 {success} 条,失败 {fail} 条"); } private static OperationLogDto MapToDto(AdminOperationLog entity) { return new OperationLogDto { Id = entity.Id, AdminId = entity.AdminId ?? "", AdminName = entity.AdminName ?? "", Module = entity.Module, ActionType = entity.ActionType ?? "", Description = entity.Description ?? "", HttpMethod = entity.HttpMethod ?? "", RequestPath = entity.RequestPath ?? "", Ip = entity.Ip ?? "", Status = entity.Status, OperateTime = entity.OperateTime }; } } public class OperationLogDto { public string Id { get; set; } public string AdminId { get; set; } public string AdminName { get; set; } public int Module { get; set; } public string ActionType { get; set; } public string Description { get; set; } public string HttpMethod { get; set; } public string RequestPath { get; set; } public string Ip { get; set; } public int Status { get; set; } public DateTime OperateTime { get; set; } } public class AdminOptionDto { public string Value { get; set; } public string Label { get; set; } } public class ModuleOptionDto { public int Value { get; set; } public string Label { get; set; } } }