| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | using CounterDrone.Core.Models; |
| | | 5 | | using CounterDrone.Core.Repository; |
| | | 6 | | |
| | | 7 | | namespace CounterDrone.Core.Services |
| | | 8 | | { |
| | | 9 | | public class ScenarioService : IScenarioService |
| | | 10 | | { |
| | | 11 | | private readonly SimTaskRepository _taskRepo; |
| | | 12 | | private readonly CombatSceneRepository _sceneRepo; |
| | | 13 | | private readonly ControlZoneRepository _zoneRepo; |
| | | 14 | | private readonly TargetConfigRepository _targetRepo; |
| | | 15 | | private readonly EquipmentDeploymentRepository _equipRepo; |
| | | 16 | | private readonly CloudDispersalRepository _cloudRepo; |
| | | 17 | | private readonly RoutePlanRepository _routeRepo; |
| | | 18 | | private readonly WaypointRepository _waypointRepo; |
| | | 19 | | |
| | 32 | 20 | | public ScenarioService( |
| | 32 | 21 | | SimTaskRepository taskRepo, |
| | 32 | 22 | | CombatSceneRepository sceneRepo, |
| | 32 | 23 | | ControlZoneRepository zoneRepo, |
| | 32 | 24 | | TargetConfigRepository targetRepo, |
| | 32 | 25 | | EquipmentDeploymentRepository equipRepo, |
| | 32 | 26 | | CloudDispersalRepository cloudRepo, |
| | 32 | 27 | | RoutePlanRepository routeRepo, |
| | 32 | 28 | | WaypointRepository waypointRepo) |
| | 32 | 29 | | { |
| | 32 | 30 | | _taskRepo = taskRepo; |
| | 32 | 31 | | _sceneRepo = sceneRepo; |
| | 32 | 32 | | _zoneRepo = zoneRepo; |
| | 32 | 33 | | _targetRepo = targetRepo; |
| | 32 | 34 | | _equipRepo = equipRepo; |
| | 32 | 35 | | _cloudRepo = cloudRepo; |
| | 32 | 36 | | _routeRepo = routeRepo; |
| | 32 | 37 | | _waypointRepo = waypointRepo; |
| | 32 | 38 | | } |
| | | 39 | | |
| | | 40 | | // ========== Task CRUD ========== |
| | | 41 | | |
| | | 42 | | public SimTask CreateTask(string name, string taskNumber) |
| | 49 | 43 | | { |
| | 49 | 44 | | if (string.IsNullOrWhiteSpace(name)) |
| | 1 | 45 | | throw new ArgumentException("任务名称不能为空"); |
| | | 46 | | |
| | 48 | 47 | | if (string.IsNullOrWhiteSpace(taskNumber)) |
| | 45 | 48 | | taskNumber = GenerateTaskNumber(); |
| | | 49 | | |
| | 48 | 50 | | var existing = _taskRepo.GetByTaskNumber(taskNumber); |
| | 48 | 51 | | if (existing != null) |
| | 1 | 52 | | throw new ArgumentException($"任务编号 {taskNumber} 已存在"); |
| | | 53 | | |
| | 47 | 54 | | var task = new SimTask |
| | 47 | 55 | | { |
| | 47 | 56 | | Name = name, |
| | 47 | 57 | | TaskNumber = taskNumber, |
| | 47 | 58 | | Status = (int)TaskStatus.Configuring, |
| | 47 | 59 | | CurrentStep = 1, |
| | 47 | 60 | | }; |
| | | 61 | | |
| | 47 | 62 | | _taskRepo.Insert(task); |
| | 47 | 63 | | return task; |
| | 47 | 64 | | } |
| | | 65 | | |
| | | 66 | | public void DeleteTask(string id) |
| | 1 | 67 | | { |
| | 1 | 68 | | _taskRepo.Delete(id); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | public PagedResult<SimTask> SearchTasks(string keyword, string? dateFrom, string? dateTo, int page, int pageSize |
| | 7 | 72 | | { |
| | 7 | 73 | | if (page < 1) page = 1; |
| | 7 | 74 | | if (pageSize < 1) pageSize = 10; |
| | 7 | 75 | | var offset = (page - 1) * pageSize; |
| | | 76 | | |
| | 7 | 77 | | var items = _taskRepo.Search(keyword, dateFrom, dateTo, offset, pageSize, out var total); |
| | | 78 | | |
| | 7 | 79 | | return new PagedResult<SimTask> |
| | 7 | 80 | | { |
| | 7 | 81 | | Items = items, |
| | 7 | 82 | | TotalCount = total, |
| | 7 | 83 | | Page = page, |
| | 7 | 84 | | PageSize = pageSize, |
| | 7 | 85 | | }; |
| | 7 | 86 | | } |
| | | 87 | | |
| | | 88 | | public TaskFullConfig? GetTaskDetail(string id) |
| | 36 | 89 | | { |
| | 36 | 90 | | var task = _taskRepo.GetById(id); |
| | 37 | 91 | | if (task == null) return null; |
| | | 92 | | |
| | 35 | 93 | | return new TaskFullConfig |
| | 35 | 94 | | { |
| | 35 | 95 | | Task = task, |
| | 35 | 96 | | Scene = _sceneRepo.GetById(id) ?? new CombatScene { TaskId = id }, |
| | 35 | 97 | | ControlZones = _zoneRepo.GetByTaskId(id), |
| | 35 | 98 | | Targets = _targetRepo.GetByTaskId(id), |
| | 35 | 99 | | Equipment = _equipRepo.GetByTaskId(id), |
| | 35 | 100 | | Cloud = _cloudRepo.GetById(id) ?? new CloudDispersal { TaskId = id }, |
| | 35 | 101 | | Route = _routeRepo.GetById(id) ?? new RoutePlan { TaskId = id }, |
| | 35 | 102 | | Waypoints = _waypointRepo.GetByTaskId(id), |
| | 35 | 103 | | }; |
| | 36 | 104 | | } |
| | | 105 | | |
| | | 106 | | // ========== Step Save ========== |
| | | 107 | | |
| | | 108 | | public void SaveScene(string taskId, CombatScene scene) |
| | 17 | 109 | | { |
| | 17 | 110 | | scene.TaskId = taskId; |
| | 17 | 111 | | var existing = _sceneRepo.GetById(taskId); |
| | 17 | 112 | | if (existing != null) |
| | 1 | 113 | | _sceneRepo.Update(scene); |
| | | 114 | | else |
| | 16 | 115 | | _sceneRepo.Insert(scene); |
| | 17 | 116 | | TouchTask(taskId); |
| | 17 | 117 | | } |
| | | 118 | | |
| | | 119 | | public void SaveControlZones(string taskId, List<ControlZone> zones) |
| | 5 | 120 | | { |
| | 5 | 121 | | _zoneRepo.DeleteByTaskId(taskId); |
| | 24 | 122 | | for (int i = 0; i < zones.Count; i++) |
| | 7 | 123 | | { |
| | 7 | 124 | | zones[i].TaskId = taskId; |
| | 7 | 125 | | zones[i].OrderIndex = i; |
| | 7 | 126 | | } |
| | 5 | 127 | | _zoneRepo.InsertAll(zones); |
| | 5 | 128 | | TouchTask(taskId); |
| | 5 | 129 | | } |
| | | 130 | | |
| | | 131 | | public void SaveTarget(string taskId, TargetConfig target) |
| | 15 | 132 | | { |
| | 15 | 133 | | target.TaskId = taskId; |
| | 15 | 134 | | if (string.IsNullOrEmpty(target.Id)) |
| | 0 | 135 | | target.Id = Guid.NewGuid().ToString(); |
| | | 136 | | |
| | 15 | 137 | | var existing = _targetRepo.GetById(target.Id); |
| | 15 | 138 | | if (existing != null) |
| | 0 | 139 | | _targetRepo.Update(target); |
| | | 140 | | else |
| | 15 | 141 | | _targetRepo.Insert(target); |
| | 15 | 142 | | TouchTask(taskId); |
| | 15 | 143 | | } |
| | | 144 | | |
| | | 145 | | public void SaveDeployment(string taskId, List<EquipmentDeployment> equips) |
| | 14 | 146 | | { |
| | 14 | 147 | | _equipRepo.DeleteByTaskId(taskId); |
| | 86 | 148 | | foreach (var e in equips) |
| | 22 | 149 | | { |
| | 22 | 150 | | e.TaskId = taskId; |
| | 22 | 151 | | if (string.IsNullOrEmpty(e.Id)) |
| | 0 | 152 | | e.Id = Guid.NewGuid().ToString(); |
| | 22 | 153 | | } |
| | 14 | 154 | | _equipRepo.InsertAll(equips); |
| | 14 | 155 | | TouchTask(taskId); |
| | 14 | 156 | | } |
| | | 157 | | |
| | | 158 | | public void SaveCloudDispersal(string taskId, CloudDispersal cloud) |
| | 14 | 159 | | { |
| | 14 | 160 | | cloud.TaskId = taskId; |
| | 14 | 161 | | var existing = _cloudRepo.GetById(taskId); |
| | 14 | 162 | | if (existing != null) |
| | 0 | 163 | | _cloudRepo.Update(cloud); |
| | | 164 | | else |
| | 14 | 165 | | _cloudRepo.Insert(cloud); |
| | 14 | 166 | | TouchTask(taskId); |
| | 14 | 167 | | } |
| | | 168 | | |
| | | 169 | | public void SaveRoute(string taskId, RoutePlan route, List<Waypoint> waypoints) |
| | 14 | 170 | | { |
| | 14 | 171 | | route.TaskId = taskId; |
| | 14 | 172 | | var existingRoute = _routeRepo.GetById(taskId); |
| | 14 | 173 | | if (existingRoute != null) |
| | 0 | 174 | | _routeRepo.Update(route); |
| | | 175 | | else |
| | 14 | 176 | | _routeRepo.Insert(route); |
| | | 177 | | |
| | 14 | 178 | | _waypointRepo.DeleteByTaskId(taskId); |
| | 86 | 179 | | for (int i = 0; i < waypoints.Count; i++) |
| | 29 | 180 | | { |
| | 29 | 181 | | waypoints[i].TaskId = taskId; |
| | 29 | 182 | | waypoints[i].OrderIndex = i; |
| | 29 | 183 | | if (string.IsNullOrEmpty(waypoints[i].Id)) |
| | 0 | 184 | | waypoints[i].Id = Guid.NewGuid().ToString(); |
| | 29 | 185 | | } |
| | 14 | 186 | | _waypointRepo.InsertAll(waypoints); |
| | 14 | 187 | | TouchTask(taskId); |
| | 14 | 188 | | } |
| | | 189 | | |
| | | 190 | | public void UpdateStep(string taskId, int step) |
| | 10 | 191 | | { |
| | 10 | 192 | | var task = _taskRepo.GetById(taskId); |
| | 10 | 193 | | if (task == null) return; |
| | | 194 | | |
| | 11 | 195 | | if (step < 1) step = 1; |
| | 11 | 196 | | if (step > 5) step = 5; |
| | | 197 | | |
| | 10 | 198 | | task.CurrentStep = step; |
| | 10 | 199 | | _taskRepo.Update(task); |
| | 10 | 200 | | } |
| | | 201 | | |
| | | 202 | | // ========== Helpers ========== |
| | | 203 | | |
| | | 204 | | private void TouchTask(string taskId) |
| | 79 | 205 | | { |
| | 79 | 206 | | var task = _taskRepo.GetById(taskId); |
| | 79 | 207 | | if (task != null) |
| | 79 | 208 | | { |
| | 79 | 209 | | task.UpdatedAt = DateTime.UtcNow.ToString("o"); |
| | 79 | 210 | | _taskRepo.Update(task); |
| | 79 | 211 | | } |
| | 79 | 212 | | } |
| | | 213 | | |
| | | 214 | | private static int _counter; |
| | | 215 | | |
| | | 216 | | public static string GenerateTaskNumber() |
| | 45 | 217 | | { |
| | 45 | 218 | | var now = DateTime.Now; |
| | 45 | 219 | | var datePart = now.ToString("yyyyMMdd"); |
| | 45 | 220 | | var seq = Interlocked.Increment(ref _counter); |
| | 45 | 221 | | return $"SIM-{datePart}-{seq:D3}"; |
| | 45 | 222 | | } |
| | | 223 | | } |
| | | 224 | | } |