feat(douyu): 支持斗鱼预售归档账号安全释放回号库并修复完成归档自动解租 (v0.4.25)

This commit is contained in:
yml
2026-11-30 14:28:00 +08:00
parent efcbd9a691
commit cc78208048
11 changed files with 1927 additions and 9 deletions
+613
View File
@@ -2817,3 +2817,616 @@ def test_collab_takeover_blocked_when_shared_lease_has_other_active_platform_tas
assert lease.purpose == "platform_fulfillment"
finally:
session.close()
def test_done_and_archive_auto_close_bound_platform_task_and_release_lease():
engine = get_test_engine()
session = make_test_session(engine)
try:
admin = User(
username="collab-auto-close-admin", password_hash="h", role="super_admin"
)
acc_done = Account(
username="collab-close-done",
password="p",
email="close-done@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=1",
elite_opened=True,
points=5000,
)
acc_legacy = Account(
username="collab-close-legacy",
password="p",
email="close-legacy@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=2",
elite_opened=True,
points=5000,
)
acc_shared = Account(
username="collab-close-shared",
password="p",
email="close-shared@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=3",
elite_opened=True,
points=5000,
)
session.add_all([admin, acc_done, acc_legacy, acc_shared])
session.flush()
# Case 1: 预售单从 scripted -> done 时,自动完结 bound 履约单并释放租约
lease_done = AccountLease(
account_id=acc_done.id,
holder_user_id=admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:pending-1",
status="active",
expires_at=None,
)
# Case 2: 历史已是 done 的单点击批量归档时,兜底完结 bound 履约单并释放租约
lease_legacy = AccountLease(
account_id=acc_legacy.id,
holder_user_id=admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:pending-2",
status="active",
expires_at=None,
)
# Case 3: 同一租约被两个履约单共享时,只完结当前单并保留租约给兄弟履约单
lease_shared = AccountLease(
account_id=acc_shared.id,
holder_user_id=admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:pending-3",
status="active",
expires_at=None,
)
session.add_all([lease_done, lease_legacy, lease_shared])
session.flush()
pf_done = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="auto-close-1",
source_order_item_id="auto-close-1",
platform_order_id="AUTO-CLOSE-1",
internal_sku_key="sku-1",
commodity_id="cid-1",
customer_role_name="角色1",
account_id=acc_done.id,
lease_id=lease_done.id,
execution_status="bound",
binding_status="bound",
exchange_status="eligible",
)
pf_legacy = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="auto-close-2",
source_order_item_id="auto-close-2",
platform_order_id="AUTO-CLOSE-2",
internal_sku_key="sku-2",
commodity_id="cid-2",
customer_role_name="角色2",
account_id=acc_legacy.id,
lease_id=lease_legacy.id,
execution_status="bound",
binding_status="bound",
exchange_status="eligible",
)
pf_shared_1 = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="auto-close-3a",
source_order_item_id="auto-close-3a",
platform_order_id="AUTO-CLOSE-3A",
internal_sku_key="sku-3a",
commodity_id="cid-3a",
customer_role_name="角色3",
account_id=acc_shared.id,
lease_id=lease_shared.id,
execution_status="bound",
binding_status="bound",
exchange_status="eligible",
)
pf_shared_2 = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="auto-close-3b",
source_order_item_id="auto-close-3b",
platform_order_id="AUTO-CLOSE-3B",
internal_sku_key="sku-3b",
commodity_id="cid-3b",
customer_role_name="角色3",
account_id=acc_shared.id,
lease_id=lease_shared.id,
execution_status="bound",
binding_status="bound",
exchange_status="eligible",
)
session.add_all([pf_done, pf_legacy, pf_shared_1, pf_shared_2])
session.flush()
lease_done.batch_id = f"pf:{pf_done.id}"
lease_legacy.batch_id = f"pf:{pf_legacy.id}"
lease_shared.batch_id = f"pf:{pf_shared_1.id}"
order_done = collab.DouyuExternalOrder(
order_no="DY-CLOSE-DONE",
source_type=collab.SOURCE_TYPE_PLATFORM_FULFILLMENT,
source_task_id=pf_done.id,
account_id=acc_done.id,
account_username=acc_done.username,
bind_game_role="角色1",
gift_name="礼包A",
commodity_id="cid-1",
required_points=100,
home_images=["collab/c1.png"],
status=collab.STATUS_SCRIPTED,
owner_id=admin.id,
created_by=admin.id,
)
order_legacy = collab.DouyuExternalOrder(
order_no="DY-CLOSE-LEGACY",
source_type=collab.SOURCE_TYPE_PLATFORM_FULFILLMENT,
source_task_id=pf_legacy.id,
account_id=acc_legacy.id,
account_username=acc_legacy.username,
bind_game_role="角色2",
gift_name="礼包B",
commodity_id="cid-2",
required_points=100,
home_images=["collab/c2.png"],
status=collab.STATUS_DONE,
owner_id=admin.id,
created_by=admin.id,
)
order_shared = collab.DouyuExternalOrder(
order_no="DY-CLOSE-SHARED",
source_type=collab.SOURCE_TYPE_PLATFORM_FULFILLMENT,
source_task_id=pf_shared_1.id,
account_id=acc_shared.id,
account_username=acc_shared.username,
bind_game_role="角色3",
gift_name="礼包C",
commodity_id="cid-3a",
required_points=100,
home_images=["collab/c3.png"],
status=collab.STATUS_SCRIPTED,
owner_id=admin.id,
created_by=admin.id,
)
session.add_all([order_done, order_legacy, order_shared])
session.commit()
collab.set_status(
session,
admin,
order_done,
status=collab.STATUS_DONE,
reason="确认抢购成功",
)
session.refresh(pf_done)
session.refresh(lease_done)
assert pf_done.execution_status == "completed"
assert pf_done.exchange_status == "manual_done"
assert pf_done.lease_id is None
assert lease_done.status == "released"
assert lease_done.release_reason == "douyu_collab_done"
archive_res = collab.archive_orders(
session, admin, [order_legacy.id], reason="归档"
)
assert archive_res["archived"] == 1
session.refresh(pf_legacy)
session.refresh(lease_legacy)
assert pf_legacy.execution_status == "completed"
assert pf_legacy.lease_id is None
assert lease_legacy.status == "released"
assert lease_legacy.release_reason == "douyu_collab_archived"
collab.set_status(
session,
admin,
order_shared,
status=collab.STATUS_DONE,
reason="同号一单先成功",
)
session.refresh(pf_shared_1)
session.refresh(pf_shared_2)
session.refresh(lease_shared)
assert pf_shared_1.execution_status == "completed"
assert pf_shared_1.lease_id is None
assert pf_shared_2.execution_status == "bound"
assert pf_shared_2.lease_id == lease_shared.id
assert lease_shared.status == "active"
assert lease_shared.batch_id == f"pf:{pf_shared_2.id}"
finally:
session.close()
def test_archived_account_pool_release_preview_and_execute_with_guards():
engine = get_test_engine()
session = make_test_session(engine)
try:
now = datetime.now(UTC)
admin = User(
username="pool-release-admin", password_hash="h", role="super_admin"
)
support = User(
username="pool-release-support", password_hash="h", role="support"
)
session.add_all([admin, support])
session.flush()
cooldown_past = now - timedelta(days=1)
cooldown_future = now + timedelta(days=3)
acc_bound = Account(
username="arch-acc-bound",
password="p",
email="ab1@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=a1",
assigned_to=support.id,
role_change_available_at=cooldown_past,
)
acc_manual = Account(
username="arch-acc-manual",
password="p",
email="ab2@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=a2",
assigned_to=support.id,
role_change_available_at=cooldown_future,
)
acc_assigned_only = Account(
username="arch-acc-assigned",
password="p",
email="ab3@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=a3",
assigned_to=support.id,
role_change_available_at=cooldown_past,
)
acc_active_collab = Account(
username="arch-acc-collab-busy",
password="p",
email="ab4@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=a4",
assigned_to=support.id,
role_change_available_at=cooldown_past,
)
acc_active_pf = Account(
username="arch-acc-pf-busy",
password="p",
email="ab5@example.com",
email_password="ep",
status="active",
ck_check_status="valid",
cookie="PHPSESSID=a5",
assigned_to=support.id,
role_change_available_at=cooldown_past,
)
session.add_all(
[acc_bound, acc_manual, acc_assigned_only, acc_active_collab, acc_active_pf]
)
session.flush()
# 保留历史兑换事件:回池不能删除历史兑换记录
exchange_evt = AccountEvent(
account_id=acc_bound.id,
event_type="exchange",
title="历史皮肤兑换",
category="business",
subject_type="commodity",
subject_key="cid-history-skin",
occurred_at=now - timedelta(days=10),
)
session.add(exchange_evt)
lease_bound = AccountLease(
account_id=acc_bound.id,
holder_user_id=admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:b",
status="active",
)
lease_manual = AccountLease(
account_id=acc_manual.id,
holder_user_id=admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:m",
status="active",
)
lease_collab_busy = AccountLease(
account_id=acc_active_collab.id,
holder_user_id=admin.id,
scope="elite",
purpose="douyu_collab",
batch_id=f"collab:{acc_active_collab.id}",
status="active",
)
lease_pf_busy = AccountLease(
account_id=acc_active_pf.id,
holder_user_id=admin.id,
scope="elite",
purpose="platform_fulfillment",
batch_id="pf:busy",
status="active",
)
session.add_all([lease_bound, lease_manual, lease_collab_busy, lease_pf_busy])
session.flush()
pf_bound = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="pr-1",
source_order_item_id="pr-1",
platform_order_id="PR-1",
internal_sku_key="sku-pr1",
commodity_id="cid-pr1",
account_id=acc_bound.id,
lease_id=lease_bound.id,
execution_status="bound",
binding_status="bound",
exchange_status="eligible",
)
pf_manual = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="pr-2",
source_order_item_id="pr-2",
platform_order_id="PR-2",
internal_sku_key="sku-pr2",
commodity_id="cid-pr2",
account_id=acc_manual.id,
lease_id=lease_manual.id,
execution_status="manual_required",
binding_status="failed",
exchange_status="not_started",
issue_code="manual_required",
)
pf_busy = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="pr-5",
source_order_item_id="pr-5",
platform_order_id="PR-5",
internal_sku_key="sku-pr5",
commodity_id="cid-pr5",
account_id=acc_active_pf.id,
lease_id=lease_pf_busy.id,
execution_status="waiting_bind",
binding_status="qr_pending",
exchange_status="not_started",
)
session.add_all([pf_bound, pf_manual, pf_busy])
session.flush()
lease_bound.batch_id = f"pf:{pf_bound.id}"
lease_manual.batch_id = f"pf:{pf_manual.id}"
lease_pf_busy.batch_id = f"pf:{pf_busy.id}"
archived_orders = [
collab.DouyuExternalOrder(
order_no="DY-ARCH-1",
source_type=collab.SOURCE_TYPE_PLATFORM_FULFILLMENT,
source_task_id=pf_bound.id,
account_id=acc_bound.id,
account_username=acc_bound.username,
gift_name="礼包1",
home_images=["collab/arch-1.png"],
status=collab.STATUS_DONE,
archived_at=now - timedelta(days=5),
archived_by=admin.id,
owner_id=admin.id,
created_by=admin.id,
),
collab.DouyuExternalOrder(
order_no="DY-ARCH-2",
source_type=collab.SOURCE_TYPE_PLATFORM_FULFILLMENT,
source_task_id=pf_manual.id,
account_id=acc_manual.id,
account_username=acc_manual.username,
gift_name="礼包2",
home_images=["collab/arch-2.png"],
status=collab.STATUS_DONE,
archived_at=now - timedelta(days=5),
archived_by=admin.id,
owner_id=admin.id,
created_by=admin.id,
),
collab.DouyuExternalOrder(
order_no="DY-ARCH-3",
account_id=acc_assigned_only.id,
account_username=acc_assigned_only.username,
gift_name="礼包3",
home_images=["collab/arch-3.png"],
status=collab.STATUS_DONE,
archived_at=now - timedelta(days=5),
archived_by=admin.id,
owner_id=admin.id,
created_by=admin.id,
),
collab.DouyuExternalOrder(
order_no="DY-ARCH-4",
account_id=acc_active_collab.id,
account_username=acc_active_collab.username,
gift_name="礼包4",
home_images=["collab/arch-4.png"],
status=collab.STATUS_DONE,
archived_at=now - timedelta(days=5),
archived_by=admin.id,
owner_id=admin.id,
created_by=admin.id,
),
collab.DouyuExternalOrder(
order_no="DY-ARCH-5",
account_id=acc_active_pf.id,
account_username=acc_active_pf.username,
gift_name="礼包5",
home_images=["collab/arch-5.png"],
status=collab.STATUS_DONE,
archived_at=now - timedelta(days=5),
archived_by=admin.id,
owner_id=admin.id,
created_by=admin.id,
),
]
# Acc 4 还有一个未归档活跃预售单,必须跳过
unarchived_active = collab.DouyuExternalOrder(
order_no="DY-UNARCH-ACTIVE-4",
account_id=acc_active_collab.id,
account_username=acc_active_collab.username,
gift_name="礼包4-新",
home_images=["collab/unarch-4.png"],
status=collab.STATUS_ROLE_ERROR,
archived_at=None,
owner_id=admin.id,
created_by=admin.id,
)
session.add_all([*archived_orders, unarchived_active])
session.commit()
# 1) 默认预览:不包含 manual_required,但包含清空 assigned_to
preview_default = collab.preview_archived_account_release(
session,
all_archived=True,
include_manual_required=False,
clear_assigned_to=True,
)
assert preview_default["matched_orders"] == 5
assert preview_default["total_accounts"] == 5
assert preview_default["releasable_total"] == 2 # acc_bound + acc_assigned_only
assert preview_default["release_lease_total"] == 1
assert preview_default["release_platform_bound_count"] == 1
assert preview_default["clear_assigned_count"] == 2
assert preview_default["skipped_active_collab"] == 1
assert preview_default["skipped_active_platform"] == 1
assert preview_default["skipped_manual_required"] == 1
assert preview_default["cooldown_ready_count"] == 2
# 2) 包含 manual_required 后再执行释放
result = collab.execute_archived_account_release(
session,
admin,
all_archived=True,
include_manual_required=True,
clear_assigned_to=True,
reason="测试活动结束回池",
)
assert (
result["processed_accounts"] == 3
) # acc_bound + acc_manual + acc_assigned_only
assert result["released_leases"] == 2
assert result["closed_platform_tasks"] == 1
assert result["cancelled_manual_tasks"] == 1
assert result["cleared_assigned_accounts"] == 3
assert result["skipped_active_collab"] == 1
assert result["skipped_active_platform"] == 1
assert result["cooldown_ready_count"] == 2
assert result["cooldown_waiting_count"] == 1
# 核查数据库状态:历史记录与冷却完整保留,限制解除
session.refresh(acc_bound)
session.refresh(acc_manual)
session.refresh(acc_assigned_only)
session.refresh(acc_active_collab)
session.refresh(acc_active_pf)
session.refresh(lease_bound)
session.refresh(lease_manual)
session.refresh(lease_collab_busy)
session.refresh(lease_pf_busy)
session.refresh(pf_bound)
session.refresh(pf_manual)
session.refresh(pf_busy)
assert acc_bound.assigned_to is None
assert acc_manual.assigned_to is None
assert acc_assigned_only.assigned_to is None
# 被保护的账号不得动
assert acc_active_collab.assigned_to == support.id
assert acc_active_pf.assigned_to == support.id
assert lease_collab_busy.status == "active"
assert lease_pf_busy.status == "active"
assert pf_busy.execution_status == "waiting_bind"
# 释放后冷却期仍保留
assert acc_bound.role_change_available_at is not None
assert acc_manual.role_change_available_at is not None
assert lease_bound.status == "released"
assert lease_bound.release_reason == "archived_collab_pool_release"
assert lease_manual.status == "released"
assert pf_bound.execution_status == "completed"
assert pf_manual.execution_status == "cancelled"
assert pf_manual.account_id is None
# 归档预售单与历史兑换事件一行都没删
assert (
session.query(collab.DouyuExternalOrder)
.filter(collab.DouyuExternalOrder.archived_at.isnot(None))
.count()
== 5
)
assert session.get(AccountEvent, exchange_evt.id) is not None
assert (
session.query(AuditLog)
.filter_by(action="douyu:collab:archived_pool_release")
.count()
== 1
)
finally:
session.close()
def test_archived_account_release_route_requires_manage_permission():
engine = get_test_engine()
session = make_test_session(engine)
try:
support = User(username="arch-route-support", password_hash="h", role="support")
operation = User(username="arch-route-op", password_hash="h", role="operation")
session.add_all([support, operation])
session.commit()
from fastapi import HTTPException
from web.backend.deps import require_permission
from web.backend.schemas import DouyuCollabArchivedAccountReleaseRequest
guard = require_permission("douyu:collab:manage")
with pytest.raises(HTTPException) as excinfo:
guard(support)
assert excinfo.value.status_code == 403
allowed = guard(operation)
assert allowed.id == operation.id
req = DouyuCollabArchivedAccountReleaseRequest(all_archived=True)
preview = collab_api.preview_archived_external_order_account_release(
req, session, operation
)
assert preview["matched_orders"] == 0
assert preview["releasable_total"] == 0
result = collab_api.release_archived_external_order_accounts(
req, session, operation
)
assert result["processed_accounts"] == 0
finally:
session.close()