fix(douyu): 修正外部单兑换记录判定
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user