|
|
@@ -1,9 +1,11 @@
|
|
|
import os
|
|
|
import uuid
|
|
|
import json
|
|
|
+import requests
|
|
|
from typing import Optional, Dict, Any
|
|
|
from enum import Enum
|
|
|
from fastapi import APIRouter, File, UploadFile, Depends, HTTPException, Form, Path
|
|
|
+from fastapi.concurrency import run_in_threadpool
|
|
|
from fastapi.responses import JSONResponse, FileResponse
|
|
|
from mysql.connector.pooling import PooledMySQLConnection
|
|
|
from mysql.connector import IntegrityError
|
|
|
@@ -25,6 +27,15 @@ class ImageType(str, Enum):
|
|
|
back_edge = "back_edge"
|
|
|
|
|
|
|
|
|
+# "图片类型和计算分数分数类型映射表"
|
|
|
+IMAGE_TYPE_TO_SCORE_TYPE = {
|
|
|
+ "front_face": "front_face",
|
|
|
+ "back_face": "back_face",
|
|
|
+ "front_edge": "front_corner_edge",
|
|
|
+ "back_edge": "back_corner_edge",
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
@router.post("/insert/{card_id}", response_model=CardImageResponse, status_code=201,
|
|
|
summary="为卡牌上传并关联一张图片")
|
|
|
async def upload_image_for_card(
|
|
|
@@ -128,8 +139,84 @@ def get_image_jsons(id: int, db_conn: PooledMySQLConnection = db_dependency):
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
-@router.put("/update/json/{id}", status_code=200, summary="修改图片的 modified_json")
|
|
|
-def update_image_modified_json(
|
|
|
+@router.put("/update/json/{id}", status_code=200, summary="重新计算分数, 并修改图片的 modified_json")
|
|
|
+async def update_image_modified_json(
|
|
|
+ id: int,
|
|
|
+ new_json_data: dict,
|
|
|
+ db_conn: PooledMySQLConnection = db_dependency
|
|
|
+):
|
|
|
+ """
|
|
|
+ 根据 id 获取 image_type, 调用外部接口重新计算分数, 并更新 modified_json。
|
|
|
+ updated_at 会自动更新
|
|
|
+ """
|
|
|
+ cursor = None
|
|
|
+ try:
|
|
|
+ cursor = db_conn.cursor(dictionary=True)
|
|
|
+
|
|
|
+ # 1️ 获取图片信息
|
|
|
+ cursor.execute(f"SELECT image_type FROM {settings.DB_IMAGE_TABLE_NAME} WHERE id = %s", (id,))
|
|
|
+ row = cursor.fetchone()
|
|
|
+ if not row:
|
|
|
+ raise HTTPException(status_code=404, detail=f"ID为 {id} 的图片未找到。")
|
|
|
+
|
|
|
+ image_type = row["image_type"]
|
|
|
+ score_type = IMAGE_TYPE_TO_SCORE_TYPE.get(image_type)
|
|
|
+ if not score_type:
|
|
|
+ raise HTTPException(status_code=400, detail=f"未知的 image_type: {image_type}")
|
|
|
+
|
|
|
+ # 2️ 调用远程计算接口
|
|
|
+ try:
|
|
|
+ params = {"score_type": score_type}
|
|
|
+ payload = new_json_data
|
|
|
+ response = await run_in_threadpool(
|
|
|
+ lambda: requests.post(
|
|
|
+ settings.SCORE_RECALCULATE_ENDPOINT,
|
|
|
+ params=params,
|
|
|
+ json=payload,
|
|
|
+ timeout=30
|
|
|
+ )
|
|
|
+ )
|
|
|
+ except Exception as e:
|
|
|
+ raise HTTPException(status_code=500, detail=f"调用分数计算服务失败: {e}")
|
|
|
+
|
|
|
+ if response.status_code != 200:
|
|
|
+ logger.error(f"分数计算接口返回错误: {response.status_code}, {response.text}")
|
|
|
+ raise HTTPException(status_code=response.status_code,
|
|
|
+ detail=f"分数计算接口返回错误: {response.text}")
|
|
|
+
|
|
|
+ recalculated_json = response.json()
|
|
|
+
|
|
|
+ # 3️ 保存结果到数据库
|
|
|
+ recalculated_json_str = json.dumps(recalculated_json, ensure_ascii=False)
|
|
|
+ update_query = f"UPDATE {settings.DB_IMAGE_TABLE_NAME} SET modified_json = %s WHERE id = %s"
|
|
|
+ cursor.execute(update_query, (recalculated_json_str, id))
|
|
|
+
|
|
|
+ if cursor.rowcount == 0:
|
|
|
+ raise HTTPException(status_code=404, detail=f"未找到ID {id} 的记录。")
|
|
|
+
|
|
|
+ db_conn.commit()
|
|
|
+ logger.info(f"图片ID {id} 的 modified_json 已更新并重新计算。")
|
|
|
+
|
|
|
+ return {
|
|
|
+ "message": f"成功更新图片ID {id} 的JSON数据",
|
|
|
+ "image_type": image_type,
|
|
|
+ "score_type": score_type
|
|
|
+ }
|
|
|
+
|
|
|
+ except HTTPException:
|
|
|
+ db_conn.rollback()
|
|
|
+ raise
|
|
|
+ except Exception as e:
|
|
|
+ db_conn.rollback()
|
|
|
+ logger.error(f"更新JSON失败 ({id}): {e}")
|
|
|
+ raise HTTPException(status_code=500, detail=f"更新JSON数据失败: {e}")
|
|
|
+ finally:
|
|
|
+ if cursor:
|
|
|
+ cursor.close()
|
|
|
+
|
|
|
+
|
|
|
+@router.put("/_update/json/{id}", status_code=200, summary="修改图片的 modified_json")
|
|
|
+def _update_image_modified_json(
|
|
|
id: int,
|
|
|
new_json_data: Dict[str, Any],
|
|
|
db_conn: PooledMySQLConnection = db_dependency
|