2025-06-10 22:13:06 +08:00
|
|
|
using PMSWPF.Data.Entities;
|
2025-06-12 18:41:46 +08:00
|
|
|
using PMSWPF.Excptions;
|
2025-06-20 18:53:29 +08:00
|
|
|
using PMSWPF.Extensions;
|
2025-06-23 15:15:10 +08:00
|
|
|
using PMSWPF.Helper;
|
2025-06-20 18:53:29 +08:00
|
|
|
using PMSWPF.Models;
|
2025-06-24 20:48:38 +08:00
|
|
|
using SqlSugar;
|
2025-06-10 22:13:06 +08:00
|
|
|
|
|
|
|
|
namespace PMSWPF.Data.Repositories;
|
|
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
public class DeviceRepository
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-24 20:48:38 +08:00
|
|
|
private SqlSugarClient _db;
|
2025-06-26 19:36:27 +08:00
|
|
|
public DeviceRepository()
|
2025-06-24 20:48:38 +08:00
|
|
|
{
|
|
|
|
|
_db = DbContext.GetInstance();
|
|
|
|
|
}
|
2025-06-20 18:53:29 +08:00
|
|
|
public async Task<bool> Add(Device device)
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-23 17:01:06 +08:00
|
|
|
var exist = await _db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
2025-06-26 22:40:20 +08:00
|
|
|
if (exist != null)
|
|
|
|
|
throw new InvalidOperationException("设备名称已经存在。");
|
2025-06-23 17:01:06 +08:00
|
|
|
var dbDevice = new DbDevice();
|
|
|
|
|
device.CopyTo(dbDevice);
|
|
|
|
|
dbDevice.VariableTables = new List<DbVariableTable>();
|
|
|
|
|
// 添加默认变量表
|
|
|
|
|
var dbVariableTable = new DbVariableTable();
|
|
|
|
|
dbVariableTable.Name = "默认变量表";
|
|
|
|
|
dbVariableTable.Description = "默认变量表";
|
|
|
|
|
dbVariableTable.ProtocolType = dbDevice.ProtocolType;
|
|
|
|
|
dbDevice.VariableTables.Add(dbVariableTable);
|
|
|
|
|
return await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteCommandAsync();
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
2025-06-23 15:15:10 +08:00
|
|
|
public async Task<List<Device>> GetAll()
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-23 17:01:06 +08:00
|
|
|
var dlist = await _db.Queryable<DbDevice>().Includes(d => d.VariableTables).ToListAsync();
|
|
|
|
|
var devices = new List<Device>();
|
|
|
|
|
foreach (var dbDevice in dlist)
|
|
|
|
|
{
|
2025-06-24 20:48:38 +08:00
|
|
|
var device = dbDevice.CopyTo<Device>();
|
2025-06-23 17:01:06 +08:00
|
|
|
devices.Add(device);
|
|
|
|
|
}
|
2025-06-23 15:15:10 +08:00
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
return devices;
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
2025-06-12 13:15:55 +08:00
|
|
|
public async Task<DbDevice> GetById(int id)
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-23 17:01:06 +08:00
|
|
|
return await _db.Queryable<DbDevice>().FirstAsync(p => p.Id == id);
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
2025-06-10 22:13:06 +08:00
|
|
|
public async Task<int> DeleteById(int id)
|
|
|
|
|
{
|
2025-06-23 17:01:06 +08:00
|
|
|
return await _db.Deleteable<DbDevice>(new DbDevice { Id = id }).ExecuteCommandAsync();
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
|
|
|
|
}
|