Kaynağa Gözat

refactor(decodeWorkflow): 修改搜索接口为GET并支持可选参数查询

将POST接口改为GET接口,支持可选参数查询任务列表
重构数据库查询逻辑,动态构建SQL条件
更新错误提示信息为"暂无数据"
max_liu 1 gün önce
ebeveyn
işleme
47609d8939
2 değiştirilmiş dosya ile 36 ekleme ve 7 silme
  1. 23 3
      decode_task/topicTask.py
  2. 13 4
      main.py

+ 23 - 3
decode_task/topicTask.py

@@ -34,9 +34,29 @@ def get_topic_result_by_id(task_id:str):
 
 
 
-def search_topic_list(param:DecodeWorkflowParam):
-    sql = "SELECT * FROM decode_workflow WHERE video_id = %s AND video_url = %s AND title = %s AND task_status = %s"
-    tasks = mysql.fetchall(sql, (param.video_id,param.video_url,param.title,param.task_status))
+def search_topic_list(param=None):
+    base_sql = "SELECT * FROM decode_workflow"
+    conditions = []
+    values = []
+    if isinstance(param, dict):
+        v_id = param.get("video_id")
+        v_url = param.get("video_url")
+        title = param.get("title")
+        status = param.get("task_status")
+        if v_id:
+            conditions.append("video_id = %s")
+            values.append(v_id)
+        if v_url:
+            conditions.append("video_url = %s")
+            values.append(v_url)
+        if title:
+            conditions.append("title = %s")
+            values.append(title)
+        if status is not None:
+            conditions.append("task_status = %s")
+            values.append(status)
+    sql = base_sql if not conditions else f"{base_sql} WHERE " + " AND ".join(conditions)
+    tasks = mysql.fetchall(sql, tuple(values))
     if not tasks:
         logger.info(f"任务不存在")
         return None

+ 13 - 4
main.py

@@ -141,13 +141,22 @@ def get_topic_result(param: TaskStatusParam):
   
 
 
-@app.post("/decodeWorkflow/list")
-def search_topic_list(param:DecodeWorkflowParam):
-    tasks = search_topic_list_db(param)
+@app.get("/decodeWorkflow/list")
+def search_topic_list(video_id: str = None, video_url: str = None, title: str = None, task_status: int = None):
+    params = {}
+    if video_id:
+        params["video_id"] = video_id
+    if video_url:
+        params["video_url"] = video_url
+    if title:
+        params["title"] = title
+    if task_status is not None:
+        params["task_status"] = task_status
+    tasks = search_topic_list_db(params if params else None)
     if not tasks:
         return {
             "code": -1,
-            "message": "任务不存在",
+            "message": "暂无数据",
             "data": []
         }
     return {