fix(fulfillment): 保持外部协作人工数据并支持转人工
This commit is contained in:
@@ -315,6 +315,30 @@ def release_task(
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/release-manual")
|
||||
def release_task_to_manual(
|
||||
task_id: int,
|
||||
req: PlatformFulfillmentReasonRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:use")),
|
||||
) -> dict[str, Any]:
|
||||
result = service.release_task_to_manual(
|
||||
db,
|
||||
task_id=task_id,
|
||||
current=current,
|
||||
reason=req.reason,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="platform_fulfillment:release_to_manual",
|
||||
target=f"platform_fulfillment_task:{task_id}",
|
||||
detail={"reason": req.reason},
|
||||
)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/manual")
|
||||
def mark_manual(
|
||||
task_id: int,
|
||||
|
||||
@@ -731,6 +731,8 @@ def _apply_platform_fulfillment_binding(
|
||||
db: Session,
|
||||
order: DouyuExternalOrder,
|
||||
task: PlatformFulfillmentTask,
|
||||
*,
|
||||
overwrite: bool,
|
||||
) -> None:
|
||||
"""把履约任务已确认的本地绑定结果投影到协作单,不额外联网。"""
|
||||
bind_task = (
|
||||
@@ -742,11 +744,13 @@ def _apply_platform_fulfillment_binding(
|
||||
role = binding_role_from_result(result)
|
||||
if not role:
|
||||
return
|
||||
order.bind_game_role = _text(role, max_length=128)
|
||||
if overwrite or not order.bind_game_role:
|
||||
order.bind_game_role = _text(role, max_length=128)
|
||||
channel = binding_channel(result)
|
||||
if channel:
|
||||
if channel and (overwrite or not order.bind_game_channel):
|
||||
order.bind_game_channel = _text(channel, max_length=128)
|
||||
order.bind_snapshot_at = order.bind_snapshot_at or _now()
|
||||
if overwrite or not order.bind_snapshot_at:
|
||||
order.bind_snapshot_at = _now()
|
||||
|
||||
|
||||
def _apply_platform_fulfillment_status(
|
||||
@@ -855,12 +859,19 @@ def sync_platform_fulfillment_order(
|
||||
order.gift_name = gift_name
|
||||
order.commodity_id = commodity_id
|
||||
order.required_points = required_points
|
||||
if account is not None:
|
||||
if account is not None and (
|
||||
created or (order.account_id is None and not order.account_username)
|
||||
):
|
||||
order.account_id = int(account.id)
|
||||
order.account_username = _text(account.username, max_length=128)
|
||||
order.account_uid = _text(account.uid, max_length=32)
|
||||
order.account_nickname = _text(account.nickname, max_length=128)
|
||||
_apply_platform_fulfillment_binding(db, order, task)
|
||||
_apply_platform_fulfillment_binding(
|
||||
db,
|
||||
order,
|
||||
task,
|
||||
overwrite=created,
|
||||
)
|
||||
_apply_platform_fulfillment_status(order, task, created=created)
|
||||
order.version = int(order.version or 0) + 1
|
||||
db.flush()
|
||||
@@ -871,11 +882,16 @@ def sync_platform_fulfillment_order(
|
||||
"created": created,
|
||||
"status": order.status,
|
||||
}
|
||||
if account is not None:
|
||||
linked_account = (
|
||||
db.query(Account).filter(Account.id == order.account_id).first()
|
||||
if order.account_id
|
||||
else None
|
||||
)
|
||||
if linked_account is not None:
|
||||
record_account_action(
|
||||
db,
|
||||
current,
|
||||
account=account,
|
||||
account=linked_account,
|
||||
action="douyu:collab:sync_from_platform_fulfillment",
|
||||
event_type=EVENT_COLLAB_SYNCED,
|
||||
title=f"同步外部单协作:{order.order_no}",
|
||||
|
||||
@@ -2383,6 +2383,21 @@ def restore_cancelled_task(
|
||||
if len(restore_reason) < 2:
|
||||
raise HTTPException(status_code=400, detail="请填写恢复原因")
|
||||
|
||||
restored_collab = (
|
||||
db.query(DouyuExternalOrder)
|
||||
.filter(DouyuExternalOrder.source_task_id == task.id)
|
||||
.first()
|
||||
)
|
||||
if restored_collab is not None and (
|
||||
restored_collab.status not in {"incomplete", "void"}
|
||||
or restored_collab.bind_game_role
|
||||
or restored_collab.home_images
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="关联外部协作单已在人工处理,请继续使用外部协作单",
|
||||
)
|
||||
|
||||
mapping = _mapping_for_sku(db, task.internal_sku_key)
|
||||
_apply_mapping(task, mapping)
|
||||
task.account_id = None
|
||||
@@ -2396,11 +2411,6 @@ def restore_cancelled_task(
|
||||
task.operator_id = current.id
|
||||
task.updated_at = _now()
|
||||
|
||||
restored_collab = (
|
||||
db.query(DouyuExternalOrder)
|
||||
.filter(DouyuExternalOrder.source_task_id == task.id)
|
||||
.first()
|
||||
)
|
||||
restored_collab_status = ""
|
||||
if restored_collab is not None and restored_collab.status == "void":
|
||||
restored_collab.status = "incomplete"
|
||||
@@ -2430,6 +2440,55 @@ def restore_cancelled_task(
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def release_task_to_manual(
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
current: User,
|
||||
reason: str,
|
||||
) -> dict[str, Any]:
|
||||
"""释放履约账号并转人工,不再进入自动选号。"""
|
||||
task = _load_task(db, task_id)
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
raise HTTPException(status_code=409, detail="已结束任务不能转人工")
|
||||
manual_reason = reason.strip()[:2000] or "以外部单协作人工处理为准"
|
||||
account_id = task.account_id
|
||||
lease_id = task.lease_id
|
||||
lease = (
|
||||
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if task.lease_id
|
||||
else None
|
||||
)
|
||||
release_lease(db, lease, reason="platform_fulfillment_manual")
|
||||
task.account_id = None
|
||||
task.lease_id = None
|
||||
task.bind_task_id = None
|
||||
task.exchange_task_id = None
|
||||
task.execution_status = "manual_required"
|
||||
task.binding_status = "not_started"
|
||||
task.exchange_status = "not_started"
|
||||
task.issue_code = "manual_required"
|
||||
task.next_allocate_at = None
|
||||
task.operator_id = current.id
|
||||
task.last_error = manual_reason
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"released_to_manual",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"reason": manual_reason,
|
||||
"releasedAccountId": account_id,
|
||||
"releasedLeaseId": lease_id,
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"allocate_task",
|
||||
"cancel_task",
|
||||
@@ -2444,6 +2503,7 @@ __all__ = [
|
||||
"refresh_binding",
|
||||
"refresh_exchange_eligibility",
|
||||
"release_task_account",
|
||||
"release_task_to_manual",
|
||||
"reset_task_binding",
|
||||
"restore_cancelled_task",
|
||||
"start_binding",
|
||||
|
||||
@@ -98,6 +98,11 @@ export const platformFulfillmentApi = {
|
||||
api.post<PlatformFulfillmentDetail, PlatformFulfillmentDetail>(
|
||||
`/platform-fulfillment/tasks/${taskId}/release`,
|
||||
),
|
||||
releaseToManual: (taskId: number, reason: string) =>
|
||||
api.post<PlatformFulfillmentDetail, PlatformFulfillmentDetail>(
|
||||
`/platform-fulfillment/tasks/${taskId}/release-manual`,
|
||||
{ reason },
|
||||
),
|
||||
markManual: (taskId: number, reason = '') =>
|
||||
api.post<PlatformFulfillmentDetail, PlatformFulfillmentDetail>(
|
||||
`/platform-fulfillment/tasks/${taskId}/manual`,
|
||||
|
||||
@@ -96,6 +96,7 @@ const EVENT_LABELS: Record<string, string> = {
|
||||
exchange_eligibility_checked: '刷新兑换资格',
|
||||
manual_exchange_completed: '人工兑换完成',
|
||||
marked_manual_required: '转人工处理',
|
||||
released_to_manual: '释放账号并转人工',
|
||||
task_cancelled: '任务已取消',
|
||||
task_restored: '恢复已取消任务',
|
||||
};
|
||||
@@ -185,7 +186,7 @@ function eventLabel(value: string) {
|
||||
|
||||
function eventColor(value: string) {
|
||||
if (value.includes('failed') || value.includes('cancelled')) return 'red';
|
||||
if (value.includes('manual_required')) return 'orange';
|
||||
if (value.includes('manual_required') || value.includes('released_to_manual')) return 'orange';
|
||||
if (value.includes('completed') || value.includes('allocated') || value.includes('restored')) return 'green';
|
||||
return 'blue';
|
||||
}
|
||||
@@ -225,6 +226,9 @@ function eventDescription(event: { eventType: string; detail: Record<string, unk
|
||||
if (event.eventType === 'marked_manual_required') {
|
||||
return String(detail.reason || '已转人工处理');
|
||||
}
|
||||
if (event.eventType === 'released_to_manual') {
|
||||
return `转人工原因:${String(detail.reason || '以外部单协作人工处理为准')}`;
|
||||
}
|
||||
if (event.eventType === 'task_cancelled') {
|
||||
return `取消原因:${String(detail.reason || '人工取消')}`;
|
||||
}
|
||||
@@ -282,6 +286,9 @@ export default function PlatformFulfillmentPage() {
|
||||
const [restoreTarget, setRestoreTarget] = useState<PlatformFulfillmentTask | null>(null);
|
||||
const [restoreReason, setRestoreReason] = useState('');
|
||||
const [restoring, setRestoring] = useState(false);
|
||||
const [manualReleaseTarget, setManualReleaseTarget] = useState<PlatformFulfillmentTask | null>(null);
|
||||
const [manualReleaseReason, setManualReleaseReason] = useState('');
|
||||
const [manualReleasing, setManualReleasing] = useState(false);
|
||||
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
|
||||
const [selectedId, setSelectedId] = useState<number | null>(null);
|
||||
const [detail, setDetail] = useState<PlatformFulfillmentDetail | null>(null);
|
||||
@@ -535,6 +542,32 @@ export default function PlatformFulfillmentPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const releaseTaskToManual = async () => {
|
||||
if (!manualReleaseTarget) return;
|
||||
const reason = manualReleaseReason.trim();
|
||||
if (reason.length < 2) {
|
||||
message.warning('请填写转人工原因');
|
||||
return;
|
||||
}
|
||||
setManualReleasing(true);
|
||||
try {
|
||||
const result = await platformFulfillmentApi.releaseToManual(
|
||||
manualReleaseTarget.id,
|
||||
reason,
|
||||
);
|
||||
setDetail(result);
|
||||
setSelectedId(result.task.id);
|
||||
setManualReleaseTarget(null);
|
||||
setManualReleaseReason('');
|
||||
await loadTasks();
|
||||
message.success('账号已释放,任务已转人工处理');
|
||||
} catch (error) {
|
||||
message.error(getErrorMessage(error));
|
||||
} finally {
|
||||
setManualReleasing(false);
|
||||
}
|
||||
};
|
||||
|
||||
const runDouyuAccountAction = async (taskType: string) => {
|
||||
if (!selected?.account) {
|
||||
message.warning('当前履约任务还没有分配斗鱼账号');
|
||||
@@ -892,7 +925,25 @@ export default function PlatformFulfillmentPage() {
|
||||
查看二维码
|
||||
</Button>
|
||||
) : null}
|
||||
{selected.account ? <Button onClick={() => void runTaskAction(() => platformFulfillmentApi.release(selected.id))}>释放账号</Button> : null}
|
||||
{selected.account ? (
|
||||
<Button onClick={() => void runTaskAction(() => platformFulfillmentApi.release(selected.id))}>
|
||||
释放并重新选号
|
||||
</Button>
|
||||
) : null}
|
||||
{selected.account &&
|
||||
selected.executionStatus !== 'completed' &&
|
||||
selected.executionStatus !== 'cancelled' &&
|
||||
can('platform_fulfillment:use') ? (
|
||||
<Button
|
||||
icon={<UserSwitchOutlined />}
|
||||
onClick={() => {
|
||||
setManualReleaseTarget(selected);
|
||||
setManualReleaseReason('以外部单协作人工处理为准');
|
||||
}}
|
||||
>
|
||||
释放并转人工
|
||||
</Button>
|
||||
) : null}
|
||||
{canResetBinding(selected) ? (
|
||||
<Button danger icon={<UndoOutlined />} onClick={() => resetBinding(selected)}>
|
||||
重置角色资料
|
||||
@@ -1308,6 +1359,36 @@ export default function PlatformFulfillmentPage() {
|
||||
</Space>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
title={`释放并转人工 ${manualReleaseTarget ? `#${manualReleaseTarget.id}` : ''}`}
|
||||
open={Boolean(manualReleaseTarget)}
|
||||
okText="确认转人工"
|
||||
cancelText="取消"
|
||||
confirmLoading={manualReleasing}
|
||||
onOk={() => void releaseTaskToManual()}
|
||||
onCancel={() => {
|
||||
setManualReleaseTarget(null);
|
||||
setManualReleaseReason('');
|
||||
}}
|
||||
>
|
||||
<Space direction="vertical" size={12} style={{ width: '100%' }}>
|
||||
<Alert
|
||||
type="warning"
|
||||
showIcon
|
||||
message="释放当前账号后不会自动重新选号"
|
||||
description="任务会进入“待人工”,并通知订单侧当前由人工处理。"
|
||||
/>
|
||||
<Input.TextArea
|
||||
rows={3}
|
||||
maxLength={2000}
|
||||
showCount
|
||||
value={manualReleaseReason}
|
||||
placeholder="请填写转人工原因"
|
||||
onChange={(event) => setManualReleaseReason(event.target.value)}
|
||||
/>
|
||||
</Space>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
open={settingsOpen}
|
||||
title="履约同步与租约设置"
|
||||
|
||||
Reference in New Issue
Block a user