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:
Misaka
2026-02-26 22:44:03 +08:00
parent 1b16842a2c
commit 3b7c00377f
46 changed files with 1488 additions and 974 deletions

View File

@@ -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("")