Explorar o código

```
feat(api): 添加商品货号搜索功能到拍照记录接口

- 在get_photo_records接口中新增goods_art_no参数支持按商品货号搜索
- 重构查询逻辑,添加基础查询条件构建和动态过滤功能
- 优化分页查询性能,支持按商品货号模糊匹配
- 整理代码格式,移除多余空行提高可读性
```

rambo hai 2 semanas
pai
achega
1dc8289c41
Modificáronse 1 ficheiros con 19 adicións e 19 borrados
  1. 19 19
      python/api.py

+ 19 - 19
python/api.py

@@ -1112,36 +1112,38 @@ def reset_config(params: ModelGetDeviceConfig):
 
 
 @app.get("/get_photo_records", description="获取拍照记录")
-def get_photo_records(page: int = 1, size: int = 5):
+def get_photo_records(page: int = 1, size: int = 5, goods_art_no: str = None):
     session = SqlQuery()
     current_page = page
     # photos = CRUD(PhotoRecord)
     print("准备查询拍摄记录", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
-    
+    # 构建基础查询条件
+    base_conditions = [PhotoRecord.delete_time == None]
+    if goods_art_no:
+        base_conditions.append(PhotoRecord.goods_art_no.like(f"%{goods_art_no}%"))
     # 首先统计总数
-    count_statement = (
-        select(func.count(PhotoRecord.goods_art_no.distinct()))
-        .where(PhotoRecord.delete_time == None)
+    count_statement = select(func.count(PhotoRecord.goods_art_no.distinct())).where(
+        *base_conditions
     )
     total_count = session.exec(count_statement).one()
-    
+
     # 查询所有不重复的货号及对应的最大时间,进行分页
     base_statement = (
         select(PhotoRecord.goods_art_no, func.max(PhotoRecord.id).label('max_id'))
-        .where(PhotoRecord.delete_time == None)
+        .where(*base_conditions)
         .group_by(PhotoRecord.goods_art_no)
         .order_by(desc('max_id'))
         .offset((page - 1) * size)
         .limit(size)
     )
     paginated_results = session.exec(base_statement).all()
-    
+
     # 获取这些货号的详细记录
     list_data = []
     if paginated_results:
         # 获取当前页的货号列表
         current_goods_art_nos = [item.goods_art_no for item in paginated_results]
-        
+
         # 查询这些货号的所有记录
         query = (
             select(PhotoRecord, DeviceConfig.action_name)
@@ -1151,7 +1153,7 @@ def get_photo_records(page: int = 1, size: int = 5):
             .order_by(asc("image_index"))  # 按货号分组并按ID倒序
         )
         all_items = session.exec(query).mappings().all()
-        
+
         # 按货号分组
         items_by_goods = {}
         for item in all_items:
@@ -1159,7 +1161,7 @@ def get_photo_records(page: int = 1, size: int = 5):
             if goods_art_no not in items_by_goods:
                 items_by_goods[goods_art_no] = []
             items_by_goods[goods_art_no].append(item)
-        
+
         # 构建结果列表,保持分页的顺序
         for item in paginated_results:
             goods_art_no = item.goods_art_no
@@ -1173,15 +1175,15 @@ def get_photo_records(page: int = 1, size: int = 5):
                         "items": items_by_goods[goods_art_no],
                     }
                 )
-    
+
     session.close()
     print("循环查询 完成 ", datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
-    
+
     # 计算分页信息
     total_pages = (total_count + size - 1) // size  # 向上取整
     has_prev = page > 1
     has_next = page < total_pages
-    
+
     return {
         "code": 0,
         "msg": "",
@@ -1638,9 +1640,8 @@ def uploadImage(remove_pic_ins,token:str, local_path: str) -> str:
             url, files={"file":im}, headers=post_headers
         ).json()
         return resultData["data"]["url"]
-    
-    
-    
+
+
 @app.post("/remove_background", description="图片抠图-http请求")
 async def remove_background(params:PhotoRecordRemoveBackground):
     # await socket_manager.send_message(msg="测试")
@@ -1901,7 +1902,6 @@ async def rename_shadow_folder(params:RenameShadow):
     return {"code": 0, "message": "重命名完成", "data": {"result": success_result}}
 
 
-
 @app.get("/minimize_window", description="最小化窗口")
 def minimize_window(window_title: str):
     hwnd_list = []
@@ -2065,4 +2065,4 @@ async def import_images_from_dir(params:ImportDirs):
         raise UnicornException(str(e))
     except Exception as e:
         logger.error(f"API 调用异常: {str(e)}")
-        raise UnicornException(f"{str(e)}")
+        raise UnicornException(f"{str(e)}")