refactor: 收口跨平台重复业务实现

This commit is contained in:
yml
2026-11-13 20:35:38 +08:00
parent ad782e1954
commit f0abca3d58
25 changed files with 1386 additions and 724 deletions
+19 -88
View File
@@ -34,6 +34,14 @@ from ..models import (
User,
)
from .audit_service import record_audit
from .collab_common import apply_order_visibility, count_archived
from .douyu_goods_service import (
serialize_live_goods as _serialize_live_goods,
)
from .douyu_goods_service import (
serialized_snapshots,
upsert_goods_snapshot,
)
from .file_service import build_file_url
# ---------------------------------------------------------------------------
@@ -93,7 +101,6 @@ STATUS_TIMESTAMP_FIELDS = {
STATUS_DONE: "done_at",
}
ARCHIVE_SCOPES = ("active", "archived", "all")
ARCHIVABLE_STATUSES = (STATUS_DONE,)
# 同一账号核账只统计“还没有完成兑换”的单:done/scripted 不重复占积分。
POINTS_GROUP_STATUSES: tuple[str, ...] = (
@@ -249,34 +256,7 @@ def _client_for_account(account: Account) -> DouyuActivityClient:
def serialize_live_goods(goods: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""把 storehome 原始商品映射成前端稳定字段。"""
result: list[dict[str, Any]] = []
for raw in goods:
if not isinstance(raw, dict):
continue
commodity_id = _text(
_first_value(raw, "commodityId", "commodity_id"), max_length=64
)
name = _text(_first_value(raw, "commodityName", "name"), max_length=256)
open_status = _to_int(_first_value(raw, "openStatus", "open_status"))
mark = _text(_first_value(raw, "mark"), max_length=32)
status_value = _first_value(raw, "status")
result.append(
{
"commodity_id": commodity_id,
"name": name,
"score": _to_int(_first_value(raw, "score")),
"status": str(status_value) if status_value is not None else "",
"open_status": open_status,
"start_time": _to_int(_first_value(raw, "startTime", "start_time")),
"end_time": _to_int(_first_value(raw, "endTime", "end_time")),
"mark": mark,
"count": _to_int(_first_value(raw, "count")),
"elite_limited": _to_int(_first_value(raw, "eliteLimited")),
"is_limited": bool(mark) or open_status == 0,
"raw": _json_safe(raw),
}
)
return result
return _serialize_live_goods(goods)
def _probe_with_client(
@@ -1658,27 +1638,7 @@ def refresh_activity_goods(
manual_id=str(config.manual_id or ""), rid=str(config.rid or "")
)
goods = result.get("goods") or []
now = _now()
for raw in goods:
commodity_id = _text(
raw.get("commodityId") or raw.get("commodity_id"), max_length=64
)
if not commodity_id:
continue
row = (
db.query(DouyuGoodsSnapshot)
.filter(DouyuGoodsSnapshot.commodity_id == commodity_id)
.first()
)
if row is None:
row = DouyuGoodsSnapshot(commodity_id=commodity_id)
db.add(row)
row.name = _text(raw.get("commodityName") or raw.get("name"), max_length=256)
row.score = _to_int(raw.get("score"))
status_value = _first_value(raw, "status")
row.status = str(status_value) if status_value is not None else ""
row.raw = _json_safe(raw)
row.updated_at = now
upsert_goods_snapshot(db, goods, commit=False)
record_audit(
db,
current,
@@ -1691,31 +1651,7 @@ def refresh_activity_goods(
def serialized_goods(db: Session) -> list[dict[str, Any]]:
rows = db.query(DouyuGoodsSnapshot).order_by(DouyuGoodsSnapshot.id.asc()).all()
result: list[dict[str, Any]] = []
for row in rows:
raw = row.raw or {}
start = _to_int(_first_value(raw, "startTime", "start_time"))
end = _to_int(_first_value(raw, "endTime", "end_time"))
open_status = _to_int(_first_value(raw, "openStatus", "open_status"))
mark = _text(_first_value(raw, "mark"), max_length=32)
result.append(
{
"commodity_id": row.commodity_id,
"name": row.name,
"score": row.score,
"status": row.status,
"open_status": open_status,
"start_time": start,
"end_time": end,
"mark": mark,
"count": _to_int(_first_value(raw, "count")),
"elite_limited": _to_int(_first_value(raw, "eliteLimited")),
"is_limited": bool(mark) or open_status == 0,
"raw": raw,
}
)
return result
return serialized_snapshots(db)
def _metric_views(
@@ -1904,13 +1840,13 @@ def _apply_order_filters(
keyword: str = "",
ck_status: str = "",
):
scope = archive_scope if archive_scope in ARCHIVE_SCOPES else "active"
if scope == "active":
query = query.filter(DouyuExternalOrder.archived_at.is_(None))
elif scope == "archived":
query = query.filter(DouyuExternalOrder.archived_at.isnot(None))
if not include_void:
query = query.filter(DouyuExternalOrder.status != STATUS_VOID)
query = apply_order_visibility(
query,
DouyuExternalOrder,
archive_scope=archive_scope,
include_void=include_void,
void_status=STATUS_VOID,
)
if _text(status):
query = query.filter(DouyuExternalOrder.status == _text(status))
if _text(shop):
@@ -2082,12 +2018,7 @@ def summary(
"total": total,
"by_status": by_status,
"ck_invalid": int(ck_invalid),
"archived_total": (
db.query(func.count(DouyuExternalOrder.id))
.filter(DouyuExternalOrder.archived_at.isnot(None))
.scalar()
or 0
),
"archived_total": count_archived(db, DouyuExternalOrder),
"account_missing": (
db.query(func.count(DouyuExternalOrder.id))
.filter(