| | | 1 | | using System.IO; |
| | | 2 | | using CounterDrone.Core.Models; |
| | | 3 | | using SQLite; |
| | | 4 | | |
| | | 5 | | namespace CounterDrone.Core |
| | | 6 | | { |
| | | 7 | | /// <summary>数据库管理器 — 建表 / 连接管理</summary> |
| | | 8 | | public class DatabaseManager |
| | | 9 | | { |
| | | 10 | | private readonly IPathProvider _paths; |
| | | 11 | | |
| | 62 | 12 | | public DatabaseManager(IPathProvider paths) |
| | 62 | 13 | | { |
| | 62 | 14 | | _paths = paths; |
| | 62 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <summary>初始化主数据库(建表)</summary> |
| | | 18 | | public SQLiteConnection OpenMainDb() |
| | 61 | 19 | | { |
| | 61 | 20 | | var dbPath = _paths.GetMainDbPath(); |
| | 61 | 21 | | var db = new SQLiteConnection(dbPath); |
| | 61 | 22 | | CreateMainTables(db); |
| | 61 | 23 | | return db; |
| | 61 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>创建/打开任务帧数据库</summary> |
| | | 27 | | public SQLiteConnection OpenFrameDb(string taskId) |
| | 2 | 28 | | { |
| | 2 | 29 | | var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db"); |
| | 2 | 30 | | var db = new SQLiteConnection(dbPath); |
| | 2 | 31 | | db.CreateTable<SimFrameRecord>(); |
| | 2 | 32 | | db.CreateIndex("SimFrameRecord", new[] { "TaskId", "FrameIndex" }); |
| | 2 | 33 | | return db; |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary>删除任务帧数据库文件</summary> |
| | | 37 | | public void DeleteFrameDb(string taskId) |
| | 1 | 38 | | { |
| | 1 | 39 | | var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db"); |
| | 1 | 40 | | if (File.Exists(dbPath)) |
| | 1 | 41 | | File.Delete(dbPath); |
| | 1 | 42 | | } |
| | | 43 | | |
| | | 44 | | private void CreateMainTables(SQLiteConnection db) |
| | 61 | 45 | | { |
| | 61 | 46 | | db.CreateTable<ModelInfo>(); |
| | 61 | 47 | | db.CreateTable<AmmunitionSpec>(); |
| | 61 | 48 | | db.CreateTable<SimTask>(); |
| | 61 | 49 | | db.CreateTable<CombatScene>(); |
| | 61 | 50 | | db.CreateTable<ControlZone>(); |
| | 61 | 51 | | db.CreateTable<TargetConfig>(); |
| | 61 | 52 | | db.CreateTable<EquipmentDeployment>(); |
| | 61 | 53 | | db.CreateTable<CloudDispersal>(); |
| | 61 | 54 | | db.CreateTable<RoutePlan>(); |
| | 61 | 55 | | db.CreateTable<Waypoint>(); |
| | 61 | 56 | | db.CreateTable<Group>(); |
| | 61 | 57 | | db.CreateTable<SimulationReport>(); |
| | 61 | 58 | | db.CreateTable<SimEvent>(); |
| | | 59 | | |
| | 61 | 60 | | db.CreateIndex("SimTask", "TaskNumber"); |
| | 61 | 61 | | db.CreateIndex("ControlZone", "TaskId"); |
| | 61 | 62 | | db.CreateIndex("TargetConfig", "TaskId"); |
| | 61 | 63 | | db.CreateIndex("EquipmentDeployment", "TaskId"); |
| | 61 | 64 | | db.CreateIndex("Waypoint", "TaskId"); |
| | 61 | 65 | | db.CreateIndex("SimEvent", "TaskId"); |
| | 61 | 66 | | db.CreateIndex("SimulationReport", "TaskId"); |
| | 61 | 67 | | } |
| | | 68 | | } |
| | | 69 | | } |