fix(固资履历): 解决本地共享网盘Windows与Linux访问路径兼容性问题
This commit is contained in:
1 parent
202d626a7a
commit
49c149eb3e
8 files changed
+606
-20
No files matched your search
@@ -4,7 +4,8 @@ backend/app/services/fixed_asset_history.py
|
||||
用途:为固资管理提供“资产履历”共享盘检索能力,按资产编号/资产名称匹配履历文件。
|
||||
依赖:app.core.config.settings.Config、chat.py 里的预览代理会复用这里返回的共享盘文件路径。
|
||||
逻辑:聊天表格点击资产编号 -> chat API 调用本服务检索履历目录 -> 前端拿到候选履历文件后继续走既有预览弹窗;
|
||||
支持 Windows 直接访问 UNC 共享盘,也支持 Linux 通过本地挂载目录访问同一批履历文件。
|
||||
支持 Windows 直接访问 UNC 共享盘,也支持 Linux 通过本地挂载目录访问同一批履历文件;
|
||||
编号命中优先;共享盘按设备名归档时,允许用资产名称匹配文件夹/文件名作为回退。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -192,18 +193,18 @@ class FixedAssetHistoryService:
|
||||
cls._merge_candidate(candidates, candidate)
|
||||
|
||||
if asset_name_value:
|
||||
for entry in os.scandir(root):
|
||||
if not entry.is_file():
|
||||
continue
|
||||
candidate = cls._build_candidate(
|
||||
root=root,
|
||||
file_path=entry.path,
|
||||
asset_id_variants=asset_id_variants,
|
||||
asset_name=asset_name_value,
|
||||
match_reason="asset_name_file",
|
||||
)
|
||||
if candidate:
|
||||
cls._merge_candidate(candidates, candidate)
|
||||
# 全树按资产名称匹配文件名(共享盘常见「设备名+履历.xls」,不一定含资产编号)
|
||||
for current_root, _dirs, files in os.walk(root):
|
||||
for filename in files:
|
||||
candidate = cls._build_candidate(
|
||||
root=root,
|
||||
file_path=os.path.join(current_root, filename),
|
||||
asset_id_variants=asset_id_variants,
|
||||
asset_name=asset_name_value,
|
||||
match_reason="asset_name_file",
|
||||
)
|
||||
if candidate:
|
||||
cls._merge_candidate(candidates, candidate)
|
||||
|
||||
ordered = sorted(
|
||||
candidates.values(),
|
||||
@@ -244,6 +245,14 @@ class FixedAssetHistoryService:
|
||||
asset_name: str,
|
||||
match_reason: str,
|
||||
) -> AssetHistoryCandidate | None:
|
||||
"""
|
||||
将单个文件构造成履历候选;按匹配原因施加不同门槛。
|
||||
|
||||
核心逻辑:
|
||||
- asset_id:文件名/相对路径必须包含资产编号(或去字母前缀变体);
|
||||
- asset_name_folder / asset_name_file:共享盘大量按设备名归档,不强制编号出现在文件名;
|
||||
名称文件夹命中后,目录内履历文件均可作为候选。
|
||||
"""
|
||||
if not os.path.isfile(file_path):
|
||||
return None
|
||||
ext = os.path.splitext(file_path)[1].strip().lower()
|
||||
@@ -259,9 +268,17 @@ class FixedAssetHistoryService:
|
||||
if not searchable_text:
|
||||
return None
|
||||
|
||||
if asset_id_variants and not any(variant in searchable_text for variant in asset_id_variants):
|
||||
return None
|
||||
if asset_name and asset_name not in searchable_text and match_reason != "asset_id":
|
||||
# [STEP] 按匹配原因分流门槛,避免“有资产编号就永远要求文件名含编号”导致设备名归档全部 miss
|
||||
if match_reason == "asset_id":
|
||||
if not asset_id_variants or not any(variant in searchable_text for variant in asset_id_variants):
|
||||
return None
|
||||
elif match_reason == "asset_name_file":
|
||||
if not asset_name or asset_name not in searchable_text:
|
||||
return None
|
||||
elif match_reason == "asset_name_folder":
|
||||
# 文件夹已按资产名称命中;文件本身可不含编号/全名
|
||||
pass
|
||||
else:
|
||||
return None
|
||||
|
||||
score = cls._score_candidate(
|
||||
@@ -296,6 +313,12 @@ class FixedAssetHistoryService:
|
||||
asset_name: str,
|
||||
match_reason: str,
|
||||
) -> int:
|
||||
"""
|
||||
为履历候选打分;编号命中优先于名称命中。
|
||||
|
||||
核心逻辑:编号精确/模糊命中给高分;名称命中与「履历」关键字加分;
|
||||
纯名称归档场景(无编号)至少要靠名称或文件夹命中拿到正分,否则会被过滤。
|
||||
"""
|
||||
score = 0
|
||||
if asset_id_variants:
|
||||
for index, asset_id in enumerate(asset_id_variants):
|
||||
@@ -310,6 +333,9 @@ class FixedAssetHistoryService:
|
||||
score += 60
|
||||
elif asset_name in searchable_text:
|
||||
score += 40
|
||||
elif match_reason == "asset_name_folder":
|
||||
# 文件夹已按名称命中,目录内文件给基础分,避免 score=0 被丢弃
|
||||
score += 30
|
||||
if "履历" in filename:
|
||||
score += 20
|
||||
if match_reason == "asset_name_folder":
|
||||
|
||||
Reference in new issue
Block a user