feat(fulfillment): 支持选号优先级、三皮肤联合资格捆绑与同角色共享租约
This commit is contained in:
@@ -33,6 +33,22 @@ from .common import (
|
||||
_now,
|
||||
binding_failed_lease_ttl_minutes,
|
||||
)
|
||||
from .policy import normalize_allocation_policy
|
||||
|
||||
|
||||
def _settings_out(row: PlatformFulfillmentSetting) -> dict[str, Any]:
|
||||
return {
|
||||
"leaseDurationMinutes": row.lease_duration_minutes,
|
||||
"completedReleaseDelayMinutes": row.completed_release_delay_minutes,
|
||||
"manualCompletedReleaseDelayMinutes": (
|
||||
row.manual_completed_release_delay_minutes
|
||||
),
|
||||
"presaleWaitingReleaseTtlMinutes": (row.presale_waiting_release_ttl_minutes),
|
||||
"manualRequiredLeaseTtlMinutes": (row.manual_required_lease_ttl_minutes),
|
||||
"orderMatchStartedAt": _jsonable(row.order_match_started_at),
|
||||
"allocationPolicy": normalize_allocation_policy(row.allocation_policy_json),
|
||||
"updatedAt": _jsonable(row.updated_at),
|
||||
}
|
||||
|
||||
|
||||
def get_settings(db: Session) -> dict[str, Any]:
|
||||
@@ -55,17 +71,7 @@ def get_settings(db: Session) -> dict[str, Any]:
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return {
|
||||
"leaseDurationMinutes": row.lease_duration_minutes,
|
||||
"completedReleaseDelayMinutes": row.completed_release_delay_minutes,
|
||||
"manualCompletedReleaseDelayMinutes": (
|
||||
row.manual_completed_release_delay_minutes
|
||||
),
|
||||
"presaleWaitingReleaseTtlMinutes": (row.presale_waiting_release_ttl_minutes),
|
||||
"manualRequiredLeaseTtlMinutes": (row.manual_required_lease_ttl_minutes),
|
||||
"orderMatchStartedAt": _jsonable(row.order_match_started_at),
|
||||
"updatedAt": _jsonable(row.updated_at),
|
||||
}
|
||||
return _settings_out(row)
|
||||
|
||||
|
||||
def _lease_policy_for_task(
|
||||
@@ -141,7 +147,28 @@ def _apply_task_lease_policy(
|
||||
):
|
||||
return active_lease
|
||||
|
||||
if str(task.execution_status or "") in {"completed", "cancelled"}:
|
||||
holders = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.lease_id == int(active_lease.id),
|
||||
PlatformFulfillmentTask.execution_status != "cancelled",
|
||||
)
|
||||
.all()
|
||||
)
|
||||
if task.execution_status != "cancelled" and all(
|
||||
int(holder.id) != int(task.id) for holder in holders
|
||||
):
|
||||
holders.append(task)
|
||||
has_other_active_pf = any(
|
||||
int(holder.id) != int(task.id)
|
||||
and str(holder.execution_status or "") not in {"completed", "cancelled"}
|
||||
for holder in holders
|
||||
)
|
||||
|
||||
if (
|
||||
str(task.execution_status or "") in {"completed", "cancelled"}
|
||||
and not has_other_active_pf
|
||||
):
|
||||
# 终态释放前先检查活动预售单。若账号仍被预售单使用,租约转换为
|
||||
# douyu_collab 并保持无限,不能被平台 TTL 抢走。
|
||||
from ..douyu_collab_service import retain_lease_for_active_collab
|
||||
@@ -158,7 +185,33 @@ def _apply_task_lease_policy(
|
||||
if retained is not None:
|
||||
return retained
|
||||
|
||||
policy, minutes = _lease_policy_for_task(task, settings or get_settings(db))
|
||||
resolved_settings = settings or get_settings(db)
|
||||
if holders:
|
||||
holder_policies = [
|
||||
_lease_policy_for_task(holder, resolved_settings) for holder in holders
|
||||
]
|
||||
if any(holder_minutes is None for _, holder_minutes in holder_policies):
|
||||
policy = next(
|
||||
(
|
||||
holder_policy
|
||||
for holder_policy, holder_minutes in holder_policies
|
||||
if holder_minutes is None
|
||||
),
|
||||
"active",
|
||||
)
|
||||
minutes = None
|
||||
else:
|
||||
best_policy, best_minutes = max(
|
||||
(
|
||||
(holder_policy, max(0, int(holder_minutes or 0)))
|
||||
for holder_policy, holder_minutes in holder_policies
|
||||
),
|
||||
key=lambda item: item[1],
|
||||
)
|
||||
policy = best_policy
|
||||
minutes = best_minutes
|
||||
else:
|
||||
policy, minutes = _lease_policy_for_task(task, resolved_settings)
|
||||
if minutes is not None:
|
||||
minutes = max(0, int(minutes))
|
||||
old_expiry = active_lease.expires_at
|
||||
@@ -181,7 +234,7 @@ def _apply_task_lease_policy(
|
||||
},
|
||||
)
|
||||
|
||||
if minutes == 0:
|
||||
if minutes == 0 and not any(int(holder.id) != int(task.id) for holder in holders):
|
||||
release_lease(
|
||||
db,
|
||||
active_lease,
|
||||
@@ -202,6 +255,7 @@ def update_settings(
|
||||
presale_waiting_release_ttl_minutes: int | None = None,
|
||||
manual_required_lease_ttl_minutes: int | None = None,
|
||||
order_match_started_at: datetime | None = None,
|
||||
allocation_policy: dict[str, Any] | None = None,
|
||||
current: User,
|
||||
) -> dict[str, Any]:
|
||||
row = (
|
||||
@@ -223,6 +277,8 @@ def update_settings(
|
||||
row.manual_completed_release_delay_minutes = manual_completed_release_delay_minutes
|
||||
row.presale_waiting_release_ttl_minutes = presale_waiting_release_ttl_minutes
|
||||
row.manual_required_lease_ttl_minutes = manual_required_lease_ttl_minutes
|
||||
if allocation_policy is not None:
|
||||
row.allocation_policy_json = normalize_allocation_policy(allocation_policy)
|
||||
row.updated_by = current.id
|
||||
row.updated_at = now
|
||||
settings = {
|
||||
@@ -258,17 +314,7 @@ def update_settings(
|
||||
now=now,
|
||||
)
|
||||
db.commit()
|
||||
return {
|
||||
"leaseDurationMinutes": row.lease_duration_minutes,
|
||||
"completedReleaseDelayMinutes": row.completed_release_delay_minutes,
|
||||
"manualCompletedReleaseDelayMinutes": (
|
||||
row.manual_completed_release_delay_minutes
|
||||
),
|
||||
"presaleWaitingReleaseTtlMinutes": (row.presale_waiting_release_ttl_minutes),
|
||||
"manualRequiredLeaseTtlMinutes": (row.manual_required_lease_ttl_minutes),
|
||||
"orderMatchStartedAt": _jsonable(row.order_match_started_at),
|
||||
"updatedAt": _jsonable(row.updated_at),
|
||||
}
|
||||
return _settings_out(row)
|
||||
|
||||
|
||||
def _lease_duration_minutes(db: Session) -> int | None:
|
||||
|
||||
Reference in New Issue
Block a user