This commit implements a complete migration from JSON-based configuration to .env environment variables, providing better security and flexibility. Key Changes: - Add python-dotenv dependency for environment variable support - Create config/env_loader.py with type conversion utilities - Add from_env() class methods to all config dataclasses - Update ConfigLoader to prioritize environment variables - Add save_to_env() method for .env file management - Implement database connection factory pattern - Add base DAO and connection classes for better abstraction - Support both SQL Server and MySQL with unified interface - Create migration script (scripts/migrate_to_env.py) - Update GUI to read/write .env files - Add comprehensive migration documentation New Files: - config/env_loader.py - Environment variable loader - db/base_connection.py - Base database connection interface - db/base_dao.py - Base DAO with common utilities - db/connection_factory.py - Factory for creating connections - db/mysql_connection.py - MySQL-specific connection - db/sqlserver_connection.py - SQL Server-specific connection - db/table_name_converter.py - SQL dialect converter - scripts/migrate_to_env.py - Configuration migration tool - docs/ENV_MIGRATION.md - Complete migration guide - .env.example - Environment variable template Testing: - Verified MySQL connection (8.0.44) - Tested all DAO operations - Confirmed 150 tables accessible - Validated configuration loading Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
"""
|
|
Data Access Object for production contract data.
|
|
|
|
This module provides query operations for accessing production contract data
|
|
from the [productionContractData].[26年压力表合同数据] table.
|
|
"""
|
|
|
|
from typing import List, Dict, Any
|
|
from db.base_dao import BaseDAO
|
|
from db.connection import get_connection
|
|
from config.schema import DatabaseType
|
|
|
|
|
|
class ProductionContractDataDAO(BaseDAO):
|
|
"""Data Access Object for production contract data queries"""
|
|
|
|
def query_by_总排号(self, 总排号_list: List[str]) -> List[Dict[str, Any]]:
|
|
"""
|
|
Query production contract data by 总排号 list.
|
|
|
|
Args:
|
|
总排号_list: List of 总排号 values to query
|
|
|
|
Returns:
|
|
List of dictionaries containing 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
|
|
"""
|
|
if not 总排号_list:
|
|
return []
|
|
|
|
# SQL Server parameter limit requires batching
|
|
batch_size = 2000
|
|
all_results = []
|
|
|
|
for i in range(0, len(总排号_list), batch_size):
|
|
batch = 总排号_list[i:i + batch_size]
|
|
placeholder = self._get_placeholder()
|
|
placeholders = ','.join([placeholder for _ in batch])
|
|
|
|
# 根据数据库类型选择表名
|
|
table_name = self._convert_sql('[productionContractData].[26年压力表合同数据]')
|
|
|
|
# 根据数据库类型选择列名格式
|
|
if self._db_type == DatabaseType.MYSQL:
|
|
sql = f"""
|
|
SELECT 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
|
|
FROM {table_name}
|
|
WHERE 总排号 IN ({placeholders})
|
|
ORDER BY 序号
|
|
"""
|
|
else:
|
|
sql = f"""
|
|
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
|
|
FROM {table_name}
|
|
WHERE [总排号] IN ({placeholders})
|
|
ORDER BY [序号]
|
|
"""
|
|
|
|
with get_connection() as db:
|
|
results = db.execute_query(sql, tuple(batch))
|
|
all_results.extend(results)
|
|
|
|
return all_results
|
|
|
|
def get_source_numbers_by_总排号(self, 总排号_list: List[str]) -> List[str]:
|
|
"""
|
|
Extract unique 生产订单号 values by 总排号 list.
|
|
|
|
Args:
|
|
总排号_list: List of 总排号 values to query
|
|
|
|
Returns:
|
|
List of unique 生产订单号 values (SourceNumber)
|
|
"""
|
|
results = self.query_by_总排号(总排号_list)
|
|
# Extract unique 生产订单号 values, excluding None/null values
|
|
source_numbers = list(set(
|
|
[r['生产订单号'] for r in results if r.get('生产订单号')]
|
|
))
|
|
return source_numbers
|
|
|
|
def get_生产订单号_map(self, 总排号_list: List[str]) -> Dict[str, str]:
|
|
"""
|
|
Get mapping between 总排号 and 生产订单号.
|
|
|
|
Args:
|
|
总排号_list: List of 总排号 values to query
|
|
|
|
Returns:
|
|
Dictionary mapping 总排号 -> 生产订单号
|
|
"""
|
|
results = self.query_by_总排号(总排号_list)
|
|
return {
|
|
r['总排号']: r['生产订单号']
|
|
for r in results
|
|
if r.get('总排号') and r.get('生产订单号')
|
|
}
|