using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Haoliang.Data; using Haoliang.Data.Entities; using Haoliang.Models.System; using Haoliang.Data.Repositories; namespace Haoliang.Data.Repositories { public interface ILogRepository : IRepository { Task> GetLogsAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null, string category = null); Task GetLogCountAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null); Task ArchiveLogsAsync(DateTime cutoffDate); Task ClearLogsAsync(); Task> GetRecentLogsAsync(int count = 100); Task> GetErrorLogsAsync(DateTime? startDate = null, DateTime? endDate = null); Task GetLogStatisticsAsync(DateTime date); Task LogExistsAsync(string logId); Task> GetLogsBySourceAsync(string source); } public class LogRepository : Repository, ILogRepository { private readonly CNCDbContext _context; public LogRepository(CNCDbContext context) : base(context) { _context = context; } public async Task> GetLogsAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null, string category = null) { var query = _context.Logs.AsQueryable(); if (logLevel.HasValue) { query = query.Where(l => l.LogLevel == logLevel.Value); } if (startDate.HasValue) { query = query.Where(l => l.LogTime >= startDate.Value); } if (endDate.HasValue) { query = query.Where(l => l.LogTime <= endDate.Value); } if (!string.IsNullOrEmpty(category)) { query = query.Where(l => l.Category == category); } return await query .OrderByDescending(l => l.LogTime) .ToListAsync(); } public async Task GetLogCountAsync(LogLevel? logLevel = null, DateTime? startDate = null, DateTime? endDate = null) { var query = _context.Logs.AsQueryable(); if (logLevel.HasValue) { query = query.Where(l => l.LogLevel == logLevel.Value); } if (startDate.HasValue) { query = query.Where(l => l.LogTime >= startDate.Value); } if (endDate.HasValue) { query = query.Where(l => l.LogTime <= endDate.Value); } return await query.CountAsync(); } public async Task ArchiveLogsAsync(DateTime cutoffDate) { var logsToArchive = await _context.Logs .Where(l => l.LogTime < cutoffDate) .ToListAsync(); if (logsToArchive.Any()) { // In a real implementation, you would move these to an archive table or file // For now, we'll just delete them _context.Logs.RemoveRange(logsToArchive); await SaveAsync(); } } public async Task ClearLogsAsync() { _context.Logs.RemoveRange(_context.Logs); await SaveAsync(); } public async Task> GetRecentLogsAsync(int count = 100) { return await _context.Logs .OrderByDescending(l => l.LogTime) .Take(count) .ToListAsync(); } public async Task> GetErrorLogsAsync(DateTime? startDate = null, DateTime? endDate = null) { var query = _context.Logs .Where(l => l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical); if (startDate.HasValue) { query = query.Where(l => l.LogTime >= startDate.Value); } if (endDate.HasValue) { query = query.Where(l => l.LogTime <= endDate.Value); } return await query .OrderByDescending(l => l.LogTime) .ToListAsync(); } public async Task GetLogStatisticsAsync(DateTime date) { var startOfDay = date.Date; var endOfDay = startOfDay.AddDays(1); var logs = await _context.Logs .Where(l => l.LogTime >= startOfDay && l.LogTime <= endOfDay) .ToListAsync(); var stats = new LogStatistics { Date = date, TotalLogs = logs.Count, ErrorLogs = logs.Count(l => l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical), WarningLogs = logs.Count(l => l.LogLevel == LogLevel.Warning), InfoLogs = logs.Count(l => l.LogLevel == LogLevel.Information), DebugLogs = logs.Count(l => l.LogLevel == LogLevel.Debug), LogSources = logs.GroupBy(l => l.Source) .ToDictionary(g => g.Key, g => g.Count()) }; return stats; } public async Task LogExistsAsync(string logId) { return await _context.Logs .AnyAsync(l => l.LogId == logId); } public async Task> GetLogsBySourceAsync(string source) { return await _context.Logs .Where(l => l.Source == source) .OrderByDescending(l => l.LogTime) .ToListAsync(); } } }