fix: ensure page2 is closed even on exception in process_single_order
Wrap the order processing logic in try-finally to guarantee cleanup of the popup page regardless of whether processing succeeds or fails. Co-Authored-By: Claude (glm-5) <noreply@anthropic.com>
This commit is contained in:
@@ -220,179 +220,181 @@ class DiscreteMaterialPlanCleaner:
|
||||
).group(1)
|
||||
|
||||
# 6. 执行清理逻辑
|
||||
if detail_status == "审批通过":
|
||||
if detail_count > 0:
|
||||
# 更新总物料数统计
|
||||
self.stats['total_materials'] += detail_count
|
||||
try:
|
||||
if detail_status == "审批通过":
|
||||
if detail_count > 0:
|
||||
# 更新总物料数统计
|
||||
self.stats['total_materials'] += detail_count
|
||||
|
||||
# --- 点击修改并等待状态切换 (保留原逻辑) ---
|
||||
detail_inner_frame.get_by_role("button", name="修改").click()
|
||||
# --- 点击修改并等待状态切换 (保留原逻辑) ---
|
||||
detail_inner_frame.get_by_role("button", name="修改").click()
|
||||
|
||||
# 关键判断:等待保存按钮出现,确认进入编辑模式
|
||||
save_button_locator = detail_inner_frame.get_by_role(
|
||||
"button", name="保存"
|
||||
)
|
||||
save_button_locator.wait_for(state="visible", timeout=30000)
|
||||
self._log("已进入编辑模式(保存按钮已就绪)")
|
||||
# ---------------------------------------
|
||||
|
||||
detail_inner_frame.get_by_text("展开").first.click()
|
||||
|
||||
child_form = detail_inner_frame.locator(".card-table-side-box")
|
||||
button_wrapper = child_form.locator(".button-wrapper")
|
||||
|
||||
delete_row_btn = button_wrapper.get_by_role("button", name="删行")
|
||||
next_btn = button_wrapper.locator(".icon-jiantouyou")
|
||||
collapse_btn = button_wrapper.locator(".icon-celashouqi")
|
||||
|
||||
last_row_number = None
|
||||
material_idx = 0 # 物料计数器
|
||||
# page2.pause() # 调试用,正式运行时可删除
|
||||
while True:
|
||||
material_idx += 1
|
||||
self.stats['processed_materials'] += 1
|
||||
|
||||
# 稳定性检查:等待行号更新
|
||||
current_row = self._get_input_value(child_form, r"^行号$")
|
||||
row_num_int = int(current_row)
|
||||
if current_row == last_row_number:
|
||||
time.sleep(0.5)
|
||||
|
||||
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"^累计待发数量$")
|
||||
|
||||
# 报告物料进度
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "检查"
|
||||
# 关键判断:等待保存按钮出现,确认进入编辑模式
|
||||
save_button_locator = detail_inner_frame.get_by_role(
|
||||
"button", name="保存"
|
||||
)
|
||||
save_button_locator.wait_for(state="visible", timeout=30000)
|
||||
self._log("已进入编辑模式(保存按钮已就绪)")
|
||||
# ---------------------------------------
|
||||
|
||||
if material_code in self.to_delete_set:
|
||||
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, "删除"
|
||||
)
|
||||
# 记录删除前的行号
|
||||
old_row_number = current_row
|
||||
delete_row_btn.click()
|
||||
self._log(f"✅ 已点击删行,等待删除完成...")
|
||||
detail_inner_frame.get_by_text("展开").first.click()
|
||||
|
||||
# 等待行号变化(表示删除完成且新数据已加载)
|
||||
max_wait_time = 10 # 最大等待10秒
|
||||
start_time = time.time()
|
||||
delete_success = False
|
||||
while time.time() - start_time < max_wait_time:
|
||||
try:
|
||||
new_row_number = self._get_input_value(
|
||||
child_form, r"^行号$"
|
||||
)
|
||||
if new_row_number != old_row_number:
|
||||
self._log(
|
||||
f"✓ 删除完成,行号已从 {old_row_number} 变更为 {new_row_number}"
|
||||
)
|
||||
delete_success = True
|
||||
break
|
||||
time.sleep(0.2) # 每200ms检查一次
|
||||
except Exception as e:
|
||||
self._log(f"获取新行号时出错: {e}", "warn")
|
||||
time.sleep(0.2)
|
||||
else:
|
||||
self._log(
|
||||
f"⚠️ 等待删除完成超时({max_wait_time}秒)", "warn"
|
||||
child_form = detail_inner_frame.locator(".card-table-side-box")
|
||||
button_wrapper = child_form.locator(".button-wrapper")
|
||||
|
||||
delete_row_btn = button_wrapper.get_by_role("button", name="删行")
|
||||
next_btn = button_wrapper.locator(".icon-jiantouyou")
|
||||
collapse_btn = button_wrapper.locator(".icon-celashouqi")
|
||||
|
||||
last_row_number = None
|
||||
material_idx = 0 # 物料计数器
|
||||
# page2.pause() # 调试用,正式运行时可删除
|
||||
while True:
|
||||
material_idx += 1
|
||||
self.stats['processed_materials'] += 1
|
||||
|
||||
# 稳定性检查:等待行号更新
|
||||
current_row = self._get_input_value(child_form, r"^行号$")
|
||||
row_num_int = int(current_row)
|
||||
if current_row == last_row_number:
|
||||
time.sleep(0.5)
|
||||
|
||||
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"^累计待发数量$")
|
||||
|
||||
# 报告物料进度
|
||||
self._report_material_progress(
|
||||
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})")
|
||||
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, "删除"
|
||||
)
|
||||
# 记录删除前的行号
|
||||
old_row_number = current_row
|
||||
delete_row_btn.click()
|
||||
self._log(f"✅ 已点击删行,等待删除完成...")
|
||||
|
||||
# 记录删除统计
|
||||
if delete_success:
|
||||
self.stats['deleted_materials'].append({
|
||||
# 等待行号变化(表示删除完成且新数据已加载)
|
||||
max_wait_time = 10 # 最大等待10秒
|
||||
start_time = time.time()
|
||||
delete_success = False
|
||||
while time.time() - start_time < max_wait_time:
|
||||
try:
|
||||
new_row_number = self._get_input_value(
|
||||
child_form, r"^行号$"
|
||||
)
|
||||
if new_row_number != old_row_number:
|
||||
self._log(
|
||||
f"✓ 删除完成,行号已从 {old_row_number} 变更为 {new_row_number}"
|
||||
)
|
||||
delete_success = True
|
||||
break
|
||||
time.sleep(0.2) # 每200ms检查一次
|
||||
except Exception as e:
|
||||
self._log(f"获取新行号时出错: {e}", "warn")
|
||||
time.sleep(0.2)
|
||||
else:
|
||||
self._log(
|
||||
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
|
||||
})
|
||||
|
||||
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, "跳过"
|
||||
)
|
||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
||||
self.stats['skipped_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name
|
||||
'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, "跳过"
|
||||
)
|
||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
||||
self.stats['skipped_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name,
|
||||
'reason': reason
|
||||
})
|
||||
|
||||
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, "跳过"
|
||||
)
|
||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
||||
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, "跳过"
|
||||
)
|
||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
||||
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, "跳过"
|
||||
)
|
||||
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
|
||||
})
|
||||
|
||||
else:
|
||||
reason = "不满足删除条件"
|
||||
self._report_material_progress(
|
||||
order_index, total_orders,
|
||||
material_idx, detail_count,
|
||||
order_id, material_name, "跳过"
|
||||
)
|
||||
self._log(
|
||||
f"⚠️ {reason},跳过物料 {material_name} ({material_code})",
|
||||
"warn",
|
||||
f"ℹ️ 物料 {material_name} ({material_code}) 不在删除列表中,不做处理"
|
||||
)
|
||||
self.stats['skipped_materials'].append({
|
||||
'order_id': order_id,
|
||||
'material_code': material_code,
|
||||
'material_name': material_name,
|
||||
'reason': reason
|
||||
})
|
||||
if self._is_button_enabled(next_btn):
|
||||
last_row_number = current_row
|
||||
next_btn.click()
|
||||
else:
|
||||
break
|
||||
collapse_btn.click()
|
||||
|
||||
# 执行最终保存逻辑(如业务需要)
|
||||
if self.dryrun:
|
||||
self._log("[DRYRUN] 跳过保存操作")
|
||||
else:
|
||||
self._log(
|
||||
f"ℹ️ 物料 {material_name} ({material_code}) 不在删除列表中,不做处理"
|
||||
)
|
||||
if self._is_button_enabled(next_btn):
|
||||
last_row_number = current_row
|
||||
next_btn.click()
|
||||
else:
|
||||
break
|
||||
collapse_btn.click()
|
||||
|
||||
# 执行最终保存逻辑(如业务需要)
|
||||
if self.dryrun:
|
||||
self._log("[DRYRUN] 跳过保存操作")
|
||||
save_button_locator.click()
|
||||
self._log("已点击保存,等待保存完成...")
|
||||
save_button_locator.wait_for(
|
||||
state="hidden", timeout=60000
|
||||
) # 等待按钮消失,超时60秒
|
||||
self._log("✅ 保存成功(保存按钮已消失)")
|
||||
else:
|
||||
save_button_locator.click()
|
||||
self._log("已点击保存,等待保存完成...")
|
||||
save_button_locator.wait_for(
|
||||
state="hidden", timeout=60000
|
||||
) # 等待按钮消失,超时60秒
|
||||
self._log("✅ 保存成功(保存按钮已消失)")
|
||||
self._log("订单无备料计划数据,无需处理")
|
||||
elif detail_status == "完成":
|
||||
self._log("订单已完成,无需处理")
|
||||
else:
|
||||
self._log("订单无备料计划数据,无需处理")
|
||||
elif detail_status == "完成":
|
||||
self._log("订单已完成,无需处理")
|
||||
else:
|
||||
self._log(f"订单状态为 [{detail_status}],不符合处理条件", "warn")
|
||||
page2.close()
|
||||
self._log(f"订单状态为 [{detail_status}],不符合处理条件", "warn")
|
||||
finally:
|
||||
page2.close()
|
||||
|
||||
def setup_query_interface(self, inner_frame):
|
||||
"""初始化查询界面配置"""
|
||||
|
||||
Reference in New Issue
Block a user