feat(fulfillment): 增加账号商品能力索引与生命周期事件
This commit is contained in:
+118
-10
@@ -1,5 +1,6 @@
|
||||
import csv
|
||||
import io
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
@@ -10,6 +11,7 @@ from ..database import get_db
|
||||
from ..deps import get_current_user, require_permission
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AuditLog,
|
||||
DouyuTask,
|
||||
@@ -32,13 +34,16 @@ from ..schemas import (
|
||||
BatchAssign,
|
||||
)
|
||||
from ..services.account_binding import bind_payload
|
||||
from ..services.account_capability_service import account_capability_counts
|
||||
from ..services.account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
EVENT_ASSIGNED,
|
||||
EVENT_AUTOMATION_CHANGED,
|
||||
EVENT_CK_INVALID,
|
||||
EVENT_ELITE_OPENED,
|
||||
EVENT_ESPORTS_OPENED,
|
||||
EVENT_EXCHANGE,
|
||||
EVENT_IMPORTED,
|
||||
EVENT_RECHARGE,
|
||||
EVENT_REMARK_CHANGED,
|
||||
EVENT_TAG_CHANGED,
|
||||
@@ -967,6 +972,7 @@ def import_accounts(
|
||||
|
||||
if accounts:
|
||||
db.add_all(accounts)
|
||||
db.flush()
|
||||
db.add(
|
||||
AuditLog(
|
||||
user_id=current.id,
|
||||
@@ -975,6 +981,22 @@ def import_accounts(
|
||||
target=f"导入{len(accounts)}个",
|
||||
)
|
||||
)
|
||||
emit_bulk(
|
||||
db,
|
||||
[
|
||||
{
|
||||
"account_id": account.id,
|
||||
"event_type": EVENT_IMPORTED,
|
||||
"title": "账号已导入",
|
||||
"category": CATEGORY_OPS,
|
||||
"source": "audit",
|
||||
"actor_type": "user",
|
||||
"actor_id": current.id,
|
||||
"source_ref": f"account_imported:{account.id}",
|
||||
}
|
||||
for account in accounts
|
||||
],
|
||||
)
|
||||
db.commit()
|
||||
|
||||
duplicated_note = f",重复跳过 {duplicated} 个" if duplicated else ""
|
||||
@@ -1254,16 +1276,23 @@ def set_account_automation(
|
||||
acc = db.query(Account).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
previous = bool(acc.auto_managed)
|
||||
acc.auto_managed = bool(req.auto_managed)
|
||||
db.add(
|
||||
AuditLog(
|
||||
user_id=current.id,
|
||||
username=current.username,
|
||||
action="account:automation",
|
||||
target=acc.username,
|
||||
account_id=acc.id,
|
||||
detail="参与自动化" if req.auto_managed else "不参与自动化",
|
||||
)
|
||||
record_account_action(
|
||||
db,
|
||||
current,
|
||||
account=acc,
|
||||
action="account:automation",
|
||||
event_type=EVENT_AUTOMATION_CHANGED,
|
||||
title=(
|
||||
"自动化:不参与 → 参与"
|
||||
if req.auto_managed
|
||||
else "自动化:参与 → 不参与"
|
||||
),
|
||||
detail={
|
||||
"from": previous,
|
||||
"to": bool(req.auto_managed),
|
||||
},
|
||||
)
|
||||
db.commit()
|
||||
return {
|
||||
@@ -1509,6 +1538,81 @@ def batch_delete_accounts_selection(
|
||||
return {"message": f"已删除 {deleted} 个账号", "deleted": deleted, "success": True}
|
||||
|
||||
|
||||
@router.get("/{account_id}/commodities")
|
||||
def account_commodities(
|
||||
account_id: int,
|
||||
scope: str = Query("", max_length=16),
|
||||
availability: str = Query("", max_length=16),
|
||||
limit: int = Query(500, ge=1, le=1000),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""账号 × 商品当前兑换能力及最后确认时间。"""
|
||||
if (
|
||||
not _visible_accounts_query(db, current)
|
||||
.filter(Account.id == account_id)
|
||||
.first()
|
||||
):
|
||||
raise HTTPException(status_code=404, detail="账号不存在或无权查看")
|
||||
|
||||
now = datetime.now(UTC)
|
||||
query = db.query(AccountCommodityState).filter(
|
||||
AccountCommodityState.account_id == account_id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
)
|
||||
scope_value = scope.strip()
|
||||
availability_value = availability.strip()
|
||||
if scope_value:
|
||||
query = query.filter(AccountCommodityState.scope == scope_value)
|
||||
if availability_value:
|
||||
query = query.filter(
|
||||
AccountCommodityState.availability == availability_value
|
||||
)
|
||||
rows = (
|
||||
query.order_by(
|
||||
case(
|
||||
(AccountCommodityState.availability == "available", 0),
|
||||
(AccountCommodityState.availability == "limited", 1),
|
||||
else_=2,
|
||||
),
|
||||
AccountCommodityState.valid_until.desc(),
|
||||
AccountCommodityState.commodity_id.asc(),
|
||||
)
|
||||
.limit(limit)
|
||||
.all()
|
||||
)
|
||||
return {
|
||||
"items": [
|
||||
{
|
||||
"commodity_id": row.commodity_id,
|
||||
"commodity_name": row.commodity_name,
|
||||
"scope": row.scope,
|
||||
"availability": row.availability,
|
||||
"blocking_reason": row.blocking_reason,
|
||||
"raw_status": row.raw_status,
|
||||
"time_limited": bool(row.time_limited),
|
||||
"available_from": row.available_from,
|
||||
"available_until": row.available_until,
|
||||
"schedule": row.schedule_json or [],
|
||||
"observed_at": row.observed_at,
|
||||
"valid_until": row.valid_until,
|
||||
"next_check_at": row.next_check_at,
|
||||
"last_exchange_at": row.last_exchange_at,
|
||||
"exchange_count_365d": row.exchange_count_365d,
|
||||
"fresh": bool(row.valid_until and row.valid_until > now),
|
||||
"source": row.source,
|
||||
"error_code": row.error_code,
|
||||
}
|
||||
for row in rows
|
||||
],
|
||||
"summary": account_capability_counts(
|
||||
db,
|
||||
account_id=account_id,
|
||||
now=now,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{account_id}/exchange-records")
|
||||
def account_exchange_records(
|
||||
account_id: int,
|
||||
@@ -1660,6 +1764,10 @@ def account_overview(
|
||||
"points_expire_at": acc.points_expire_at,
|
||||
"overview_snapshot": acc.overview_snapshot,
|
||||
"overview_queried_at": acc.overview_queried_at,
|
||||
"commodity_capability": account_capability_counts(
|
||||
db,
|
||||
account_id=int(acc.id),
|
||||
),
|
||||
"high_value": high_value_payload(acc),
|
||||
"elite_opened": acc.elite_opened,
|
||||
"elite_expire_time": acc.elite_expire_time,
|
||||
@@ -1694,7 +1802,7 @@ def account_events(
|
||||
page_size: int = 50,
|
||||
event_type: str = "",
|
||||
category: str = Query(
|
||||
"", description="按粗分类筛选:lifecycle / auth / business / ops"
|
||||
"", description="按粗分类筛选:lifecycle / auth / business / activity / ops"
|
||||
),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
|
||||
Reference in New Issue
Block a user