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)
|
).group(1)
|
||||||
|
|
||||||
# 6. 执行清理逻辑
|
# 6. 执行清理逻辑
|
||||||
if detail_status == "审批通过":
|
try:
|
||||||
if detail_count > 0:
|
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()
|
detail_inner_frame.get_by_role("button", name="修改").click()
|
||||||
|
|
||||||
# 关键判断:等待保存按钮出现,确认进入编辑模式
|
# 关键判断:等待保存按钮出现,确认进入编辑模式
|
||||||
save_button_locator = detail_inner_frame.get_by_role(
|
save_button_locator = detail_inner_frame.get_by_role(
|
||||||
"button", name="保存"
|
"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.wait_for(state="visible", timeout=30000)
|
||||||
|
self._log("已进入编辑模式(保存按钮已就绪)")
|
||||||
|
# ---------------------------------------
|
||||||
|
|
||||||
if material_code in self.to_delete_set:
|
detail_inner_frame.get_by_text("展开").first.click()
|
||||||
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"✅ 已点击删行,等待删除完成...")
|
|
||||||
|
|
||||||
# 等待行号变化(表示删除完成且新数据已加载)
|
child_form = detail_inner_frame.locator(".card-table-side-box")
|
||||||
max_wait_time = 10 # 最大等待10秒
|
button_wrapper = child_form.locator(".button-wrapper")
|
||||||
start_time = time.time()
|
|
||||||
delete_success = False
|
delete_row_btn = button_wrapper.get_by_role("button", name="删行")
|
||||||
while time.time() - start_time < max_wait_time:
|
next_btn = button_wrapper.locator(".icon-jiantouyou")
|
||||||
try:
|
collapse_btn = button_wrapper.locator(".icon-celashouqi")
|
||||||
new_row_number = self._get_input_value(
|
|
||||||
child_form, r"^行号$"
|
last_row_number = None
|
||||||
)
|
material_idx = 0 # 物料计数器
|
||||||
if new_row_number != old_row_number:
|
# page2.pause() # 调试用,正式运行时可删除
|
||||||
self._log(
|
while True:
|
||||||
f"✓ 删除完成,行号已从 {old_row_number} 变更为 {new_row_number}"
|
material_idx += 1
|
||||||
)
|
self.stats['processed_materials'] += 1
|
||||||
delete_success = True
|
|
||||||
break
|
# 稳定性检查:等待行号更新
|
||||||
time.sleep(0.2) # 每200ms检查一次
|
current_row = self._get_input_value(child_form, r"^行号$")
|
||||||
except Exception as e:
|
row_num_int = int(current_row)
|
||||||
self._log(f"获取新行号时出错: {e}", "warn")
|
if current_row == last_row_number:
|
||||||
time.sleep(0.2)
|
time.sleep(0.5)
|
||||||
else:
|
|
||||||
self._log(
|
material_code = self._get_input_value(child_form, r"^材料编码")
|
||||||
f"⚠️ 等待删除完成超时({max_wait_time}秒)", "warn"
|
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:
|
max_wait_time = 10 # 最大等待10秒
|
||||||
self.stats['deleted_materials'].append({
|
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,
|
'order_id': order_id,
|
||||||
'material_code': material_code,
|
'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
|
else:
|
||||||
elif row_num_int >= 7000 and row_num_int < 8000:
|
reason = "不满足删除条件"
|
||||||
reason = f"行号 {row_num_int} 在 7000-8000 范围内"
|
self._report_material_progress(
|
||||||
self._report_material_progress(
|
order_index, total_orders,
|
||||||
order_index, total_orders,
|
material_idx, detail_count,
|
||||||
material_idx, detail_count,
|
order_id, material_name, "跳过"
|
||||||
order_id, material_name, "跳过"
|
)
|
||||||
)
|
self._log(
|
||||||
self._log(f"⚠️ {reason},跳过删除", "warn")
|
f"⚠️ {reason},跳过物料 {material_name} ({material_code})",
|
||||||
self.stats['skipped_materials'].append({
|
"warn",
|
||||||
'order_id': order_id,
|
)
|
||||||
'material_code': material_code,
|
self.stats['skipped_materials'].append({
|
||||||
'material_name': material_name,
|
'order_id': order_id,
|
||||||
'reason': reason
|
'material_code': material_code,
|
||||||
})
|
'material_name': material_name,
|
||||||
elif pending_qty:
|
'reason': reason
|
||||||
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:
|
else:
|
||||||
reason = "不满足删除条件"
|
|
||||||
self._report_material_progress(
|
|
||||||
order_index, total_orders,
|
|
||||||
material_idx, detail_count,
|
|
||||||
order_id, material_name, "跳过"
|
|
||||||
)
|
|
||||||
self._log(
|
self._log(
|
||||||
f"⚠️ {reason},跳过物料 {material_name} ({material_code})",
|
f"ℹ️ 物料 {material_name} ({material_code}) 不在删除列表中,不做处理"
|
||||||
"warn",
|
|
||||||
)
|
)
|
||||||
self.stats['skipped_materials'].append({
|
if self._is_button_enabled(next_btn):
|
||||||
'order_id': order_id,
|
last_row_number = current_row
|
||||||
'material_code': material_code,
|
next_btn.click()
|
||||||
'material_name': material_name,
|
else:
|
||||||
'reason': reason
|
break
|
||||||
})
|
collapse_btn.click()
|
||||||
|
|
||||||
|
# 执行最终保存逻辑(如业务需要)
|
||||||
|
if self.dryrun:
|
||||||
|
self._log("[DRYRUN] 跳过保存操作")
|
||||||
else:
|
else:
|
||||||
self._log(
|
save_button_locator.click()
|
||||||
f"ℹ️ 物料 {material_name} ({material_code}) 不在删除列表中,不做处理"
|
self._log("已点击保存,等待保存完成...")
|
||||||
)
|
save_button_locator.wait_for(
|
||||||
if self._is_button_enabled(next_btn):
|
state="hidden", timeout=60000
|
||||||
last_row_number = current_row
|
) # 等待按钮消失,超时60秒
|
||||||
next_btn.click()
|
self._log("✅ 保存成功(保存按钮已消失)")
|
||||||
else:
|
|
||||||
break
|
|
||||||
collapse_btn.click()
|
|
||||||
|
|
||||||
# 执行最终保存逻辑(如业务需要)
|
|
||||||
if self.dryrun:
|
|
||||||
self._log("[DRYRUN] 跳过保存操作")
|
|
||||||
else:
|
else:
|
||||||
save_button_locator.click()
|
self._log("订单无备料计划数据,无需处理")
|
||||||
self._log("已点击保存,等待保存完成...")
|
elif detail_status == "完成":
|
||||||
save_button_locator.wait_for(
|
self._log("订单已完成,无需处理")
|
||||||
state="hidden", timeout=60000
|
|
||||||
) # 等待按钮消失,超时60秒
|
|
||||||
self._log("✅ 保存成功(保存按钮已消失)")
|
|
||||||
else:
|
else:
|
||||||
self._log("订单无备料计划数据,无需处理")
|
self._log(f"订单状态为 [{detail_status}],不符合处理条件", "warn")
|
||||||
elif detail_status == "完成":
|
finally:
|
||||||
self._log("订单已完成,无需处理")
|
page2.close()
|
||||||
else:
|
|
||||||
self._log(f"订单状态为 [{detail_status}],不符合处理条件", "warn")
|
|
||||||
page2.close()
|
|
||||||
|
|
||||||
def setup_query_interface(self, inner_frame):
|
def setup_query_interface(self, inner_frame):
|
||||||
"""初始化查询界面配置"""
|
"""初始化查询界面配置"""
|
||||||
|
|||||||
Reference in New Issue
Block a user