"""B04 지표면 모델 확정 서비스.""" from typing import Any from uuid import UUID import aiomysql from B04_PreProcess.B04_PreProcess_Repository import ( confirm_surface_model, list_surface_models, ) from common_util.common_util_surface_confirmation import ( LAS_STASH_KEY, AnalysisTerrain, analysis_terrain_params, design_terrain, get_surface_confirmation_params, merge_surface_confirmation_params, terrain_changes, ) from common_util.common_util_workflow_state import complete_stage async def find_surface_model_for_selection( connection: aiomysql.Connection, project_id: UUID, selection: dict[str, Any], ) -> int: """필터·표현 기본값과 일치하는 COMPLETE 모델 ID를 찾는다.""" for model in await list_surface_models(connection, project_id): generation_params = model.get("generation_params") or {} if ( model["status"] == "COMPLETE" and model["model_type"].lower() == str(selection["method"]).lower() and str(generation_params.get("source_filter", "")).lower() == str(selection["source_filter"]).lower() ): return int(model["id"]) raise LookupError( "자동 확정 기본값과 일치하는 지표면 모델이 없습니다: " f"filter={selection['source_filter']}, method={selection['method']}" ) async def confirm_surface_selection( connection: aiomysql.Connection, project_id: UUID, model_id: int, selection: dict[str, Any], ) -> dict[str, Any]: """모델 확정, 선택값 스냅샷, WF1 완료를 현재 트랜잭션에서 처리한다.""" await confirm_surface_model(connection, project_id, model_id) normalized = await merge_surface_confirmation_params( connection, str(project_id), selection, ) async with connection.cursor() as cursor: await complete_stage(cursor, str(project_id), 1) return normalized async def switch_design_terrain( connection: aiomysql.Connection, project_id: UUID, terrain: AnalysisTerrain | None, ) -> tuple[int, AnalysisTerrain] | None: """노선 [확인] 때 고른 분석 지형을 설계 지형으로(PLAN 133-2) — 확정 모델 · stage 1 params. 뒤 단계(종 · 횡단 · 배수 · 유토 · 3D)는 모두 stage 1 params · 확정 모델을 읽으므로 여기 한 곳만 바꾼다. 수치지형도로 갈 때 라이다 확정값을 적어 둠(되돌릴 자리). WF1 완료 전이는 안 건드림. 바뀌면 (새 확정 모델 id, 이전 설계 지형) · 그대로면 None. 트랜잭션은 부르는 쪽. """ params = await get_surface_confirmation_params(connection, str(project_id)) target = analysis_terrain_params(params, terrain) if not terrain_changes(params, target): return None model_id = await find_surface_model_for_selection(connection, project_id, target) await confirm_surface_model(connection, project_id, model_id) previous = design_terrain(params) extra = ( {LAS_STASH_KEY: {key: params[key] for key in ("source_filter", "method", "smooth")}} if previous == "las" else None ) await merge_surface_confirmation_params(connection, str(project_id), target, extra) return model_id, previous