feat(douyu): 完善账号同步域与供给目标配置

This commit is contained in:
yml
2026-11-21 21:54:34 +08:00
parent 3baf15afda
commit e178a393e4
11 changed files with 586 additions and 9 deletions
@@ -6,6 +6,7 @@ from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from sqlalchemy import and_, or_, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from ..models import (
@@ -286,6 +287,101 @@ def evaluate_supply_targets(
]
def upsert_supply_target(
db: Session,
*,
platform: str = "douyu",
scope: str,
commodity_id: str,
value_class: str,
target_covered: int,
target_available: int,
target_allocatable: int,
safety_buffer: int,
enabled: bool,
updated_by: int | None,
) -> AccountSupplyTarget:
"""新增或更新一条供给目标。"""
if value_class not in VALID_VALUES:
raise ValueError(f"不支持的账号价值分类: {value_class}")
for name, value in (
("target_covered", target_covered),
("target_available", target_available),
("target_allocatable", target_allocatable),
("safety_buffer", safety_buffer),
):
if int(value) < 0:
raise ValueError(f"{name} 不能为负数")
target = (
db.query(AccountSupplyTarget)
.filter(
AccountSupplyTarget.platform == platform,
AccountSupplyTarget.scope == scope,
AccountSupplyTarget.commodity_id == commodity_id,
AccountSupplyTarget.value_class == value_class,
)
.first()
)
if target is None:
target = AccountSupplyTarget(
platform=platform,
scope=scope,
commodity_id=commodity_id,
value_class=value_class,
)
db.add(target)
target.target_covered = int(target_covered)
target.target_available = int(target_available)
target.target_allocatable = int(target_allocatable)
target.safety_buffer = int(safety_buffer)
target.enabled = bool(enabled)
target.updated_by = updated_by
target.updated_at = datetime.now(UTC)
try:
with db.begin_nested():
db.flush()
except IntegrityError:
target = (
db.query(AccountSupplyTarget)
.filter(
AccountSupplyTarget.platform == platform,
AccountSupplyTarget.scope == scope,
AccountSupplyTarget.commodity_id == commodity_id,
AccountSupplyTarget.value_class == value_class,
)
.one()
)
db.commit()
return target
def update_supply_target(
db: Session,
target: AccountSupplyTarget,
*,
target_covered: int,
target_available: int,
target_allocatable: int,
safety_buffer: int,
enabled: bool,
updated_by: int | None,
) -> AccountSupplyTarget:
return upsert_supply_target(
db,
platform=target.platform,
scope=target.scope,
commodity_id=target.commodity_id,
value_class=target.value_class,
target_covered=target_covered,
target_available=target_available,
target_allocatable=target_allocatable,
safety_buffer=safety_buffer,
enabled=enabled,
updated_by=updated_by,
)
def _sync_candidate_query(
db: Session,
*,
@@ -389,4 +485,6 @@ __all__ = [
"enqueue_supply_replenishment",
"evaluate_supply_target",
"evaluate_supply_targets",
"update_supply_target",
"upsert_supply_target",
]