Merge branch 'dev' into picture-feedback
This commit is contained in:
commit
34ad40778b
180 files changed
+13262
-5333
No files matched your search
@@ -4,10 +4,12 @@ backend/app/services/voucher_check/dms_client.py
|
||||
用途:销售开票附件文档库客户端(登录、列目录、建文件夹、上传、复制;不移动原件)。
|
||||
依赖:requests、Flask 配置;登录接口与合同库相同。
|
||||
逻辑:LibraryID 复用文档库;根 FolderID 必须是销售开票附件。上传走 Attachment/Upload + Item/CreateFiles;归档复制同名跳过。
|
||||
文档库复制后 LeafName 写成「原名.pdf(重命名)」,扩展名另存。改回「原名.pdf」会被判同名,必须改成不含扩展名的「原名」;多余副本用 Item/Recycle 删除。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from datetime import date
|
||||
@@ -23,6 +25,65 @@ _LOCK = threading.Lock()
|
||||
_CACHE: dict[str, Any] = {"session": None, "created_at": 0.0}
|
||||
|
||||
ARCHIVE_ROOT_LEAF = "所有归档"
|
||||
# 文档库同名冲突或复制后自动改名。标记前后可能有空格,后面有时还有 (1)。
|
||||
_DMS_RENAME_RE = re.compile(
|
||||
r"\s*[((]\s*重命名\s*[))]\s*(?:\(\s*\d+\s*\)|(\s*\d+\s*))?"
|
||||
)
|
||||
|
||||
|
||||
def canonical_leaf_name(name: str) -> str:
|
||||
"""
|
||||
去掉文档库自动加上的「重命名」标记,还原业务文件名。
|
||||
核心逻辑:`发票.pdf(重命名).pdf` 或带空格的 `发票.pdf (重命名) .pdf` 去掉标记后会多出一个扩展名,再折成 `发票.pdf`。
|
||||
"""
|
||||
text = str(name or "").strip()
|
||||
cleaned = _DMS_RENAME_RE.sub("", text)
|
||||
if cleaned == text:
|
||||
return text
|
||||
return re.sub(r"(\.[A-Za-z0-9]{1,8})\s*\1$", r"\1", cleaned).strip()
|
||||
|
||||
|
||||
def dms_stored_leaf_name(filename: str) -> str:
|
||||
"""
|
||||
算出文档库改名接口要写入的 LeafName。
|
||||
核心逻辑:库里真实名字是「发票.pdf(重命名)」,扩展名在附件上。改成「发票.pdf」会和自己判成同名;改成「发票」后界面仍显示「发票.pdf」。
|
||||
"""
|
||||
name = str(filename or "").strip()
|
||||
suffix = Path(name).suffix
|
||||
if suffix and name.lower().endswith(suffix.lower()) and len(name) > len(suffix):
|
||||
return name[: -len(suffix)]
|
||||
return name
|
||||
|
||||
|
||||
def row_display_name(row: dict[str, Any]) -> str:
|
||||
"""
|
||||
还原文档库界面上的文件名。
|
||||
核心逻辑:LeafName 没有以附件扩展名结尾时,界面会把扩展名补在后面。
|
||||
"""
|
||||
leaf = str((row or {}).get("LeafName") or "").strip()
|
||||
attachment = (row or {}).get("MainFileAttachment") or {}
|
||||
if not isinstance(attachment, dict):
|
||||
attachment = {}
|
||||
ext = str(attachment.get("Extension") or "").strip()
|
||||
if ext and not ext.startswith("."):
|
||||
ext = f".{ext}"
|
||||
if ext and leaf and not leaf.lower().endswith(ext.lower()):
|
||||
return f"{leaf}{ext}"
|
||||
return leaf
|
||||
|
||||
|
||||
def leaf_names_match(actual: str, wanted: str) -> bool:
|
||||
"""
|
||||
判断目录里的文件名是否就是要找的那一份。
|
||||
核心逻辑:完全一致,或实际名字只是想要的名字加上了文档库「重命名」。
|
||||
"""
|
||||
left = str(actual or "").strip()
|
||||
right = str(wanted or "").strip()
|
||||
if not left or not right:
|
||||
return False
|
||||
if left == right:
|
||||
return True
|
||||
return canonical_leaf_name(left) == right
|
||||
|
||||
|
||||
def _extract_item_id(payload: dict[str, Any]) -> str:
|
||||
@@ -265,14 +326,22 @@ class SalesDmsClient:
|
||||
|
||||
@classmethod
|
||||
def find_child_file(cls, *, parent_folder_id: str, leaf_name: str) -> dict[str, Any] | None:
|
||||
"""在父目录下按 LeafName 找非文件夹条目,找不到返回 None。"""
|
||||
"""
|
||||
在父目录下按 LeafName 找非文件夹条目,找不到返回 None。
|
||||
核心逻辑:优先完全同名。库内名字可能是「原名.pdf(重命名)」或已改回的「原名」,都按界面显示名认成同一份。
|
||||
"""
|
||||
wanted = str(leaf_name or "").strip()
|
||||
if not wanted:
|
||||
return None
|
||||
renamed: dict[str, Any] | None = None
|
||||
for row in cls.list_files(folder_id=parent_folder_id):
|
||||
if str(row.get("LeafName") or "").strip() == wanted:
|
||||
leaf = str(row.get("LeafName") or "").strip()
|
||||
shown = row_display_name(row)
|
||||
if leaf == wanted or shown == wanted:
|
||||
return row
|
||||
return None
|
||||
if renamed is None and (leaf_names_match(leaf, wanted) or leaf_names_match(shown, wanted)):
|
||||
renamed = row
|
||||
return renamed
|
||||
|
||||
@classmethod
|
||||
def resolve_period_folder(cls, *, period_month: date) -> tuple[str | None, str | None]:
|
||||
@@ -387,6 +456,121 @@ class SalesDmsClient:
|
||||
failure_message="文档库复制文件失败",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def rename_item(cls, *, item_id: str, leaf_name: str) -> dict[str, Any]:
|
||||
"""
|
||||
把文档库条目改成指定文件名。
|
||||
核心逻辑:POST Item/Rename,与文档库列表「重命名」一致,只改 LeafName、不另存一份。
|
||||
"""
|
||||
item = str(item_id or "").strip()
|
||||
wanted = str(leaf_name or "").strip()
|
||||
if not item:
|
||||
raise ValidationError("重命名条目不能为空", code="VOUCHER_CHECK.CONFIG_INVALID")
|
||||
if not wanted:
|
||||
raise ValidationError("重命名文件名不能为空", code="VOUCHER_CHECK.CONFIG_INVALID")
|
||||
return cls._post_json(
|
||||
"/Api/_api/APS/Public/Item/Rename",
|
||||
{"ItemID": item, "LeafName": wanted},
|
||||
timeout=30,
|
||||
failure_code="VOUCHER_CHECK.ARCHIVE_FAILED",
|
||||
failure_message="文档库去掉重命名后缀失败",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _restore_exact_leaf(
|
||||
cls,
|
||||
*,
|
||||
parent_folder_id: str,
|
||||
wanted: str,
|
||||
row: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
所有归档要求界面上不带「(重命名)」。
|
||||
核心逻辑:先确认这份就是目标文件。改名只写不含扩展名的 LeafName;写成「原名.pdf」会和「原名.pdf(重命名)」判成同名。
|
||||
"""
|
||||
leaf = str(row.get("LeafName") or "").strip()
|
||||
shown = row_display_name(row)
|
||||
target = dms_stored_leaf_name(wanted)
|
||||
if leaf in {wanted, target} or shown == wanted:
|
||||
return row
|
||||
if canonical_leaf_name(leaf) != wanted and canonical_leaf_name(shown) != wanted:
|
||||
raise ServiceUnavailableError(
|
||||
"目标目录文件名与归档文件名不一致",
|
||||
code="VOUCHER_CHECK.ARCHIVE_FAILED",
|
||||
details={"leaf_name": wanted, "actual_name": leaf},
|
||||
)
|
||||
cls.rename_item(item_id=str(row.get("ID") or ""), leaf_name=target)
|
||||
for item in cls.list_files(folder_id=parent_folder_id):
|
||||
item_leaf = str(item.get("LeafName") or "").strip()
|
||||
if item_leaf in {wanted, target} or row_display_name(item) == wanted:
|
||||
return item
|
||||
raise ServiceUnavailableError(
|
||||
"去掉重命名后缀后未找到原文件名",
|
||||
code="VOUCHER_CHECK.ARCHIVE_FAILED",
|
||||
details={"leaf_name": wanted},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def recycle_items(cls, *, item_ids: list[str]) -> dict[str, Any]:
|
||||
"""
|
||||
按文档库列表「删除」把条目放进回收站。
|
||||
核心逻辑:POST Item/Recycle,ItemID 为数组;不调用彻底删除,误删可从回收站恢复。
|
||||
"""
|
||||
ids = [str(item_id).strip() for item_id in item_ids if str(item_id).strip()]
|
||||
if not ids:
|
||||
raise ValidationError("删除条目不能为空", code="VOUCHER_CHECK.CONFIG_INVALID")
|
||||
return cls._post_json(
|
||||
"/Api/_api/APS/Public/Item/Recycle",
|
||||
{"ItemID": ids},
|
||||
timeout=30,
|
||||
failure_code="VOUCHER_CHECK.ARCHIVE_FAILED",
|
||||
failure_message="文档库删除多余重命名文件失败",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _drop_extra_renamed_copies(
|
||||
cls,
|
||||
*,
|
||||
parent_folder_id: str,
|
||||
wanted: str,
|
||||
keep_item_id: str,
|
||||
) -> None:
|
||||
"""
|
||||
删掉同一目录里多出来的「(重命名)」副本。
|
||||
核心逻辑:只删文件名还原后等于目标原名、且仍带重命名标记的条目;原名和其他文件不动。
|
||||
"""
|
||||
keep = str(keep_item_id or "").strip()
|
||||
extras: list[str] = []
|
||||
for row in cls.list_files(folder_id=parent_folder_id):
|
||||
item_id = str(row.get("ID") or "").strip()
|
||||
leaf = str(row.get("LeafName") or "").strip()
|
||||
shown = row_display_name(row)
|
||||
if not item_id or item_id == keep or ("重命名" not in leaf and "重命名" not in shown):
|
||||
continue
|
||||
if canonical_leaf_name(leaf) == wanted or canonical_leaf_name(shown) == wanted:
|
||||
extras.append(item_id)
|
||||
if extras:
|
||||
cls.recycle_items(item_ids=extras)
|
||||
|
||||
@classmethod
|
||||
def _settle_archived_file(
|
||||
cls,
|
||||
*,
|
||||
parent_folder_id: str,
|
||||
wanted: str,
|
||||
row: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
归档落盘后收成一个界面上不带后缀的原名。
|
||||
核心逻辑:先删掉其余「(重命名)」副本,再把留下的那份改成不含扩展名的 LeafName。先改名会被文档库判成同名。
|
||||
"""
|
||||
cls._drop_extra_renamed_copies(
|
||||
parent_folder_id=parent_folder_id,
|
||||
wanted=wanted,
|
||||
keep_item_id=str(row.get("ID") or "").strip(),
|
||||
)
|
||||
return cls._restore_exact_leaf(parent_folder_id=parent_folder_id, wanted=wanted, row=row)
|
||||
|
||||
@classmethod
|
||||
def copy_file_skip_same_name(
|
||||
cls,
|
||||
@@ -397,10 +581,10 @@ class SalesDmsClient:
|
||||
) -> tuple[dict[str, Any], bool]:
|
||||
"""
|
||||
复制文件到目标目录;目标已有同名文件则跳过、不覆盖。
|
||||
核心逻辑:先按 LeafName 查目标;没有再 Copy。Copy 报同名或复制后已能查到,一律视为跳过成功。
|
||||
核心逻辑:先按 LeafName 查目标。已有原名则跳过复制;只有「(重命名)」副本时改回原名。没有再 Copy。收尾时同一目录多出来的该后缀副本删除。
|
||||
|
||||
Returns:
|
||||
(目标条目, skipped):skipped=True 表示未发起覆盖,沿用已有同名文件。
|
||||
(目标条目, skipped):skipped=True 表示未发起覆盖,沿用已有文件(必要时已去掉重命名后缀并删除多余副本)。
|
||||
"""
|
||||
wanted = str(leaf_name or "").strip()
|
||||
source = str(source_item_id or "").strip()
|
||||
@@ -414,18 +598,21 @@ class SalesDmsClient:
|
||||
|
||||
existing = cls.find_child_file(parent_folder_id=dest, leaf_name=wanted)
|
||||
if existing:
|
||||
return existing, True
|
||||
return cls._settle_archived_file(parent_folder_id=dest, wanted=wanted, row=existing), True
|
||||
|
||||
try:
|
||||
cls.copy_items(item_ids=[source], dest_folder_id=dest)
|
||||
except ServiceUnavailableError as exc:
|
||||
existing = cls.find_child_file(parent_folder_id=dest, leaf_name=wanted)
|
||||
if existing:
|
||||
return existing, True
|
||||
return cls._settle_archived_file(parent_folder_id=dest, wanted=wanted, row=existing), True
|
||||
if _is_name_conflict(exc.message):
|
||||
existing = cls.find_child_file(parent_folder_id=dest, leaf_name=wanted)
|
||||
if existing:
|
||||
return existing, True
|
||||
return (
|
||||
cls._settle_archived_file(parent_folder_id=dest, wanted=wanted, row=existing),
|
||||
True,
|
||||
)
|
||||
raise
|
||||
|
||||
dest_row = cls.find_child_file(parent_folder_id=dest, leaf_name=wanted)
|
||||
@@ -435,7 +622,7 @@ class SalesDmsClient:
|
||||
code="VOUCHER_CHECK.ARCHIVE_FAILED",
|
||||
details={"leaf_name": wanted},
|
||||
)
|
||||
return dest_row, False
|
||||
return cls._settle_archived_file(parent_folder_id=dest, wanted=wanted, row=dest_row), False
|
||||
|
||||
@classmethod
|
||||
def ensure_period_type_folder(cls, *, period_month: date, type_folder: str) -> str:
|
||||
|
||||
Reference in new issue
Block a user