perf(account-supply): 合并全局库存统计并增加查询索引

This commit is contained in:
yml
2026-11-22 14:04:24 +08:00
parent f2041f2444
commit 7233bdfa50
7 changed files with 395 additions and 99 deletions
+25 -13
View File
@@ -70,7 +70,9 @@ from ..services.account_protection_service import (
update_protection_group,
)
from ..services.account_supply_service import (
SupplyInventory,
evaluate_supply_target,
evaluate_supply_targets,
update_supply_target,
upsert_supply_target,
)
@@ -701,8 +703,10 @@ def _config_out(config: DouyuConfig) -> DouyuConfigOut:
def _supply_target_out(
db: Session,
target: AccountSupplyTarget,
*,
inventory: SupplyInventory | None = None,
) -> DouyuSupplyTargetOut:
inventory = evaluate_supply_target(db, target)
resolved = inventory or evaluate_supply_target(db, target)
return DouyuSupplyTargetOut(
id=int(target.id),
target_id=int(target.id),
@@ -710,18 +714,18 @@ def _supply_target_out(
scope=target.scope,
commodity_id=target.commodity_id,
value_class=target.value_class,
target_covered=inventory.target_covered,
target_available=inventory.target_available,
target_allocatable=inventory.target_allocatable,
safety_buffer=inventory.safety_buffer,
target_covered=resolved.target_covered,
target_available=resolved.target_available,
target_allocatable=resolved.target_allocatable,
safety_buffer=resolved.safety_buffer,
enabled=bool(target.enabled),
covered=inventory.covered,
available=inventory.available,
allocatable=inventory.allocatable,
covered_shortage=inventory.covered_shortage,
available_shortage=inventory.available_shortage,
allocatable_shortage=inventory.allocatable_shortage,
needs_replenishment=inventory.needs_replenishment,
covered=resolved.covered,
available=resolved.available,
allocatable=resolved.allocatable,
covered_shortage=resolved.covered_shortage,
available_shortage=resolved.available_shortage,
allocatable_shortage=resolved.allocatable_shortage,
needs_replenishment=resolved.needs_replenishment,
updated_by=target.updated_by,
updated_at=target.updated_at,
)
@@ -1056,7 +1060,15 @@ def list_supply_targets(
)
.all()
)
return [_supply_target_out(db, row) for row in rows]
enabled_inventories = {item.target_id: item for item in evaluate_supply_targets(db)}
return [
_supply_target_out(
db,
row,
inventory=enabled_inventories.get(int(row.id)),
)
for row in rows
]
@router.post("/supply-targets", response_model=DouyuSupplyTargetOut)