Przeglądaj źródła

修改保存融合图遇到的问题

袁威 2 tygodni temu
rodzic
commit
eff1456232
1 zmienionych plików z 32 dodań i 7 usunięć
  1. 32 7
      app/api/formate_xy.py

+ 32 - 7
app/api/formate_xy.py

@@ -257,6 +257,16 @@ async def update_image_modified_json(
     # *** 1. 格式还原 ***
     # 将前端的 xy dict 格式转回 [[x,y]],并丢弃 points 里的 id
     internal_json_payload = convert_xy_to_internal_format(new_json_data)
+    
+    # 丢弃前端展示用的辅助字段,防止传给算分服务导致报错
+    _defects = internal_json_payload.get("result", {}).get("defect_result", {}).get("defects", [])
+    for d in _defects:
+        if d.get("label") == "slight_scratch":
+            d["label"] = "scratch"
+        d.pop("defectImgUrl", None)
+        d.pop("defectImgUrls", None)
+        d.pop("gray_id", None)
+        d.pop("fusion_id", None)
 
     try:
         cursor = db_conn.cursor(dictionary=True)
@@ -380,17 +390,27 @@ async def update_gray_image_json(
     # 1. 格式还原
     internal_gray_json = convert_xy_to_internal_format(new_json_data)
     gray_defects = internal_gray_json.get("result", {}).get("defect_result", {}).get("defects", [])
+    
+    # 丢弃前端展示用的辅助字段,防止传给算分服务导致报错
+    for d in gray_defects:
+        d.pop("defectImgUrl", None)
+        d.pop("defectImgUrls", None)
 
     try:
         cursor = db_conn.cursor(dictionary=True)
 
-        # 2. 获取灰度图信息
-        # 注意:灰度图存在 card_gray_images 表中
+        # 2. 获取辅助图(灰度图/融合图)信息
+        # 以前只查 card_gray_images,现在融合图是在 card_images 表里
+        # 先查 card_gray_images
         cursor.execute(f"SELECT card_id, image_type FROM {settings.DB_GRAY_IMAGE_TABLE_NAME} WHERE id = %s", (id,))
         gray_row = cursor.fetchone()
-
+        
         if not gray_row:
-            raise HTTPException(status_code=404, detail=f"ID为 {id} 的灰度图未找到。")
+            # 如果灰度表没找到,去主表找找看是不是融合图
+            cursor.execute(f"SELECT card_id, image_type FROM {settings.DB_IMAGE_TABLE_NAME} WHERE id = %s AND image_type IN ('front_fusion', 'back_fusion')", (id,))
+            gray_row = cursor.fetchone()
+            if not gray_row:
+                raise HTTPException(status_code=404, detail=f"ID为 {id} 的辅助图未找到。")
 
         card_id = gray_row['card_id']
         check_card_permission(db_conn, current_user, card_id)
@@ -398,12 +418,12 @@ async def update_gray_image_json(
 
         # 3. 确定目标 Ring 图类型
         target_ring_type = None
-        if gray_image_type == ImageType.front_gray.value:
+        if gray_image_type in (ImageType.front_gray.value, ImageType.front_fusion.value):
             target_ring_type = ImageType.front_ring.value
-        elif gray_image_type == ImageType.back_gray.value:
+        elif gray_image_type in (ImageType.back_gray.value, ImageType.back_fusion.value):
             target_ring_type = ImageType.back_ring.value
         else:
-            raise HTTPException(status_code=400, detail=f"不支持的灰度图类型: {gray_image_type}")
+            raise HTTPException(status_code=400, detail=f"不支持的辅助图类型: {gray_image_type}")
 
         # 4. 获取目标 Ring 图数据 (Card Images 表)
         cursor.execute(
@@ -439,6 +459,11 @@ async def update_gray_image_json(
             is_fusion = gray_image_type in (ImageType.front_fusion.value, ImageType.back_fusion.value)
             key_to_check = "fusion_id" if is_fusion else "gray_id"
             
+            # 把前端传来的 slight_scratch 强制转换成 scratch
+            # 这是因为算分服务可能不支持 slight_scratch 这种新标签
+            if new_defect.get("label") == "slight_scratch":
+                new_defect["label"] = "scratch"
+                
             identifier = new_defect.get(key_to_check)
 
             # 只有带有对应标识的才进行特殊合并处理