feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则

This commit is contained in:
hugo committed 2026-07-20 19:28:17 +08:00
1 parent 88408490d7
commit 34d489a2d1
167 files changed
+10365 -7978

No files matched your search

@@ -226,6 +226,44 @@ def create_draft_from_existing(template_id: int, *, actor_id: int | None) -> dic
return _payload(item)
def clone_as_new_template(template_id: int, data: dict[str, Any], *, actor_id: int | None) -> dict[str, Any]:
"""复制任意已有模板为独立模板草稿,新编码从 v1 开始且不影响来源模板。"""
source = db.session.get(ShippingExcelTemplate, template_id)
if source is None:
raise NotFoundError('Excel template not found', code='SHIPPING.EXCEL_TEMPLATE_NOT_FOUND')
code = str(data.get('template_code') or '').strip().upper()
name = str(data.get('display_name') or '').strip()
if not code or not name or not re.fullmatch(r'[A-Z0-9_-]{2,64}', code):
raise ValidationError('template_code and display_name are invalid', code='SHIPPING.EXCEL_TEMPLATE_INVALID')
if ShippingExcelTemplate.query.filter_by(template_code=code).count():
raise ConflictError('Template code already exists', code='SHIPPING.EXCEL_TEMPLATE_CODE_EXISTS')
raw = file_asset_storage.get_bytes(source.storage_key)
if hashlib.sha256(raw).hexdigest() != source.file_checksum:
raise ValidationError('Template asset is missing or changed', code='SHIPPING.EXCEL_TEMPLATE_ASSET_INVALID')
storage_key = _storage_key(code, 1)
file_asset_storage.upload_bytes(
storage_key=storage_key, data=raw,
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
)
item = ShippingExcelTemplate(
template_code=code, display_name=name, version=1, status='draft',
storage_provider='minio', storage_bucket=file_asset_storage.bucket_name, storage_key=storage_key,
original_filename=source.original_filename, file_checksum=hashlib.sha256(raw).hexdigest(),
definition_json=json.loads(json.dumps(source.definition_json or {}, ensure_ascii=False)), created_by=actor_id,
)
try:
db.session.add(item)
db.session.commit()
except Exception:
db.session.rollback()
try:
file_asset_storage.delete_object(storage_key)
except Exception:
pass
raise
return _payload(item)
def _validate_definition(item: ShippingExcelTemplate) -> dict[str, Any]:
"""验证模板定义、实际工作表和稳定字段引用,返回规范化定义。"""
definition = item.definition_json if isinstance(item.definition_json, dict) else {}