117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Data.Sqlite;
|
||
using OfficeOpenXml;
|
||
using System;
|
||
using System.IO;
|
||
using System.Windows;
|
||
using TabletTester2025.Data;
|
||
using TabletTester2025.Models;
|
||
using TabletTester2025.Services;
|
||
using TabletTester2025.ViewModels;
|
||
|
||
namespace TabletTester2025
|
||
{
|
||
public partial class App : Application
|
||
{
|
||
public static IPlcService PlcService { get; private set; }
|
||
public static PlcConfiguration PlcConfig { get; private set; }
|
||
public static PharmaParameters CurrentPharmaParams { get; set; } = new PharmaParameters();
|
||
|
||
protected override async void OnStartup(StartupEventArgs e)
|
||
{
|
||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||
base.OnStartup(e);
|
||
|
||
// 读取配置
|
||
var builder = new ConfigurationBuilder()
|
||
.SetBasePath(Directory.GetCurrentDirectory())
|
||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
||
var configuration = builder.Build();
|
||
|
||
// 数据库初始化并确保表结构最新
|
||
var connectionString = configuration.GetConnectionString("DefaultConnection") ?? "Data Source=TabletTests.db";
|
||
using (var db = new AppDbContext(connectionString))
|
||
{
|
||
db.Database.EnsureCreated();
|
||
// 自动添加缺失的列
|
||
EnsureColumnsExist(connectionString);
|
||
}
|
||
|
||
// 绑定药典参数
|
||
configuration.GetSection("PharmaStandard").Bind(CurrentPharmaParams);
|
||
|
||
// PLC配置
|
||
PlcConfig = configuration.GetSection("Plc").Get<PlcConfiguration>();
|
||
if (PlcConfig == null)
|
||
throw new InvalidOperationException("PLC配置缺失");
|
||
|
||
// 创建PLC服务(真实或模拟)
|
||
if (configuration["Plc:Type"] == "ModbusTcp")
|
||
PlcService = new ModbusTcpPlcService(PlcConfig);
|
||
else
|
||
PlcService = new PlcSimulator();
|
||
|
||
try
|
||
{
|
||
await PlcService.ConnectAsync();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"PLC连接失败,将使用模拟模式。\n{ex.Message}", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
PlcService = new PlcSimulator();
|
||
await PlcService.ConnectAsync();
|
||
}
|
||
|
||
// 业务服务
|
||
var dbService = new DatabaseService(connectionString);
|
||
var excelService = new ExcelExportService();
|
||
var alarmService = new AlarmService();
|
||
|
||
// 主窗口
|
||
var mainWindow = new MainWindow();
|
||
mainWindow.DataContext = new MainViewModel(PlcService, dbService, excelService, alarmService, PlcConfig);
|
||
MainWindow = mainWindow;
|
||
mainWindow.Show();
|
||
}
|
||
|
||
private void EnsureColumnsExist(string connectionString)
|
||
{
|
||
// 定义需要确保存在的列 (表名, 列名, 列类型, 默认值)
|
||
var requiredColumns = new[]
|
||
{
|
||
("TestBatches", "HardnessRSD", "REAL", "0"),
|
||
("TestBatches", "RemainingTubesAtEnd", "INTEGER", "0"),
|
||
// 如果将来还有新列,继续添加
|
||
};
|
||
|
||
using var connection = new SqliteConnection(connectionString);
|
||
connection.Open();
|
||
|
||
foreach (var (table, column, colType, defaultValue) in requiredColumns)
|
||
{
|
||
// 检查列是否存在
|
||
var pragmaCmd = connection.CreateCommand();
|
||
pragmaCmd.CommandText = $"PRAGMA table_info({table})";
|
||
using var reader = pragmaCmd.ExecuteReader();
|
||
bool columnExists = false;
|
||
while (reader.Read())
|
||
{
|
||
if (reader.GetString(1) == column)
|
||
{
|
||
columnExists = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!columnExists)
|
||
{
|
||
// 添加缺失的列
|
||
var alterCmd = connection.CreateCommand();
|
||
alterCmd.CommandText = $"ALTER TABLE {table} ADD COLUMN {column} {colType} DEFAULT {defaultValue}";
|
||
alterCmd.ExecuteNonQuery();
|
||
System.Diagnostics.Debug.WriteLine($"已添加缺失的列: {table}.{column}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |