CounterDroneBackend/src/CounterDrone.Core/Services/ModelService.cs
tian 225590ac19 补全所有模型的完整 CRUD 接口
- ModelService: +AddModel/UpdateModel
- IScenarioService: +UpdateScenario +GetScene +Get/DeleteControlZone
  +Add/Update/Delete/GetScenarioDrones +Add/Update/Delete/GetDeploymentUnits
  +GetCloudDispersal +GetRoutes/DeleteRoute +GetWaypoints/GetWaypointsByWave/DeleteWaypoint
- ScenarioManager 全面暴露新增接口
- 238 测试全过
2026-06-18 19:42:48 +08:00

80 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using CounterDrone.Core.Models;
using CounterDrone.Core.Repository;
namespace CounterDrone.Core.Services
{
public class ModelService : IModelService
{
private readonly ModelRepository _repo;
private readonly IPathProvider _paths;
public ModelService(ModelRepository repo, IPathProvider paths)
{
_repo = repo;
_paths = paths;
}
public ModelInfo ImportModel(string filePath, string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("模型名称不能为空");
var ext = Path.GetExtension(filePath).ToLowerInvariant();
if (ext != ".fbx" && ext != ".obj" && ext != ".stl" && ext != ".glb" && ext != ".gltf")
throw new ArgumentException($"不支持的模型格式:{ext}");
var fileInfo = new FileInfo(filePath);
if (fileInfo.Length > 500 * 1024 * 1024)
throw new ArgumentException("模型文件大小超过 500MB 限制");
var model = new ModelInfo
{
Name = name,
FilePath = filePath,
FileSize = fileInfo.Length / (1024.0 * 1024.0),
};
_repo.Insert(model);
return model;
}
public ModelInfo AddModel(ModelInfo model)
{
if (string.IsNullOrWhiteSpace(model.Name))
throw new ArgumentException("模型名称不能为空");
if (string.IsNullOrEmpty(model.Id))
model.Id = Guid.NewGuid().ToString();
model.CreatedAt = DateTime.UtcNow.ToString("o");
_repo.Insert(model);
return model;
}
public void DeleteModel(string id)
{
_repo.Delete(id);
}
public List<ModelInfo> GetAllModels()
{
return _repo.GetAll();
}
public ModelInfo GetModel(string id)
{
return _repo.GetById(id);
}
public ModelInfo UpdateModel(ModelInfo model)
{
var existing = _repo.GetById(model.Id);
if (existing == null)
throw new ArgumentException($"模型 {model.Id} 不存在");
_repo.Update(model);
return model;
}
}
}