|
|
@@ -1,4 +1,5 @@
|
|
|
from typing import Optional, List, Dict, Any
|
|
|
+from datetime import date
|
|
|
from mysql.connector.pooling import PooledMySQLConnection
|
|
|
import json
|
|
|
from datetime import datetime
|
|
|
@@ -78,6 +79,15 @@ def get_card_list_with_images(
|
|
|
card_id: Optional[int],
|
|
|
card_name: Optional[str],
|
|
|
card_type: Optional[CardType],
|
|
|
+ is_edited: Optional[bool],
|
|
|
+ min_detect_score: Optional[float],
|
|
|
+ max_detect_score: Optional[float],
|
|
|
+ min_mod_score: Optional[float],
|
|
|
+ max_mod_score: Optional[float],
|
|
|
+ created_start: Optional[date],
|
|
|
+ created_end: Optional[date],
|
|
|
+ updated_start: Optional[date],
|
|
|
+ updated_end: Optional[date],
|
|
|
sort_by: SortBy,
|
|
|
sort_order: SortOrder,
|
|
|
skip: int,
|
|
|
@@ -90,7 +100,8 @@ def get_card_list_with_images(
|
|
|
conditions = []
|
|
|
params = []
|
|
|
|
|
|
- if card_id:
|
|
|
+ # --- 基础筛选 ---
|
|
|
+ if card_id is not None:
|
|
|
conditions.append("id = %s")
|
|
|
params.append(card_id)
|
|
|
if card_name:
|
|
|
@@ -100,24 +111,67 @@ def get_card_list_with_images(
|
|
|
conditions.append("card_type = %s")
|
|
|
params.append(card_type.value)
|
|
|
|
|
|
+ # 编辑状态
|
|
|
+ if is_edited is not None:
|
|
|
+ conditions.append("is_edited = %s")
|
|
|
+ params.append(is_edited)
|
|
|
+
|
|
|
+ # --- 分数范围 ---
|
|
|
+ # detection_score
|
|
|
+ if min_detect_score is not None:
|
|
|
+ conditions.append("detection_score >= %s")
|
|
|
+ params.append(min_detect_score)
|
|
|
+ if max_detect_score is not None:
|
|
|
+ conditions.append("detection_score <= %s")
|
|
|
+ params.append(max_detect_score)
|
|
|
+
|
|
|
+ # modified_score
|
|
|
+ if min_mod_score is not None:
|
|
|
+ conditions.append("modified_score >= %s")
|
|
|
+ params.append(min_mod_score)
|
|
|
+ if max_mod_score is not None:
|
|
|
+ conditions.append("modified_score <= %s")
|
|
|
+ params.append(max_mod_score)
|
|
|
+
|
|
|
+ # --- 新增筛选 3: 日期范围 ---
|
|
|
+ # 使用 DATE() 函数将数据库的时间戳转换为日期进行比较,
|
|
|
+ # 这样 '2023-01-01' 可以匹配 '2023-01-01 12:34:56'
|
|
|
+
|
|
|
+ # Created At
|
|
|
+ if created_start:
|
|
|
+ conditions.append("DATE(created_at) >= %s")
|
|
|
+ params.append(created_start)
|
|
|
+ if created_end:
|
|
|
+ conditions.append("DATE(created_at) <= %s")
|
|
|
+ params.append(created_end)
|
|
|
+
|
|
|
+ # Updated At
|
|
|
+ if updated_start:
|
|
|
+ conditions.append("DATE(updated_at) >= %s")
|
|
|
+ params.append(updated_start)
|
|
|
+ if updated_end:
|
|
|
+ conditions.append("DATE(updated_at) <= %s")
|
|
|
+ params.append(updated_end)
|
|
|
+
|
|
|
+ # --- 拼接 WHERE 子句 ---
|
|
|
if conditions:
|
|
|
query += " WHERE " + " AND ".join(conditions)
|
|
|
|
|
|
- # 添加排序和分页
|
|
|
+ # --- 排序和分页 ---
|
|
|
+ # 注意: 这里的 user input (sort_by) 是 Enum,比较安全,但在拼接 SQL 时仍需注意
|
|
|
query += f" ORDER BY {sort_by.value} {sort_order.value}, id DESC"
|
|
|
query += " LIMIT %s OFFSET %s"
|
|
|
params.extend([limit, skip])
|
|
|
|
|
|
+ # 执行查询
|
|
|
cursor.execute(query, tuple(params))
|
|
|
cards = cursor.fetchall()
|
|
|
|
|
|
if not cards:
|
|
|
return []
|
|
|
|
|
|
- # 2. 一次性获取所有相关卡牌的图片 (避免 N+1 查询)
|
|
|
+ # 2. 一次性获取所有相关卡牌的图片 (逻辑保持不变)
|
|
|
card_ids = [card['id'] for card in cards]
|
|
|
-
|
|
|
- # 使用 IN 子句和占位符
|
|
|
format_strings = ','.join(['%s'] * len(card_ids))
|
|
|
image_query = (
|
|
|
f"SELECT id, card_id, image_type, image_path, detection_image_path, modified_image_path "
|
|
|
@@ -126,7 +180,7 @@ def get_card_list_with_images(
|
|
|
cursor.execute(image_query, tuple(card_ids))
|
|
|
images = cursor.fetchall()
|
|
|
|
|
|
- # 3. 将图片按 card_id 分组
|
|
|
+ # 3. 将图片按 card_id 分组 (逻辑保持不变)
|
|
|
images_by_card_id = {}
|
|
|
for image in images:
|
|
|
cid = image['card_id']
|
|
|
@@ -134,20 +188,16 @@ def get_card_list_with_images(
|
|
|
images_by_card_id[cid] = []
|
|
|
images_by_card_id[cid].append(image)
|
|
|
|
|
|
- # 4. 将图片附加到对应的卡牌上
|
|
|
+ # 4. 将图片附加到对应的卡牌上 (逻辑保持不变)
|
|
|
for card in cards:
|
|
|
- # 初始化新的字典字段
|
|
|
card['image_path_list'] = {}
|
|
|
card['detection_image_path_list'] = {}
|
|
|
card['modified_image_path_list'] = {}
|
|
|
|
|
|
- # 获取与当前卡牌关联的所有图片记录
|
|
|
related_images = images_by_card_id.get(card['id'], [])
|
|
|
-
|
|
|
- # 遍历图片记录,用 image_type 作为 key 来填充三个字典
|
|
|
for image_data in related_images:
|
|
|
- image_type = image_data['image_type'] # e.g., 'front_face'
|
|
|
- if image_type: # 确保 image_type 不为空
|
|
|
+ image_type = image_data['image_type']
|
|
|
+ if image_type:
|
|
|
card['image_path_list'][image_type] = image_data.get('image_path')
|
|
|
card['detection_image_path_list'][image_type] = image_data.get('detection_image_path')
|
|
|
card['modified_image_path_list'][image_type] = image_data.get('modified_image_path')
|