添加了数据库相关了类,和枚举类型相关的类,并且将枚举类型绑定到前段

This commit is contained in:
2025-06-20 18:53:29 +08:00
parent 5dfce624c4
commit 908dc60439
29 changed files with 437 additions and 151 deletions

View File

@@ -0,0 +1,40 @@
using PMSWPF.Enums;
using SqlSugar;
using SqlSugar.DbConvert;
namespace PMSWPF.Data.Entities;
[SugarTable("DataVariable")]
public class DbDataVariable
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
[SugarColumn(IsNullable = true)]
public int ?VariableTableId { get; set; }
[Navigate(NavigateType.ManyToOne, nameof(DbDataVariable.VariableTableId))]
public DbVariableTable? VariableTable { get; set; }
public string Name { get; set; }
[SugarColumn(IsNullable = true)]
public string? Description { get; set; }
public string NodeId { get; set; }
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public ProtocolType ProtocolType { get; set; }
public string DataType { get; set; }
[SugarColumn(IsNullable = true)]
public List<DbMqtt>? Mqtts { get; set; }
public string DataValue { get; set; }
public string DisplayValue { get; set; }
public DateTime UpdateTime { get; set; }
[SugarColumn(IsNullable = true)]
public DbUser? UpdateUser { get; set; }
public string Converstion { get; set; }
public bool IsDeleted { get; set; }
public bool IsActive { get; set; }
public bool IsSave { get; set; }
public Double SaveRange { get; set; }
public bool IsAlarm { get; set; }
public Double AlarmMin { get; set; }
public Double AlarmMax { get; set; }
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public SignalType SignalType { get; set; }
}

View File

@@ -1,4 +1,8 @@
using System.Net.Sockets;
using PMSWPF.Enums;
using SqlSugar;
using SqlSugar.DbConvert;
using ProtocolType = PMSWPF.Enums.ProtocolType;
namespace PMSWPF.Data.Entities;
@@ -9,9 +13,18 @@ public class DbDevice
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[SugarColumn(IsNullable = true)]
public string? Description { get; set; }
public string Ip { get; set; }
public bool IsActive { get; set; }
public bool IsRuning { get; set; }
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public DeviceType DeviceType { get; set; }
[Navigate(NavigateType.OneToMany, nameof(DbVariableTable.DeviceId))]
[SugarColumn(IsNullable = true)]
public List<DbVariableTable>? VariableTables { get; set; }
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public ProtocolType ProtocolType { get; set; }
}

11
Data/Entities/DbMqtt.cs Normal file
View File

@@ -0,0 +1,11 @@
using SqlSugar;
namespace PMSWPF.Data.Entities;
[SugarTable("Mqtt")]
public class DbMqtt
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
}

View File

@@ -1,64 +0,0 @@
using PMSWPF.Enums;
using SqlSugar;
using SqlSugar.DbConvert;
namespace PMSWPF.Data.Entities
{
public class DbPLC
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int id { get; set; }
/// <summary>
/// PLC名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// PLC品牌
/// </summary>
/// //新版本:存储字符串 SqlSugar 5.1.4.66-preview02
[SugarColumn(ColumnDataType = "varchar(30)", SqlParameterDbType = typeof(EnumToStringConvert))]
public PlcBrand PlcBrand { get; set; }
/// <summary>
/// PLC类型
/// </summary>
public int CpuType { get; set; }
/// <summary>
/// PLC节点ID
/// </summary>
public string NodeID { get; set; }
/// <summary>
/// PLC IP地址
/// </summary>
public string IP { get; set; }
/// <summary>
/// PLC状态
/// </summary>
public string Status { get; set; }
/// <summary>
/// PLC连接类型
/// </summary>
public string ConnType { get; set; }
/// <summary>
/// PLC连接时间
/// </summary>
public DateTime ConnTime { get; set; }
/// <summary>
/// 是否启用
/// </summary>
public bool IsEnable { get; set; }
public DbPLC()
{
}
public DbPLC(string name = "", string nodeID = "", string ip = "", string status = "", string connType = "")
{
this.Name = name;
this.NodeID = nodeID;
this.IP = ip;
this.Status = status;
this.ConnType = connType;
this.ConnTime = DateTime.Now;
}
}
}

View File

@@ -0,0 +1,10 @@
using SqlSugar;
namespace PMSWPF.Data.Entities;
[SugarTable("S7DataVariable")]
public class DbS7DataVariable:DbDataVariable
{
}

11
Data/Entities/DbUser.cs Normal file
View File

