fix(fulfillment): 按支付时间过滤履约订单
This commit is contained in:
@@ -57,11 +57,10 @@ def run_auto_allocation_once(
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
PlatformFulfillmentTask.updated_at <= moment - timedelta(seconds=retry_seconds),
|
||||
)
|
||||
match_started_at = service._order_match_started_at(db)
|
||||
if match_started_at is not None:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
query = service._apply_order_match_cutoff(
|
||||
query,
|
||||
service._order_match_started_at(db),
|
||||
)
|
||||
candidates = (
|
||||
query.order_by(PlatformFulfillmentTask.updated_at.asc()).limit(batch_size).all()
|
||||
)
|
||||
|
||||
@@ -179,6 +179,15 @@ def _order_match_started_at(db: Session) -> datetime | None:
|
||||
return row.order_match_started_at if row is not None else None
|
||||
|
||||
|
||||
def _apply_order_match_cutoff(
|
||||
query,
|
||||
match_started_at: datetime | None,
|
||||
):
|
||||
if match_started_at is None:
|
||||
return query
|
||||
return query.filter(PlatformFulfillmentTask.paid_at >= match_started_at)
|
||||
|
||||
|
||||
def _integration_actor(db: Session) -> User:
|
||||
raw_user_id = os.getenv("PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID", "").strip()
|
||||
query = db.query(User).filter(User.is_active.is_(True))
|
||||
@@ -210,11 +219,7 @@ def _find_customer_task(
|
||||
PlatformFulfillmentTask.source_system == "order_site",
|
||||
PlatformFulfillmentTask.source_order_item_id == item_id,
|
||||
)
|
||||
match_started_at = _order_match_started_at(db)
|
||||
if match_started_at is not None:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
query = _apply_order_match_cutoff(query, _order_match_started_at(db))
|
||||
order_no = str(platform_order_id or "").strip()
|
||||
if order_no:
|
||||
query = query.filter(PlatformFulfillmentTask.platform_order_id == order_no)
|
||||
@@ -398,6 +403,8 @@ def _sync_pending_tasks(
|
||||
task.sku_name = str(row.get("sku_name") or "")
|
||||
task.internal_sku_key = internal_sku_key
|
||||
task.quantity = 1
|
||||
if isinstance(row.get("paid_at"), datetime):
|
||||
task.paid_at = row["paid_at"]
|
||||
task.order_amount_fen = int(
|
||||
row.get("revenue_amount_fen") or row.get("total_amount") or 0
|
||||
)
|
||||
@@ -466,11 +473,7 @@ def list_tasks(
|
||||
page_size: int = 20,
|
||||
) -> dict[str, Any]:
|
||||
query = db.query(PlatformFulfillmentTask)
|
||||
match_started_at = _order_match_started_at(db)
|
||||
if match_started_at is not None:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
query = _apply_order_match_cutoff(query, _order_match_started_at(db))
|
||||
query = query.filter(
|
||||
or_(
|
||||
PlatformFulfillmentTask.commodity_mapping_id.isnot(None),
|
||||
@@ -569,6 +572,7 @@ def _task_out(
|
||||
"lastError": task.last_error,
|
||||
"operatorId": task.operator_id,
|
||||
"completedAt": task.completed_at,
|
||||
"paidAt": task.paid_at,
|
||||
"createdAt": task.created_at,
|
||||
"updatedAt": task.updated_at,
|
||||
"account": (
|
||||
@@ -879,17 +883,13 @@ def upsert_mapping(
|
||||
row.updated_at = _now()
|
||||
db.flush()
|
||||
|
||||
match_started_at = _order_match_started_at(db)
|
||||
pending = db.query(PlatformFulfillmentTask).filter(
|
||||
PlatformFulfillmentTask.internal_sku_key == sku_key,
|
||||
PlatformFulfillmentTask.execution_status.in_(
|
||||
["pending_mapping", "ready", "manual_required"]
|
||||
),
|
||||
)
|
||||
if match_started_at is not None:
|
||||
pending = pending.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
pending = _apply_order_match_cutoff(pending, _order_match_started_at(db))
|
||||
pending = pending.all()
|
||||
for task in pending:
|
||||
_apply_mapping(task, row if row.enabled else None)
|
||||
|
||||
Reference in New Issue
Block a user