Просмотр исходного кода

合同管理筛查+分页查询

mengqiankang 2 месяцев назад
Родитель
Сommit
87131ae9b4
1 измененных файлов с 36 добавлено и 18 удалено
  1. 36 18
      alien_store/api/router.py

+ 36 - 18
alien_store/api/router.py

@@ -115,13 +115,15 @@ async def create_esign_templates(
         contract_url=contract_url
     )
 
-@router.get("/contracts/{store_id}", response_model=Union[List[ContractStoreResponse], dict])
+@router.get("/contracts/{store_id}", response_model=Union[dict, Any])
 async def list_contracts(
     store_id: int, 
     status: Optional[int] = Query(None, description="筛选合同状态:0 未签署,1 已签署"),
+    page: int = Query(1, ge=1, description="页码,从1开始"),
+    page_size: int = Query(10, ge=1, le=100, description="每页条数,默认10"),
     templates_server: ContractServer = Depends(get_contract_service)
 ) -> Any:
-    """根据 store_id 查询所有合同,支持根据 status 筛选"""
+    """根据 store_id 查询所有合同,支持根据 status 筛选和分页"""
     # 1. 检查 store_info 中的审核状态
     reason = await templates_server.get_store_reason(store_id)
     if reason != "审核通过":
@@ -130,11 +132,8 @@ async def list_contracts(
     # 2. 返回合同列表
     rows = await templates_server.list_by_store(store_id)
     
-    if status is None:
-        return [ContractStoreResponse(**row) for row in rows]
-
-    # 3. 根据 status 筛选 contract_url 中的项
-    filtered_rows = []
+    all_filtered_items = []
+    # 3. 解析并筛选所有符合条件的合同项
     for row in rows:
         contract_url_raw = row.get("contract_url")
         if not contract_url_raw:
@@ -144,19 +143,38 @@ async def list_contracts(
             if not isinstance(items, list):
                 continue
             
-            # 过滤列表中的合同项
-            filtered_items = [item for item in items if item.get("status") == status]
-            
-            if filtered_items:
-                # 只有当存在符合条件的合同时,才返回该记录,并只包含符合条件的合同项
-                row_copy = dict(row)
-                row_copy["contract_url"] = json.dumps(filtered_items, ensure_ascii=False)
-                filtered_rows.append(ContractStoreResponse(**row_copy))
+            for item in items:
+                # 如果传了 status,则进行筛选
+                if status is not None and item.get("status") != status:
+                    continue
+                
+                # 将店铺基础信息混入每个合同项中,方便前端展示
+                item_with_info = dict(item)
+                item_with_info["id"] = row.get("id")
+                item_with_info["store_id"] = row.get("store_id")
+                item_with_info["store_name"] = row.get("store_name")
+                item_with_info["merchant_name"] = row.get("merchant_name")
+                item_with_info["contact_phone"] = row.get("contact_phone")
+                all_filtered_items.append(item_with_info)
         except Exception as e:
-            logger.error(f"Error filtering contracts for store_id {store_id}: {e}")
+            logger.error(f"Error processing contracts for store_id {store_id}: {e}")
             continue
-            
-    return filtered_rows
+
+    # 4. 手动分页
+    total = len(all_filtered_items)
+    start = (page - 1) * page_size
+    end = start + page_size
+    paged_items = all_filtered_items[start:end]
+    
+    total_pages = (total + page_size - 1) // page_size if total > 0 else 0
+
+    return {
+        "items": paged_items,
+        "total": total,
+        "page": page,
+        "page_size": page_size,
+        "total_pages": total_pages
+    }
 
 @router.get("/get_all_templates", response_model=PaginatedResponse)
 async def get_all_templates(