You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
5.6 KiB
C#
164 lines
5.6 KiB
C#
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<CNCDevice>
|
|
{
|
|
Task<IEnumerable<CNCDevice>> GetOnlineDevicesAsync();
|
|
Task<IEnumerable<CNCDevice>> GetAvailableDevicesAsync();
|
|
Task<CNCDevice> GetByDeviceCodeAsync(string deviceCode);
|
|
Task<IEnumerable<CNCDevice>> GetByTemplateIdAsync(int templateId);
|
|
Task<bool> DeviceExistsAsync(string deviceCode);
|
|
Task UpdateDeviceStatusAsync(int deviceId, bool isOnline, bool isAvailable);
|
|
Task<int> CountOnlineDevicesAsync();
|
|
Task<int> CountAvailableDevicesAsync();
|
|
}
|
|
|
|
public class DeviceRepository : Repository<CNCDevice>, IDeviceRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public DeviceRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<CNCDevice>> GetOnlineDevicesAsync()
|
|
{
|
|
return await _context.Devices
|
|
.Where(d => d.IsOnline)
|
|
.OrderBy(d => d.DeviceName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<CNCDevice>> GetAvailableDevicesAsync()
|
|
{
|
|
return await _context.Devices
|
|
.Where(d => d.IsAvailable)
|
|
.OrderBy(d => d.DeviceName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<CNCDevice> GetByDeviceCodeAsync(string deviceCode)
|
|
{
|
|
return await _context.Devices
|
|
.FirstOrDefaultAsync(d => d.DeviceCode == deviceCode);
|
|
}
|
|
|
|
public async Task<IEnumerable<CNCDevice>> GetByTemplateIdAsync(int templateId)
|
|
{
|
|
return await _context.Devices
|
|
.Where(d => d.TemplateId == templateId)
|
|
.OrderBy(d => d.DeviceName)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> 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<int> CountOnlineDevicesAsync()
|
|
{
|
|
return await _context.Devices
|
|
.CountAsync(d => d.IsOnline);
|
|
}
|
|
|
|
public async Task<int> CountAvailableDevicesAsync()
|
|
{
|
|
return await _context.Devices
|
|
.CountAsync(d => d.IsAvailable);
|
|
}
|
|
}
|
|
|
|
public interface IDeviceStatusRepository : IRepository<DeviceStatus>
|
|
{
|
|
Task<IEnumerable<DeviceStatus>> GetLatestStatusAsync(int deviceId, int count = 10);
|
|
Task<DeviceStatus> GetLatestStatusByDeviceIdAsync(int deviceId);
|
|
Task<IEnumerable<DeviceStatus>> GetStatusByDateRangeAsync(int deviceId, DateTime startDate, DateTime endDate);
|
|
Task<int> CountStatusRecordsAsync(int deviceId);
|
|
Task DeleteOldStatusRecordsAsync(int deviceId, int keepDays = 30);
|
|
}
|
|
|
|
public class DeviceStatusRepository : Repository<DeviceStatus>, IDeviceStatusRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public DeviceStatusRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<DeviceStatus>> 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<DeviceStatus> GetLatestStatusByDeviceIdAsync(int deviceId)
|
|
{
|
|
return await _context.DeviceStatus
|
|
.Where(ds => ds.DeviceId == deviceId)
|
|
.OrderByDescending(ds => ds.RecordTime)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<DeviceStatus>> 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<int> 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();
|
|
}
|
|
}
|
|
}
|
|
} |