feat: add database-driven material validation with multi-mode support

This commit enhances the material validation functionality to support
database-driven workflows alongside the existing Excel-based approach.

## New Features

### DAO Layer
- Add ProductionContractDataDAO for querying production contract data
- Add MaterialsToBeDeletedDAO with full CRUD operations
- Enhance DiscreteMaterialPlanDAO with query_all(), query_by_source_numbers(),
  and get_unique_material_names() methods

### Validation Modes
Support for 4 validation modes in MaterialValidationTab:
1. Database Full - Query all materials from DiscreteMaterialPlanData
2. Database Filtered - Query by ProductionID.txt file
3. Excel Existing - Validate from existing Excel file
4. Excel Full - Complete workflow with ERP extraction

### Configuration
- Add ValidationConfig dataclass with data_source, batch_size, match_mode,
  enable_crud_operations, and default_manager fields
- Update ConfigLoader to support validation configuration
- Add validation settings section in SettingsTab GUI

### Query Chain
Implementation of full query chain:
ProductionID.txt (总排号) → productionContractData (生产订单号) →
DiscreteMaterialPlanData (SourceNumber) → MaterialName →
MaterialsToBeDeleted comparison

## Backward Compatibility
All existing Excel-based validation methods remain unchanged, ensuring
no breaking changes for existing workflows.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-02-06 13:08:31 +08:00
parent 53a1e33e45
commit 2096a51f55
9 changed files with 976 additions and 57 deletions

View File

@@ -26,12 +26,12 @@ class DiscreteMaterialPlanDAO:
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit context manager and close database connection"""
if self.db:
self.db.close()
self.db.disconnect()
def close(self):
"""Close database connection"""
if self.db:
self.db.close()
self.db.disconnect()
def save_dataframe_with_replace(self, df: pd.DataFrame) -> Dict[str, int]:
"""
@@ -311,3 +311,100 @@ class DiscreteMaterialPlanDAO:
"""
result = db.execute_query(sql)
return result[0] if result else {}
# ==================== ENHANCED QUERY METHODS ====================
def query_all(self) -> List[Dict]:
"""
Query all records from DiscreteMaterialPlanData table.
Returns:
List of dictionaries representing all records
"""
with get_connection() as db:
sql = "SELECT * FROM DiscreteMaterialPlanData"
return db.execute_query(sql)
def query_by_source_numbers(self, source_numbers: List[str]) -> List[Dict]:
"""
Query records by SourceNumber list (生产订单号).
Args:
source_numbers: List of SourceNumber values to query
Returns:
List of dictionaries representing records
"""
if not source_numbers:
return []
# SQL Server parameter limit requires batching
batch_size = 2000
all_results = []
for i in range(0, len(source_numbers), batch_size):
batch = source_numbers[i:i + batch_size]
placeholders = ','.join(['?' for _ in batch])
sql = f"SELECT * FROM DiscreteMaterialPlanData WHERE SourceNumber IN ({placeholders})"
with get_connection() as db:
results = db.execute_query(sql, tuple(batch))
all_results.extend(results)
return all_results
def get_unique_material_names(self, source_numbers: List[str] = None) -> List[str]:
"""
Get unique material names, optionally filtered by SourceNumber.
Args:
source_numbers: Optional list of SourceNumber values to filter by
Returns:
List of unique material names
"""
if source_numbers is None or not source_numbers:
# No filter - get all unique material names
sql = "SELECT DISTINCT MaterialName FROM DiscreteMaterialPlanData WHERE MaterialName IS NOT NULL"
with get_connection() as db:
results = db.execute_query(sql)
return [r['MaterialName'] for r in results if r.get('MaterialName')]
else:
# Filter by SourceNumber list
batch_size = 2000
all_material_names = set()
for i in range(0, len(source_numbers), batch_size):
batch = source_numbers[i:i + batch_size]
placeholders = ','.join(['?' for _ in batch])
sql = f"""
SELECT DISTINCT MaterialName
FROM DiscreteMaterialPlanData
WHERE SourceNumber IN ({placeholders})
AND MaterialName IS NOT NULL
"""
with get_connection() as db:
results = db.execute_query(sql, tuple(batch))
batch_materials = [r['MaterialName'] for r in results if r.get('MaterialName')]
all_material_names.update(batch_materials)
return list(all_material_names)
def get_material_names_by_总排号(self, 总排号_list: List[str]) -> List[str]:
"""
Get unique material names by 总排号 list.
This method combines query from production contract data and discrete material plan.
Args:
总排号_list: List of 总排号 values
Returns:
List of unique material names
"""
from db.production_contract_data_dao import ProductionContractDataDAO
# First get SourceNumbers from production contract data
contract_dao = ProductionContractDataDAO()
source_numbers = contract_dao.get_source_numbers_by_总排号(总排号_list)
# Then get material names filtered by these SourceNumbers
return self.get_unique_material_names(source_numbers)