refactor: optimize material deletion with exact code matching and database queries

- Change from fuzzy material name matching to exact material code matching
- Migrate from MaterialsTypeToBeDeleted to MaterialsToBeDeleted table
- Add should_delete_material() for on-demand database queries
- Remove in-memory material list loading to reduce memory footprint
- Update documentation to reflect new matching logic
- Remove main scripts from git tracking (contain user-specific config)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-02-10 10:53:53 +08:00
parent 6ec7484036
commit cff5e78c1d
7 changed files with 405 additions and 149 deletions

View File

@@ -64,7 +64,7 @@ class DiscreteMaterialPlanCleaner:
order_id,
order_index,
page1,
materials_to_delete=None,
manager_name=None,
debug_mode=False,
debug_order=None,
):
@@ -75,12 +75,12 @@ class DiscreteMaterialPlanCleaner:
order_id: 订单号
order_index: 订单索引
page1: 页面对象
materials_to_delete: 待删除物料关键字列表
manager_name: 负责人姓名(用于查询待删除物料)
debug_mode: 是否启用调试模式
debug_order: 调试订单号
"""
if materials_to_delete is None:
materials_to_delete = []
if manager_name is None:
manager_name = self.manager_name
from playwright.sync_api import TimeoutError
import re
import time
@@ -180,22 +180,23 @@ class DiscreteMaterialPlanCleaner:
child_form.wait_for(state="visible", timeout=5000)
self._print(f"父容器 .card-table-side-box 已找到")
page2.pause()
# page2.pause()
for id in range(detail_count):
id_lable_locator = child_form.get_by_text("序号 " + str(id + 1))
id_lable_locator.wait_for(state="visible", timeout=10000)
self._print(f"处理 {id_lable_locator.inner_text()} ")
# 获取材料编码(通过文本定位取第一个input
# 获取材料编码(用于精确匹配
input_box = (
child_form.locator("div")
.filter(has_text=re.compile(r"^材料编码\d{11}$", re.MULTILINE))
.locator("input")
.first
)
self._print(f"材料编码:{input_box.input_value()}")
material_code = input_box.input_value()
self._print(f"材料编码:{material_code}")
# 获取材料名称
# 获取材料名称(仅用于日志)
input_box = (
child_form.locator("div")
.filter(has_text=re.compile(r"^材料名称$"))
@@ -220,22 +221,17 @@ class DiscreteMaterialPlanCleaner:
)
self._print(f"累计出库数量:{input_box.input_value()}")
# 检查是否需要清理该物料
should_delete = False
matched_keyword = None
for keyword in materials_to_delete:
if keyword in material_name:
should_delete = True
matched_keyword = keyword
break
# 检查是否需要清理该物料(直接数据库查询)
from db.materials_to_delete import should_delete_material
should_delete = should_delete_material(manager_name, material_code)
if should_delete:
self._print(
f">>> 需要清理:材料名称【{material_name}匹配关键字{matched_keyword}"
f">>> 需要清理:材料名称【{material_name}材料编码{material_code}"
)
# TODO: 执行删除操作
else:
self._print(f"保留:材料名称【{material_name}】无需清理")
self._print(f"保留:材料名称【{material_name}材料编码【{material_code}无需清理")
if id != detail_count - 1:
child_form.get_by_role("button").filter(
@@ -298,10 +294,7 @@ class DiscreteMaterialPlanCleaner:
debug_mode: 是否启用调试模式
debug_order: 调试订单索引
"""
# 获取待删除物料列表
self._print(f"正在查询负责人 [{self.manager_name}] 的待删除物料列表...")
materials_to_delete = get_materials_to_delete(self.manager_name)
self._print(f"查询到 {len(materials_to_delete)} 个待删除物料关键字")
self._print(f"使用负责人 [{self.manager_name}] 进行数据清理")
with sync_playwright() as playwright:
# 调用登录模块
@@ -348,7 +341,7 @@ class DiscreteMaterialPlanCleaner:
order_id,
order_index,
page1,
materials_to_delete,
self.manager_name,
debug_mode=debug_mode,
debug_order=debug_order,
)