33b4c734aa
后端: - 新增认证(auth)、任务(tasks)、映射(mappings)、对账(reconciliation)、异常(exceptions)、导出(exports) API - 新增核心模块: database, security, permissions, tenant, exceptions, error_handlers - 新增数据模型: user, company, reconciliation_task, field_mapping, uploaded_file 等 - 新增服务层: ai_recognizer, file_parser, file_storage, mapping, reconciliation 等 - 添加数据库迁移脚本 前端: - 新增登录页面和仪表盘页面 - 新增任务列表、任务详情、字段映射页面 - 新增异常处理页面和规则设置页面 - 新增 API 代理路由 /api/[...path] - 新增 UI 组件库 (button, card, dialog, input, table 等) - 新增 auth 组件 (ProtectedRoute, PermissionGate) - 新增 layout 组件 (Header, Sidebar) - 新增 mapping 组件 (FieldMappingTable, AISuggestionPanel) - 新增 API 客户端和 hooks (useAsync, useToast, usePermission 等) - 新增状态管理 (auth-store, company-store, ui-store) - 集成 Tailwind CSS 和 shadcn/ui 组件库 其他: - 添加 Alembic 数据库迁移配置 - 添加初始化示例数据脚本 - 更新项目文档
114 lines
3.1 KiB
Python
114 lines
3.1 KiB
Python
import csv
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import openpyxl
|
|
import pandas as pd
|
|
|
|
|
|
class FileParserService:
|
|
"""文件解析服务"""
|
|
|
|
@staticmethod
|
|
def parse_excel(file_path: str | Path) -> dict[str, Any]:
|
|
"""
|
|
解析 Excel 文件
|
|
|
|
Args:
|
|
file_path: 文件路径
|
|
|
|
Returns:
|
|
解析结果:headers, sample_rows, total_rows
|
|
"""
|
|
file_path = Path(file_path)
|
|
|
|
# 使用 openpyxl 解析 .xlsx
|
|
if file_path.suffix.lower() == ".xlsx":
|
|
wb = openpyxl.load_workbook(file_path, read_only=True, data_only=True)
|
|
ws = wb.active
|
|
|
|
# 获取表头(第一行)
|
|
headers = [cell.value for cell in ws[1]]
|
|
|
|
# 获取样例数据(前5行,不含表头)
|
|
sample_rows = []
|
|
for row_idx, row in enumerate(ws.iter_rows(min_row=2, max_row=6, values_only=True), start=2):
|
|
row_data = dict(zip(headers, row))
|
|
sample_rows.append(row_data)
|
|
|
|
# 获取总行数
|
|
total_rows = ws.max_row - 1 # 减去表头行
|
|
|
|
wb.close()
|
|
|
|
return {
|
|
"headers": headers,
|
|
"sample_rows": sample_rows,
|
|
"total_rows": total_rows,
|
|
}
|
|
|
|
# 使用 pandas 解析其他格式
|
|
else:
|
|
df = pd.read_excel(file_path)
|
|
|
|
return {
|
|
"headers": df.columns.tolist(),
|
|
"sample_rows": df.head(5).to_dict(orient="records"),
|
|
"total_rows": len(df),
|
|
}
|
|
|
|
@staticmethod
|
|
def parse_csv(file_path: str | Path) -> dict[str, Any]:
|
|
"""
|
|
解析 CSV 文件
|
|
|
|
Args:
|
|
file_path: 文件路径
|
|
|
|
Returns:
|
|
解析结果
|
|
"""
|
|
file_path = Path(file_path)
|
|
|
|
# 自动检测编码
|
|
encodings = ["utf-8", "gbk", "gb2312", "utf-8-sig"]
|
|
df = None
|
|
|
|
for encoding in encodings:
|
|
try:
|
|
df = pd.read_csv(file_path, encoding=encoding)
|
|
break
|
|
except (UnicodeDecodeError, Exception):
|
|
continue
|
|
|
|
if df is None:
|
|
raise ValueError("无法解析CSV文件,编码格式不支持")
|
|
|
|
return {
|
|
"headers": df.columns.tolist(),
|
|
"sample_rows": df.head(5).to_dict(orient="records"),
|
|
"total_rows": len(df),
|
|
}
|
|
|
|
@staticmethod
|
|
def detect_file_type(file_content: bytes) -> str:
|
|
"""
|
|
检测文件类型
|
|
|
|
Args:
|
|
file_content: 文件内容
|
|
|
|
Returns:
|
|
文件类型
|
|
"""
|
|
# 简单的文件类型检测
|
|
if file_content[:2] == b"PK":
|
|
return "xlsx"
|
|
elif file_content[:8] == b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1":
|
|
return "xls"
|
|
else:
|
|
return "csv"
|
|
|
|
|
|
# 单例实例
|
|
file_parser_service = FileParserService() |