using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Haoliang.Models.DataCollection; using Haoliang.Data.Repositories; namespace Haoliang.Data.Repositories { public interface ICollectionLogRepository : IRepository { Task> GetByDeviceAsync(int deviceId); Task> GetByLogLevelAsync(LogLevel logLevel); Task GetErrorCountAsync(int deviceId); Task ArchiveLogsAsync(int daysToKeep = 30); Task ClearLogsAsync(); Task> GetRecentLogsAsync(int count = 100); Task GetLogStatisticsAsync(DateTime date); Task> GetLogsByCategoryAsync(string category); Task LogExistsAsync(int logId); } public class CollectionLogRepository : Repository, ICollectionLogRepository { private readonly CNCDbContext _context; public CollectionLogRepository(CNCDbContext context) : base(context) { _context = context; } public async Task> GetByDeviceAsync(int deviceId) { return await _context.CollectionLogs .Where(l => l.DeviceId == deviceId) .OrderByDescending(l => l.LogTime) .ToListAsync(); } public async Task> GetByLogLevelAsync(LogLevel logLevel) { return await _context.CollectionLogs .Where(l => l.LogLevel == logLevel) .OrderByDescending(l => l.LogTime) .ToListAsync(); } public async Task GetErrorCountAsync(int deviceId) { return await _context.CollectionLogs .CountAsync(l => l.DeviceId == deviceId && (l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical)); } public async Task ArchiveLogsAsync(int daysToKeep = 30) { var cutoffDate = DateTime.Now.AddDays(-daysToKeep); var logsToArchive = await _context.CollectionLogs .Where(l => l.LogTime < cutoffDate) .ToListAsync(); if (logsToArchive.Any()) { // In a real implementation, you would move these to an archive table _context.CollectionLogs.RemoveRange(logsToArchive); await SaveAsync(); } } public async Task ClearLogsAsync() { _context.CollectionLogs.RemoveRange(_context.CollectionLogs); await SaveAsync(); } public async Task> GetRecentLogsAsync(int count = 100) { return await _context.CollectionLogs .OrderByDescending(l => l.LogTime) .Take(count) .ToListAsync(); } public async Task GetLogStatisticsAsync(DateTime date) { var startOfDay = date.Date; var endOfDay = startOfDay.AddDays(1); var logs = await _context.CollectionLogs .Where(l => l.LogTime >= startOfDay && l.LogTime <= endOfDay) .ToListAsync(); var stats = new CollectionLogStatistics { 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), DeviceLogs = logs.GroupBy(l => l.DeviceId) .ToDictionary(g => g.Key, g => g.Count()), LogCategories = logs.GroupBy(l => l.LogCategory) .ToDictionary(g => g.Key, g => g.Count()) }; return stats; } public async Task> GetLogsByCategoryAsync(string category) { return await _context.CollectionLogs .Where(l => l.LogCategory == category) .OrderByDescending(l => l.LogTime) .ToListAsync(); } public async Task LogExistsAsync(int logId) { return await _context.CollectionLogs .AnyAsync(l => l.LogId == logId); } } }