feat(fulfillment): 新增直播账号履约工作台
This commit is contained in:
@@ -161,6 +161,14 @@ class Account(Base):
|
||||
# S6 拆解时差点把这个信息丢掉,所以补成独立布尔列。
|
||||
esports_opened: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
|
||||
change_role_wait_time: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
# 精英宝典**可换绑时刻**(绝对时间)。`change_role_wait_time` 是接口原文的
|
||||
# 「剩余秒数」,落库那一刻就开始过期 —— 过几天再读它,既不知道是不是还在冷却,
|
||||
# 也不知道什么时候能换绑。所以每次写入时同时记下 `now + wait_time`。
|
||||
# 不同活动的换绑冷却不同,因此**按作用域分列**(电竞手册用既有的
|
||||
# `esports_can_change_time`,它本来就是绝对时刻,不另开副本)。
|
||||
role_change_available_at: Mapped[datetime | None] = mapped_column(
|
||||
UTCDateTime, nullable=True
|
||||
)
|
||||
# 精英宝典开通状态(getUserInfo 的 manualType / expireTime)。
|
||||
# 光看积分区分不了「开通后积分花完」和「从未开通」,必须单独落库。
|
||||
elite_opened: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
|
||||
@@ -1623,6 +1631,270 @@ class AccountEvent(Base):
|
||||
"subject_key",
|
||||
"occurred_at",
|
||||
),
|
||||
# 「某个客服这段时间做了什么」——池化后客服不再持有账号,
|
||||
# 这条是管理侧追责与对账的主查询路径,此前没有任何索引可用。
|
||||
Index("ix_account_events_actor_occurred", "actor_id", "occurred_at"),
|
||||
)
|
||||
|
||||
|
||||
class AccountLease(Base):
|
||||
"""账号租约:谁在**此刻**可以使用这个账号。
|
||||
|
||||
## 为什么要有它
|
||||
|
||||
以前「能不能操作某账号」的唯一判据是 `accounts.assigned_to == 我`。
|
||||
账号因此被**永久独占**:客服名下的账号积分花完了也不会流向别人,
|
||||
管理员只能人工调拨。池化之后这层隐式互斥消失,必须有一个显式的
|
||||
「临时使用权」——就是这张表。
|
||||
|
||||
## 归属与使用权分离
|
||||
|
||||
- `accounts.assigned_to` 收窄为**维护责任**(谁负责重登、开通、进货),
|
||||
由管理员/运营承担,客服名下不再持有账号;
|
||||
- `account_leases` 表示**此刻谁在用**,用完即释放。
|
||||
|
||||
## 一个账号同时只能有一个有效租约
|
||||
|
||||
靠生成列 `active_account_id` + 唯一索引在**数据库层**保证:非 active 时为 NULL,
|
||||
MySQL 唯一索引允许多个 NULL,所以历史记录不受限。并发抢号由数据库裁决 ——
|
||||
两个事务抢同一账号必有一个撞唯一键,捕获后换下一个候选即可,不需要应用层加锁。
|
||||
|
||||
⚠️ `active_account_id` 必须声明在 `account_id` 与 `status` **之后**:
|
||||
MySQL 要求生成列只能引用它前面已定义的列。
|
||||
"""
|
||||
|
||||
__tablename__ = "account_leases"
|
||||
|
||||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
||||
account_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("accounts.id"), nullable=False, index=True
|
||||
)
|
||||
holder_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
|
||||
# 作用域:elite / esports / peace(与 douyu_tasks.handbook_scope 同域)
|
||||
scope: Mapped[str] = mapped_column(String(16), nullable=False, default="elite")
|
||||
purpose: Mapped[str] = mapped_column(
|
||||
String(24), nullable=False, default="exchange", server_default="exchange"
|
||||
)
|
||||
commodity_id: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
batch_id: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
# active / released / expired
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="active", server_default="active"
|
||||
)
|
||||
leased_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
# TTL 兜底:进程重启、页面关掉都能靠它把租约收回来。
|
||||
expires_at: Mapped[datetime] = mapped_column(UTCDateTime, nullable=False)
|
||||
released_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True)
|
||||
release_reason: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
# 生成列:active 时等于 account_id,其余为 NULL。唯一索引因此只约束「有效租约」。
|
||||
active_account_id: Mapped[int | None] = mapped_column(
|
||||
Integer,
|
||||
Computed(
|
||||
"CASE WHEN status = 'active' THEN account_id ELSE NULL END",
|
||||
persisted=True,
|
||||
),
|
||||
nullable=True,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
|
||||
__table_args__ = (
|
||||
Index("uq_lease_active_account", "active_account_id", unique=True),
|
||||
Index("ix_leases_holder_status", "holder_user_id", "status"),
|
||||
Index("ix_leases_status_expires", "status", "expires_at"),
|
||||
)
|
||||
|
||||
account: Mapped[Account] = relationship("Account")
|
||||
holder: Mapped[User] = relationship("User")
|
||||
|
||||
|
||||
class PlatformCommodityMapping(Base):
|
||||
"""order_site 内部 SKU 到斗鱼商品的显式映射。"""
|
||||
|
||||
__tablename__ = "platform_commodity_mappings"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
target_platform: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="douyu", server_default="douyu"
|
||||
)
|
||||
internal_sku_key: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
commodity_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
goods_name: Mapped[str] = mapped_column(
|
||||
String(255), nullable=False, default="", server_default=""
|
||||
)
|
||||
required_points: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
scope: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="elite", server_default="elite"
|
||||
)
|
||||
enabled: Mapped[bool] = mapped_column(
|
||||
Boolean, nullable=False, default=True, server_default="1"
|
||||
)
|
||||
created_by: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("users.id"), nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
UTCDateTime, default=_utcnow, onupdate=_utcnow
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint(
|
||||
"target_platform",
|
||||
"internal_sku_key",
|
||||
name="uq_platform_commodity_mapping_sku",
|
||||
),
|
||||
Index(
|
||||
"ix_platform_commodity_mapping_enabled",
|
||||
"target_platform",
|
||||
"enabled",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class PlatformFulfillmentTask(Base):
|
||||
"""一笔 order_site 订单项在 live-hub 中的直播账号履约作业。"""
|
||||
|
||||
__tablename__ = "platform_fulfillment_tasks"
|
||||
|
||||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
||||
source_system: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="order_site", server_default="order_site"
|
||||
)
|
||||
source_order_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
source_order_item_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
source_fulfillment_id: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
platform_order_id: Mapped[str] = mapped_column(
|
||||
String(128), nullable=False, default="", server_default=""
|
||||
)
|
||||
provider: Mapped[str] = mapped_column(
|
||||
String(32), nullable=False, default="", server_default=""
|
||||
)
|
||||
shop_id: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
shop_name: Mapped[str] = mapped_column(
|
||||
String(255), nullable=False, default="", server_default=""
|
||||
)
|
||||
sku_code: Mapped[str] = mapped_column(
|
||||
String(255), nullable=False, default="", server_default=""
|
||||
)
|
||||
sku_name: Mapped[str] = mapped_column(
|
||||
String(512), nullable=False, default="", server_default=""
|
||||
)
|
||||
internal_sku_key: Mapped[str] = mapped_column(
|
||||
String(255), nullable=False, default="", server_default="", index=True
|
||||
)
|
||||
quantity: Mapped[int] = mapped_column(
|
||||
Integer, nullable=False, default=1, server_default="1"
|
||||
)
|
||||
order_amount_fen: Mapped[int] = mapped_column(
|
||||
Integer, nullable=False, default=0, server_default="0"
|
||||
)
|
||||
target_platform: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="douyu", server_default="douyu"
|
||||
)
|
||||
scope: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="elite", server_default="elite"
|
||||
)
|
||||
commodity_mapping_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("platform_commodity_mappings.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
)
|
||||
commodity_id: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
required_points: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
account_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("accounts.id", ondelete="SET NULL"), nullable=True, index=True
|
||||
)
|
||||
lease_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("account_leases.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
bind_task_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("douyu_tasks.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
exchange_task_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("douyu_tasks.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
binding_status: Mapped[str] = mapped_column(
|
||||
String(24), nullable=False, default="not_started", server_default="not_started"
|
||||
)
|
||||
execution_status: Mapped[str] = mapped_column(
|
||||
String(24),
|
||||
nullable=False,
|
||||
default="pending_mapping",
|
||||
server_default="pending_mapping",
|
||||
)
|
||||
exchange_status: Mapped[str] = mapped_column(
|
||||
String(24), nullable=False, default="not_started", server_default="not_started"
|
||||
)
|
||||
issue_code: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
last_error: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
||||
source_snapshot_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
operator_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
completed_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
UTCDateTime, default=_utcnow, onupdate=_utcnow
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint(
|
||||
"source_system",
|
||||
"source_order_item_id",
|
||||
name="uq_platform_fulfillment_source_item",
|
||||
),
|
||||
Index(
|
||||
"ix_platform_fulfillment_status_updated",
|
||||
"execution_status",
|
||||
"updated_at",
|
||||
),
|
||||
Index(
|
||||
"ix_platform_fulfillment_order",
|
||||
"source_order_id",
|
||||
"source_order_item_id",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class PlatformFulfillmentEvent(Base):
|
||||
"""直播账号履约作业事件,只追加。"""
|
||||
|
||||
__tablename__ = "platform_fulfillment_events"
|
||||
|
||||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
||||
task_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("platform_fulfillment_tasks.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
event_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
actor_type: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="system", server_default="system"
|
||||
)
|
||||
actor_id: Mapped[str] = mapped_column(
|
||||
String(64), nullable=False, default="", server_default=""
|
||||
)
|
||||
detail_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"ix_platform_fulfillment_events_task_created",
|
||||
"task_id",
|
||||
"created_at",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user