# Conflicts: # backend/app/services/schedule/job_options.py # frontend/src/locales/th/admin.json # frontend/src/locales/zh-TW/admin.json
131 lines
4.5 KiB
Python
131 lines
4.5 KiB
Python
"""
|
||
backend/app/services/schedule/job_options.py
|
||
|
||
用途:平台任务参数 schema 的动态选项来源(options_source → 统一 label/value 列表)。
|
||
依赖:models.Agent、shipping_notice.inventory_source_repository。
|
||
逻辑:api/schedule/platform_tasks.py options 接口与 job_params 选项范围校验调用;
|
||
前端只认识 options_source 字符串,不感知数据来自哪张表/哪个配置文件。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any, Callable
|
||
|
||
from app import db
|
||
|
||
|
||
def _agent_apps_options() -> list[dict[str, Any]]:
|
||
"""启用中的 Agent 应用列表(value=agents.id)。"""
|
||
from app.models import Agent
|
||
|
||
agents = (
|
||
Agent.query.filter(Agent.is_active.is_(True))
|
||
.order_by(Agent.sort_order.asc(), Agent.id.asc())
|
||
.all()
|
||
)
|
||
return [
|
||
{
|
||
"label": agent.name,
|
||
"value": agent.id,
|
||
"description": (agent.description or "")[:120] or None,
|
||
}
|
||
for agent in agents
|
||
]
|
||
|
||
|
||
def _shipping_inventory_sources_options() -> list[dict[str, Any]]:
|
||
"""启用中的货代库存源(value=source_code,来自数据库主数据)。"""
|
||
from app.services.shipping_notice.inventory_source_repository import list_database_inventory_sources
|
||
|
||
return [
|
||
{
|
||
"label": source.get("display_name") or source.get("source_code"),
|
||
"value": source.get("source_code"),
|
||
"description": (
|
||
f"{source.get('connector_type')} · "
|
||
f"{source.get('warehouse_resolution_mode')} · "
|
||
f"{source.get('fixed_warehouse_id') or '-'}"
|
||
),
|
||
}
|
||
for source in list_database_inventory_sources()
|
||
]
|
||
|
||
|
||
def _shipping_receipt_sources_options() -> list[dict[str, Any]]:
|
||
"""已启用且已配置收料报表的站点,供独立收料排程选择。"""
|
||
from app.models import ShippingInventorySource, ShippingReceiptReportConfig
|
||
|
||
rows = db.session.query(ShippingInventorySource, ShippingReceiptReportConfig).join(
|
||
ShippingReceiptReportConfig, ShippingReceiptReportConfig.inventory_source_id == ShippingInventorySource.id,
|
||
).filter(ShippingInventorySource.enabled.is_(True)).all()
|
||
return [{
|
||
'label': source.display_name or source.source_code, 'value': source.source_code,
|
||
'description': f'{source.connector_type} · 收料已配置',
|
||
} for source, _config in rows]
|
||
|
||
|
||
def _hxops_users_options() -> list[dict[str, Any]]:
|
||
"""全部启用用户(value=users.id);订单交期回复未回复提醒收件人等场景用。"""
|
||
from app.models import User
|
||
|
||
users = User.query.filter(User.status == "active").order_by(User.real_name.asc(), User.id.asc()).all()
|
||
return [
|
||
{
|
||
"label": f"{u.real_name or u.username}({u.employee_id or '-'})",
|
||
"value": u.id,
|
||
"description": u.department or None,
|
||
}
|
||
for u in users
|
||
]
|
||
|
||
|
||
def _users_options() -> list[dict[str, Any]]:
|
||
"""启用中的用户列表(value=user.id),供排程通知人类参数选择。"""
|
||
from app.models import User
|
||
|
||
users = (
|
||
User.query.filter(User.status == "active")
|
||
.order_by(User.username.asc())
|
||
.all()
|
||
)
|
||
return [
|
||
{
|
||
"label": user.real_name or user.username,
|
||
"value": user.id,
|
||
"description": None if not user.wecom_userid else "wecom",
|
||
}
|
||
for user in users
|
||
]
|
||
|
||
|
||
# options_source 注册表:新增来源只需在此登记 provider,不动 API 层
|
||
_OPTIONS_PROVIDERS: dict[str, Callable[[], list[dict[str, Any]]]] = {
|
||
"agent_apps": _agent_apps_options,
|
||
"shipping_inventory_sources": _shipping_inventory_sources_options,
|
||
"shipping_receipt_sources": _shipping_receipt_sources_options,
|
||
"hxops_users": _hxops_users_options,
|
||
"users": _users_options,
|
||
}
|
||
|
||
|
||
def is_known_options_source(source: str) -> bool:
|
||
"""判断 options_source 是否已登记。"""
|
||
return str(source or "").strip() in _OPTIONS_PROVIDERS
|
||
|
||
|
||
def list_job_options(source: str) -> list[dict[str, Any]]:
|
||
"""
|
||
返回指定 options_source 的选项列表(统一 {label, value, description?} 结构)。
|
||
Args:
|
||
source: params_schema 中声明的 options_source 标识。
|
||
Returns:
|
||
选项列表;value 为 str/int,前端直接写入 job_params。
|
||
Raises:
|
||
ValueError: 未登记的 options_source。
|
||
"""
|
||
key = str(source or "").strip()
|
||
provider = _OPTIONS_PROVIDERS.get(key)
|
||
if not provider:
|
||
raise ValueError(f"Unknown options source: {source}")
|
||
return provider()
|