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 CNCDbContext : DbContext { public CNCDbContext(DbContextOptions options) : base(options) { } public DbSet Devices { get; set; } public DbSet DeviceCurrentStatus { get; set; } = null!; public DbSet CollectionTagData { get; set; } = null!; public DbSet CNCTemplates { get; set; } public DbSet ProductionRecords { get; set; } public DbSet ProgramProductionSummary { get; set; } public DbSet Users { get; set; } public DbSet Alarms { get; set; } public DbSet AlarmRules { get; set; } public DbSet StatisticRules { get; set; } public DbSet ScheduledTasks { get; set; } public DbSet SystemConfig { get; set; } public DbSet LogEntries { get; set; } public DbSet AlarmStatistics { get; set; } public DbSet AlarmNotifications { get; set; } public DbSet Permissions { get; set; } public DbSet UserPermissions { get; set; } public DbSet RolePermissions { get; set; } public DbSet UserSessions { get; set; } public DbSet PasswordResets { get; set; } public DbSet StatisticResults { get; set; } public DbSet CollectionTasks { get; set; } public DbSet CollectionResults { get; set; } public DbSet CollectionLogs { get; set; } public DbSet CollectionConfigs { get; set; } public DbSet TaskExecutionResults { get; set; } public DbSet TaskExecutionSummaries { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // 设备配置 modelBuilder.Entity() .Property(d => d.DeviceCode) .IsRequired() .HasMaxLength(50); modelBuilder.Entity() .Property(d => d.DeviceName) .IsRequired() .HasMaxLength(100); modelBuilder.Entity() .Property(d => d.IPAddress) .IsRequired() .HasMaxLength(15); modelBuilder.Entity() .Property(d => d.HttpUrl) .IsRequired(); // 用户配置 modelBuilder.Entity() .Property(u => u.Username) .IsRequired() .HasMaxLength(50); modelBuilder.Entity() .Property(u => u.PasswordHash) .IsRequired() .HasMaxLength(255); modelBuilder.Entity() .Property(u => u.RealName) .IsRequired() .HasMaxLength(50); // 角色配置 modelBuilder.Entity() .Property(r => r.RoleName) .IsRequired() .HasMaxLength(50); modelBuilder.Entity() .Property(r => r.Permissions) .HasColumnType("json"); // 员工配置 modelBuilder.Entity() .Property(e => e.EmployeeCode) .IsRequired() .HasMaxLength(50); modelBuilder.Entity() .Property(e => e.Name) .IsRequired() .HasMaxLength(50); modelBuilder.Entity() .Property(e => e.AssignedDevices) .HasColumnType("json"); // 生产记录配置 modelBuilder.Entity() .Property(p => p.NCProgram) .IsRequired() .HasMaxLength(100); // 模板配置 modelBuilder.Entity() .Property(t => t.FieldMappings) .HasColumnType("json"); modelBuilder.Entity() .Property(t => t.ConversionRule) .HasColumnType("json"); // 统计规则配置 modelBuilder.Entity() .Property(s => s.GroupByDimensions) .HasColumnType("json"); // 告警配置 modelBuilder.Entity() .Property(a => a.AlarmContent) .HasColumnType("text"); // 系统配置配置 modelBuilder.Entity() .Property(s => s.ConfigValue) .HasColumnType("text"); // 告警统计配置 modelBuilder.Entity() .Property(a => a.ResolutionRate) .HasColumnType("decimal(5,2)"); // 告警通知配置 modelBuilder.Entity() .Property(a => a.LogData) .HasColumnType("json"); // 权限配置 modelBuilder.Entity() .Property(p => p.Name) .IsRequired() .HasMaxLength(100); modelBuilder.Entity() .Property(p => p.Category) .IsRequired() .HasMaxLength(50); // 用户权限配置 modelBuilder.Entity() .HasKey(up => new { up.UserId, up.PermissionId }); modelBuilder.Entity() .HasKey(rp => new { rp.RoleId, rp.PermissionId }); // 用户会话配置 modelBuilder.Entity() .Property(u => u.SessionToken) .IsRequired() .HasMaxLength(500); // 密码重置配置 modelBuilder.Entity() .Property(p => p.Token) .IsRequired() .HasMaxLength(500); // 统计结果配置 modelBuilder.Entity() .Property(s => s.GroupValues) .HasColumnType("json"); // 采集任务配置 modelBuilder.Entity() .Property(c => c.ErrorMessage) .HasColumnType("text"); // 采集结果配置 modelBuilder.Entity() .Property(c => c.RawJson) .HasColumnType("json"); modelBuilder.Entity() .Property(c => c.ErrorMessage) .HasColumnType("text"); // 采集日志配置 modelBuilder.Entity() .Property(c => c.LogData) .HasColumnType("json"); // 采集配置配置 modelBuilder.Entity() .Property(c => c.ConfigValue) .HasColumnType("text"); } } public class CNCLLogDbContext : DbContext { public CNCLLogDbContext(DbContextOptions options) : base(options) { } public DbSet RawCollectionData { get; set; } public DbSet SystemLogs { get; set; } public DbSet CollectionHistory { get; set; } public DbSet PerformanceMetrics { get; set; } public DbSet AlarmHistory { get; set; } public DbSet OperationLogs { get; set; } public DbSet DataArchive { get; set; } public DbSet DbPerformanceStats { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // 日志配置 modelBuilder.Entity() .Property(s => s.LogData) .HasColumnType("json"); modelBuilder.Entity() .Property(r => r.RawJson) .HasColumnType("json"); modelBuilder.Entity() .Property(c => c.DataSize) .IsRequired(false); // 性能指标配置 modelBuilder.Entity() .Property(p => p.AvgExecutionTime) .HasColumnType("decimal(10,3)"); modelBuilder.Entity() .Property(o => o.LogData) .HasColumnType("json"); // 数据归档配置 modelBuilder.Entity() .Property(d => d.ArchiveData) .HasColumnType("json"); // 数据库性能统计配置 modelBuilder.Entity() .Property(d => d.AvgExecutionTime) .HasPrecision(10, 3); modelBuilder.Entity() .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; } } }