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.

365 lines
13 KiB
C#

using Microsoft.EntityFrameworkCore;
using Haoliang.Models.Device;
using Haoliang.Models.Template;
using Haoliang.Models.Production;
using Haoliang.Models.User;
using Haoliang.Models.System;
using Haoliang.Models.DataCollection;
namespace Haoliang.Data.Entities
{
public class CNCBusinessDbContext : DbContext
{
public CNCBusinessDbContext(DbContextOptions<CNCBusinessDbContext> options) : base(options) { }
public DbSet<CNCDevice> Devices { get; set; }
public DbSet<DeviceCurrentStatus> DeviceCurrentStatus { get; set; } = null!;
public DbSet<TagData> TagData { get; set; } = null!;
public DbSet<CNCBrandTemplate> CNCTemplates { get; set; }
public DbSet<ProductionRecord> ProductionRecords { get; set; }
public DbSet<ProgramProductionSummary> ProgramProductionSummary { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Alarm> Alarms { get; set; }
public DbSet<AlarmRule> AlarmRules { get; set; }
public DbSet<StatisticRule> StatisticRules { get; set; }
public DbSet<ScheduledTask> ScheduledTasks { get; set; }
public DbSet<SystemConfig> SystemConfig { get; set; }
public DbSet<LogEntry> LogEntries { get; set; }
public DbSet<AlarmStatistics> AlarmStatistics { get; set; }
public DbSet<AlarmNotification> AlarmNotifications { get; set; }
public DbSet<Permission> Permissions { get; set; }
public DbSet<UserPermission> UserPermissions { get; set; }
public DbSet<RolePermission> RolePermissions { get; set; }
public DbSet<UserSession> UserSessions { get; set; }
public DbSet<PasswordReset> PasswordResets { get; set; }
public DbSet<StatisticResult> StatisticResults { get; set; }
public DbSet<CollectionTask> CollectionTasks { get; set; }
public DbSet<CollectionResult> CollectionResults { get; set; }
public DbSet<CollectionLog> CollectionLogs { get; set; }
public DbSet<CollectionConfig> CollectionConfigs { get; set; }
public DbSet<TaskExecutionResult> TaskExecutionResults { get; set; }
public DbSet<TaskExecutionSummary> TaskExecutionSummaries { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// 设备配置
modelBuilder.Entity<CNCDevice>()
.Property(d => d.DeviceCode)
.IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<CNCDevice>()
.Property(d => d.DeviceName)
.IsRequired()
.HasMaxLength(100);
modelBuilder.Entity<CNCDevice>()
.Property(d => d.IPAddress)
.IsRequired()
.HasMaxLength(15);
modelBuilder.Entity<CNCDevice>()
.Property(d => d.HttpUrl)
.IsRequired();
// 用户配置
modelBuilder.Entity<User>()
.Property(u => u.Username)
.IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<User>()
.Property(u => u.PasswordHash)
.IsRequired()
.HasMaxLength(255);
modelBuilder.Entity<User>()
.Property(u => u.RealName)
.IsRequired()
.HasMaxLength(50);
// 角色配置
modelBuilder.Entity<Role>()
.Property(r => r.RoleName)
.IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Role>()
.Property(r => r.Permissions)
.HasColumnType("json");
// 员工配置
modelBuilder.Entity<Employee>()
.Property(e => e.EmployeeCode)
.IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Employee>()
.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Employee>()
.Property(e => e.AssignedDevices)
.HasColumnType("json");
// 生产记录配置
modelBuilder.Entity<ProductionRecord>()
.Property(p => p.NCProgram)
.IsRequired()
.HasMaxLength(100);
// 模板配置
modelBuilder.Entity<CNCBrandTemplate>()
.Property(t => t.FieldMappings)
.HasColumnType("json");
modelBuilder.Entity<TemplateFieldMapping>()
.Property(t => t.ConversionRule)
.HasColumnType("json");
// 统计规则配置
modelBuilder.Entity<StatisticRule>()
.Property(s => s.GroupByDimensions)
.HasColumnType("json");
// 告警配置
modelBuilder.Entity<Alarm>()
.Property(a => a.AlarmContent)
.HasColumnType("text");
// 系统配置配置
modelBuilder.Entity<SystemConfig>()
.Property(s => s.ConfigValue)
.HasColumnType("text");
// 告警统计配置
modelBuilder.Entity<AlarmStatistics>()
.Property(a => a.ResolutionRate)
.HasColumnType("decimal(5,2)");
// 告警通知配置
modelBuilder.Entity<AlarmNotification>()
.Property(a => a.LogData)
.HasColumnType("json");
// 权限配置
modelBuilder.Entity<Permission>()
.Property(p => p.Name)
.IsRequired()
.HasMaxLength(100);
modelBuilder.Entity<Permission>()
.Property(p => p.Category)
.IsRequired()
.HasMaxLength(50);
// 用户权限配置
modelBuilder.Entity<UserPermission>()
.HasKey(up => new { up.UserId, up.PermissionId });
modelBuilder.Entity<RolePermission>()
.HasKey(rp => new { rp.RoleId, rp.PermissionId });
// 用户会话配置
modelBuilder.Entity<UserSession>()
.Property(u => u.SessionToken)
.IsRequired()
.HasMaxLength(500);
// 密码重置配置
modelBuilder.Entity<PasswordReset>()
.Property(p => p.Token)
.IsRequired()
.HasMaxLength(500);
// 统计结果配置
modelBuilder.Entity<StatisticResult>()
.Property(s => s.GroupValues)
.HasColumnType("json");
// 采集任务配置
modelBuilder.Entity<CollectionTask>()
.Property(c => c.ErrorMessage)
.HasColumnType("text");
// 采集结果配置
modelBuilder.Entity<CollectionResult>()
.Property(c => c.RawJson)
.HasColumnType("json");
modelBuilder.Entity<CollectionResult>()
.Property(c => c.ErrorMessage)
.HasColumnType("text");
// 采集日志配置
modelBuilder.Entity<CollectionLog>()
.Property(c => c.LogData)
.HasColumnType("json");
// 采集配置配置
modelBuilder.Entity<CollectionConfig>()
.Property(c => c.ConfigValue)
.HasColumnType("text");
}
}
public class CNCLLogDbContext : DbContext
{
public CNCLLogDbContext(DbContextOptions<CNCLLogDbContext> options) : base(options) { }
public DbSet<RawCollectionData> RawCollectionData { get; set; }
public DbSet<SystemLog> SystemLogs { get; set; }
public DbSet<CollectionHistory> CollectionHistory { get; set; }
public DbSet<PerformanceMetrics> PerformanceMetrics { get; set; }
public DbSet<AlarmHistory> AlarmHistory { get; set; }
public DbSet<OperationLog> OperationLogs { get; set; }
public DbSet<DataArchive> DataArchive { get; set; }
public DbSet<DbPerformanceStats> DbPerformanceStats { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// 日志配置
modelBuilder.Entity<SystemLog>()
.Property(s => s.LogData)
.HasColumnType("json");
modelBuilder.Entity<RawCollectionData>()
.Property(r => r.RawJson)
.HasColumnType("json");
modelBuilder.Entity<CollectionHistory>()
.Property(c => c.DataSize)
.IsRequired(false);
// 性能指标配置
modelBuilder.Entity<PerformanceMetrics>()
.Property(p => p.AvgExecutionTime)
.HasColumnType("decimal(10,3)");
modelBuilder.Entity<OperationLog>()
.Property(o => o.LogData)
.HasColumnType("json");
// 数据归档配置
modelBuilder.Entity<DataArchive>()
.Property(d => d.ArchiveData)
.HasColumnType("json");
// 数据库性能统计配置
modelBuilder.Entity<DbPerformanceStats>()
.Property(d => d.AvgExecutionTime)
.HasPrecision(10, 3);
modelBuilder.Entity<DbPerformanceStats>()
.Property(d => d.TotalExecutionTime)
.HasPrecision(10, 3);
}
}
// 日志库实体类
public class RawCollectionData
{
public int Id { get; set; }
public int DeviceId { get; set; }
public DateTime CollectionTime { get; set; }
public string RawJson { get; set; }
public bool IsSuccess { get; set; }
public string ErrorMessage { get; set; }
public int RetryCount { get; set; }
public DateTime CreatedAt { get; set; }
}
public class SystemLog
{
public int Id { get; set; }
public string LogLevel { get; set; }
public string LogCategory { get; set; }
public string LogMessage { get; set; }
public string LogData { get; set; }
public string SourceMethod { get; set; }
public string SourceFile { get; set; }
public DateTime LogTime { get; set; }
public DateTime CreatedAt { get; set; }
}
public class CollectionHistory
{
public int Id { get; set; }
public int DeviceId { get; set; }
public string CollectionStatus { get; set; }
public int? ResponseTime { get; set; }
public int? DataSize { get; set; }
public string ErrorMessage { get; set; }
public DateTime CollectionTime { get; set; }
public DateTime CreatedAt { get; set; }
}
public class PerformanceMetrics
{
public int Id { get; set; }
public string MetricName { get; set; }
public decimal MetricValue { get; set; }
public decimal AvgExecutionTime { get; set; }
public string MetricUnit { get; set; }
public DateTime CollectionTime { get; set; }
public DateTime CreatedAt { get; set; }
}
public class AlarmHistory
{
public int Id { get; set; }
public string AlarmType { get; set; }
public string AlarmLevel { get; set; }
public string AlarmContent { get; set; }
public int? DeviceId { get; set; }
public string DeviceName { get; set; }
public bool IsResolved { get; set; }
public DateTime OccurrenceTime { get; set; }
public DateTime? ResolutionTime { get; set; }
public string ResolutionNote { get; set; }
public DateTime CreatedAt { get; set; }
}
public class OperationLog
{
public int Id { get; set; }
public int? UserId { get; set; }
public string Username { get; set; }
public string OperationType { get; set; }
public string OperationTarget { get; set; }
public string OperationDetails { get; set; }
public string LogData { get; set; }
public string IPAddress { get; set; }
public string UserAgent { get; set; }
public DateTime OperationTime { get; set; }
public DateTime CreatedAt { get; set; }
}
public class DataArchive
{
public int Id { get; set; }
public string ArchiveType { get; set; }
public string TableName { get; set; }
public int RecordId { get; set; }
public string ArchiveData { get; set; }
public DateTime ArchiveTime { get; set; }
public DateTime CreatedAt { get; set; }
}
public class DbPerformanceStats
{
public int Id { get; set; }
public string QueryType { get; set; }
public int QueryCount { get; set; }
public decimal AvgExecutionTime { get; set; }
public decimal TotalExecutionTime { get; set; }
public int SlowQueryCount { get; set; }
public DateTime StatsDate { get; set; }
public DateTime CreatedAt { get; set; }
}
}