feat: add model file library links
This commit is contained in:
116
server/db.ts
116
server/db.ts
@@ -6,6 +6,7 @@ import {
|
||||
vehicleSeries, InsertVehicleSeries, VehicleSeries,
|
||||
vehicleModels, InsertVehicleModel, VehicleModel,
|
||||
modelCadFiles, InsertModelCadFile, ModelCadFile,
|
||||
modelPointcloudFiles, InsertModelPointcloudFile, ModelPointcloudFile,
|
||||
taskCadFiles, InsertTaskCadFile, TaskCadFile,
|
||||
inspectionTasks, InsertInspectionTask, InspectionTask,
|
||||
taskPointcloudFiles, InsertTaskPointcloudFile, TaskPointcloudFile,
|
||||
@@ -229,6 +230,37 @@ export async function deleteModelCadFile(id: number): Promise<void> {
|
||||
await db.delete(modelCadFiles).where(eq(modelCadFiles.id, id));
|
||||
}
|
||||
|
||||
// ─── 车型点云文件 ─────────────────────────────────────────────────────────────
|
||||
|
||||
export async function createModelPointcloudFile(data: InsertModelPointcloudFile): Promise<ModelPointcloudFile> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
const result = await db.insert(modelPointcloudFiles).values(data);
|
||||
const [row] = await db.select().from(modelPointcloudFiles).where(eq(modelPointcloudFiles.id, getMysqlInsertId(result)));
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function listModelPointcloudFiles(modelId: number): Promise<ModelPointcloudFile[]> {
|
||||
const db = await getDb();
|
||||
if (!db) return [];
|
||||
return db.select().from(modelPointcloudFiles)
|
||||
.where(eq(modelPointcloudFiles.modelId, modelId))
|
||||
.orderBy(desc(modelPointcloudFiles.createdAt));
|
||||
}
|
||||
|
||||
export async function getModelPointcloudFileById(id: number): Promise<ModelPointcloudFile | undefined> {
|
||||
const db = await getDb();
|
||||
if (!db) return undefined;
|
||||
const [row] = await db.select().from(modelPointcloudFiles).where(eq(modelPointcloudFiles.id, id));
|
||||
return row;
|
||||
}
|
||||
|
||||
export async function deleteModelPointcloudFile(id: number): Promise<void> {
|
||||
const db = await getDb();
|
||||
if (!db) return;
|
||||
await db.delete(modelPointcloudFiles).where(eq(modelPointcloudFiles.id, id));
|
||||
}
|
||||
|
||||
// ─── 任务 CAD 文件 ─────────────────────────────────────────────────────────────
|
||||
|
||||
export async function createTaskCadFile(data: InsertTaskCadFile): Promise<TaskCadFile> {
|
||||
@@ -260,6 +292,46 @@ export async function deleteTaskCadFile(id: number): Promise<void> {
|
||||
await db.delete(taskCadFiles).where(eq(taskCadFiles.id, id));
|
||||
}
|
||||
|
||||
export async function linkTaskCadFilesFromModel(taskId: number, modelCadFileIds: number[]): Promise<TaskCadFile[]> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
const task = await getInspectionTaskById(taskId);
|
||||
if (!task) throw new Error("Task not found");
|
||||
|
||||
const existing = await listTaskCadFiles(taskId);
|
||||
const existingSourceIds = new Set(
|
||||
existing
|
||||
.map((row) => row.sourceModelCadFileId)
|
||||
.filter((id): id is number => typeof id === "number"),
|
||||
);
|
||||
|
||||
const inserted: TaskCadFile[] = [];
|
||||
for (const id of modelCadFileIds) {
|
||||
if (existingSourceIds.has(id)) continue;
|
||||
|
||||
const source = await getModelCadFileById(id);
|
||||
if (!source) throw new Error("Model CAD file not found");
|
||||
if (source.modelId !== task.modelId) {
|
||||
throw new Error("Cannot link CAD file from another model");
|
||||
}
|
||||
|
||||
const record = await createTaskCadFile({
|
||||
taskId,
|
||||
name: source.name,
|
||||
format: source.format,
|
||||
fileSize: source.fileSize,
|
||||
filePath: source.filePath,
|
||||
source: "model_library",
|
||||
sourceModelCadFileId: source.id,
|
||||
} as InsertTaskCadFile);
|
||||
inserted.push(record);
|
||||
existingSourceIds.add(id);
|
||||
}
|
||||
|
||||
return inserted;
|
||||
}
|
||||
|
||||
// ─── 检测任务 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
export async function createInspectionTask(data: InsertInspectionTask): Promise<InspectionTask> {
|
||||
@@ -315,6 +387,7 @@ export async function createRegisteredPointcloudFile(
|
||||
name: string,
|
||||
filePath: string,
|
||||
sourceResultId: number,
|
||||
sourcePointcloudFileId?: number | null,
|
||||
): Promise<TaskPointcloudFile> {
|
||||
const fs = await import('node:fs');
|
||||
const fileSize = fs.existsSync(filePath) ? fs.statSync(filePath).size : 0;
|
||||
@@ -325,6 +398,7 @@ export async function createRegisteredPointcloudFile(
|
||||
fileSize,
|
||||
source: 'registered',
|
||||
sourceResultId,
|
||||
sourcePointcloudFileId: sourcePointcloudFileId ?? null,
|
||||
} as InsertTaskPointcloudFile);
|
||||
}
|
||||
|
||||
@@ -342,6 +416,48 @@ export async function deleteTaskPointcloudFile(id: number): Promise<void> {
|
||||
await db.delete(taskPointcloudFiles).where(eq(taskPointcloudFiles.id, id));
|
||||
}
|
||||
|
||||
export async function linkTaskPointcloudFilesFromModel(
|
||||
taskId: number,
|
||||
modelPointcloudFileIds: number[],
|
||||
): Promise<TaskPointcloudFile[]> {
|
||||
const db = await getDb();
|
||||
if (!db) throw new Error("Database not available");
|
||||
|
||||
const task = await getInspectionTaskById(taskId);
|
||||
if (!task) throw new Error("Task not found");
|
||||
|
||||
const existing = await listTaskPointcloudFiles(taskId);
|
||||
const existingSourceIds = new Set(
|
||||
existing
|
||||
.map((row) => row.sourceModelPointcloudId)
|
||||
.filter((id): id is number => typeof id === "number"),
|
||||
);
|
||||
|
||||
const inserted: TaskPointcloudFile[] = [];
|
||||
for (const id of modelPointcloudFileIds) {
|
||||
if (existingSourceIds.has(id)) continue;
|
||||
|
||||
const source = await getModelPointcloudFileById(id);
|
||||
if (!source) throw new Error("Model pointcloud file not found");
|
||||
if (source.modelId !== task.modelId) {
|
||||
throw new Error("Cannot link pointcloud file from another model");
|
||||
}
|
||||
|
||||
const record = await createTaskPointcloudFile({
|
||||
taskId,
|
||||
name: source.name,
|
||||
fileSize: source.fileSize,
|
||||
filePath: source.filePath,
|
||||
source: "model_library",
|
||||
sourceModelPointcloudId: source.id,
|
||||
} as InsertTaskPointcloudFile);
|
||||
inserted.push(record);
|
||||
existingSourceIds.add(id);
|
||||
}
|
||||
|
||||
return inserted;
|
||||
}
|
||||
|
||||
// ─── 任务分析结果 ──────────────────────────────────────────────────────────────
|
||||
|
||||
export async function createTaskAnalysisResult(data: InsertTaskAnalysisResult): Promise<TaskAnalysisResult> {
|
||||
|
||||
Reference in New Issue
Block a user