using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Haoliang.Models.Device; using Haoliang.Data.Repositories; namespace Haoliang.Data.Repositories { public interface IDeviceRepository : IRepository { Task> GetOnlineDevicesAsync(); Task> GetAvailableDevicesAsync(); Task GetByDeviceCodeAsync(string deviceCode); Task> GetByTemplateIdAsync(int templateId); Task DeviceExistsAsync(string deviceCode); Task UpdateDeviceStatusAsync(int deviceId, bool isOnline, bool isAvailable); Task CountOnlineDevicesAsync(); Task CountAvailableDevicesAsync(); } public class DeviceRepository : Repository, IDeviceRepository { private readonly CNCDbContext _context; public DeviceRepository(CNCDbContext context) : base(context) { _context = context; } public async Task> GetOnlineDevicesAsync() { return await _context.Devices .Where(d => d.IsOnline) .OrderBy(d => d.DeviceName) .ToListAsync(); } public async Task> GetAvailableDevicesAsync() { return await _context.Devices .Where(d => d.IsAvailable) .OrderBy(d => d.DeviceName) .ToListAsync(); } public async Task GetByDeviceCodeAsync(string deviceCode) { return await _context.Devices .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode); } public async Task> GetByTemplateIdAsync(int templateId) { return await _context.Devices .Where(d => d.TemplateId == templateId) .OrderBy(d => d.DeviceName) .ToListAsync(); } public async Task DeviceExistsAsync(string deviceCode) { return await _context.Devices .AnyAsync(d => d.DeviceCode == deviceCode); } public async Task UpdateDeviceStatusAsync(int deviceId, bool isOnline, bool isAvailable) { var device = await GetByIdAsync(deviceId); if (device != null) { device.IsOnline = isOnline; device.IsAvailable = isAvailable; if (isOnline) { device.LastCollectionTime = DateTime.Now; } Update(device); await SaveAsync(); } } public async Task CountOnlineDevicesAsync() { return await _context.Devices .CountAsync(d => d.IsOnline); } public async Task CountAvailableDevicesAsync() { return await _context.Devices .CountAsync(d => d.IsAvailable); } } public interface IDeviceStatusRepository : IRepository { Task> GetLatestStatusAsync(int deviceId, int count = 10); Task GetLatestStatusByDeviceIdAsync(int deviceId); Task> GetStatusByDateRangeAsync(int deviceId, DateTime startDate, DateTime endDate); Task CountStatusRecordsAsync(int deviceId); Task DeleteOldStatusRecordsAsync(int deviceId, int keepDays = 30); } public class DeviceStatusRepository : Repository, IDeviceStatusRepository { private readonly CNCDbContext _context; public DeviceStatusRepository(CNCDbContext context) : base(context) { _context = context; } public async Task> GetLatestStatusAsync(int deviceId, int count = 10) { return await _context.DeviceStatus .Where(ds => ds.DeviceId == deviceId) .OrderByDescending(ds => ds.RecordTime) .Take(count) .ToListAsync(); } public async Task GetLatestStatusByDeviceIdAsync(int deviceId) { return await _context.DeviceStatus .Where(ds => ds.DeviceId == deviceId) .OrderByDescending(ds => ds.RecordTime) .FirstOrDefaultAsync(); } public async Task> GetStatusByDateRangeAsync(int deviceId, DateTime startDate, DateTime endDate) { return await _context.DeviceStatus .Where(ds => ds.DeviceId == deviceId && ds.RecordTime >= startDate && ds.RecordTime <= endDate) .OrderBy(ds => ds.RecordTime) .ToListAsync(); } public async Task CountStatusRecordsAsync(int deviceId) { return await _context.DeviceStatus .CountAsync(ds => ds.DeviceId == deviceId); } public async Task DeleteOldStatusRecordsAsync(int deviceId, int keepDays = 30) { var cutoffDate = DateTime.Now.AddDays(-keepDays); var oldRecords = await _context.DeviceStatus .Where(ds => ds.DeviceId == deviceId && ds.RecordTime < cutoffDate) .ToListAsync(); if (oldRecords.Any()) { RemoveRange(oldRecords); await SaveAsync(); } } } }