完成编辑设备和删除设备
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
using SqlSugar.DbConvert;
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Excptions;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
@@ -9,49 +7,80 @@ namespace PMSWPF.Data.Repositories;
|
||||
|
||||
public class DeviceRepository
|
||||
{
|
||||
private SqlSugarClient _db;
|
||||
public DeviceRepository()
|
||||
{
|
||||
_db = DbContext.GetInstance();
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加设备
|
||||
/// </summary>
|
||||
/// <param name="device"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public async Task<Device> Add(Device device)
|
||||
{
|
||||
var exist = await _db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||
if (exist != null)
|
||||
throw new InvalidOperationException("设备名称已经存在。");
|
||||
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);
|
||||
var addDbDevice= await _db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync();
|
||||
return addDbDevice.CopyTo<Device>();
|
||||
using (var db=DbContext.GetInstance())
|
||||
{
|
||||
var exist = await db.Queryable<DbDevice>().Where(d => d.Name == device.Name).FirstAsync();
|
||||
if (exist != null)
|
||||
throw new InvalidOperationException("设备名称已经存在。");
|
||||
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);
|
||||
var addDbDevice= await db.InsertNav(dbDevice).Include(d => d.VariableTables).ExecuteReturnEntityAsync();
|
||||
return addDbDevice.CopyTo<Device>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<int> Edit(Device device)
|
||||
{
|
||||
using (var db=DbContext.GetInstance())
|
||||
{
|
||||
return await db.Updateable<DbDevice>(device.CopyTo<DbDevice>()).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<Device>> GetAll()
|
||||
{
|
||||
var dlist = await _db.Queryable<DbDevice>().Includes(d => d.VariableTables,dv=>dv.Device).ToListAsync();
|
||||
var devices = new List<Device>();
|
||||
foreach (var dbDevice in dlist)
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var device = dbDevice.CopyTo<Device>();
|
||||
devices.Add(device);
|
||||
}
|
||||
var dlist = await db.Queryable<DbDevice>().Includes(d => d.VariableTables,dv=>dv.Device).ToListAsync();
|
||||
var devices = new List<Device>();
|
||||
foreach (var dbDevice in dlist)
|
||||
{
|
||||
var device = dbDevice.CopyTo<Device>();
|
||||
devices.Add(device);
|
||||
}
|
||||
|
||||
return devices;
|
||||
return devices;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<DbDevice> GetById(int id)
|
||||
{
|
||||
return await _db.Queryable<DbDevice>().FirstAsync(p => p.Id == id);
|
||||
using (var db=DbContext.GetInstance())
|
||||
{
|
||||
return await db.Queryable<DbDevice>().FirstAsync(p => p.Id == id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async Task<int> DeleteById(int id)
|
||||
{
|
||||
return await _db.Deleteable<DbDevice>(new DbDevice { Id = id }).ExecuteCommandAsync();
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
return await db.Deleteable<DbDevice>(new DbDevice { Id = id }).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,8 @@
|
||||
using System.Windows.Controls;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
|
||||
@@ -17,7 +12,16 @@ public class MenuRepository
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<MenuBean>> GetMenu()
|
||||
public async Task<int> DeleteMenu(MenuBean menu)
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
var childList = await db.Queryable<DbMenu>().ToChildListAsync(it => it.ParentId, menu.Id);
|
||||
return await db.Deleteable<DbMenu>(childList).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<MenuBean>> GetMenuTrees()
|
||||
{
|
||||
// //无主键用法新:5.1.4.110
|
||||
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
||||
@@ -27,10 +31,9 @@ public class MenuRepository
|
||||
{
|
||||
List<MenuBean> menuTree = new();
|
||||
var dbMenuTree = await db.Queryable<DbMenu>().ToTreeAsync(dm => dm.Items, dm => dm.ParentId, 0);
|
||||
|
||||
foreach (var dbMenu in dbMenuTree)
|
||||
{
|
||||
menuTree.Add(dbMenu.CopyTo<MenuBean>());
|
||||
}
|
||||
|
||||
return menuTree;
|
||||
}
|
||||
@@ -102,4 +105,12 @@ public class MenuRepository
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> Edit(MenuBean menu)
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
return await db.Updateable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user