import requests import json from enum import Enum from fastapi import APIRouter, Depends, HTTPException, Query, Body from fastapi.concurrency import run_in_threadpool from mysql.connector.pooling import PooledMySQLConnection from app.core.config import settings from app.core.logger import get_logger from app.core.database_loader import get_db_connection from app.utils.scheme import ( CardDetailResponse, IMAGE_TYPE_TO_SCORE_TYPE ) from app.crud import crud_card # 导入新写的工具函数 from app.utils.xy_process import convert_internal_to_xy_format, convert_xy_to_internal_format logger = get_logger(__name__) router = APIRouter() db_dependency = Depends(get_db_connection) class QueryMode(str, Enum): current = "current" next = "next" prev = "prev" def _process_images_to_xy_format(card_data: dict): """ 内部辅助函数:遍历卡牌数据中的图片,将 JSON 格式转换为前端需要的 XY 格式。 直接修改传入的 card_data 字典。 """ if "images" in card_data and card_data["images"]: for img in card_data["images"]: # 处理 detection_json if img.detection_json: d_json = img.detection_json if isinstance(d_json, str): d_json = json.loads(d_json) # *** 转换逻辑 *** img.detection_json = convert_internal_to_xy_format(d_json) # 处理 modified_json if img.modified_json: m_json = img.modified_json if isinstance(m_json, str): m_json = json.loads(m_json) # *** 转换逻辑 *** img.modified_json = convert_internal_to_xy_format(m_json) return card_data @router.get("/query", response_model=CardDetailResponse, summary="获取卡牌详细信息(格式化xy), 支持前后翻页") def get_card_details( card_id: int = Query(..., description="基准卡牌ID"), mode: QueryMode = Query(QueryMode.current, description="查询模式: current(当前), next(下一个), prev(上一个)"), db_conn: PooledMySQLConnection = db_dependency ): """ 获取卡牌元数据以及所有与之关联的图片信息,并将坐标转换为 xy 格式。 同时返回上一张和下一张卡牌的ID。 - **current**: 查询 card_id 对应的卡牌。 - **next**: 查询 ID 比 card_id 大的第一张卡牌。 - **prev**: 查询 ID 比 card_id 小的第一张卡牌。 """ target_id = card_id cursor = None try: cursor = db_conn.cursor(dictionary=True) # 1. 如果是查询上一个或下一个,先计算目标ID if mode != QueryMode.current: if mode == QueryMode.next: query_target = (f"SELECT id FROM {settings.DB_CARD_TABLE_NAME} " f"WHERE id > %s ORDER BY id ASC LIMIT 1") else: # mode == QueryMode.prev query_target = (f"SELECT id FROM {settings.DB_CARD_TABLE_NAME} " f"WHERE id < %s ORDER BY id DESC LIMIT 1") cursor.execute(query_target, (card_id,)) row = cursor.fetchone() if not row: msg = "没有下一张" if mode == QueryMode.next else "没有上一张" raise HTTPException(status_code=200, detail=msg) target_id = row['id'] # 2. 获取目标卡牌的详细数据 (Dict 格式) card_data = crud_card.get_card_with_details(db_conn, target_id) if not card_data: raise HTTPException(status_code=404, detail=f"ID为 {target_id} 的卡牌未找到。") # 3. 补充当前目标卡牌的 id_prev 和 id_next # 注意:这里需要重新获取 cursor,或者使用 cursor (非 dict 模式可能更方便取值,但 dict 模式也行) # 这里为了简单直接用 raw SQL # 查询上一个ID sql_prev = f"SELECT id FROM {settings.DB_CARD_TABLE_NAME} WHERE id < %s ORDER BY id DESC LIMIT 1" cursor.execute(sql_prev, (target_id,)) row_prev = cursor.fetchone() card_data['id_prev'] = row_prev['id'] if row_prev else None # 查询下一个ID sql_next = f"SELECT id FROM {settings.DB_CARD_TABLE_NAME} WHERE id > %s ORDER BY id ASC LIMIT 1" cursor.execute(sql_next, (target_id,)) row_next = cursor.fetchone() card_data['id_next'] = row_next['id'] if row_next else None # 4. 遍历图片,转换格式 (使用抽取出的辅助函数) _process_images_to_xy_format(card_data) # 5. 验证并返回 return CardDetailResponse.model_validate(card_data) except HTTPException: raise except Exception as e: logger.error(f"查询卡牌详情失败 (Mode: {mode}, BaseID: {card_id}): {e}") raise HTTPException(status_code=500, detail="数据库查询失败") finally: if cursor: cursor.close() @router.put("/update/json/{id}", status_code=200, summary="接收xy格式, 还原后重计算分数并保存") async def update_image_modified_json( id: int, new_json_data: dict = Body(..., description="前端传来的包含xy对象格式的JSON"), db_conn: PooledMySQLConnection = db_dependency ): """ 接收前端传来的特殊格式 JSON (points 为对象列表)。 1. 将格式还原为后端标准格式 (points 为 [[x,y]])。 2. 根据 id 获取 image_type。 3. 调用外部接口重新计算分数。 4. 更新 modified_json。 """ card_id_to_update = None cursor = None # *** 1. 格式还原 *** # 将前端的 xy dict 格式转回 [[x,y]],并丢弃 points 里的 id internal_json_payload = convert_xy_to_internal_format(new_json_data) try: cursor = db_conn.cursor(dictionary=True) # 2. 获取图片信息 cursor.execute(f"SELECT image_type, card_id 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} 的图片未找到。") card_id_to_update = row["card_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}") logger.info(f"开始计算分数 (ID: {id}, Type: {score_type})") # 3. 调用远程计算接口 (使用还原后的 JSON) try: response = await run_in_threadpool( lambda: requests.post( settings.SCORE_RECALCULATE_ENDPOINT, params={"score_type": score_type}, json=internal_json_payload, # 传递还原后的数据 timeout=20 ) ) 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}") logger.info("分数计算完成") # 获取计算服务返回的结果(这个结果通常已经是标准的 internal 格式,带有分数和面积) final_json_data = response.json() # 4. 保存结果到数据库 recalculated_json_str = json.dumps(final_json_data, ensure_ascii=False) update_query = (f"UPDATE {settings.DB_IMAGE_TABLE_NAME} " f"SET modified_json = %s, is_edited = TRUE " f"WHERE id = %s") cursor.execute(update_query, (recalculated_json_str, id)) db_conn.commit() logger.info(f"图片ID {id} 的 modified_json 已更新并重新计算。") # 更新对应的 cards 的分数状态 try: crud_card.update_card_scores_and_status(db_conn, card_id_to_update) logger.info(f"卡牌 {card_id_to_update} 的分数和状态已更新。") except Exception as score_update_e: logger.error(f"更新卡牌 {card_id_to_update} 分数失败: {score_update_e}") return { "detail": 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()