using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Common; using CommonInfrastructure; using Microsoft.AspNetCore.Mvc; using MongoRepository; using UserDomain; using UserDomain.Entity; using UserDomain.Param; namespace AdminApi.LoginLogs { [ApiController, Route("adminapi/[controller]")] public class UserLoginLogController : BaseController { private readonly IMongoBaseRepository _logRepo; public UserLoginLogController(IMongoBaseRepository logRepo) { _logRepo = logRepo; } /// /// 分页查询登录日志 /// [HttpPost("List")] public async Task> GetList([FromBody] UserLoginLogQueryParam param) { var result = await _logRepo.QueryPageAsync( filterExpression: x => (string.IsNullOrEmpty(param.UserId) || x.UserId == param.UserId) && (string.IsNullOrEmpty(param.UserNumber) || x.UserNumber == param.UserNumber) && (string.IsNullOrEmpty(param.UserName) || (x.UserName != null && x.UserName.Contains(param.UserName))) && (!param.LoginType.HasValue || x.LoginType == param.LoginType) && (!param.Status.HasValue || x.Status == param.Status) && (!param.Platform.HasValue || x.Platform == param.Platform) && (!param.StartTime.HasValue || x.LoginTime >= param.StartTime) && (!param.EndTime.HasValue || x.LoginTime <= param.EndTime), orderExpression: x => x.LoginTime, desc: true, pageSize: param.PageSize, pageIndex: param.PageIndex ); var dtos = result.list.Select(MapToDto); return this.ToPageResult(dtos, (int)result.count); } /// /// 获取登录日志详情 /// [HttpGet("Detail")] public async Task> GetDetail(string id) { var log = await _logRepo.FindOneAsync(x => x.Id == id); if (log == null) return this.ToFailResult("日志不存在"); return this.ToResult(MapToDto(log)); } /// /// 删除登录日志 /// [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 UserLoginLogDto MapToDto(UserLoginLog entity) { return new UserLoginLogDto { Id = entity.Id, UserId = entity.UserId ?? "", UserNumber = entity.UserNumber ?? "", UserName = entity.UserName ?? "", LoginType = entity.LoginType, Status = entity.Status, Ip = entity.Ip ?? "", IpLocation = entity.IpLocation ?? "", DeviceType = entity.DeviceType ?? "", DeviceId = entity.DeviceId ?? "", Platform = entity.Platform, ClientVersion = entity.ClientVersion ?? "", FailReason = entity.FailReason ?? "", LoginTime = entity.LoginTime }; } } public class UserLoginLogDto { public string Id { get; set; } public string UserId { get; set; } public string UserNumber { get; set; } public string UserName { get; set; } public int LoginType { get; set; } public int Status { get; set; } public string Ip { get; set; } public string IpLocation { get; set; } public string DeviceType { get; set; } public string DeviceId { get; set; } public int Platform { get; set; } public string ClientVersion { get; set; } public string FailReason { get; set; } public DateTime LoginTime { get; set; } } }