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-10 22:13:06 +08:00
|
|
|
|
|
|
|
|
namespace PMSWPF.Data.Repositories;
|
|
|
|
|
|
|
|
|
|
public class DevicesRepositories:BaseRepositories
|
|
|
|
|
{
|
2025-06-12 18:41:46 +08:00
|
|
|
public DevicesRepositories():base()
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-12 18:41:46 +08:00
|
|
|
var tableExist= _db.DbMaintenance.IsAnyTable<DbDevice>();
|
|
|
|
|
if (!tableExist)
|
|
|
|
|
{
|
|
|
|
|
_db.CodeFirst.InitTables<DbDevice>();
|
|
|
|
|
}
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 13:15:55 +08:00
|
|
|
public async Task<int> Add(DbDevice dbDevice)
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-12 18:41:46 +08:00
|
|
|
var exist=await _db.Queryable<DbDevice>().Where(d=>d.Name==dbDevice.Name).FirstAsync();
|
|
|
|
|
if (exist != null)
|
|
|
|
|
{
|
|
|
|
|
throw new DbExistException("设备名称已经存在。");
|
|
|
|
|
}
|
|
|
|
|
var res= await _db.Insertable<DbDevice>(dbDevice).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
return res;
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 13:15:55 +08:00
|
|
|
public async Task<List<DbDevice>> GetAll()
|
2025-06-10 22:13:06 +08:00
|
|
|
{
|
2025-06-12 13:15:55 +08:00
|
|
|
return await _db.Queryable<DbDevice>().ToListAsync();
|
2025-06-10 22:13: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-12 13:15:55 +08:00
|
|
|
return await _db.Queryable<DbDevice>().FirstAsync(p=>p.Id == id);
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
|
|
|
|
public async Task<int> DeleteById(int id)
|
|
|
|
|
{
|
2025-06-12 13:15:55 +08:00
|
|
|
return await _db.Deleteable<DbDevice>(new DbDevice() { Id = id }).ExecuteCommandAsync();
|
2025-06-10 22:13:06 +08:00
|
|
|
}
|
|
|
|
|
}
|