@@ -0,0 +1,11 @@
using SqlSugar;
namespace PMSWPF.Data.Entities;
[SugarTable("User")]
public class DbUser
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
}

View File

@@ -0,0 +1,25 @@
using PMSWPF.Enums;
using PMSWPF.Models;
using SqlSugar;
using SqlSugar.DbConvert;
namespace PMSWPF.Data.Entities;
[SugarTable("VariableTable")]
public class DbVariableTable
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//数据库是自增才配自增
public int Id { get; set; }
public string Name { get; set; }
[SugarColumn(IsNullable = true)]
public string? Description { get; set; }
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public ProtocolType ProtocolType { get; set; }
[Navigate(NavigateType.OneToMany, nameof(DbDataVariable.VariableTableId))]
public List<DbDataVariable>? DataVariables { get; set; }
[SugarColumn(IsNullable = true)]
public int? DeviceId { get; set; }
[Navigate(NavigateType.ManyToOne, nameof(DbVariableTable.DeviceId))]
public Device? Device { get; set; }
}

View File

@@ -1,3 +1,4 @@
using PMSWPF.Data.Entities;
using SqlSugar;
namespace PMSWPF.Data.Repositories;
@@ -9,5 +10,23 @@ public class BaseRepositories
public BaseRepositories()
{
_db = DbContext.GetInstance();
_db.DbMaintenance.CreateDatabase();
CheckDbTables();
}
private void CheckDbTables()
{
if(!_db.DbMaintenance.IsAnyTable<DbDevice>())
_db.CodeFirst.InitTables<DbDevice>();
if(!_db.DbMaintenance.IsAnyTable<DbVariableTable>())
_db.CodeFirst.InitTables<DbVariableTable>();
if(!_db.DbMaintenance.IsAnyTable<DbDataVariable>())
_db.CodeFirst.InitTables<DbDataVariable>();
if(!_db.DbMaintenance.IsAnyTable<DbS7DataVariable>())
_db.CodeFirst.InitTables<DbS7DataVariable>();
if(!_db.DbMaintenance.IsAnyTable<DbUser>())
_db.CodeFirst.InitTables<DbUser>();
if(!_db.DbMaintenance.IsAnyTable<DbMqtt>())
_db.CodeFirst.InitTables<DbMqtt>();
}
}

View File

@@ -1,5 +1,8 @@
using PMSWPF.Data.Entities;
using PMSWPF.Enums;
using PMSWPF.Excptions;
using PMSWPF.Extensions;
using PMSWPF.Models;
namespace PMSWPF.Data.Repositories;
@@ -7,23 +10,26 @@ public class DevicesRepositories:BaseRepositories
{
public DevicesRepositories():base()
{
var tableExist= _db.DbMaintenance.IsAnyTable<DbDevice>();
if (!tableExist)
{
_db.CodeFirst.InitTables<DbDevice>();
}
}
public async Task<int> Add(DbDevice dbDevice)
public async Task<bool> Add(Device device)
{
var exist=await _db.Queryable<DbDevice>().Where(d=>d.Name==dbDevice.Name).FirstAsync();
var exist=await _db.Queryable<DbDevice>().Where(d=>d.Name==device.Name).FirstAsync();
if (exist != null)
{
throw new DbExistException("设备名称已经存在。");
}
var res= await _db.Insertable<DbDevice>(dbDevice).ExecuteCommandAsync();
return res;
DbDevice dbDevice=new DbDevice();
device.CopyTo<DbDevice>(dbDevice);
dbDevice.VariableTables=new List<DbVariableTable>();
DbVariableTable dbVariableTable=new DbVariableTable();
dbVariableTable.Name = "默认变量表";
dbVariableTable.Description = "默认变量表";
dbVariableTable.ProtocolType = ProtocolType.S7;
dbDevice.VariableTables.Add(dbVariableTable);
return await _db.InsertNav(dbDevice).Include(d=>d.VariableTables).ExecuteCommandAsync();
}
public async Task<List<DbDevice>> GetAll()

View File

@@ -1,21 +0,0 @@
using PMSWPF.Data.Entities;
using SqlSugar;
namespace PMSWPF.Data.Repositories
{
internal class PlcRepositories
{
private SqlSugarClient _db;
public PlcRepositories()
{
_db = DbContext.GetInstance();
var tabExist = _db.DbMaintenance.IsAnyTable(nameof(DbPLC), false);
if (tabExist)
{
_db.CodeFirst.InitTables<DbPLC>();
}
}
}
}