refactor: 重构文件读取和日志处理以提升性能和稳定性
- 新增 smart_read_excel 工具函数,统一 Excel 读取逻辑并自动选择引擎 - 重构 ConfigManager.get_path 方法,使用 pathlib 提升路径处理可靠性 - 将 GUI 日志处理改为异步队列模式,避免 UI 阻塞 - 优化 ExcelProcessor 的表头识别逻辑,避免重复读取文件 - 更新配置文件中的版本号
This commit is contained in:
@@ -122,21 +122,15 @@ class OrderService:
|
||||
try:
|
||||
import pandas as pd
|
||||
import os
|
||||
from app.core.utils.file_utils import smart_read_excel
|
||||
|
||||
def _read_df(path):
|
||||
ap = os.path.abspath(path)
|
||||
if ap.lower().endswith('.xlsx'):
|
||||
return pd.read_excel(ap, engine='openpyxl')
|
||||
else:
|
||||
return pd.read_excel(ap, engine='xlrd')
|
||||
|
||||
item_path = os.path.join('templates', '商品资料.xlsx')
|
||||
if not os.path.exists(item_path):
|
||||
logger.warning(f"未找到商品资料文件: {item_path}")
|
||||
return []
|
||||
|
||||
df_item = _read_df(item_path)
|
||||
df_res = _read_df(result_path)
|
||||
df_item = smart_read_excel(item_path)
|
||||
df_res = smart_read_excel(result_path)
|
||||
|
||||
def _find_col(df, candidates, contains=None):
|
||||
cols = list(df.columns)
|
||||
|
||||
@@ -41,10 +41,11 @@ class SpecialSuppliersService:
|
||||
return c
|
||||
return None
|
||||
|
||||
from app.core.utils.file_utils import smart_read_excel
|
||||
try:
|
||||
df_raw = pd.read_excel(src_path, header=2)
|
||||
df_raw = smart_read_excel(src_path, header=2)
|
||||
except Exception:
|
||||
df_raw = pd.read_excel(src_path)
|
||||
df_raw = smart_read_excel(src_path)
|
||||
df_raw = df_raw.iloc[2:].reset_index(drop=True)
|
||||
|
||||
# 去除全空列与行
|
||||
|
||||
@@ -165,8 +165,9 @@ class TobaccoService:
|
||||
columns = ['商品', '盒码', '条码', '建议零售价', '批发价', '需求量', '订单量', '金额']
|
||||
|
||||
try:
|
||||
from app.core.utils.file_utils import smart_read_excel
|
||||
# 读取Excel文件
|
||||
df_old = pd.read_excel(file_path, header=None, skiprows=3, names=columns)
|
||||
df_old = smart_read_excel(file_path, header=None, skiprows=3, names=columns)
|
||||
|
||||
# 过滤订单量不为0的数据,并计算采购量和单价
|
||||
df_filtered = df_old[df_old['订单量'] != 0].copy()
|
||||
|
||||
Reference in New Issue
Block a user