fix(douyu): 修正外部单兑换记录判定
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"""斗鱼外部单协作:账号导入、状态机、鱼翅/积分与兑换记录查询。"""
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
@@ -28,15 +30,32 @@ from web.backend.schemas import (
|
||||
from web.backend.services import douyu_collab_service as collab
|
||||
|
||||
|
||||
def exchange_record(
|
||||
*,
|
||||
name: str = "礼包A",
|
||||
commodity_id: str = "gift-a",
|
||||
occurred_at: datetime | None = None,
|
||||
) -> dict[str, str]:
|
||||
moment = occurred_at or datetime.now(UTC)
|
||||
return {
|
||||
"commodityId": commodity_id,
|
||||
"commodityName": name,
|
||||
"time": str(int(moment.timestamp())),
|
||||
}
|
||||
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, *, points=5000, fish=1000, records=None):
|
||||
self.points = points
|
||||
self.fish = fish
|
||||
self.records = records if records is not None else []
|
||||
self.elite_calls = 0
|
||||
self.gold_calls = 0
|
||||
self.records_calls = 0
|
||||
self.orders_calls = 0
|
||||
|
||||
def elite_user_info(self, **_):
|
||||
self.elite_calls += 1
|
||||
return {
|
||||
"manual_type": 1,
|
||||
"manual_score": self.points,
|
||||
@@ -65,6 +84,7 @@ class FakeClient:
|
||||
}
|
||||
|
||||
def gold_account(self):
|
||||
self.gold_calls += 1
|
||||
return {"gold": self.fish}
|
||||
|
||||
def list_goods(self, **_):
|
||||
@@ -223,6 +243,7 @@ class TestDouyuCollabOrders:
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包A",
|
||||
commodity_id="gift-a",
|
||||
required_points=100,
|
||||
home_images=["collab/unlink.png"],
|
||||
client=FakeClient(),
|
||||
@@ -950,7 +971,7 @@ class TestDouyuCollabOrders:
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=FakeClient(records=[{"commodityName": "礼包A"}]),
|
||||
client=FakeClient(records=[exchange_record()]),
|
||||
)
|
||||
assert result["verdict"] == collab.VERDICT_SUCCESS
|
||||
assert result["order"].status == collab.STATUS_DONE
|
||||
@@ -1395,7 +1416,7 @@ class TestDouyuCollabOrders:
|
||||
assert order.status == collab.STATUS_VOID
|
||||
assert order.void_reason == "资料不全"
|
||||
|
||||
def test_points_shortage_enters_points_error(self):
|
||||
def test_record_query_does_not_change_status_from_points_snapshot(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
@@ -1422,8 +1443,8 @@ class TestDouyuCollabOrders:
|
||||
order,
|
||||
client=FakeClient(points=50, records=[]),
|
||||
)
|
||||
assert result["points_reason"] == "shortage"
|
||||
assert result["order"].status == collab.STATUS_POINTS_ERROR
|
||||
assert result["verdict"] == collab.VERDICT_UNKNOWN
|
||||
assert result["order"].status == collab.STATUS_RECHARGED
|
||||
|
||||
def test_balance_query_skips_records_and_never_changes_status(self):
|
||||
order = collab.create_order(
|
||||
@@ -1446,7 +1467,7 @@ class TestDouyuCollabOrders:
|
||||
)
|
||||
order.points_threshold = 10
|
||||
self.session.commit()
|
||||
client = FakeClient(points=50, records=[{"commodityName": "礼包A"}])
|
||||
client = FakeClient(points=50, records=[exchange_record()])
|
||||
|
||||
result = collab.query_activity(
|
||||
self.session,
|
||||
@@ -1496,7 +1517,7 @@ class TestDouyuCollabOrders:
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=FakeClient(records=[{"commodityName": "礼包A"}]),
|
||||
client=FakeClient(records=[exchange_record()]),
|
||||
)
|
||||
assert record_result["order"].status == collab.STATUS_DONE
|
||||
|
||||
@@ -1515,6 +1536,247 @@ class TestDouyuCollabOrders:
|
||||
assert payload["last_records_matched"] == 1
|
||||
assert payload["last_metric_message"] == "仅查询鱼翅与积分,未查询兑换记录"
|
||||
|
||||
def test_record_query_only_calls_exchange_records(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-RECORD-ONLY",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包A",
|
||||
required_points=100,
|
||||
home_images=["collab/home-record-only.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_RECHARGED,
|
||||
reason="鱼翅已充值",
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_SCRIPTED,
|
||||
reason="脚本已上",
|
||||
)
|
||||
client = FakeClient(records=[exchange_record()])
|
||||
|
||||
result = collab.query_activity(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=client,
|
||||
)
|
||||
|
||||
assert result["verdict"] == collab.VERDICT_SUCCESS
|
||||
assert client.records_calls == 1
|
||||
assert client.elite_calls == 0
|
||||
assert client.gold_calls == 0
|
||||
assert client.orders_calls == 0
|
||||
|
||||
def test_record_query_paginates_until_short_page(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-RECORD-PAGES",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包A",
|
||||
commodity_id="gift-a",
|
||||
required_points=100,
|
||||
home_images=["collab/home-record-pages.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
records = [exchange_record() for _ in range(101)]
|
||||
|
||||
class PaginatedClient(FakeClient):
|
||||
def __init__(self):
|
||||
super().__init__(records=records)
|
||||
self.offsets: list[int] = []
|
||||
|
||||
def exchange_records(self, *, limit, offset, **_):
|
||||
self.offsets.append(offset)
|
||||
self.records_calls += 1
|
||||
return {"records": self.records[offset : offset + limit]}
|
||||
|
||||
client = PaginatedClient()
|
||||
loaded = collab._load_exchange_records_for_orders(
|
||||
self.session,
|
||||
[order],
|
||||
client=client,
|
||||
)
|
||||
|
||||
assert client.offsets == [0, 100]
|
||||
assert len(loaded) == 101
|
||||
|
||||
def test_record_query_rejects_old_record_and_wrong_commodity(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-RECORD-WINDOW",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包A",
|
||||
commodity_id="gift-a",
|
||||
required_points=100,
|
||||
home_images=["collab/home-record-window.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_RECHARGED,
|
||||
reason="鱼翅已充值",
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_SCRIPTED,
|
||||
reason="脚本已上",
|
||||
)
|
||||
old_record = exchange_record(occurred_at=datetime.now(UTC) - timedelta(days=1))
|
||||
wrong_commodity = exchange_record(
|
||||
commodity_id="gift-other",
|
||||
name="礼包A",
|
||||
)
|
||||
|
||||
result = collab.query_activity(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=FakeClient(records=[old_record, wrong_commodity]),
|
||||
)
|
||||
|
||||
assert result["verdict"] == collab.VERDICT_UNKNOWN
|
||||
assert result["metric"].records_matched == 0
|
||||
assert result["order"].status == collab.STATUS_SCRIPTED
|
||||
|
||||
def test_record_query_waits_for_settlement_before_failure(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-RECORD-SETTLE",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包A",
|
||||
required_points=100,
|
||||
home_images=["collab/home-record-settle.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_RECHARGED,
|
||||
reason="鱼翅已充值",
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_SCRIPTED,
|
||||
reason="脚本已上",
|
||||
)
|
||||
|
||||
waiting = collab.query_activity(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=FakeClient(records=[]),
|
||||
)
|
||||
assert waiting["verdict"] == collab.VERDICT_UNKNOWN
|
||||
assert "结算等待期" in waiting["message"]
|
||||
assert order.status == collab.STATUS_SCRIPTED
|
||||
|
||||
order.scripted_at = datetime.now(UTC) - timedelta(
|
||||
seconds=collab.ACTIVITY_RECORD_SETTLE_SECONDS + 1
|
||||
)
|
||||
self.session.commit()
|
||||
failed = collab.query_activity(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
client=FakeClient(records=[]),
|
||||
)
|
||||
assert failed["verdict"] == collab.VERDICT_FAILED
|
||||
assert failed["order"].status == collab.STATUS_FAILED
|
||||
|
||||
def test_batch_record_query_fetches_once_per_account(self, monkeypatch):
|
||||
first = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-BATCH-RECORD-1",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包A",
|
||||
commodity_id="gift-a",
|
||||
required_points=100,
|
||||
home_images=["collab/home-batch-record-1.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
second = collab.create_order(
|
||||
self.session,
|
||||
self.admin,
|
||||
order_no="DY-BATCH-RECORD-2",
|
||||
shop="店铺A",
|
||||
account_username="douyu-account-a",
|
||||
gift_name="礼包B",
|
||||
commodity_id="gift-b",
|
||||
required_points=200,
|
||||
home_images=["collab/home-batch-record-2.png"],
|
||||
client=FakeClient(),
|
||||
)
|
||||
for order in (first, second):
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_RECHARGED,
|
||||
reason="鱼翅已充值",
|
||||
)
|
||||
collab.set_status(
|
||||
self.session,
|
||||
self.admin,
|
||||
order,
|
||||
status=collab.STATUS_SCRIPTED,
|
||||
reason="脚本已上",
|
||||
)
|
||||
|
||||
calls: list[int] = []
|
||||
records = [
|
||||
exchange_record(name="礼包A", commodity_id="gift-a"),
|
||||
exchange_record(name="礼包B", commodity_id="gift-b"),
|
||||
]
|
||||
|
||||
def load_records(_db, account_orders, *, client=None):
|
||||
del client
|
||||
calls.append(int(account_orders[0].account_id or 0))
|
||||
return records
|
||||
|
||||
monkeypatch.setattr(
|
||||
collab,
|
||||
"_load_exchange_records_for_orders",
|
||||
load_records,
|
||||
)
|
||||
results = collab.query_activity_batch(
|
||||
self.session,
|
||||
self.admin,
|
||||
[first, second],
|
||||
)
|
||||
|
||||
assert len(calls) == 1
|
||||
assert [item["verdict"] for item in results] == [
|
||||
collab.VERDICT_SUCCESS,
|
||||
collab.VERDICT_SUCCESS,
|
||||
]
|
||||
assert first.status == collab.STATUS_DONE
|
||||
assert second.status == collab.STATUS_DONE
|
||||
|
||||
def test_draft_can_be_created_before_account_resolution(self):
|
||||
order = collab.create_order(
|
||||
self.session,
|
||||
|
||||
@@ -634,21 +634,16 @@ def query_external_orders_activity(
|
||||
current: User = Depends(require_permission("douyu:collab:edit")),
|
||||
):
|
||||
results: list[dict] = []
|
||||
orders = []
|
||||
for order_id in ids:
|
||||
try:
|
||||
order = collab.get_order(db, order_id)
|
||||
result = collab.query_activity(db, current, order, source="batch")
|
||||
results.append(
|
||||
{
|
||||
"id": order_id,
|
||||
"success": True,
|
||||
"verdict": result["verdict"],
|
||||
"message": result["message"],
|
||||
}
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
db.rollback()
|
||||
orders.append(collab.get_order(db, order_id))
|
||||
except Exception as exc: # noqa: BLE001 - 不存在的单逐行隔离
|
||||
results.append({"id": order_id, "success": False, "message": str(exc)})
|
||||
if orders:
|
||||
results.extend(collab.query_activity_batch(db, current, orders, source="batch"))
|
||||
order_index = {order_id: index for index, order_id in enumerate(ids)}
|
||||
results.sort(key=lambda item: order_index.get(int(item["id"]), len(ids)))
|
||||
if any(item["success"] for item in results):
|
||||
_publish_change(current, scope="orders", action="order.activity_batch")
|
||||
return {"results": results, "success": all(item["success"] for item in results)}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
1. **一行 = 一单 = 一个内部斗鱼账号**。协作单只存 ``account_id`` 和用户名快照,
|
||||
**不复制 CK**;所有查询实时读 ``accounts.cookie``,账号换 CK 后自动生效。
|
||||
2. 鱼翅和手册积分是两笔资产:先充值鱼翅,再用鱼翅赠送精英令换积分。
|
||||
查询时两笔都要返回,核账只看手册积分。
|
||||
余额查询单独返回两笔资产,兑换记录查询不混查余额。
|
||||
3. 同一斗鱼账号允许挂多单;积分核账按账号合并计算所有未作废订单的礼包积分。
|
||||
4. 兑换是否成功只看当前账号的 ``exchangeList``。``getRecordList`` 是全站动态,
|
||||
不能拿来判断某一单。
|
||||
@@ -16,8 +16,9 @@ from __future__ import annotations
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from concurrent.futures import TimeoutError as FutureTimeoutError
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
from unicodedata import normalize as unicode_normalize
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import func, or_
|
||||
@@ -70,10 +71,7 @@ from .douyu_account_probe import (
|
||||
persist_probe_result,
|
||||
probe_account,
|
||||
)
|
||||
from .douyu_activity_query import (
|
||||
query_exchange_orders,
|
||||
query_exchange_records,
|
||||
)
|
||||
from .douyu_activity_query import query_exchange_records
|
||||
from .douyu_goods_service import (
|
||||
serialize_live_goods as _serialize_live_goods,
|
||||
)
|
||||
@@ -203,6 +201,12 @@ POINTS_GROUP_STATUSES: tuple[str, ...] = (
|
||||
|
||||
POINTS_THRESHOLD_DEFAULT = 100
|
||||
POINTS_THRESHOLD_MAX = 1_000_000
|
||||
ACTIVITY_RECORD_SETTLE_SECONDS = 300
|
||||
ACTIVITY_RECORD_TIME_SKEW_SECONDS = 60
|
||||
ACTIVITY_RECORD_FETCH_LIMIT = 100
|
||||
ACTIVITY_RECORD_MAX_PAGES = 5
|
||||
# 兑换记录最多翻 5 页,但指标快照只保留必要样本,避免 JSON 列按订单放大存储。
|
||||
ACTIVITY_METRIC_RECORD_LIMIT = 50
|
||||
MAX_IMAGES = 20
|
||||
CK_EXPORT_BATCH_MAX = 50
|
||||
CK_EXPORT_ACTION = "douyu:collab:ck_export"
|
||||
@@ -2643,19 +2647,78 @@ def set_status(
|
||||
return order
|
||||
|
||||
|
||||
def _match_gift(records: list[dict[str, Any]], gift_name: str) -> int:
|
||||
name = _text(gift_name)
|
||||
if len(name) < 2:
|
||||
return 0
|
||||
matched = 0
|
||||
for record in records:
|
||||
record_name = _text(
|
||||
record.get("commodityName") or record.get("commodity_name") or ""
|
||||
def _normalize_exchange_name(value: Any) -> str:
|
||||
return "".join(unicode_normalize("NFKC", _text(value)).casefold().split())
|
||||
|
||||
|
||||
def _exchange_record_time(record: Mapping[str, Any]) -> datetime | None:
|
||||
raw = (
|
||||
record.get("time")
|
||||
or record.get("exchangeTime")
|
||||
or record.get("exchange_time")
|
||||
or record.get("createTime")
|
||||
or record.get("create_time")
|
||||
)
|
||||
if raw is None or raw == "":
|
||||
return None
|
||||
text = str(raw).strip()
|
||||
try:
|
||||
timestamp = float(text)
|
||||
except (TypeError, ValueError):
|
||||
try:
|
||||
parsed = datetime.fromisoformat(text)
|
||||
except ValueError:
|
||||
return None
|
||||
return (
|
||||
parsed.replace(tzinfo=UTC)
|
||||
if parsed.tzinfo is None
|
||||
else parsed.astimezone(UTC)
|
||||
)
|
||||
if not record_name or len(record_name) < 2:
|
||||
if timestamp > 10_000_000_000:
|
||||
timestamp /= 1000
|
||||
try:
|
||||
return datetime.fromtimestamp(timestamp, UTC)
|
||||
except (OverflowError, OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _matching_exchange_records(
|
||||
records: Sequence[Mapping[str, Any]],
|
||||
order: DouyuExternalOrder,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""按商品 ID、精确名称和上脚本时间匹配,避免历史记录串单。"""
|
||||
expected_cid = _text(order.commodity_id)
|
||||
expected_name = _normalize_exchange_name(order.gift_name)
|
||||
not_before = order.scripted_at
|
||||
if isinstance(not_before, datetime):
|
||||
not_before = not_before - timedelta(seconds=ACTIVITY_RECORD_TIME_SKEW_SECONDS)
|
||||
|
||||
matched: list[dict[str, Any]] = []
|
||||
for record in records:
|
||||
if not isinstance(record, Mapping):
|
||||
continue
|
||||
if name in record_name or record_name in name:
|
||||
matched += 1
|
||||
record_cid = _text(
|
||||
record.get("commodityId")
|
||||
or record.get("commodity_id")
|
||||
or record.get("goodsId")
|
||||
)
|
||||
if expected_cid and record_cid:
|
||||
identity_matched = record_cid == expected_cid
|
||||
else:
|
||||
record_name = _normalize_exchange_name(
|
||||
record.get("commodityName")
|
||||
or record.get("commodity_name")
|
||||
or record.get("goodsName")
|
||||
)
|
||||
identity_matched = bool(
|
||||
expected_name and record_name and record_name == expected_name
|
||||
)
|
||||
if not identity_matched:
|
||||
continue
|
||||
record_time = _exchange_record_time(record)
|
||||
if not_before is not None and (record_time is None or record_time < not_before):
|
||||
continue
|
||||
matched.append(dict(record))
|
||||
return matched
|
||||
|
||||
|
||||
@@ -2674,17 +2737,15 @@ def group_points_total(db: Session, order: DouyuExternalOrder) -> int:
|
||||
return sum(int(item.required_points or 0) for item in rows)
|
||||
|
||||
|
||||
def query_activity(
|
||||
def _load_exchange_records_for_orders(
|
||||
db: Session,
|
||||
current: User,
|
||||
order: DouyuExternalOrder,
|
||||
orders: Sequence[DouyuExternalOrder],
|
||||
*,
|
||||
client: Any | None = None,
|
||||
source: str = "manual",
|
||||
include_records: bool = True,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""查询鱼翅、积分;``include_records`` 时同时查兑换记录并判定。"""
|
||||
) -> list[dict[str, Any]]:
|
||||
if not orders:
|
||||
return []
|
||||
order = orders[0]
|
||||
account = account_by_id(db, order.account_id) or account_by_username(
|
||||
db, order.account_username
|
||||
)
|
||||
@@ -2692,71 +2753,139 @@ def query_activity(
|
||||
raise CollabError("订单关联的斗鱼账号不存在", code="account_not_found")
|
||||
config = resolve_config(db)
|
||||
active_client = client or _client_for_account(account)
|
||||
probe = probe_account(
|
||||
account,
|
||||
config,
|
||||
client=active_client,
|
||||
session_checker=None,
|
||||
with_gold=True,
|
||||
with_elite_role=False,
|
||||
)
|
||||
elite = (probe.get("elite") or {}).get("raw") or {}
|
||||
gold = {
|
||||
"gold": (probe.get("gold") or {}).get("balance"),
|
||||
}
|
||||
errors = list((probe.get("errors") or {}).values())
|
||||
cutoffs = [
|
||||
item.scripted_at - timedelta(seconds=ACTIVITY_RECORD_TIME_SKEW_SECONDS)
|
||||
for item in orders
|
||||
if item.scripted_at is not None
|
||||
]
|
||||
not_before = min(cutoffs) if cutoffs else None
|
||||
records: list[dict[str, Any]] = []
|
||||
orders: list[dict[str, Any]] = []
|
||||
if include_records:
|
||||
try:
|
||||
records = query_exchange_records(
|
||||
active_client,
|
||||
manual_id=str(config.manual_id or ""),
|
||||
limit=100,
|
||||
offset=0,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors.append(f"兑换记录: {exc}")
|
||||
try:
|
||||
orders = query_exchange_orders(
|
||||
active_client,
|
||||
manual_id=str(config.manual_id or ""),
|
||||
page=1,
|
||||
page_size=10,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors.append(f"锁单: {exc}")
|
||||
for page in range(ACTIVITY_RECORD_MAX_PAGES):
|
||||
page_records = query_exchange_records(
|
||||
active_client,
|
||||
manual_id=str(config.manual_id or ""),
|
||||
limit=ACTIVITY_RECORD_FETCH_LIMIT,
|
||||
offset=page * ACTIVITY_RECORD_FETCH_LIMIT,
|
||||
)
|
||||
records.extend(page_records)
|
||||
if len(page_records) < ACTIVITY_RECORD_FETCH_LIMIT:
|
||||
break
|
||||
if not_before is not None:
|
||||
page_times = [
|
||||
record_time
|
||||
for record in page_records
|
||||
if (record_time := _exchange_record_time(record)) is not None
|
||||
]
|
||||
if page_times and max(page_times) < not_before:
|
||||
break
|
||||
return records
|
||||
|
||||
manual_score = _to_int(elite.get("manual_score"))
|
||||
fish = _to_int(gold.get("gold"))
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
probe,
|
||||
current=current,
|
||||
source="douyu_collab.query_activity",
|
||||
record_event=False,
|
||||
|
||||
def query_activity(
|
||||
db: Session,
|
||||
current: User,
|
||||
order: DouyuExternalOrder,
|
||||
*,
|
||||
client: Any | None = None,
|
||||
records: Sequence[Mapping[str, Any]] | None = None,
|
||||
record_query_error: str = "",
|
||||
source: str = "manual",
|
||||
include_records: bool = True,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""查兑换记录并判定;``include_records=False`` 时只刷新鱼翅和积分。"""
|
||||
account = account_by_id(db, order.account_id) or account_by_username(
|
||||
db, order.account_username
|
||||
)
|
||||
if manual_score is not None:
|
||||
order.points_balance = manual_score
|
||||
account.points = manual_score
|
||||
if fish is not None:
|
||||
order.fish_fin_balance = fish
|
||||
account.gold_balance = fish
|
||||
account.updated_at = _now()
|
||||
required_total = group_points_total(db, order)
|
||||
surplus = (
|
||||
(manual_score - required_total)
|
||||
if manual_score is not None and required_total > 0
|
||||
else None
|
||||
)
|
||||
order.points_required_total = required_total
|
||||
order.points_surplus = surplus
|
||||
order.points_checked_at = _now()
|
||||
threshold = normalize_threshold(order.points_threshold)
|
||||
order.points_threshold = threshold
|
||||
if account is None:
|
||||
raise CollabError("订单关联的斗鱼账号不存在", code="account_not_found")
|
||||
errors: list[str] = []
|
||||
probe: dict[str, Any] = {}
|
||||
loaded_records: list[dict[str, Any]] = []
|
||||
if include_records:
|
||||
if records is None:
|
||||
try:
|
||||
loaded_records = _load_exchange_records_for_orders(
|
||||
db, [order], client=client
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors.append(f"兑换记录: {exc}")
|
||||
else:
|
||||
loaded_records = [dict(record) for record in records]
|
||||
if record_query_error:
|
||||
errors.append(f"兑换记录: {record_query_error}")
|
||||
manual_score = _to_int(order.points_balance)
|
||||
fish = _to_int(order.fish_fin_balance)
|
||||
required_total = _to_int(order.points_required_total)
|
||||
surplus = _to_int(order.points_surplus)
|
||||
points_reason = _text(order.points_check_reason)
|
||||
points_message = _text(order.points_check_message)
|
||||
threshold = normalize_threshold(order.points_threshold)
|
||||
else:
|
||||
config = resolve_config(db)
|
||||
active_client = client or _client_for_account(account)
|
||||
probe = probe_account(
|
||||
account,
|
||||
config,
|
||||
client=active_client,
|
||||
session_checker=None,
|
||||
with_gold=True,
|
||||
with_elite_role=False,
|
||||
)
|
||||
elite = (probe.get("elite") or {}).get("raw") or {}
|
||||
gold = {
|
||||
"gold": (probe.get("gold") or {}).get("balance"),
|
||||
}
|
||||
errors = list((probe.get("errors") or {}).values())
|
||||
manual_score = _to_int(elite.get("manual_score"))
|
||||
fish = _to_int(gold.get("gold"))
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
probe,
|
||||
current=current,
|
||||
source="douyu_collab.query_activity",
|
||||
record_event=False,
|
||||
)
|
||||
if manual_score is not None:
|
||||
order.points_balance = manual_score
|
||||
account.points = manual_score
|
||||
if fish is not None:
|
||||
order.fish_fin_balance = fish
|
||||
account.gold_balance = fish
|
||||
account.updated_at = _now()
|
||||
required_total = group_points_total(db, order)
|
||||
surplus = (
|
||||
(manual_score - required_total)
|
||||
if manual_score is not None and required_total > 0
|
||||
else None
|
||||
)
|
||||
order.points_required_total = required_total
|
||||
order.points_surplus = surplus
|
||||
order.points_checked_at = _now()
|
||||
threshold = normalize_threshold(order.points_threshold)
|
||||
order.points_threshold = threshold
|
||||
points_reason = ""
|
||||
if surplus is not None:
|
||||
if surplus < 0:
|
||||
points_reason = "shortage"
|
||||
elif surplus > threshold:
|
||||
points_reason = "excess"
|
||||
else:
|
||||
points_reason = "ok"
|
||||
points_message = ""
|
||||
if required_total > 0 or manual_score is not None:
|
||||
points_message = (
|
||||
f"积分 {manual_score if manual_score is not None else '-'} / "
|
||||
f"本账号未完成订单合计 {required_total},结余 "
|
||||
f"{surplus if surplus is not None else '-'}"
|
||||
)
|
||||
order.points_check_reason = points_reason
|
||||
order.points_check_message = points_message
|
||||
|
||||
matched = 0
|
||||
matched_records: list[dict[str, Any]] = []
|
||||
now = _now()
|
||||
if not include_records:
|
||||
verdict = VERDICT_UNKNOWN
|
||||
message = ";".join(errors) if errors else "仅查询鱼翅与积分,未查询兑换记录"
|
||||
@@ -2767,41 +2896,37 @@ def query_activity(
|
||||
verdict = VERDICT_UNKNOWN
|
||||
message = "未填写礼包名,无法判定兑换结果"
|
||||
else:
|
||||
matched = _match_gift(records, order.gift_name)
|
||||
verdict = VERDICT_SUCCESS if matched else VERDICT_FAILED
|
||||
message = (
|
||||
f"兑换记录命中「{order.gift_name}」{matched} 条"
|
||||
if matched
|
||||
else f"兑换记录里没有「{order.gift_name}」"
|
||||
)
|
||||
|
||||
reason = ""
|
||||
if surplus is not None:
|
||||
if surplus < 0:
|
||||
reason = "shortage"
|
||||
elif surplus > threshold:
|
||||
reason = "excess"
|
||||
matched_records = _matching_exchange_records(loaded_records, order)
|
||||
matched = len(matched_records)
|
||||
if matched:
|
||||
verdict = VERDICT_SUCCESS
|
||||
message = f"兑换记录命中「{order.gift_name}」{matched} 条"
|
||||
elif order.status != STATUS_SCRIPTED:
|
||||
verdict = VERDICT_UNKNOWN
|
||||
message = "订单当前状态不再由兑换记录自动判定"
|
||||
elif order.scripted_at is None:
|
||||
verdict = VERDICT_UNKNOWN
|
||||
message = "订单尚未记录上脚本时间,暂不自动判定"
|
||||
elif now < order.scripted_at + timedelta(
|
||||
seconds=ACTIVITY_RECORD_SETTLE_SECONDS
|
||||
):
|
||||
verdict = VERDICT_UNKNOWN
|
||||
message = f"兑换记录暂未命中「{order.gift_name}」,仍在结算等待期"
|
||||
else:
|
||||
reason = "ok"
|
||||
points_message = ""
|
||||
if required_total > 0 or manual_score is not None:
|
||||
points_message = (
|
||||
f"积分 {manual_score if manual_score is not None else '-'} / "
|
||||
f"本账号未完成订单合计 {required_total},结余 "
|
||||
f"{surplus if surplus is not None else '-'}"
|
||||
)
|
||||
order.points_check_reason = reason
|
||||
order.points_check_message = points_message
|
||||
order.points_check_detail = {
|
||||
"fish_fin_balance": fish,
|
||||
"points_balance": manual_score,
|
||||
"required_total": required_total,
|
||||
"surplus": surplus,
|
||||
"threshold": threshold,
|
||||
"errors": errors,
|
||||
"exchange_records": len(records),
|
||||
"locked_orders": len(orders),
|
||||
}
|
||||
verdict = VERDICT_FAILED
|
||||
message = f"兑换记录里没有「{order.gift_name}」"
|
||||
|
||||
if not include_records:
|
||||
order.points_check_detail = {
|
||||
"fish_fin_balance": fish,
|
||||
"points_balance": manual_score,
|
||||
"required_total": required_total,
|
||||
"surplus": surplus,
|
||||
"threshold": threshold,
|
||||
"errors": errors,
|
||||
"exchange_records": 0,
|
||||
"locked_orders": 0,
|
||||
}
|
||||
|
||||
metric = DouyuExternalOrderMetric(
|
||||
order_id=order.id,
|
||||
@@ -2812,17 +2937,20 @@ def query_activity(
|
||||
points_balance=manual_score,
|
||||
points_required_total=required_total,
|
||||
points_surplus=surplus,
|
||||
records_total=len(records),
|
||||
records_total=len(loaded_records),
|
||||
records_matched=matched,
|
||||
verdict=verdict,
|
||||
message=message,
|
||||
source=_text(source, max_length=16) or "manual",
|
||||
raw=_json_safe(
|
||||
{
|
||||
"elite": elite,
|
||||
"gold": gold,
|
||||
"records": records,
|
||||
"orders": orders,
|
||||
"verdict": verdict,
|
||||
"scope": "activity" if include_records else "balances",
|
||||
"probe": probe,
|
||||
"records_count": len(loaded_records),
|
||||
"records": loaded_records[:ACTIVITY_METRIC_RECORD_LIMIT],
|
||||
"matched": matched_records[:5],
|
||||
"record_query_error": record_query_error,
|
||||
}
|
||||
),
|
||||
created_by=current.id,
|
||||
@@ -2843,7 +2971,7 @@ def query_activity(
|
||||
"points": manual_score,
|
||||
"required_total": required_total,
|
||||
"surplus": surplus,
|
||||
"records": len(records),
|
||||
"records": len(loaded_records),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -2869,31 +2997,18 @@ def query_activity(
|
||||
expected_version=None,
|
||||
commit=False,
|
||||
)
|
||||
elif order.status in (STATUS_RECHARGED, STATUS_SCRIPTED) and reason in (
|
||||
"shortage",
|
||||
"excess",
|
||||
):
|
||||
set_status(
|
||||
db,
|
||||
current,
|
||||
order,
|
||||
status=STATUS_POINTS_ERROR,
|
||||
reason=points_message,
|
||||
expected_version=None,
|
||||
commit=False,
|
||||
)
|
||||
|
||||
record_account_query(
|
||||
db,
|
||||
account,
|
||||
query_type="collab_activity",
|
||||
title="查询鱼翅与积分" if not include_records else "查询积分与兑换记录",
|
||||
title="查询鱼翅与积分" if not include_records else "查询兑换记录",
|
||||
ok=not bool(errors),
|
||||
detail={
|
||||
"order_no": order.order_no,
|
||||
"include_records": include_records,
|
||||
"verdict": verdict,
|
||||
"records": len(records),
|
||||
"records": len(loaded_records),
|
||||
"errors": errors[:10],
|
||||
},
|
||||
actor_id=current.id,
|
||||
@@ -2909,7 +3024,7 @@ def query_activity(
|
||||
"scope": "activity" if include_records else "balances",
|
||||
"verdict": verdict,
|
||||
"message": message,
|
||||
"points_reason": reason,
|
||||
"points_reason": points_reason,
|
||||
"points_message": points_message,
|
||||
"fish_fin_balance": fish,
|
||||
"points_balance": manual_score,
|
||||
@@ -2919,6 +3034,65 @@ def query_activity(
|
||||
}
|
||||
|
||||
|
||||
def query_activity_batch(
|
||||
db: Session,
|
||||
current: User,
|
||||
orders: Sequence[DouyuExternalOrder],
|
||||
*,
|
||||
source: str = "batch",
|
||||
) -> list[dict[str, Any]]:
|
||||
"""按账号复用兑换记录请求,再逐单用商品与时间窗口判定。"""
|
||||
orders_by_account: dict[tuple[str, int | str], list[DouyuExternalOrder]] = {}
|
||||
for order in orders:
|
||||
account_key: tuple[str, int | str]
|
||||
if order.account_id:
|
||||
account_key = ("id", int(order.account_id))
|
||||
else:
|
||||
account_key = ("username", _text(order.account_username).casefold())
|
||||
orders_by_account.setdefault(account_key, []).append(order)
|
||||
|
||||
records_by_account: dict[
|
||||
tuple[str, int | str], tuple[list[dict[str, Any]], str]
|
||||
] = {}
|
||||
for account_key, account_orders in orders_by_account.items():
|
||||
try:
|
||||
records_by_account[account_key] = (
|
||||
_load_exchange_records_for_orders(db, account_orders),
|
||||
"",
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 账号级错误复用到同账号订单
|
||||
records_by_account[account_key] = ([], str(exc))
|
||||
|
||||
results: list[dict[str, Any]] = []
|
||||
for order in orders:
|
||||
if order.account_id:
|
||||
account_key = ("id", int(order.account_id))
|
||||
else:
|
||||
account_key = ("username", _text(order.account_username).casefold())
|
||||
records, record_error = records_by_account[account_key]
|
||||
try:
|
||||
result = query_activity(
|
||||
db,
|
||||
current,
|
||||
order,
|
||||
records=records,
|
||||
record_query_error=record_error,
|
||||
source=source,
|
||||
)
|
||||
results.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"success": True,
|
||||
"verdict": result["verdict"],
|
||||
"message": result["message"],
|
||||
}
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 批量逐单隔离
|
||||
db.rollback()
|
||||
results.append({"id": int(order.id), "success": False, "message": str(exc)})
|
||||
return results
|
||||
|
||||
|
||||
def ensure_shops_registered(
|
||||
db: Session, current: User | None, names: list[str]
|
||||
) -> None:
|
||||
|
||||
@@ -322,6 +322,13 @@ export interface DouyuCollabQueryResult {
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
export interface DouyuCollabBatchQueryItem {
|
||||
id: number;
|
||||
success: boolean;
|
||||
verdict?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export const douyuCollabApi = {
|
||||
listOrders: (params: {
|
||||
page?: number;
|
||||
@@ -488,7 +495,7 @@ export const douyuCollabApi = {
|
||||
data,
|
||||
),
|
||||
batchActivity: (ids: number[]) =>
|
||||
api.post<{ results: Array<Record<string, unknown>>; success: boolean }, { results: Array<Record<string, unknown>>; success: boolean }>(
|
||||
api.post<{ results: DouyuCollabBatchQueryItem[]; success: boolean }, { results: DouyuCollabBatchQueryItem[]; success: boolean }>(
|
||||
'/douyu/external-orders/activity/query',
|
||||
ids,
|
||||
),
|
||||
|
||||
@@ -1142,10 +1142,8 @@ export default function DouyuCollabPage() {
|
||||
try {
|
||||
message.loading({ content: '正在查询兑换记录…', key: 'dy-collab-records' });
|
||||
const result = await douyuCollabApi.queryActivity(order.id);
|
||||
message.success({
|
||||
content: `${result.message}${result.points_reason ? `;${pointsReasonMeta(result.points_reason).label}` : ''}`,
|
||||
key: 'dy-collab-records',
|
||||
});
|
||||
const notify = result.verdict === 'success' ? message.success : message.warning;
|
||||
notify({ content: result.message, key: 'dy-collab-records' });
|
||||
refreshAll();
|
||||
if (detail?.id === order.id) void openDetail(order);
|
||||
} catch (error) {
|
||||
@@ -1312,8 +1310,11 @@ export default function DouyuCollabPage() {
|
||||
setRecordQuerying(true);
|
||||
try {
|
||||
const result = await douyuCollabApi.batchActivity(selectedIds);
|
||||
const ok = result.results.filter((item) => item.success).length;
|
||||
message.success(`兑换记录查询完成:${ok}/${result.results.length}`);
|
||||
const confirmed = result.results.filter((item) => item.success && item.verdict === 'success').length;
|
||||
const pending = result.results.filter((item) => item.success && item.verdict === 'unknown').length;
|
||||
const failed = result.results.length - confirmed - pending;
|
||||
const notify = confirmed === result.results.length ? message.success : message.warning;
|
||||
notify(`兑换记录查询完成:确认成功 ${confirmed},待结算/无法判定 ${pending},失败或查询异常 ${failed}`);
|
||||
refreshAll();
|
||||
} catch (error) {
|
||||
message.error(getErrorMessage(error));
|
||||
@@ -1830,7 +1831,7 @@ export default function DouyuCollabPage() {
|
||||
查鱼翅/积分
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="查询当前账号兑换记录,命中礼包名后自动推进订单状态">
|
||||
<Tooltip title="只查当前账号兑换记录;按商品、礼包名和上脚本时间匹配,命中后自动完成订单">
|
||||
<Button size="small" icon={<SearchOutlined />} onClick={() => void queryOneRecords(row)}>
|
||||
查兑换记录
|
||||
</Button>
|
||||
@@ -2180,7 +2181,7 @@ export default function DouyuCollabPage() {
|
||||
批量查鱼翅/积分{selectedCount > 0 ? `(${selectedCount})` : ''}
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="查询勾选订单的兑换记录;命中礼包名后自动改状态,积分短缺/超额也会标记">
|
||||
<Tooltip title="只查勾选订单的兑换记录;按商品、礼包名和上脚本时间匹配,不会混查鱼翅或积分">
|
||||
<Button
|
||||
icon={<SearchOutlined />}
|
||||
disabled={selectedCount === 0 || loading || recordQuerying}
|
||||
|
||||
Reference in New Issue
Block a user