fix(fulfillment): 使用真实订单号并支持协作单跳转
This commit is contained in:
@@ -714,6 +714,19 @@ def _platform_fulfillment_gift(
|
||||
return name, commodity_id, required_points
|
||||
|
||||
|
||||
def _platform_fulfillment_order_no(task: PlatformFulfillmentTask) -> str:
|
||||
"""外部协作单直接使用 order_site 的真实平台订单号。"""
|
||||
order_no = _text(task.platform_order_id)
|
||||
if not order_no:
|
||||
raise CollabError("履约任务缺少平台订单号", code="platform_order_no_required")
|
||||
if len(order_no) > 64:
|
||||
raise CollabError(
|
||||
"平台订单号超过协作单号长度限制",
|
||||
code="platform_order_no_too_long",
|
||||
)
|
||||
return order_no
|
||||
|
||||
|
||||
def _apply_platform_fulfillment_binding(
|
||||
db: Session,
|
||||
order: DouyuExternalOrder,
|
||||
@@ -785,7 +798,28 @@ def sync_platform_fulfillment_order(
|
||||
.filter(DouyuExternalOrder.source_task_id == task.id)
|
||||
.first()
|
||||
)
|
||||
order_no = _platform_fulfillment_order_no(task)
|
||||
created = order is None
|
||||
order_by_no = (
|
||||
db.query(DouyuExternalOrder)
|
||||
.filter(DouyuExternalOrder.order_no == order_no)
|
||||
.first()
|
||||
)
|
||||
if order is not None and order_by_no is not None and order_by_no.id != order.id:
|
||||
raise CollabError(
|
||||
f"平台订单号 {order_no} 已关联其他履约任务",
|
||||
code="order_no_conflict",
|
||||
)
|
||||
if order is None and order_by_no is not None:
|
||||
if order_by_no.source_task_id not in (None, task.id):
|
||||
raise CollabError(
|
||||
f"平台订单号 {order_no} 已关联其他履约任务",
|
||||
code="order_no_conflict",
|
||||
)
|
||||
# 手工先建过同一真实订单号时,直接接管为来源单,保留人工字段。
|
||||
order = order_by_no
|
||||
created = False
|
||||
|
||||
now = _now()
|
||||
account = (
|
||||
db.query(Account).filter(Account.id == task.account_id).first()
|
||||
@@ -795,17 +829,6 @@ def sync_platform_fulfillment_order(
|
||||
gift_name, commodity_id, required_points = _platform_fulfillment_gift(db, task)
|
||||
|
||||
if order is None:
|
||||
order_no = f"PF{task.id}"
|
||||
occupied = (
|
||||
db.query(DouyuExternalOrder)
|
||||
.filter(DouyuExternalOrder.order_no == order_no)
|
||||
.first()
|
||||
)
|
||||
if occupied is not None:
|
||||
raise CollabError(
|
||||
f"协作单号 {order_no} 已被其他订单占用",
|
||||
code="order_no_conflict",
|
||||
)
|
||||
order = DouyuExternalOrder(
|
||||
order_no=order_no,
|
||||
source_type=SOURCE_TYPE_PLATFORM_FULFILLMENT,
|
||||
@@ -819,6 +842,7 @@ def sync_platform_fulfillment_order(
|
||||
)
|
||||
db.add(order)
|
||||
|
||||
order.order_no = order_no
|
||||
order.source_type = SOURCE_TYPE_PLATFORM_FULFILLMENT
|
||||
order.source_task_id = int(task.id)
|
||||
order.source_order_id = _text(task.source_order_id, max_length=64)
|
||||
|
||||
Reference in New Issue
Block a user