146 lines
4.9 KiB
C#
146 lines
4.9 KiB
C#
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<SystemUser> _systemUserRepo;
|
|
private readonly IMongoBaseRepository<AdminOperationLog> _logRepo;
|
|
|
|
public OperationLogController(IAdminOperationLogService operationLogService, IMongoBaseRepository<SystemUser> systemUserRepo, IMongoBaseRepository<AdminOperationLog> logRepo)
|
|
{
|
|
_operationLogService = operationLogService;
|
|
_systemUserRepo = systemUserRepo;
|
|
_logRepo = logRepo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询操作日志
|
|
/// </summary>
|
|
[HttpPost("list")]
|
|
public async Task<PageResponse<OperationLogDto>> GetList([FromBody] OperationLogQueryParam param)
|
|
{
|
|
var (list, count) = await _operationLogService.QueryPage(param);
|
|
if (list == null)
|
|
return this.ToPageResult(new List<OperationLogDto>(), 0);
|
|
var dtos = list.Select(MapToDto);
|
|
return this.ToPageResult(dtos, (int)count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有管理员(下拉选择用)
|
|
/// </summary>
|
|
[HttpGet("admins")]
|
|
public async Task<BaseResponse<List<AdminOptionDto>>> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有模块(下拉选择用)
|
|
/// </summary>
|
|
[HttpGet("modules")]
|
|
public BaseResponse<List<ModuleOptionDto>> GetModules()
|
|
{
|
|
var modules = Enum.GetValues<EnumOperationModule>()
|
|
.Select(x => new ModuleOptionDto { Value = (int)x, Label = ((int)x).ToString() })
|
|
.ToList();
|
|
return this.ToResult(modules);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除操作日志
|
|
/// </summary>
|
|
[HttpPost("Delete")]
|
|
public async Task<BaseResponse<string>> Delete(string id)
|
|
{
|
|
var result = await _logRepo.DeleteByIdAsync(id);
|
|
if (result.ok)
|
|
return this.ToResult("删除成功");
|
|
else
|
|
return this.ToFailResult<string>("删除失败");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除操作日志
|
|
/// </summary>
|
|
[HttpPost("BatchDelete")]
|
|
public async Task<BaseResponse<string>> BatchDelete([FromBody] List<string> 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; }
|
|
}
|
|
}
|