Merge branch 'dev' of https://gitee.com/justop/hxops into dev

This commit is contained in:
sophie committed 2026-07-13 14:35:59 +08:00
commit 1ee4c69dee
9 files changed
+154 -125

No files matched your search

+78 -23
View File
@@ -3,7 +3,8 @@ backend/app/services/fixed_asset_history.py
用途:为固资管理提供“资产履历”共享盘检索能力,按资产编号/资产名称匹配履历文件。
依赖:app.core.config.settings.Config、chat.py 里的预览代理会复用这里返回的共享盘文件路径。
逻辑:聊天表格点击资产编号 -> chat API 调用本服务检索共享盘 -> 前端拿到候选履历文件后继续走既有预览弹窗。
逻辑:聊天表格点击资产编号 -> chat API 调用本服务检索履历目录 -> 前端拿到候选履历文件后继续走既有预览弹窗;
支持 Windows 直接访问 UNC 共享盘,也支持 Linux 通过本地挂载目录访问同一批履历文件。
"""
from __future__ import annotations
@@ -17,6 +18,7 @@ from dataclasses import dataclass
from flask import current_app, has_app_context
from app.core.config.settings import Config
from app.core.errors import ServiceUnavailableError
_CONNECT_LOCK = threading.Lock()
@@ -75,12 +77,64 @@ class FixedAssetHistoryService:
@classmethod
def is_enabled(cls) -> bool:
return bool(cls.get_share_root())
return bool(cls.get_local_root() or cls.get_share_root())
@classmethod
def get_share_root(cls) -> str:
return cls._get_runtime_setting('FIXED_ASSET_HISTORY_SHARE_ROOT').strip()
@classmethod
def get_local_root(cls) -> str:
return cls._get_runtime_setting('FIXED_ASSET_HISTORY_LOCAL_ROOT').strip()
@classmethod
def get_access_root(cls) -> str:
"""
返回当前环境实际用于检索/预览履历文件的可访问根目录。
AI 触发时机:
- Windows 本地开发环境通常直接访问 UNC 共享盘;
- Linux 测试/生产环境通常通过宿主机或容器挂载目录访问共享盘;
- 因此优先读取 `FIXED_ASSET_HISTORY_LOCAL_ROOT`,未配置时再回退到原共享盘根目录。
"""
return cls.get_local_root() or cls.get_share_root()
@classmethod
def _resolve_share_root(cls) -> str:
"""
解析并校验资产履历共享盘根目录。
AI 触发时机:当用户点击资产编号查询履历时,必须先确认后端能访问配置的共享盘;
若测试/生产环境未挂载成功,不应误报成“未找到履历文件”,而应返回更准确的环境异常提示。
"""
root = cls.get_access_root()
if not root:
raise ServiceUnavailableError('asset history share is not configured')
if not os.path.exists(root):
cls._ensure_share_connected(root)
if not os.path.exists(root):
raise ServiceUnavailableError('asset history share is unreachable')
if not os.path.isdir(root):
raise ServiceUnavailableError('asset history share is invalid')
return root
@classmethod
def _build_asset_id_variants(cls, asset_id: str) -> tuple[str, ...]:
"""
生成资产编号匹配变体。
AI 触发时机:测试/生产环境的共享盘文件名可能出现“带前缀 A”与“不带前缀”两种写法;
这里统一构造多个匹配 key,避免因为命名差一个前缀就查不到履历。
"""
normalized = cls._normalize_keyword(asset_id)
if not normalized:
return tuple()
variants = [normalized]
stripped = re.sub(r'^[a-z]+', '', normalized)
if stripped and stripped not in variants:
variants.append(stripped)
return tuple(variants)
@classmethod
def search_history_files(
cls,
@@ -97,17 +151,12 @@ class FixedAssetHistoryService:
[STEP 3] 再用资产名称命中同名文件夹/文件,补足部分历史文件只按设备名称归档的情况。
"""
asset_id_value = cls._normalize_keyword(asset_id)
asset_id_variants = cls._build_asset_id_variants(asset_id)
asset_name_value = cls._normalize_keyword(asset_name)
if not asset_id_value and not asset_name_value:
if not asset_id_variants and not asset_name_value:
return []
root = cls.get_share_root()
if not root:
return []
if not os.path.exists(root):
cls._ensure_share_connected(root)
if not os.path.isdir(root):
return []
root = cls._resolve_share_root()
max_results = max(
1,
@@ -115,13 +164,13 @@ class FixedAssetHistoryService:
)
candidates: dict[str, AssetHistoryCandidate] = {}
if asset_id_value:
if asset_id_variants:
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=asset_id_value,
asset_id_variants=asset_id_variants,
asset_name=asset_name_value,
match_reason="asset_id",
)
@@ -135,7 +184,7 @@ class FixedAssetHistoryService:
candidate = cls._build_candidate(
root=root,
file_path=os.path.join(current_root, filename),
asset_id=asset_id_value,
asset_id_variants=asset_id_variants,
asset_name=asset_name_value,
match_reason="asset_name_folder",
)
@@ -149,7 +198,7 @@ class FixedAssetHistoryService:
candidate = cls._build_candidate(
root=root,
file_path=entry.path,
asset_id=asset_id_value,
asset_id_variants=asset_id_variants,
asset_name=asset_name_value,
match_reason="asset_name_file",
)
@@ -191,7 +240,7 @@ class FixedAssetHistoryService:
*,
root: str,
file_path: str,
asset_id: str,
asset_id_variants: tuple[str, ...],
asset_name: str,
match_reason: str,
) -> AssetHistoryCandidate | None:
@@ -210,7 +259,7 @@ class FixedAssetHistoryService:
if not searchable_text:
return None
if asset_id and asset_id not in searchable_text:
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":
return None
@@ -218,7 +267,7 @@ class FixedAssetHistoryService:
score = cls._score_candidate(
searchable_text=searchable_text,
filename=cls._normalize_keyword(filename),
asset_id=asset_id,
asset_id_variants=asset_id_variants,
asset_name=asset_name,
match_reason=match_reason,
)
@@ -243,16 +292,19 @@ class FixedAssetHistoryService:
*,
searchable_text: str,
filename: str,
asset_id: str,
asset_id_variants: tuple[str, ...],
asset_name: str,
match_reason: str,
) -> int:
score = 0
if asset_id:
if asset_id in filename:
score += 120
elif asset_id in searchable_text:
score += 80
if asset_id_variants:
for index, asset_id in enumerate(asset_id_variants):
exact_bonus = 120 if index == 0 else 100
fuzzy_bonus = 80 if index == 0 else 60
if asset_id in filename:
score = max(score, exact_bonus)
elif asset_id in searchable_text:
score = max(score, fuzzy_bonus)
if asset_name:
if asset_name in filename:
score += 60
@@ -277,7 +329,10 @@ class FixedAssetHistoryService:
尝试用配置的账号密码建立共享盘会话。
账号密码只从环境变量读取,避免把敏感信息写进仓库。
Linux 测试/生产环境应优先通过挂载目录访问共享盘,此处仅作为 Windows UNC 共享盘的兜底建连。
"""
if os.name != 'nt':
return
username = cls._get_runtime_setting('FIXED_ASSET_HISTORY_SHARE_USERNAME').strip()
password = cls._get_runtime_setting('FIXED_ASSET_HISTORY_SHARE_PASSWORD')
if not username or not password: