feat(consignment): 寄卖配置、内部匹配与打手端升级
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import type { PoolClient } from 'pg'
|
||||
|
||||
export type ConsignmentAllocationRow = {
|
||||
id: number
|
||||
inventory_id: number
|
||||
work_order_id: number
|
||||
work_order_share_id: number | null
|
||||
quantity: number
|
||||
unit_price_fen: number
|
||||
status: 'reserved' | 'consumed' | 'released'
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 占用库存(部分占用时 remaining_quantity 递减,扣到 0 才 consumed)。
|
||||
* 返回是否成功;并发下由 UPDATE 的条件兜底。
|
||||
*/
|
||||
export async function reserveConsignmentInventoryWithClient(
|
||||
client: PoolClient,
|
||||
input: {
|
||||
inventoryId: number
|
||||
workOrderId: number
|
||||
workOrderShareId?: number | null
|
||||
quantity?: number
|
||||
unitPriceFen: number
|
||||
now: string
|
||||
},
|
||||
): Promise<ConsignmentAllocationRow | null> {
|
||||
const quantity = Math.max(1, Math.round(Number(input.quantity || 1)))
|
||||
const inventory = await client.query<{
|
||||
id: number
|
||||
remaining_quantity: number
|
||||
status: string
|
||||
allow_matching: boolean
|
||||
}>(
|
||||
`
|
||||
UPDATE consignment_inventories
|
||||
SET
|
||||
remaining_quantity = remaining_quantity - $1,
|
||||
status = CASE WHEN remaining_quantity - $1 <= 0 THEN 'consumed' ELSE status END,
|
||||
allocation_status = '已安排',
|
||||
consumed_at = CASE WHEN remaining_quantity - $1 <= 0 THEN $2 ELSE consumed_at END,
|
||||
updated_at = $2
|
||||
WHERE id = $3
|
||||
AND status = 'in_stock'
|
||||
AND allow_matching = TRUE
|
||||
AND remaining_quantity >= $1
|
||||
RETURNING id, remaining_quantity, status, allow_matching
|
||||
`,
|
||||
[quantity, input.now, input.inventoryId],
|
||||
)
|
||||
if (!inventory.rows[0]) return null
|
||||
|
||||
const allocation = await client.query<ConsignmentAllocationRow>(
|
||||
`
|
||||
INSERT INTO consignment_inventory_allocations (
|
||||
inventory_id, work_order_id, work_order_share_id, quantity, unit_price_fen,
|
||||
status, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, $5, 'reserved', $6, $6)
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.inventoryId,
|
||||
input.workOrderId,
|
||||
input.workOrderShareId || null,
|
||||
quantity,
|
||||
Math.max(0, Math.round(Number(input.unitPriceFen || 0))),
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return allocation.rows[0] || null
|
||||
}
|
||||
|
||||
async function releaseAllocationRows(
|
||||
client: PoolClient,
|
||||
rows: ConsignmentAllocationRow[],
|
||||
now: string,
|
||||
): Promise<void> {
|
||||
for (const row of rows) {
|
||||
// 已被下架作废的库存不再恢复可匹配,只回补数量便于审计。
|
||||
await client.query(
|
||||
`
|
||||
UPDATE consignment_inventories
|
||||
SET
|
||||
remaining_quantity = remaining_quantity + $1,
|
||||
status = CASE WHEN status = 'consumed' THEN 'in_stock' ELSE status END,
|
||||
consumed_at = CASE WHEN status = 'consumed' THEN NULL ELSE consumed_at END,
|
||||
allocation_status = CASE WHEN status = 'consumed' THEN '待安排' ELSE allocation_status END,
|
||||
updated_at = $2
|
||||
WHERE id = $3
|
||||
`,
|
||||
[row.quantity, now, row.inventory_id],
|
||||
)
|
||||
await client.query(
|
||||
`UPDATE consignment_inventory_allocations SET status = 'released', updated_at = $1 WHERE id = $2`,
|
||||
[now, row.id],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** 释放某个拼单份额占用的库存(后台撤份额 / 超时 / 退单统一走这里)。 */
|
||||
export async function releaseConsignmentAllocationForShareWithClient(
|
||||
client: PoolClient,
|
||||
shareId: number,
|
||||
now: string,
|
||||
): Promise<number> {
|
||||
const result = await client.query<ConsignmentAllocationRow>(
|
||||
`SELECT * FROM consignment_inventory_allocations WHERE work_order_share_id = $1 AND status = 'reserved' FOR UPDATE`,
|
||||
[shareId],
|
||||
)
|
||||
await releaseAllocationRows(client, result.rows, now)
|
||||
return result.rows.length
|
||||
}
|
||||
|
||||
/** 释放整个工单占用的全部库存(整单取消 / 退回大厅 / 撤销履约)。 */
|
||||
export async function releaseConsignmentAllocationsForWorkOrderWithClient(
|
||||
client: PoolClient,
|
||||
workOrderId: number,
|
||||
now: string,
|
||||
): Promise<number> {
|
||||
const result = await client.query<ConsignmentAllocationRow>(
|
||||
`SELECT * FROM consignment_inventory_allocations WHERE work_order_id = $1 AND status = 'reserved' FOR UPDATE`,
|
||||
[workOrderId],
|
||||
)
|
||||
await releaseAllocationRows(client, result.rows, now)
|
||||
return result.rows.length
|
||||
}
|
||||
|
||||
/** 匹配成功后把分配标记为已消耗(仅用于统计;释放时按 reserved 处理)。 */
|
||||
export async function markConsignmentAllocationsConsumedWithClient(
|
||||
client: PoolClient,
|
||||
workOrderId: number,
|
||||
now: string,
|
||||
): Promise<void> {
|
||||
await client.query(
|
||||
`UPDATE consignment_inventory_allocations SET status = 'consumed', updated_at = $1 WHERE work_order_id = $2 AND status = 'reserved'`,
|
||||
[now, workOrderId],
|
||||
)
|
||||
}
|
||||
@@ -50,6 +50,17 @@ export type ConsignmentInventoryRow = {
|
||||
worker_phone?: string
|
||||
assigned_worker_id?: string | number | null
|
||||
deposit_amount_fen?: number
|
||||
/** 分类级寄卖配置与发布快照 */
|
||||
profile_id?: string | number | null
|
||||
profile_version_id?: string | number | null
|
||||
profile_snapshot_json?: string | Record<string, unknown> | null
|
||||
/** 该批次总数量 / 剩余可匹配数量 */
|
||||
quantity?: number
|
||||
remaining_quantity?: number
|
||||
/** 寄卖售价(分):匹配工单时的结算价 */
|
||||
sale_price_fen?: number
|
||||
listed_at?: string | null
|
||||
price_updated_at?: string | null
|
||||
/** 对接客服(发布/审核入库时选定,用于提成归属与联系) */
|
||||
contact_supporter_id?: string | number | null
|
||||
contact_supporter_name?: string
|
||||
@@ -82,6 +93,13 @@ export type ConsignmentSubmissionRow = {
|
||||
worker_name?: string
|
||||
worker_phone?: string
|
||||
deposit_amount_fen?: number
|
||||
/** 分类级寄卖配置与发布快照 */
|
||||
profile_id?: string | number | null
|
||||
profile_version_id?: string | number | null
|
||||
profile_snapshot_json?: string | Record<string, unknown> | null
|
||||
/** 本次提报数量与期望售价 */
|
||||
quantity?: number
|
||||
requested_sale_price_fen?: number
|
||||
/** 对接客服(发布时选定,用于提成归属与联系) */
|
||||
contact_supporter_id?: string | number | null
|
||||
contact_supporter_name?: string
|
||||
@@ -394,6 +412,7 @@ export async function updateConsignmentInventory(
|
||||
diamondLevel?: number | undefined
|
||||
brillianceCount?: number | undefined
|
||||
costPriceFen?: number | undefined
|
||||
salePriceFen?: number | undefined
|
||||
buyerName?: string | undefined
|
||||
inboundBatch?: string | undefined
|
||||
sellerContact?: string | undefined
|
||||
@@ -444,6 +463,10 @@ export async function updateConsignmentInventory(
|
||||
values.push(input.costPriceFen)
|
||||
updates.push(`cost_price_fen = $${values.length}`)
|
||||
}
|
||||
if (input.salePriceFen !== undefined) {
|
||||
values.push(input.salePriceFen)
|
||||
updates.push(`sale_price_fen = $${values.length}`)
|
||||
}
|
||||
if (input.buyerName !== undefined) {
|
||||
values.push(input.buyerName)
|
||||
updates.push(`buyer_name = $${values.length}`)
|
||||
@@ -1312,6 +1335,11 @@ export async function createConsignmentSubmissionWithClient(
|
||||
workerPhone?: string
|
||||
depositAmountFen?: number
|
||||
contactSupporterId?: number | null
|
||||
profileId?: number | null
|
||||
profileVersionId?: number | null
|
||||
profileSnapshotJson?: Record<string, unknown> | null
|
||||
quantity?: number
|
||||
requestedSalePriceFen?: number
|
||||
now: string
|
||||
},
|
||||
): Promise<ConsignmentSubmissionRow> {
|
||||
@@ -1322,14 +1350,18 @@ export async function createConsignmentSubmissionWithClient(
|
||||
seller_game_id, seller_payment_info, seller_phone, contact_info,
|
||||
content_json, screenshots_json, asking_price_fen,
|
||||
worker_id, worker_name, worker_phone, deposit_amount_fen,
|
||||
contact_supporter_id, status, created_at, updated_at
|
||||
contact_supporter_id,
|
||||
profile_id, profile_version_id, profile_snapshot_json, quantity, requested_sale_price_fen,
|
||||
status, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4,
|
||||
$5, $6, $7, $8, $9,
|
||||
$10, $11, $12, $13,
|
||||
$14::jsonb, $15::jsonb, $16,
|
||||
$17, $18, $19, $20,
|
||||
$21, 'pending_review', $22, $22
|
||||
$21,
|
||||
$22, $23, $24::jsonb, $25, $26,
|
||||
'pending_review', $27, $27
|
||||
)
|
||||
RETURNING *
|
||||
`
|
||||
@@ -1355,6 +1387,11 @@ export async function createConsignmentSubmissionWithClient(
|
||||
input.workerPhone || '',
|
||||
input.depositAmountFen || 0,
|
||||
input.contactSupporterId || null,
|
||||
input.profileId || null,
|
||||
input.profileVersionId || null,
|
||||
input.profileSnapshotJson ? toJsonString(input.profileSnapshotJson) : null,
|
||||
Math.max(1, Math.round(Number(input.quantity || 1))),
|
||||
Math.max(0, Math.round(Number(input.requestedSalePriceFen || input.askingPriceFen || 0))),
|
||||
input.now,
|
||||
])
|
||||
return res.rows[0]!
|
||||
@@ -1423,6 +1460,11 @@ export async function approveConsignmentSubmissionWithInbound(input: {
|
||||
workerPhone?: string | undefined
|
||||
depositAmountFen?: number | undefined
|
||||
contactSupporterId?: number | null | undefined
|
||||
profileId?: number | null | undefined
|
||||
profileVersionId?: number | null | undefined
|
||||
profileSnapshotJson?: string | Record<string, unknown> | null | undefined
|
||||
quantity?: number | undefined
|
||||
salePriceFen?: number | undefined
|
||||
now: string
|
||||
}): Promise<{ submission: ConsignmentSubmissionRow; inventory: ConsignmentInventoryRow }> {
|
||||
return withTransaction(async (client: PoolClient) => {
|
||||
@@ -1456,7 +1498,10 @@ export async function approveConsignmentSubmissionWithInbound(input: {
|
||||
seller_phone, credentials_json, screenshots_json, remark,
|
||||
allow_matching, status, allocation_status, source_type,
|
||||
source_submission_id, worker_id, worker_name, worker_phone, deposit_amount_fen,
|
||||
contact_supporter_id, inbound_at, created_at, updated_at
|
||||
contact_supporter_id,
|
||||
profile_id, profile_version_id, profile_snapshot_json, quantity, remaining_quantity,
|
||||
sale_price_fen, listed_at,
|
||||
inbound_at, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10, $11,
|
||||
@@ -1464,10 +1509,14 @@ export async function approveConsignmentSubmissionWithInbound(input: {
|
||||
$16, $17::jsonb, $18::jsonb, $19,
|
||||
TRUE, 'in_stock', '待安排', 'worker_portal',
|
||||
$20, $21, $22, $23, $24,
|
||||
$25, $26, $26, $26
|
||||
$25,
|
||||
$26, $27, $28::jsonb, $29, $29,
|
||||
$30, $31,
|
||||
$31, $31, $31
|
||||
)
|
||||
RETURNING *
|
||||
`
|
||||
const quantity = Math.max(1, Math.round(Number(input.quantity ?? submission.quantity ?? 1)))
|
||||
const invValues = [
|
||||
input.inventoryNo,
|
||||
submission.game_name || '和平精英',
|
||||
@@ -1494,6 +1543,25 @@ export async function approveConsignmentSubmissionWithInbound(input: {
|
||||
workerPhone,
|
||||
depositAmountFen,
|
||||
contactSupporterId,
|
||||
input.profileId ?? submission.profile_id ?? null,
|
||||
input.profileVersionId ?? submission.profile_version_id ?? null,
|
||||
input.profileSnapshotJson
|
||||
? toJsonString(input.profileSnapshotJson)
|
||||
: submission.profile_snapshot_json
|
||||
? toJsonString(submission.profile_snapshot_json)
|
||||
: null,
|
||||
quantity,
|
||||
Math.max(
|
||||
0,
|
||||
Math.round(
|
||||
Number(
|
||||
input.salePriceFen ??
|
||||
submission.requested_sale_price_fen ??
|
||||
submission.asking_price_fen ??
|
||||
0,
|
||||
),
|
||||
),
|
||||
),
|
||||
input.now,
|
||||
]
|
||||
const invRes = await client.query<ConsignmentInventoryRow>(invSql, invValues)
|
||||
|
||||
@@ -381,6 +381,8 @@ export type WorkOrderShareRow = {
|
||||
problem_marked_by?: string
|
||||
/** 客户端幂等键 */
|
||||
request_id?: string
|
||||
/** 拼单份额对应的寄卖库存;非寄卖份额为 NULL */
|
||||
consignment_inventory_id?: number | null
|
||||
worker_username?: string
|
||||
worker_display_name?: string
|
||||
}
|
||||
@@ -415,6 +417,11 @@ export type WorkOrderRow = {
|
||||
service_profile_id?: number | null
|
||||
service_profile_version_id?: number | null
|
||||
service_profile_snapshot_json?: string | Record<string, unknown> | null
|
||||
/** 命中的寄卖配置与内部匹配截止/大厅可见时间 */
|
||||
consignment_profile_id?: number | null
|
||||
internal_match_deadline_at?: string | null
|
||||
hall_visible_at?: string | null
|
||||
internal_match_attempts?: number
|
||||
acceptance_json: string | Record<string, unknown>
|
||||
draft_acceptance_json: string | Record<string, unknown>
|
||||
admin_note: string
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { PoolClient } from 'pg'
|
||||
import { isWorkOrderGiftDepositForfeitable } from '../../domain/work-order-acceptance-mode.js'
|
||||
import { forfeitConsignmentPublishDepositByWorkOrderWithClient } from './consignment-deposit-repo.js'
|
||||
import { releaseConsignmentInventoryBindingForWorkOrderWithClient } from './consignment-repo.js'
|
||||
import { releaseConsignmentAllocationsForWorkOrderWithClient } from './consignment-allocation-repo.js'
|
||||
import {
|
||||
ensureWorkerWalletWithClient,
|
||||
getWorkerWalletWithClient,
|
||||
@@ -698,6 +699,13 @@ export async function settleOverdueWorkOrder(input: {
|
||||
now: input.now,
|
||||
reason: `超时处置(${effectivePolicy})`,
|
||||
})
|
||||
// 新版寄卖分配(支持数量/拼单):回补剩余数量,并按平台规则没收寄卖保证金。
|
||||
await releaseConsignmentAllocationsForWorkOrderWithClient(client, input.workOrderId, input.now)
|
||||
await forfeitConsignmentPublishDepositByWorkOrderWithClient(client, {
|
||||
workOrderId: input.workOrderId,
|
||||
note: `寄卖履约超时,扣除寄卖保证金(${effectivePolicy})`,
|
||||
now: input.now,
|
||||
})
|
||||
|
||||
await createWorkOrderEventWithClient(client, {
|
||||
workOrderId: input.workOrderId,
|
||||
|
||||
@@ -25,9 +25,14 @@ import {
|
||||
import { maybeUpgradeWorkerLevel } from './worker-ranking-repo.js'
|
||||
import type { WorkOrderRow, WorkOrderShareRow } from './types.js'
|
||||
import {
|
||||
forfeitConsignmentPublishDepositByWorkOrderWithClient,
|
||||
releaseConsignmentPublishDepositBySubmissionWithClient,
|
||||
settleConsignmentSupplierPayoutByWorkOrderWithClient,
|
||||
} from './consignment-deposit-repo.js'
|
||||
import {
|
||||
releaseConsignmentAllocationForShareWithClient,
|
||||
releaseConsignmentAllocationsForWorkOrderWithClient,
|
||||
} from './consignment-allocation-repo.js'
|
||||
|
||||
/**
|
||||
* 验收事务提交后再跑自动升级。
|
||||
@@ -879,6 +884,9 @@ export async function cancelWorkOrderShare(input: {
|
||||
}
|
||||
}
|
||||
|
||||
// 寄卖拼单份额:取消时把占用的库存还回可匹配池。
|
||||
await releaseConsignmentAllocationForShareWithClient(client, Number(share.id), input.now)
|
||||
|
||||
const workerId = Number(share.worker_id || 0)
|
||||
let releasedDepositAmount = 0
|
||||
if (workerId > 0) {
|
||||
@@ -1119,6 +1127,16 @@ export async function settleOverdueWorkOrderShare(input: {
|
||||
}
|
||||
}
|
||||
|
||||
// 寄卖份额超时:库存回池 + 按平台规则扣除该供货打手的寄卖保证金。
|
||||
await releaseConsignmentAllocationForShareWithClient(client, Number(share.id), input.now)
|
||||
if (share.consignment_inventory_id) {
|
||||
await forfeitConsignmentPublishDepositByWorkOrderWithClient(client, {
|
||||
workOrderId: input.workOrderId,
|
||||
note: '寄卖拼单超时,扣除寄卖保证金',
|
||||
now: input.now,
|
||||
}).catch(() => 0)
|
||||
}
|
||||
|
||||
await client.query(
|
||||
`UPDATE work_order_shares
|
||||
SET status = 'cancelled',
|
||||
@@ -1231,6 +1249,9 @@ export async function cancelWorkOrderSharing(input: {
|
||||
}
|
||||
}
|
||||
|
||||
// 整单撤销拼单:释放该工单占用的全部寄卖库存。
|
||||
await releaseConsignmentAllocationsForWorkOrderWithClient(client, input.workOrderId, input.now)
|
||||
|
||||
const cancellableShares = shares.filter((share) =>
|
||||
['joined', 'submitted'].includes(share.status),
|
||||
)
|
||||
@@ -1418,6 +1439,9 @@ export async function cancelWorkOrderSharingPart(input: {
|
||||
}
|
||||
}
|
||||
|
||||
// 取消剩余份额:释放该工单占用的全部寄卖库存。
|
||||
await releaseConsignmentAllocationsForWorkOrderWithClient(client, input.workOrderId, input.now)
|
||||
|
||||
const cancellableShares = shares.filter((share) =>
|
||||
['joined', 'submitted'].includes(String(share.status || '')),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user