perf(fulfillment): 优化本地能力选号查询与索引
This commit is contained in:
@@ -185,7 +185,12 @@ def reset_task_binding(
|
||||
|
||||
|
||||
def refresh_exchange_eligibility(
|
||||
db: Session, *, task_id: int, current: User
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
current: User,
|
||||
automatic: bool = False,
|
||||
mark_failed_on_error: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
task = _load_task(db, task_id)
|
||||
if task.binding_status != "bound":
|
||||
@@ -231,20 +236,28 @@ def refresh_exchange_eligibility(
|
||||
detail={"error": str(exc)[:1000], "commodity_id": task.commodity_id},
|
||||
actor_id=current.id,
|
||||
)
|
||||
task.execution_status = "manual_required"
|
||||
task.exchange_status = "check_failed"
|
||||
task.issue_code = "eligibility_check_failed"
|
||||
task.last_error = str(exc)
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"eligibility_check_failed",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={"error": str(exc)},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
if mark_failed_on_error:
|
||||
task.execution_status = "manual_required"
|
||||
task.exchange_status = "check_failed"
|
||||
task.issue_code = "eligibility_check_failed"
|
||||
task.last_error = str(exc)
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"eligibility_check_failed",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={"error": str(exc)},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
else:
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"eligibility_auto_refresh_failed",
|
||||
detail={"error": str(exc), "automatic": True},
|
||||
)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
@@ -366,11 +379,12 @@ def refresh_exchange_eligibility(
|
||||
db,
|
||||
task.id,
|
||||
"exchange_eligibility_checked",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
actor_type="system" if automatic else "user",
|
||||
actor_id=None if automatic else current.id,
|
||||
detail={
|
||||
"exchangeStatus": task.exchange_status,
|
||||
"limitedCount": len(limited_ids),
|
||||
"automatic": automatic,
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
@@ -378,6 +392,58 @@ def refresh_exchange_eligibility(
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def refresh_active_exchange_eligibility_for_account(
|
||||
db: Session,
|
||||
*,
|
||||
account_id: int,
|
||||
scope: str,
|
||||
actor_id: int | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""账号资产变化后,自动刷新当前活动履约任务的兑换投影。
|
||||
|
||||
自动刷新失败只记录时间线,不把已绑定任务打成人工;运营仍可手动重试。
|
||||
"""
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.join(AccountLease, AccountLease.id == PlatformFulfillmentTask.lease_id)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.account_id == int(account_id),
|
||||
PlatformFulfillmentTask.scope == str(scope or SCOPE_ELITE),
|
||||
PlatformFulfillmentTask.binding_status == "bound",
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
("completed", "cancelled")
|
||||
),
|
||||
AccountLease.status == "active",
|
||||
)
|
||||
.order_by(
|
||||
PlatformFulfillmentTask.updated_at.desc(),
|
||||
PlatformFulfillmentTask.id.desc(),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if task is None:
|
||||
return None
|
||||
actor = db.get(User, int(actor_id)) if actor_id is not None else None
|
||||
if actor is None and task.operator_id is not None:
|
||||
actor = db.get(User, int(task.operator_id))
|
||||
if actor is None:
|
||||
actor = (
|
||||
db.query(User)
|
||||
.filter(User.role == "super_admin", User.is_active.is_(True))
|
||||
.order_by(User.id.asc())
|
||||
.first()
|
||||
)
|
||||
if actor is None:
|
||||
return None
|
||||
return refresh_exchange_eligibility(
|
||||
db,
|
||||
task_id=int(task.id),
|
||||
current=actor,
|
||||
automatic=True,
|
||||
mark_failed_on_error=False,
|
||||
)
|
||||
|
||||
|
||||
def complete_manual_exchange(
|
||||
db: Session,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user