|
|
- import sys
-
- def rewrite():
- with open('/Users/getmind/ds/kf/erp_automation.py', 'r') as f:
- lines = f.readlines()
-
- start_idx = -1
- end_idx = -1
-
- for i, line in enumerate(lines):
- if '步骤C: 切换筛选条件 - 取消"等待订单合并",勾选"已付款待审核"' in line:
- start_idx = i
- if 'close_msg_dialog("审核结果提示")' in line:
- end_idx = i + 1
- break
-
- if start_idx == -1 or end_idx == -1:
- print("未找到起点或终点")
- return
-
- original_block = lines[start_idx:end_idx]
-
- indented_block = [" " + line[12:] if line.startswith(" ") else " " + line for line in original_block]
-
- # 增加 E10 转异常的逻辑
- exception_logic = """
- # E10: 转异常流程
- print(" -> 开始'转异常'流程...")
- print(" -> 再次执行组合查询...")
- click_search()
- time.sleep(3)
-
- print(" -> 滚动检查并只勾选'预售'和'补色'的订单...")
- table_frame = None
- for frame in page.frames:
- try:
- if frame.locator("#_jt_h_checked").count() > 0:
- table_frame = frame
- break
- except:
- continue
-
- check_count = 0
- if table_frame:
- try:
- select_all_cb = table_frame.locator("#_jt_h_checked")
- if select_all_cb.count() > 0 and select_all_cb.is_checked():
- select_all_cb.click(force=True)
- time.sleep(1)
-
- try:
- table_frame.locator("._jt_row").first.wait_for(state="attached", timeout=5000)
- except:
- pass
-
- processed_indices = set()
- for scroll_round in range(20):
- rows = table_frame.locator("._jt_row")
- row_count = rows.count()
- new_rows_in_view = 0
-
- for idx in range(row_count):
- try:
- row = rows.nth(idx)
- row_idx = row.get_attribute("index")
- if not row_idx:
- row_idx = f"unknown_{idx}_{scroll_round}"
-
- if row_idx in processed_indices:
- continue
-
- processed_indices.add(row_idx)
- new_rows_in_view += 1
-
- row_text = row.inner_text(timeout=2000)
- try:
- row_html = row.inner_html(timeout=2000)
- except:
- row_html = ""
-
- should_check = False
- reason = ""
- if "预售" in row_text or "预售" in row_html:
- should_check = True
- reason = "预售"
- elif "补色" in row_text or "补色" in row_html:
- should_check = True
- reason = "补色"
-
- if should_check:
- cb = row.locator("input[type='checkbox']._jt_cbx").first
- if cb.count() > 0 and not cb.is_checked():
- cb.click(force=True)
- time.sleep(0.5)
- check_count += 1
- print(f" ✅ 已勾选第{row_idx}行 (含'{reason}')。")
- except:
- continue
-
- if new_rows_in_view == 0 and scroll_round > 0:
- break
- if len(processed_indices) >= 50:
- print(" -> 已检查满 50 行,停止滚动检查。")
- break
-
- try:
- scroll_target = table_frame.locator("#_jt_body")
- if scroll_target.count() > 0:
- scroll_target.first.evaluate("el => el.scrollTop += 800")
- time.sleep(1)
- else:
- break
- except:
- break
- except Exception as e:
- print(f" ⚠️ 勾选异常订单失败: {e}")
-
- if check_count > 0:
- print(" -> 点击'转异常'...")
- question_clicked = False
- for frame in page.frames:
- try:
- btn = frame.locator("span#Questions:has-text('转异常'), span._jt_tool:has-text('转异常')").first
- if btn.count() > 0 and btn.is_visible():
- btn.click(force=True, timeout=3000)
- time.sleep(1)
- print(" ✅ 已点击'转异常'。")
- question_clicked = True
- break
- except:
- continue
-
- time.sleep(2)
-
- if question_clicked:
- print(" -> 选择'特殊单'...")
- special_selected = False
- for frame in page.frames:
- try:
- radio = frame.locator("input#i6[value='特殊单'], label:text-is('特殊单')").first
- if radio.count() > 0:
- radio.click(force=True, timeout=3000)
- time.sleep(1)
- print(" ✅ 已选择'特殊单'。")
- special_selected = True
- break
- except:
- continue
-
- time.sleep(1)
- for confirm_round in range(2):
- print(f" -> 点击确认转异常 (第{confirm_round+1}次)...")
- time.sleep(2)
- click_confirmed = False
- for frame in page.frames:
- if click_confirmed:
- break
- try:
- btn = frame.locator("input#confirm_confirm, span#confirmBtn, span.btn_1:has-text('确认'), span:text-is('确认'), .layui-layer-btn0, a:text-is('确定'), button:text-is('确定')").first
- if btn.count() > 0:
- btn.click(force=True, timeout=3000)
- time.sleep(1)
- print(f" ✅ 已点击确认按钮 (第{confirm_round+1}次)。")
- click_confirmed = True
- except:
- continue
-
- if not click_confirmed:
- print(f" ℹ️ 第{confirm_round+1}次确认弹窗未出现。")
- break
- time.sleep(2)
-
- close_msg_dialog("转异常结果提示")
- time.sleep(1)
- else:
- print(" -> 未发现任何需要转异常的'预售'或'补色'订单。")
- """
-
- loop_wrapper_start = [
- " # ========== 步骤C-E: 全流程处理(执行两遍以防遗漏) ==========\n",
- " for process_round in range(2):\n",
- " print(f'\\n ========== 开始第 {process_round+1} 轮“已付款待审核”全流程处理 ==========')\n"
- ]
-
- new_content = loop_wrapper_start + indented_block + [exception_logic + "\n"]
-
- final_lines = lines[:start_idx] + new_content + lines[end_idx:]
-
- with open('/Users/getmind/ds/kf/erp_automation.py', 'w') as f:
- f.writelines(final_lines)
-
- if __name__ == '__main__':
- rewrite()
|