style: format all Python files with Black
Apply Black formatter to the entire codebase for consistent code style. Co-Authored-By: Claude (glm-5) <noreply@anthropic.com>
This commit is contained in:
@@ -65,16 +65,16 @@ class DiscreteMaterialPlanCleaner:
|
||||
|
||||
# 统计信息
|
||||
self.stats = {
|
||||
'total_orders': 0,
|
||||
'processed_orders': 0,
|
||||
'total_materials': 0, # 总物料数
|
||||
'processed_materials': 0, # 已处理物料数
|
||||
'deleted_materials': [], # [{order_id, material_code, material_name}]
|
||||
'skipped_materials': [], # [{order_id, material_code, material_name, reason}]
|
||||
'unmatched_materials': [], # [{order_id, material_code, material_name}] 不在删除列表的物料
|
||||
'errors': [], # [{order_id, error_message}]
|
||||
'start_time': None,
|
||||
'end_time': None,
|
||||
"total_orders": 0,
|
||||
"processed_orders": 0,
|
||||
"total_materials": 0, # 总物料数
|
||||
"processed_materials": 0, # 已处理物料数
|
||||
"deleted_materials": [], # [{order_id, material_code, material_name}]
|
||||
"skipped_materials": [], # [{order_id, material_code, material_name, reason}]
|
||||
"unmatched_materials": [], # [{order_id, material_code, material_name}] 不在删除列表的物料
|
||||
"errors": [], # [{order_id, error_message}]
|
||||
"start_time": None,
|
||||
"end_time": None,
|
||||
}
|
||||
|
||||
def _log(self, message, level="info"):
|
||||
@@ -100,7 +100,7 @@ class DiscreteMaterialPlanCleaner:
|
||||
total_materials: int,
|
||||
order_id: str,
|
||||
material_name: str,
|
||||
action: str
|
||||
action: str,
|
||||
):
|
||||
"""报告物料处理进度
|
||||
|
||||
@@ -166,7 +166,9 @@ class DiscreteMaterialPlanCleaner:
|
||||
)
|
||||
return order_ids
|
||||
|
||||
def process_order(self, inner_frame, order_id, order_index, page1, total_orders: int = 1):
|
||||
def process_order(
|
||||
self, inner_frame, order_id, order_index, page1, total_orders: int = 1
|
||||
):
|
||||
"""清理单个订单的数据
|
||||
|
||||
Args:
|
||||
@@ -225,7 +227,7 @@ class DiscreteMaterialPlanCleaner:
|
||||
if detail_status == "审批通过":
|
||||
if detail_count > 0:
|
||||
# 更新总物料数统计
|
||||
self.stats['total_materials'] += detail_count
|
||||
self.stats["total_materials"] += detail_count
|
||||
|
||||
# --- 点击修改并等待状态切换 (保留原逻辑) ---
|
||||
detail_inner_frame.get_by_role("button", name="修改").click()
|
||||
@@ -252,7 +254,7 @@ class DiscreteMaterialPlanCleaner:
|
||||
# page2.pause() # 调试用,正式运行时可删除
|
||||
while True:
|
||||
material_idx += 1
|
||||
self.stats['processed_materials'] += 1
|
||||
self.stats["processed_materials"] += 1
|
||||
|
||||
# 稳定性检查:等待行号更新
|
||||
current_row = self._get_input_value(child_form, r"^行号$")
|
||||
@@ -262,25 +264,37 @@ class DiscreteMaterialPlanCleaner:
|
||||
|
||||
material_code = self._get_input_value(child_form, r"^材料编码")
|
||||
material_name = self._get_input_value(child_form, r"^材料名称")
|
||||
pending_qty = self._get_input_value(child_form, r"^累计待发数量$")
|
||||
pending_qty = self._get_input_value(
|
||||
child_form, r"^累计待发数量$"
|
||||
)
|
||||
|
||||
# 报告物料进度
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "检查"
|
||||
order_index,
|
||||
total_orders,
|
||||
material_idx,
|
||||
detail_count,
|
||||
order_id,
|
||||
material_name,
|
||||
"检查",
|
||||
)
|
||||
|
||||
if material_code in self.to_delete_set:
|
||||
self._log(f"发现匹配物料: {material_name} ({material_code})")
|
||||
self._log(
|
||||
f"发现匹配物料: {material_name} ({material_code})"
|
||||
)
|
||||
if (not pending_qty) and not (
|
||||
row_num_int >= 7000 and row_num_int < 8000
|
||||
):
|
||||
# 报告删除进度
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "删除"
|
||||
order_index,
|
||||
total_orders,
|
||||
material_idx,
|
||||
detail_count,
|
||||
order_id,
|
||||
material_name,
|
||||
"删除",
|
||||
)
|
||||
# 记录删除前的行号
|
||||
old_row_number = current_row
|
||||
@@ -308,75 +322,98 @@ class DiscreteMaterialPlanCleaner:
|
||||
time.sleep(0.2)
|
||||
else:
|
||||
self._log(
|
||||
f"⚠️ 等待删除完成超时({max_wait_time}秒)", "warn"
|
||||
f"⚠️ 等待删除完成超时({max_wait_time}秒)",
|
||||
"warn",
|
||||
)
|
||||
|
||||
# 记录删除统计
|
||||
if delete_success:
|
||||
self.stats['deleted_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name
|
||||
})
|
||||
self.stats["deleted_materials"].append(
|
||||
{
|
||||
"order_id": order_id,
|
||||
"material_code": material_code,
|
||||
"material_name": material_name,
|
||||
}
|
||||
)
|
||||
|
||||
continue
|
||||
elif row_num_int >= 7000 and row_num_int < 8000:
|
||||
reason = f"行号 {row_num_int} 在 7000-8000 范围内"
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "跳过"
|
||||
order_index,
|
||||
total_orders,
|
||||
material_idx,
|
||||
detail_count,
|
||||
order_id,
|
||||
material_name,
|
||||
"跳过",
|
||||
)
|
||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
||||
self.stats['skipped_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name,
|
||||
'reason': reason
|
||||
})
|
||||
self.stats["skipped_materials"].append(
|
||||
{
|
||||
"order_id": order_id,
|
||||
"material_code": material_code,
|
||||
"material_name": material_name,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
elif pending_qty:
|
||||
reason = f"待发数量为 {pending_qty}"
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "跳过"
|
||||
order_index,
|
||||
total_orders,
|
||||
material_idx,
|
||||
detail_count,
|
||||
order_id,
|
||||
material_name,
|
||||
"跳过",
|
||||
)
|
||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
||||
self.stats['skipped_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name,
|
||||
'reason': reason
|
||||
})
|
||||
self.stats["skipped_materials"].append(
|
||||
{
|
||||
"order_id": order_id,
|
||||
"material_code": material_code,
|
||||
"material_name": material_name,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
|
||||
else:
|
||||
reason = "不满足删除条件"
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "跳过"
|
||||
order_index,
|
||||
total_orders,
|
||||
material_idx,
|
||||
detail_count,
|
||||
order_id,
|
||||
material_name,
|
||||
"跳过",
|
||||
)
|
||||
self._log(
|
||||
f"⚠️ {reason},跳过物料 {material_name} ({material_code})",
|
||||
"warn",
|
||||
)
|
||||
self.stats['skipped_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name,
|
||||
'reason': reason
|
||||
})
|
||||
self.stats["skipped_materials"].append(
|
||||
{
|
||||
"order_id": order_id,
|
||||
"material_code": material_code,
|
||||
"material_name": material_name,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
|
||||
else:
|
||||
self._log(
|
||||
f"ℹ️ 物料 {material_name} ({material_code}) 不在删除列表中,不做处理"
|
||||
)
|
||||
# 记录到 unmatched_materials
|
||||
self.stats['unmatched_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name
|
||||
})
|
||||
self.stats["unmatched_materials"].append(
|
||||
{
|
||||
"order_id": order_id,
|
||||
"material_code": material_code,
|
||||
"material_name": material_name,
|
||||
}
|
||||
)
|
||||
if self._is_button_enabled(next_btn):
|
||||
last_row_number = current_row
|
||||
next_btn.click()
|
||||
@@ -417,7 +454,7 @@ class DiscreteMaterialPlanCleaner:
|
||||
def clean(self, production_id_file):
|
||||
"""执行完整清理流程"""
|
||||
# 初始化统计
|
||||
self.stats['start_time'] = datetime.now()
|
||||
self.stats["start_time"] = datetime.now()
|
||||
|
||||
# 0. 预加载数据库数据
|
||||
self.preload_data()
|
||||
@@ -450,20 +487,21 @@ class DiscreteMaterialPlanCleaner:
|
||||
order_ids = self.get_production_order_numbers(production_id_file)
|
||||
|
||||
# 设置总订单数
|
||||
self.stats['total_orders'] = len(order_ids)
|
||||
self.stats["total_orders"] = len(order_ids)
|
||||
|
||||
# 遍历处理
|
||||
for index, order_id in enumerate(order_ids):
|
||||
self._log(f"进度: [{index+1}/{len(order_ids)}] 处理单号: {order_id}")
|
||||
try:
|
||||
self.process_order(inner_frame, order_id, index, page1, len(order_ids))
|
||||
self.stats['processed_orders'] += 1
|
||||
self.process_order(
|
||||
inner_frame, order_id, index, page1, len(order_ids)
|
||||
)
|
||||
self.stats["processed_orders"] += 1
|
||||
except Exception as e:
|
||||
self._log(f"处理单号 {order_id} 时发生异常: {e}", "error")
|
||||
self.stats['errors'].append({
|
||||
'order_id': order_id,
|
||||
'error_message': str(e)
|
||||
})
|
||||
self.stats["errors"].append(
|
||||
{"order_id": order_id, "error_message": str(e)}
|
||||
)
|
||||
continue # 单个失败不影响整体执行
|
||||
|
||||
# 登出清理
|
||||
@@ -473,7 +511,7 @@ class DiscreteMaterialPlanCleaner:
|
||||
self._log("=" * 30 + " 任务全部完成 " + "=" * 30)
|
||||
|
||||
# 记录结束时间
|
||||
self.stats['end_time'] = datetime.now()
|
||||
self.stats["end_time"] = datetime.now()
|
||||
|
||||
def generate_report(self) -> str:
|
||||
"""生成 Markdown 格式的执行报告
|
||||
@@ -490,62 +528,78 @@ class DiscreteMaterialPlanCleaner:
|
||||
# 概述
|
||||
report_lines.append("## 概述")
|
||||
report_lines.append("")
|
||||
start_time = self.stats.get('start_time')
|
||||
end_time = self.stats.get('end_time')
|
||||
start_time = self.stats.get("start_time")
|
||||
end_time = self.stats.get("end_time")
|
||||
duration = None
|
||||
if start_time and end_time:
|
||||
duration = end_time - start_time
|
||||
report_lines.append(f"- 开始时间: {start_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
report_lines.append(
|
||||
f"- 开始时间: {start_time.strftime('%Y-%m-%d %H:%M:%S')}"
|
||||
)
|
||||
report_lines.append(f"- 结束时间: {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
report_lines.append(f"- 执行时长: {duration}")
|
||||
report_lines.append(f"- 处理订单: {self.stats['processed_orders']}/{self.stats['total_orders']} 个")
|
||||
report_lines.append(f"- 处理物料: {self.stats['processed_materials']}/{self.stats['total_materials']} 条")
|
||||
report_lines.append(
|
||||
f"- 处理订单: {self.stats['processed_orders']}/{self.stats['total_orders']} 个"
|
||||
)
|
||||
report_lines.append(
|
||||
f"- 处理物料: {self.stats['processed_materials']}/{self.stats['total_materials']} 条"
|
||||
)
|
||||
report_lines.append(f"- 删除物料: {len(self.stats['deleted_materials'])} 条")
|
||||
report_lines.append(f"- 跳过物料: {len(self.stats['skipped_materials'])} 条")
|
||||
report_lines.append(f"- 未处理物料: {len(self.stats['unmatched_materials'])} 条")
|
||||
report_lines.append(
|
||||
f"- 未处理物料: {len(self.stats['unmatched_materials'])} 条"
|
||||
)
|
||||
report_lines.append(f"- 错误数量: {len(self.stats['errors'])} 个")
|
||||
report_lines.append(f"- 执行模式: {'预览模式 (dryrun)' if self.dryrun else '正常执行'}")
|
||||
report_lines.append(
|
||||
f"- 执行模式: {'预览模式 (dryrun)' if self.dryrun else '正常执行'}"
|
||||
)
|
||||
if self.manager_names:
|
||||
report_lines.append(f"- 负责人: {', '.join(self.manager_names)}")
|
||||
report_lines.append("")
|
||||
|
||||
# 删除明细
|
||||
if self.stats['deleted_materials']:
|
||||
if self.stats["deleted_materials"]:
|
||||
report_lines.append("## 删除明细")
|
||||
report_lines.append("")
|
||||
report_lines.append("| 订单号 | 物料编码 | 物料名称 |")
|
||||
report_lines.append("|--------|----------|----------|")
|
||||
for item in self.stats['deleted_materials']:
|
||||
report_lines.append(f"| {item['order_id']} | {item['material_code']} | {item['material_name']} |")
|
||||
for item in self.stats["deleted_materials"]:
|
||||
report_lines.append(
|
||||
f"| {item['order_id']} | {item['material_code']} | {item['material_name']} |"
|
||||
)
|
||||
report_lines.append("")
|
||||
|
||||
# 跳过明细
|
||||
if self.stats['skipped_materials']:
|
||||
if self.stats["skipped_materials"]:
|
||||
report_lines.append("## 跳过明细")
|
||||
report_lines.append("")
|
||||
report_lines.append("| 订单号 | 物料编码 | 物料名称 | 跳过原因 |")
|
||||
report_lines.append("|--------|----------|----------|----------|")
|
||||
for item in self.stats['skipped_materials']:
|
||||
report_lines.append(f"| {item['order_id']} | {item['material_code']} | {item['material_name']} | {item['reason']} |")
|
||||
for item in self.stats["skipped_materials"]:
|
||||
report_lines.append(
|
||||
f"| {item['order_id']} | {item['material_code']} | {item['material_name']} | {item['reason']} |"
|
||||
)
|
||||
report_lines.append("")
|
||||
|
||||
# 未处理物料明细
|
||||
if self.stats['unmatched_materials']:
|
||||
if self.stats["unmatched_materials"]:
|
||||
report_lines.append("## 未处理物料")
|
||||
report_lines.append("")
|
||||
report_lines.append("| 订单号 | 物料编码 | 物料名称 |")
|
||||
report_lines.append("|--------|----------|----------|")
|
||||
for item in self.stats['unmatched_materials']:
|
||||
report_lines.append(f"| {item['order_id']} | {item['material_code']} | {item['material_name']} |")
|
||||
for item in self.stats["unmatched_materials"]:
|
||||
report_lines.append(
|
||||
f"| {item['order_id']} | {item['material_code']} | {item['material_name']} |"
|
||||
)
|
||||
report_lines.append("")
|
||||
|
||||
# 错误明细
|
||||
if self.stats['errors']:
|
||||
if self.stats["errors"]:
|
||||
report_lines.append("## 错误明细")
|
||||
report_lines.append("")
|
||||
report_lines.append("| 订单号 | 错误信息 |")
|
||||
report_lines.append("|--------|----------|")
|
||||
for item in self.stats['errors']:
|
||||
for item in self.stats["errors"]:
|
||||
report_lines.append(f"| {item['order_id']} | {item['error_message']} |")
|
||||
report_lines.append("")
|
||||
|
||||
|
||||
@@ -27,22 +27,24 @@ except ImportError:
|
||||
|
||||
# --- 全局日志配置 ---
|
||||
# 调整格式:增加 [] 使其与 UI 控件的默认风格保持一致
|
||||
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
|
||||
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
|
||||
LOG_FORMAT = "[%(asctime)s] [%(levelname)s] %(message)s"
|
||||
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format=LOG_FORMAT,
|
||||
datefmt=DATE_FORMAT
|
||||
)
|
||||
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DiscreteMaterialPlanExtractor:
|
||||
"""离散备料计划维护数据提取器"""
|
||||
|
||||
def __init__(
|
||||
self, username, password, headless=False, verbose=True, batch_size=100,
|
||||
enable_db_persistence=False
|
||||
self,
|
||||
username,
|
||||
password,
|
||||
headless=False,
|
||||
verbose=True,
|
||||
batch_size=100,
|
||||
enable_db_persistence=False,
|
||||
):
|
||||
self.username = username
|
||||
self.password = password
|
||||
@@ -53,10 +55,11 @@ class DiscreteMaterialPlanExtractor:
|
||||
self.converter = ExcelConverter(verbose=verbose)
|
||||
self.enable_db_persistence = enable_db_persistence
|
||||
self.dao = None
|
||||
|
||||
|
||||
if self.enable_db_persistence:
|
||||
try:
|
||||
from db.discrete_material_plan_dao import DiscreteMaterialPlanDAO
|
||||
|
||||
self.dao = DiscreteMaterialPlanDAO()
|
||||
except ImportError:
|
||||
self._log("无法加载数据库 DAO 模块,持久化功能将不可用", "error")
|
||||
@@ -67,11 +70,7 @@ class DiscreteMaterialPlanExtractor:
|
||||
"""
|
||||
level = level.lower()
|
||||
# 1. 记录到标准控制台
|
||||
log_map = {
|
||||
"info": logger.info,
|
||||
"warn": logger.warning,
|
||||
"error": logger.error
|
||||
}
|
||||
log_map = {"info": logger.info, "warn": logger.warning, "error": logger.error}
|
||||
log_func = log_map.get(level, logger.info)
|
||||
log_func(message)
|
||||
|
||||
@@ -80,7 +79,9 @@ class DiscreteMaterialPlanExtractor:
|
||||
if self.progress_callback:
|
||||
self._report_progress("log", 0, 0, message, log_level=level.upper())
|
||||
|
||||
def _report_progress(self, stage: str, current: int, total: int, message: str, **detail):
|
||||
def _report_progress(
|
||||
self, stage: str, current: int, total: int, message: str, **detail
|
||||
):
|
||||
"""标准化进度汇报"""
|
||||
if self.progress_callback and ProgressInfo:
|
||||
try:
|
||||
@@ -98,20 +99,30 @@ class DiscreteMaterialPlanExtractor:
|
||||
def get_production_order_numbers(self, production_id_file, report_progress=False):
|
||||
"""读取总排号并查询数据库获取生产订单号"""
|
||||
if report_progress:
|
||||
self._report_progress("query", 1, 3, "正在读取总排号文件...", action="read_file")
|
||||
|
||||
self._report_progress(
|
||||
"query", 1, 3, "正在读取总排号文件...", action="read_file"
|
||||
)
|
||||
|
||||
production_ids = read_production_ids(production_id_file)
|
||||
self._log(f"文件读取完成: 找到 {len(production_ids)} 个 Production ID")
|
||||
|
||||
if report_progress:
|
||||
self._report_progress("query", 2, 3, "正在查询数据库获取生产订单号...", action="query_database")
|
||||
self._report_progress(
|
||||
"query",
|
||||
2,
|
||||
3,
|
||||
"正在查询数据库获取生产订单号...",
|
||||
action="query_database",
|
||||
)
|
||||
|
||||
order_ids = query_production_order_numbers(production_ids)
|
||||
self._log(f"数据库查询完成: 共匹配到 {len(order_ids)} 条生产订单号")
|
||||
|
||||
if report_progress:
|
||||
self._report_progress("query", 3, 3, "订单号查询阶段结束", action="query_complete")
|
||||
|
||||
self._report_progress(
|
||||
"query", 3, 3, "订单号查询阶段结束", action="query_complete"
|
||||
)
|
||||
|
||||
return order_ids
|
||||
|
||||
def group_order_ids(self, order_ids, group_size=100):
|
||||
@@ -121,9 +132,14 @@ class DiscreteMaterialPlanExtractor:
|
||||
|
||||
def download_batch(self, inner_frame, order_ids, batch_index, total_batches, page1):
|
||||
"""执行单批次数据的下载流程"""
|
||||
self._report_progress("download", batch_index * 7 + 1, total_batches * 7,
|
||||
f"第 {batch_index + 1} 批: 正在填充订单号", action="fill_orders")
|
||||
|
||||
self._report_progress(
|
||||
"download",
|
||||
batch_index * 7 + 1,
|
||||
total_batches * 7,
|
||||
f"第 {batch_index + 1} 批: 正在填充订单号",
|
||||
action="fill_orders",
|
||||
)
|
||||
|
||||
textbox = inner_frame.get_by_role("textbox", name="来源生产订单号")
|
||||
textbox.fill("")
|
||||
textbox.fill(",".join(order_ids))
|
||||
@@ -139,8 +155,12 @@ class DiscreteMaterialPlanExtractor:
|
||||
inner_frame.get_by_role("row", name="序号").get_by_label("").click()
|
||||
inner_frame.get_by_role("button", name="更多").hover()
|
||||
inner_frame.get_by_text("输出", exact=True).click()
|
||||
|
||||
threshold_box = inner_frame.locator("div").filter(has_text=re.compile(r"^行数阈值$")).locator("input[type='text']")
|
||||
|
||||
threshold_box = (
|
||||
inner_frame.locator("div")
|
||||
.filter(has_text=re.compile(r"^行数阈值$"))
|
||||
.locator("input[type='text']")
|
||||
)
|
||||
threshold_box.fill("300000")
|
||||
|
||||
download_path = f"D:/python/playwrite/data/temp_batch_{batch_index + 1}.xlsx"
|
||||
@@ -165,36 +185,46 @@ class DiscreteMaterialPlanExtractor:
|
||||
total_steps = len(file_paths) * 2 + 3
|
||||
|
||||
for i, path in enumerate(file_paths, 1):
|
||||
self._report_progress("convert", 1 + (i-1)*2 + 1, total_steps, f"正在转换 Excel {i}/{len(file_paths)}")
|
||||
self._report_progress(
|
||||
"convert",
|
||||
1 + (i - 1) * 2 + 1,
|
||||
total_steps,
|
||||
f"正在转换 Excel {i}/{len(file_paths)}",
|
||||
)
|
||||
df = self.converter.convert(path, output_file=None)
|
||||
all_dfs.append(df)
|
||||
self._log(f"文件 {i} 转换完成: 提取到 {len(df)} 条记录")
|
||||
|
||||
if all_dfs:
|
||||
self._report_progress("convert", total_steps - 1, total_steps, "正在进行最终数据合并...")
|
||||
self._report_progress(
|
||||
"convert", total_steps - 1, total_steps, "正在进行最终数据合并..."
|
||||
)
|
||||
merged_df = pd.concat(all_dfs, ignore_index=True)
|
||||
merged_df.to_excel(output_path, index=False)
|
||||
|
||||
|
||||
for p in file_paths:
|
||||
try: os.remove(p)
|
||||
except: pass
|
||||
|
||||
try:
|
||||
os.remove(p)
|
||||
except:
|
||||
pass
|
||||
|
||||
return output_path, merged_df
|
||||
return None, None
|
||||
|
||||
def _save_to_database(self, df: pd.DataFrame):
|
||||
"""将结果存入数据库并打印详细统计信息"""
|
||||
if not self.dao: return
|
||||
if not self.dao:
|
||||
return
|
||||
try:
|
||||
self._report_progress("database", 1, 3, "正在将数据同步至数据库...")
|
||||
# 使用 with 关键字确保资源安全释放
|
||||
with self.dao as db:
|
||||
stats = db.save_dataframe_with_replace(df)
|
||||
|
||||
|
||||
# 保留并输出完整的处理细节:删除条数和新增条数
|
||||
msg = f"数据库保存完成: 删除 {stats.get('deleted', 0)} 条, 新增 {stats.get('inserted', 0)} 条"
|
||||
self._log(msg, "info")
|
||||
|
||||
|
||||
except Exception as e:
|
||||
self._log(f"数据库保存失败: {str(e)}", "error")
|
||||
|
||||
@@ -209,8 +239,10 @@ class DiscreteMaterialPlanExtractor:
|
||||
input_box.press("Enter")
|
||||
|
||||
def extract(
|
||||
self, production_id_file, output_file="D:/python/playwrite/data/离散备料计划维护_合并.xlsx",
|
||||
progress_callback=None
|
||||
self,
|
||||
production_id_file,
|
||||
output_file="D:/python/playwrite/data/离散备料计划维护_合并.xlsx",
|
||||
progress_callback=None,
|
||||
):
|
||||
"""主入口:执行全流程数据提取任务"""
|
||||
self.progress_callback = progress_callback
|
||||
@@ -220,15 +252,22 @@ class DiscreteMaterialPlanExtractor:
|
||||
with sync_playwright() as playwright:
|
||||
self._report_progress("login", 1, 3, "启动浏览器并尝试登录 ERP...")
|
||||
browser, context, page, main_frame = login(
|
||||
playwright=playwright, username=self.username, password=self.password,
|
||||
headless=self.headless, ignore_https_errors=True
|
||||
playwright=playwright,
|
||||
username=self.username,
|
||||
password=self.password,
|
||||
headless=self.headless,
|
||||
ignore_https_errors=True,
|
||||
)
|
||||
|
||||
self._log(
|
||||
"======================================== 开始执行数据提取任务 ========================================"
|
||||
)
|
||||
|
||||
self._log("======================================== 开始执行数据提取任务 ========================================")
|
||||
|
||||
main_frame.locator("i").first.click()
|
||||
with page.expect_popup() as page1_info:
|
||||
main_frame.get_by_title("离散备料计划维护", exact=True).first.click()
|
||||
main_frame.get_by_title(
|
||||
"离散备料计划维护", exact=True
|
||||
).first.click()
|
||||
page1 = page1_info.value
|
||||
|
||||
f_frame = page1.locator("#forwardFrame").content_frame
|
||||
@@ -237,16 +276,22 @@ class DiscreteMaterialPlanExtractor:
|
||||
work_frame = inner_frame_locator.content_frame
|
||||
|
||||
self.setup_query_interface(work_frame)
|
||||
order_ids = self.get_production_order_numbers(production_id_file, report_progress=True)
|
||||
order_ids = self.get_production_order_numbers(
|
||||
production_id_file, report_progress=True
|
||||
)
|
||||
|
||||
batch_list = list(self.group_order_ids(order_ids, self.batch_size))
|
||||
for i, batch_ids in enumerate(batch_list):
|
||||
self._log(f"正在处理第 {i+1} 批次 (共 {len(batch_list)} 批)")
|
||||
try:
|
||||
f_path = self.download_batch(work_frame, batch_ids, i, len(batch_list), page1)
|
||||
f_path = self.download_batch(
|
||||
work_frame, batch_ids, i, len(batch_list), page1
|
||||
)
|
||||
downloaded_files.append(f_path)
|
||||
except Exception as e:
|
||||
self._log(f"批次 {i+1} 处理异常,已跳过。详细错误: {e}", "error")
|
||||
self._log(
|
||||
f"批次 {i+1} 处理异常,已跳过。详细错误: {e}", "error"
|
||||
)
|
||||
continue
|
||||
|
||||
self._log("正在注销并关闭浏览器环境...")
|
||||
@@ -255,28 +300,32 @@ class DiscreteMaterialPlanExtractor:
|
||||
browser.close()
|
||||
|
||||
if downloaded_files:
|
||||
final_path, final_df = self.convert_and_merge_files(downloaded_files, output_file)
|
||||
final_path, final_df = self.convert_and_merge_files(
|
||||
downloaded_files, output_file
|
||||
)
|
||||
if self.enable_db_persistence and final_df is not None:
|
||||
self._save_to_database(final_df)
|
||||
|
||||
|
||||
self._log(f"所有流程已顺利结束,结果文件: {final_path}")
|
||||
self._report_progress("complete", 1, 1, "任务完成")
|
||||
return final_path
|
||||
|
||||
|
||||
self._log("未获得任何有效数据,任务终止", "warn")
|
||||
return None
|
||||
|
||||
finally:
|
||||
self.progress_callback = None
|
||||
|
||||
|
||||
def main():
|
||||
extractor = DiscreteMaterialPlanExtractor(
|
||||
username="BLDpengqiangqiang",
|
||||
password="your_password",
|
||||
enable_db_persistence=True
|
||||
enable_db_persistence=True,
|
||||
)
|
||||
id_file = os.path.join(os.path.dirname(__file__), "productionID.txt")
|
||||
extractor.extract(id_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -23,19 +23,22 @@ from db.discrete_material_plan_dao import DiscreteMaterialPlanDAO
|
||||
from db.materials_to_be_deleted_dao import MaterialsTypeToBeDeletedDAO
|
||||
from db.materials_to_be_deleted_records_dao import MaterialsToBeDeletedDAO
|
||||
|
||||
|
||||
# ==================== DATA STRUCTURES ====================
|
||||
|
||||
|
||||
@dataclass
|
||||
class MaterialValidationResult:
|
||||
"""Enhanced material validation result with complete record information"""
|
||||
|
||||
material_name: str
|
||||
material_code: str
|
||||
specification: Optional[str] = None
|
||||
model: Optional[str] = None
|
||||
manager_name: Optional[str] = None
|
||||
is_marked_for_deletion: bool = False
|
||||
matched_type_keyword: Optional[str] = None # Matched keyword from MaterialsTypeToBeDeleted
|
||||
matched_type_keyword: Optional[str] = (
|
||||
None # Matched keyword from MaterialsTypeToBeDeleted
|
||||
)
|
||||
|
||||
|
||||
class MaterialStatusValidator:
|
||||
@@ -261,13 +264,11 @@ class MaterialStatusValidator:
|
||||
Returns:
|
||||
List[str]: 输入项列表(可能是总排号或生产订单号)
|
||||
"""
|
||||
with open(production_id_file, 'r', encoding='utf-8') as f:
|
||||
with open(production_id_file, "r", encoding="utf-8") as f:
|
||||
items = [line.strip() for line in f if line.strip()]
|
||||
return items
|
||||
|
||||
def _get_source_numbers_from_inputs(
|
||||
self, inputs: List[str]
|
||||
) -> List[str]:
|
||||
def _get_source_numbers_from_inputs(self, inputs: List[str]) -> List[str]:
|
||||
"""
|
||||
根据输入列表智能获取 SourceNumber(生产订单号)列表
|
||||
|
||||
@@ -281,7 +282,7 @@ class MaterialStatusValidator:
|
||||
List[str]: 生产订单号列表
|
||||
"""
|
||||
production_ids = [] # 需要查询数据库的
|
||||
order_numbers = [] # 直接使用的
|
||||
order_numbers = [] # 直接使用的
|
||||
|
||||
for item in inputs:
|
||||
input_type = self._identify_input_type(item)
|
||||
@@ -298,7 +299,9 @@ class MaterialStatusValidator:
|
||||
|
||||
# 查询数据库获取总排号对应的生产订单号
|
||||
if production_ids:
|
||||
self._print(f"[INFO] 正在查询 {len(production_ids)} 个总排号对应的生产订单号...")
|
||||
self._print(
|
||||
f"[INFO] 正在查询 {len(production_ids)} 个总排号对应的生产订单号..."
|
||||
)
|
||||
contract_dao = ProductionContractDataDAO()
|
||||
db_order_numbers = contract_dao.get_source_numbers_by_总排号(production_ids)
|
||||
self._print(f"[INFO] 从数据库获取到 {len(db_order_numbers)} 个生产订单号")
|
||||
@@ -307,7 +310,9 @@ class MaterialStatusValidator:
|
||||
# 去重
|
||||
unique_order_numbers = list(dict.fromkeys(order_numbers))
|
||||
if len(unique_order_numbers) != len(order_numbers):
|
||||
self._print(f"[INFO] 去重后得到 {len(unique_order_numbers)} 个唯一生产订单号")
|
||||
self._print(
|
||||
f"[INFO] 去重后得到 {len(unique_order_numbers)} 个唯一生产订单号"
|
||||
)
|
||||
|
||||
return unique_order_numbers
|
||||
|
||||
@@ -326,7 +331,9 @@ class MaterialStatusValidator:
|
||||
if source_numbers is None or not source_numbers:
|
||||
self._print("[INFO] 查询所有材料的名称...")
|
||||
else:
|
||||
self._print(f"[INFO] 查询 {len(source_numbers)} 个生产订单对应的材料名称...")
|
||||
self._print(
|
||||
f"[INFO] 查询 {len(source_numbers)} 个生产订单对应的材料名称..."
|
||||
)
|
||||
|
||||
dao = DiscreteMaterialPlanDAO()
|
||||
material_names = dao.get_unique_material_names(source_numbers)
|
||||
@@ -338,7 +345,7 @@ class MaterialStatusValidator:
|
||||
self,
|
||||
production_id_file: str = None,
|
||||
full_table: bool = False,
|
||||
output_file: str = None
|
||||
output_file: str = None,
|
||||
) -> str:
|
||||
"""
|
||||
使用数据库作为数据源执行校验
|
||||
@@ -392,9 +399,7 @@ class MaterialStatusValidator:
|
||||
# 3. 获取材料名称
|
||||
material_names = self._get_material_names_from_db(source_numbers)
|
||||
else:
|
||||
raise ValueError(
|
||||
"必须指定 full_table=True 或提供 production_id_file 参数"
|
||||
)
|
||||
raise ValueError("必须指定 full_table=True 或提供 production_id_file 参数")
|
||||
|
||||
# 从数据库获取待删除物料
|
||||
self._print("\n从数据库获取待删除物料...")
|
||||
@@ -426,7 +431,9 @@ class MaterialStatusValidator:
|
||||
self,
|
||||
material_records: List[Dict[str, Any]],
|
||||
type_keywords: List[Dict[str, Any]],
|
||||
marked_codes_dict: Dict[str, str] # Changed: MaterialCode -> ManagerName mapping
|
||||
marked_codes_dict: Dict[
|
||||
str, str
|
||||
], # Changed: MaterialCode -> ManagerName mapping
|
||||
) -> List[MaterialValidationResult]:
|
||||
"""
|
||||
Match materials with detailed information.
|
||||
@@ -442,14 +449,16 @@ class MaterialStatusValidator:
|
||||
results = []
|
||||
|
||||
for record in material_records:
|
||||
material_name = record.get('MaterialName', '') or ''
|
||||
material_code = record.get('MaterialCode', '') or ''
|
||||
specification = record.get('Specification', '') or None
|
||||
model = record.get('Model', '') or None
|
||||
material_name = record.get("MaterialName", "") or ""
|
||||
material_code = record.get("MaterialCode", "") or ""
|
||||
specification = record.get("Specification", "") or None
|
||||
model = record.get("Model", "") or None
|
||||
|
||||
# Priority 1: Check MaterialsToBeDeleted (MaterialCode exact match)
|
||||
# This has highest priority - if MaterialCode exists, use its ManagerName
|
||||
manager_name = marked_codes_dict.get(material_code) if material_code else None
|
||||
manager_name = (
|
||||
marked_codes_dict.get(material_code) if material_code else None
|
||||
)
|
||||
is_marked = manager_name is not None
|
||||
matched_keyword = None
|
||||
|
||||
@@ -457,10 +466,10 @@ class MaterialStatusValidator:
|
||||
# (MaterialName contains match)
|
||||
if not manager_name:
|
||||
for type_record in type_keywords:
|
||||
type_material_name = type_record.get('MaterialName', '')
|
||||
type_material_name = type_record.get("MaterialName", "")
|
||||
if type_material_name and type_material_name in material_name:
|
||||
matched_keyword = type_material_name
|
||||
manager_name = type_record.get('ManagerName')
|
||||
manager_name = type_record.get("ManagerName")
|
||||
break
|
||||
|
||||
result = MaterialValidationResult(
|
||||
@@ -470,7 +479,7 @@ class MaterialStatusValidator:
|
||||
model=model,
|
||||
manager_name=manager_name,
|
||||
is_marked_for_deletion=is_marked,
|
||||
matched_type_keyword=matched_keyword
|
||||
matched_type_keyword=matched_keyword,
|
||||
)
|
||||
results.append(result)
|
||||
|
||||
@@ -480,7 +489,7 @@ class MaterialStatusValidator:
|
||||
self,
|
||||
production_id_file: str = None,
|
||||
full_table: bool = False,
|
||||
output_file: str = None
|
||||
output_file: str = None,
|
||||
) -> tuple:
|
||||
"""
|
||||
Enhanced database validation with complete record information.
|
||||
@@ -510,7 +519,9 @@ class MaterialStatusValidator:
|
||||
# Get material records (complete records, not just MaterialName)
|
||||
if full_table:
|
||||
self._print("\n模式: 全表校验")
|
||||
self._print("[INFO] 查询 DiscreteMaterialPlanData 表中的所有完整记录(启用 MaterialCode 去重)...")
|
||||
self._print(
|
||||
"[INFO] 查询 DiscreteMaterialPlanData 表中的所有完整记录(启用 MaterialCode 去重)..."
|
||||
)
|
||||
dao = DiscreteMaterialPlanDAO()
|
||||
|
||||
# Get original count for deduplication statistics
|
||||
@@ -521,7 +532,9 @@ class MaterialStatusValidator:
|
||||
|
||||
self._print(f"[INFO] 获取到 {len(material_records)} 条记录")
|
||||
if dedup_count > 0:
|
||||
self._print(f"[INFO] 基于 MaterialCode 去重:移除了 {dedup_count} 条重复记录")
|
||||
self._print(
|
||||
f"[INFO] 基于 MaterialCode 去重:移除了 {dedup_count} 条重复记录"
|
||||
)
|
||||
elif production_id_file:
|
||||
self._print("\n模式: 输入过滤校验")
|
||||
self._print(f"[INFO] 读取输入文件: {production_id_file}")
|
||||
@@ -542,7 +555,9 @@ class MaterialStatusValidator:
|
||||
return output_file, []
|
||||
|
||||
# 3. Get complete material records with deduplication
|
||||
self._print(f"[INFO] 查询 {len(source_numbers)} 个生产订单对应的完整物料记录(启用 MaterialCode 去重)...")
|
||||
self._print(
|
||||
f"[INFO] 查询 {len(source_numbers)} 个生产订单对应的完整物料记录(启用 MaterialCode 去重)..."
|
||||
)
|
||||
dao = DiscreteMaterialPlanDAO()
|
||||
|
||||
# Get original count for deduplication statistics
|
||||
@@ -554,7 +569,9 @@ class MaterialStatusValidator:
|
||||
self._print(f"[INFO] 获取到 {len(material_records)} 条记录")
|
||||
|
||||
if dedup_count > 0:
|
||||
self._print(f"[INFO] 基于 MaterialCode 去重:移除了 {dedup_count} 条重复记录")
|
||||
self._print(
|
||||
f"[INFO] 基于 MaterialCode 去重:移除了 {dedup_count} 条重复记录"
|
||||
)
|
||||
|
||||
# 如果没有找到物料记录,给出友好提示
|
||||
if not material_records:
|
||||
@@ -563,7 +580,9 @@ class MaterialStatusValidator:
|
||||
self._print("[ERROR] 1. 这些生产订单的物料数据还没有提取到数据库")
|
||||
self._print("[ERROR] 2. 请先运行【正式备料计划数据提取】工具")
|
||||
self._print("[ERROR] 3. 提取时勾选【持久化到数据库】选项")
|
||||
self._print(f"[ERROR] 4. 将这些输入项的物料数据保存到 DiscreteMaterialPlanData 表")
|
||||
self._print(
|
||||
f"[ERROR] 4. 将这些输入项的物料数据保存到 DiscreteMaterialPlanData 表"
|
||||
)
|
||||
else:
|
||||
raise ValueError("必须指定 full_table=True 或提供 production_id_file 参数")
|
||||
|
||||
@@ -580,15 +599,17 @@ class MaterialStatusValidator:
|
||||
|
||||
# Build dictionary: MaterialCode -> ManagerName
|
||||
marked_codes_dict = {
|
||||
r['MaterialCode']: r['ManagerName']
|
||||
r["MaterialCode"]: r["ManagerName"]
|
||||
for r in marked_records
|
||||
if r.get('MaterialCode') and r.get('ManagerName')
|
||||
if r.get("MaterialCode") and r.get("ManagerName")
|
||||
}
|
||||
self._print(f"获取到 {len(marked_codes_dict)} 个已标记的物料代码")
|
||||
|
||||
# Match materials
|
||||
self._print("\n匹配物料...")
|
||||
results = self.match_materials_detailed(material_records, type_keywords, marked_codes_dict)
|
||||
results = self.match_materials_detailed(
|
||||
material_records, type_keywords, marked_codes_dict
|
||||
)
|
||||
|
||||
# Output to Excel
|
||||
self._print("\n输出结果...")
|
||||
@@ -596,15 +617,17 @@ class MaterialStatusValidator:
|
||||
# Convert to DataFrame for Excel export
|
||||
df_data = []
|
||||
for r in results:
|
||||
df_data.append({
|
||||
"材料名称": r.material_name,
|
||||
"材料代码": r.material_code,
|
||||
"规格": r.specification or '',
|
||||
"型号": r.model or '',
|
||||
"负责人": r.manager_name or '',
|
||||
"已标记删除": "是" if r.is_marked_for_deletion else "否",
|
||||
"匹配的关键词": r.matched_type_keyword or ''
|
||||
})
|
||||
df_data.append(
|
||||
{
|
||||
"材料名称": r.material_name,
|
||||
"材料代码": r.material_code,
|
||||
"规格": r.specification or "",
|
||||
"型号": r.model or "",
|
||||
"负责人": r.manager_name or "",
|
||||
"已标记删除": "是" if r.is_marked_for_deletion else "否",
|
||||
"匹配的关键词": r.matched_type_keyword or "",
|
||||
}
|
||||
)
|
||||
|
||||
result_df = pd.DataFrame(df_data)
|
||||
result_df.to_excel(output_file, index=False)
|
||||
|
||||
Reference in New Issue
Block a user