From c9e68a2bf0ca2a01b548f61fe982f197fc94bf29 Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Thu, 5 Feb 2026 11:28:33 +0800 Subject: [PATCH] fix: handle large dataset queries and type conversion - Add batch processing to production order query to handle SQL Server's 2100 parameter limit - Fix material name column index from Q to R column - Add string type conversion to prevent TypeError in material matching Co-Authored-By: Claude Sonnet 4.5 --- db/production_order_query.py | 32 +++++++++++++++++++----------- utils/material_status_validator.py | 6 ++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/db/production_order_query.py b/db/production_order_query.py index 4612cf3..ce91524 100644 --- a/db/production_order_query.py +++ b/db/production_order_query.py @@ -34,17 +34,25 @@ def query_production_order_numbers(production_ids): if not production_ids: return [] - # 构建 IN 子句的占位符 - placeholders = ','.join(['?' for _ in production_ids]) + # SQL Server 限制每个查询最多 2100 个参数 + BATCH_SIZE = 2000 + all_results = [] - query = f""" - SELECT [生产订单号] - FROM [productionContractData].[26年压力表合同数据] - WHERE [总排号] IN ({placeholders}) - """ + # 分批查询 + for i in range(0, len(production_ids), BATCH_SIZE): + batch = production_ids[i:i + BATCH_SIZE] + placeholders = ','.join(['?' for _ in batch]) - with get_connection() as conn: - results = conn.execute_query(query, tuple(production_ids)) - # 提取生产订单号并去除空值 - production_order_numbers = [row['生产订单号'] for row in results if row['生产订单号']] - return production_order_numbers + query = f""" + SELECT [生产订单号] + FROM [productionContractData].[26年压力表合同数据] + WHERE [总排号] IN ({placeholders}) + """ + + with get_connection() as conn: + results = conn.execute_query(query, tuple(batch)) + # 提取生产订单号并去除空值 + batch_numbers = [row['生产订单号'] for row in results if row['生产订单号']] + all_results.extend(batch_numbers) + + return all_results diff --git a/utils/material_status_validator.py b/utils/material_status_validator.py index a85ba2e..8dbeb89 100644 --- a/utils/material_status_validator.py +++ b/utils/material_status_validator.py @@ -43,8 +43,10 @@ class MaterialStatusValidator: List[str]: 去重后的材料名称列表 """ df = pd.read_excel(excel_file) - # Q列索引为16(Python从0开始) - material_names = df.iloc[:, 16].dropna().unique().tolist() + # R列索引为17(Python从0开始) + material_names = df.iloc[:, 17].dropna().unique().tolist() + # 确保所有元素都是字符串类型 + material_names = [str(name) for name in material_names] return material_names def match_materials(self, material_names: List[str],