ai客服
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
9.3 KiB

1 month ago
  1. import sys
  2. def rewrite():
  3. with open('/Users/getmind/ds/kf/erp_automation.py', 'r') as f:
  4. lines = f.readlines()
  5. start_idx = -1
  6. end_idx = -1
  7. for i, line in enumerate(lines):
  8. if '步骤C: 切换筛选条件 - 取消"等待订单合并",勾选"已付款待审核"' in line:
  9. start_idx = i
  10. if 'close_msg_dialog("审核结果提示")' in line:
  11. end_idx = i + 1
  12. break
  13. if start_idx == -1 or end_idx == -1:
  14. print("未找到起点或终点")
  15. return
  16. original_block = lines[start_idx:end_idx]
  17. indented_block = [" " + line[12:] if line.startswith(" ") else " " + line for line in original_block]
  18. # 增加 E10 转异常的逻辑
  19. exception_logic = """
  20. # E10: 转异常流程
  21. print(" -> 开始'转异常'流程...")
  22. print(" -> 再次执行组合查询...")
  23. click_search()
  24. time.sleep(3)
  25. print(" -> 滚动检查并只勾选'预售''补色'的订单...")
  26. table_frame = None
  27. for frame in page.frames:
  28. try:
  29. if frame.locator("#_jt_h_checked").count() > 0:
  30. table_frame = frame
  31. break
  32. except:
  33. continue
  34. check_count = 0
  35. if table_frame:
  36. try:
  37. select_all_cb = table_frame.locator("#_jt_h_checked")
  38. if select_all_cb.count() > 0 and select_all_cb.is_checked():
  39. select_all_cb.click(force=True)
  40. time.sleep(1)
  41. try:
  42. table_frame.locator("._jt_row").first.wait_for(state="attached", timeout=5000)
  43. except:
  44. pass
  45. processed_indices = set()
  46. for scroll_round in range(20):
  47. rows = table_frame.locator("._jt_row")
  48. row_count = rows.count()
  49. new_rows_in_view = 0
  50. for idx in range(row_count):
  51. try:
  52. row = rows.nth(idx)
  53. row_idx = row.get_attribute("index")
  54. if not row_idx:
  55. row_idx = f"unknown_{idx}_{scroll_round}"
  56. if row_idx in processed_indices:
  57. continue
  58. processed_indices.add(row_idx)
  59. new_rows_in_view += 1
  60. row_text = row.inner_text(timeout=2000)
  61. try:
  62. row_html = row.inner_html(timeout=2000)
  63. except:
  64. row_html = ""
  65. should_check = False
  66. reason = ""
  67. if "预售" in row_text or "预售" in row_html:
  68. should_check = True
  69. reason = "预售"
  70. elif "补色" in row_text or "补色" in row_html:
  71. should_check = True
  72. reason = "补色"
  73. if should_check:
  74. cb = row.locator("input[type='checkbox']._jt_cbx").first
  75. if cb.count() > 0 and not cb.is_checked():
  76. cb.click(force=True)
  77. time.sleep(0.5)
  78. check_count += 1
  79. print(f" ✅ 已勾选第{row_idx}行 (含'{reason}')。")
  80. except:
  81. continue
  82. if new_rows_in_view == 0 and scroll_round > 0:
  83. break
  84. if len(processed_indices) >= 50:
  85. print(" -> 已检查满 50 行,停止滚动检查。")
  86. break
  87. try:
  88. scroll_target = table_frame.locator("#_jt_body")
  89. if scroll_target.count() > 0:
  90. scroll_target.first.evaluate("el => el.scrollTop += 800")
  91. time.sleep(1)
  92. else:
  93. break
  94. except:
  95. break
  96. except Exception as e:
  97. print(f" ⚠️ 勾选异常订单失败: {e}")
  98. if check_count > 0:
  99. print(" -> 点击'转异常'...")
  100. question_clicked = False
  101. for frame in page.frames:
  102. try:
  103. btn = frame.locator("span#Questions:has-text('转异常'), span._jt_tool:has-text('转异常')").first
  104. if btn.count() > 0 and btn.is_visible():
  105. btn.click(force=True, timeout=3000)
  106. time.sleep(1)
  107. print(" ✅ 已点击'转异常'")
  108. question_clicked = True
  109. break
  110. except:
  111. continue
  112. time.sleep(2)
  113. if question_clicked:
  114. print(" -> 选择'特殊单'...")
  115. special_selected = False
  116. for frame in page.frames:
  117. try:
  118. radio = frame.locator("input#i6[value='特殊单'], label:text-is('特殊单')").first
  119. if radio.count() > 0:
  120. radio.click(force=True, timeout=3000)
  121. time.sleep(1)
  122. print(" ✅ 已选择'特殊单'")
  123. special_selected = True
  124. break
  125. except:
  126. continue
  127. time.sleep(1)
  128. for confirm_round in range(2):
  129. print(f" -> 点击确认转异常 (第{confirm_round+1}次)...")
  130. time.sleep(2)
  131. click_confirmed = False
  132. for frame in page.frames:
  133. if click_confirmed:
  134. break
  135. try:
  136. 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
  137. if btn.count() > 0:
  138. btn.click(force=True, timeout=3000)
  139. time.sleep(1)
  140. print(f" ✅ 已点击确认按钮 (第{confirm_round+1}次)。")
  141. click_confirmed = True
  142. except:
  143. continue
  144. if not click_confirmed:
  145. print(f" ℹ️ 第{confirm_round+1}次确认弹窗未出现。")
  146. break
  147. time.sleep(2)
  148. close_msg_dialog("转异常结果提示")
  149. time.sleep(1)
  150. else:
  151. print(" -> 未发现任何需要转异常的'预售''补色'订单。")
  152. """
  153. loop_wrapper_start = [
  154. " # ========== 步骤C-E: 全流程处理(执行两遍以防遗漏) ==========\n",
  155. " for process_round in range(2):\n",
  156. " print(f'\\n ========== 开始第 {process_round+1} 轮“已付款待审核”全流程处理 ==========')\n"
  157. ]
  158. new_content = loop_wrapper_start + indented_block + [exception_logic + "\n"]
  159. final_lines = lines[:start_idx] + new_content + lines[end_idx:]
  160. with open('/Users/getmind/ds/kf/erp_automation.py', 'w') as f:
  161. f.writelines(final_lines)
  162. if __name__ == '__main__':
  163. rewrite()