Update: Refactor to absolute imports, fix QPS limits, and enhance Gitea sync with SQLite support
This commit is contained in:
+39
-103
@@ -27,6 +27,27 @@ from .command_runner import get_running_task, set_running_task
|
||||
from .file_operations import select_file, select_excel_file, validate_unit_price_against_item_data
|
||||
|
||||
|
||||
def _get_missing_barcodes_callback(log_widget):
|
||||
"""创建并返回缺失条码的回调函数"""
|
||||
def callback(missing_barcodes):
|
||||
if not missing_barcodes:
|
||||
return
|
||||
|
||||
msg = f"发现以下条码不在商品资料中,已记录到数据库:\n\n" + "\n".join([f"• {bc}" for bc in missing_barcodes])
|
||||
# 在主线程中弹出对话框
|
||||
def show_msg():
|
||||
messagebox.showwarning("发现缺失条码", msg)
|
||||
|
||||
# 尝试使用 log_widget 的 winfo_toplevel() 来调用 after,确保在 UI 线程执行
|
||||
try:
|
||||
log_widget.after(0, show_msg)
|
||||
except Exception:
|
||||
# 降级处理
|
||||
show_msg()
|
||||
|
||||
return callback
|
||||
|
||||
|
||||
def _ask_and_merge_purchase_orders(order_service, log_widget, add_to_recent=False):
|
||||
"""弹窗询问是否合并采购单,返回合并结果路径或 None。
|
||||
|
||||
@@ -138,108 +159,23 @@ def process_single_image_with_status(log_widget, status_bar):
|
||||
|
||||
|
||||
def run_pipeline_directly(log_widget, status_bar):
|
||||
"""直接运行完整处理流程"""
|
||||
"""运行完整处理流程:先选择图片,再执行 OCR+Excel 处理"""
|
||||
if get_running_task() is not None:
|
||||
messagebox.showinfo("任务进行中", "请等待当前任务完成后再执行新的操作。")
|
||||
return
|
||||
|
||||
def run_in_thread():
|
||||
set_running_task("pipeline")
|
||||
# 先选择图片
|
||||
file_path = select_file(
|
||||
log_widget,
|
||||
[("支持文件", "*.jpg *.jpeg *.png *.bmp *.xlsx *.xls"), ("图片文件", "*.jpg *.jpeg *.png *.bmp"), ("Excel文件", "*.xlsx *.xls"), ("所有文件", "*.*")],
|
||||
"选择待处理文件"
|
||||
)
|
||||
if not file_path:
|
||||
add_to_log(log_widget, "未选择文件,一键处理已取消\n", "warning")
|
||||
return
|
||||
|
||||
if status_bar:
|
||||
status_bar.set_running(True)
|
||||
status_bar.set_status("开始完整处理流程...")
|
||||
|
||||
start_time = datetime.datetime.now()
|
||||
start_perf = time.perf_counter()
|
||||
log_widget.configure(state=tk.NORMAL)
|
||||
log_widget.delete(1.0, tk.END)
|
||||
log_widget.insert(tk.END, "执行命令: 完整处理流程\n", "command")
|
||||
log_widget.insert(tk.END, f"开始时间: {start_time.strftime('%Y-%m-%d %H:%M:%S')}\n", "time")
|
||||
log_widget.insert(tk.END, "=" * 50 + "\n\n", "separator")
|
||||
log_widget.configure(state=tk.DISABLED)
|
||||
|
||||
try:
|
||||
config = ConfigManager()
|
||||
|
||||
gui_handler = init_gui_logger(log_widget)
|
||||
|
||||
ocr_service = OCRService(config)
|
||||
order_service = OrderService(config)
|
||||
|
||||
reporter = ProgressReporter(status_bar)
|
||||
reporter.running()
|
||||
reporter.set("开始OCR批量处理...", 10)
|
||||
|
||||
total, success = ocr_service.batch_process(progress_cb=lambda p: reporter.set("OCR处理中...", p))
|
||||
if total == 0:
|
||||
add_to_log(log_widget, "没有找到需要处理的图片\n", "warning")
|
||||
if status_bar:
|
||||
status_bar.set_status("未找到图片文件")
|
||||
return
|
||||
elif success == 0:
|
||||
add_to_log(log_widget, "OCR处理没有成功处理任何新文件\n", "warning")
|
||||
else:
|
||||
add_to_log(log_widget, f"OCR处理完成,共处理 {success}/{total} 个文件\n", "success")
|
||||
try:
|
||||
processed_map = {}
|
||||
config = ConfigManager()
|
||||
pjson = config.get('Paths', 'processed_record', fallback='data/processed_files.json')
|
||||
if os.path.exists(pjson):
|
||||
with open(pjson, 'r', encoding='utf-8') as f:
|
||||
processed_map = json.load(f)
|
||||
outputs = list(processed_map.values())
|
||||
for p in outputs[-10:]:
|
||||
if p:
|
||||
add_recent_file(os.path.abspath(p))
|
||||
except Exception as e:
|
||||
logger.debug(f"加载已处理文件记录失败: {e}")
|
||||
reporter.set("开始Excel处理...", 92)
|
||||
|
||||
add_to_log(log_widget, "开始Excel处理...\n", "info")
|
||||
result = order_service.process_excel()
|
||||
|
||||
if not result:
|
||||
add_to_log(log_widget, "Excel处理失败\n", "error")
|
||||
else:
|
||||
add_to_log(log_widget, "Excel处理完成\n", "success")
|
||||
try:
|
||||
add_recent_file(result)
|
||||
except Exception as e:
|
||||
logger.debug(f"添加最近文件失败: {e}")
|
||||
try:
|
||||
validate_unit_price_against_item_data(result, log_widget)
|
||||
except Exception as e:
|
||||
logger.debug(f"单价校验失败: {e}")
|
||||
|
||||
reporter.set("检查是否需要合并采购单...", 80)
|
||||
_ask_and_merge_purchase_orders(order_service, log_widget, add_to_recent=True)
|
||||
|
||||
end_time = datetime.datetime.now()
|
||||
duration_sec = max(0.0, time.perf_counter() - start_perf)
|
||||
|
||||
add_to_log(log_widget, f"\n{'=' * 50}\n", "separator")
|
||||
add_to_log(log_widget, "完整处理流程执行完毕!\n", "success")
|
||||
add_to_log(log_widget, f"结束时间: {end_time.strftime('%Y-%m-%d %H:%M:%S')}\n", "time")
|
||||
add_to_log(log_widget, f"耗时: {duration_sec:.2f} 秒\n", "time")
|
||||
reporter.set("处理完成", 100)
|
||||
|
||||
except Exception as e:
|
||||
add_to_log(log_widget, f"执行过程中发生错误: {str(e)}\n", "error")
|
||||
import traceback
|
||||
add_to_log(log_widget, f"详细错误信息: {traceback.format_exc()}\n", "error")
|
||||
finally:
|
||||
dispose_gui_logger()
|
||||
reporter.done()
|
||||
|
||||
set_running_task(None)
|
||||
if status_bar:
|
||||
status_bar.set_running(False)
|
||||
status_bar.set_status("就绪")
|
||||
|
||||
thread = Thread(target=run_in_thread)
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
# 复用拖拽处理的逻辑,实现“先选图,后全流程”
|
||||
process_dropped_file(log_widget, status_bar, file_path)
|
||||
|
||||
|
||||
def batch_ocr_with_status(log_widget, status_bar):
|
||||
@@ -318,7 +254,7 @@ def batch_process_all_inputs(log_widget, status_bar):
|
||||
from .memory_editor import show_memory_editor # noqa: F401 触发 import 顺序
|
||||
cfg = ConfigManager()
|
||||
|
||||
svc = BatchService(cfg)
|
||||
svc = BatchService(cfg, missing_barcodes_cb=_get_missing_barcodes_callback(log_widget))
|
||||
|
||||
def progress(done, total, entry):
|
||||
pct = int(done / total * 100) if total else 100
|
||||
@@ -404,7 +340,7 @@ def batch_process_orders_with_status(log_widget, status_bar):
|
||||
|
||||
init_gui_logger(log_widget)
|
||||
|
||||
order_service = OrderService()
|
||||
order_service = OrderService(missing_barcodes_cb=_get_missing_barcodes_callback(log_widget))
|
||||
|
||||
add_to_log(log_widget, "开始Excel处理...\n", "info")
|
||||
try:
|
||||
@@ -461,7 +397,7 @@ def merge_orders_with_status(log_widget, status_bar):
|
||||
|
||||
init_gui_logger(log_widget)
|
||||
|
||||
order_service = OrderService()
|
||||
order_service = OrderService(missing_barcodes_cb=_get_missing_barcodes_callback(log_widget))
|
||||
|
||||
result = order_service.merge_all_purchase_orders(progress_cb=lambda p: reporter.set("合并处理中...", p))
|
||||
|
||||
@@ -511,7 +447,7 @@ def process_excel_file_with_status(log_widget, status_bar):
|
||||
|
||||
init_gui_logger(log_widget)
|
||||
|
||||
order_service = OrderService()
|
||||
order_service = OrderService(missing_barcodes_cb=_get_missing_barcodes_callback(log_widget))
|
||||
|
||||
if file_path:
|
||||
try:
|
||||
@@ -589,7 +525,7 @@ def process_dropped_file(log_widget, status_bar, file_path):
|
||||
|
||||
# 步骤2: Excel处理
|
||||
reporter.set("Excel处理中...", 40)
|
||||
order_service = OrderService()
|
||||
order_service = OrderService(missing_barcodes_cb=_get_missing_barcodes_callback(log_widget))
|
||||
result = order_service.process_excel(excel_path, progress_cb=lambda p: reporter.set("Excel处理中...", p))
|
||||
if not result:
|
||||
add_to_log(log_widget, "Excel处理失败\n", "error")
|
||||
@@ -622,7 +558,7 @@ def process_dropped_file(log_widget, status_bar, file_path):
|
||||
reporter = ProgressReporter(status_bar)
|
||||
reporter.running()
|
||||
init_gui_logger(log_widget)
|
||||
order_service = OrderService()
|
||||
order_service = OrderService(missing_barcodes_cb=_get_missing_barcodes_callback(log_widget))
|
||||
add_to_log(log_widget, f"开始一键处理Excel文件: {file_path}\n", "info")
|
||||
try:
|
||||
add_recent_file(file_path)
|
||||
|
||||
Reference in New Issue
Block a user