feat(consignment): 新增和平精英寄卖库存管理、定向收集审核与工单智能匹配履约
This commit is contained in:
@@ -0,0 +1,935 @@
|
||||
import type { PoolClient } from 'pg'
|
||||
import { query, withTransaction } from '../../db/client.js'
|
||||
import { toJsonString } from './shared.js'
|
||||
|
||||
export type ConsignmentInventoryRow = {
|
||||
id: string | number
|
||||
inventory_no: string
|
||||
game_name: string
|
||||
category_id: string | number | null
|
||||
item_name: string
|
||||
system: string
|
||||
server_zone: string
|
||||
series: string
|
||||
diamond_level: number
|
||||
brilliance_count: number
|
||||
cost_price_fen: number
|
||||
buyer_name: string
|
||||
inbound_batch: string
|
||||
seller_contact: string
|
||||
seller_game_id: string
|
||||
seller_payment_info: string
|
||||
seller_phone: string
|
||||
credentials_json: string | Record<string, unknown>
|
||||
screenshots_json: string | unknown[]
|
||||
remark: string
|
||||
allow_matching: boolean
|
||||
status: 'in_stock' | 'consumed' | 'archived'
|
||||
allocation_status: string
|
||||
bound_order_id: string | number | null
|
||||
bound_work_order_id: string | number | null
|
||||
bound_platform_order_id: string
|
||||
shop_name: string
|
||||
source_type: 'manual' | 'collection_link' | 'recycle'
|
||||
source_submission_id: string | number | null
|
||||
inbound_at: string
|
||||
consumed_at: string | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
category_name?: string
|
||||
}
|
||||
|
||||
export type ConsignmentCollectLinkRow = {
|
||||
id: string | number
|
||||
token: string
|
||||
title: string
|
||||
game_name: string
|
||||
category_id: string | number | null
|
||||
system: string
|
||||
server_zone: string
|
||||
series: string
|
||||
diamond_level: number
|
||||
brilliance_count: number
|
||||
preset_price_fen: number
|
||||
inbound_batch: string
|
||||
buyer_name: string
|
||||
template_json: string | Record<string, unknown>
|
||||
is_multi_use: boolean
|
||||
expires_at: string | null
|
||||
enabled: boolean
|
||||
created_by: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
category_name?: string
|
||||
submission_count?: number
|
||||
}
|
||||
|
||||
export type ConsignmentSubmissionRow = {
|
||||
id: string | number
|
||||
submission_no: string
|
||||
link_id: string | number | null
|
||||
game_name: string
|
||||
category_id: string | number | null
|
||||
item_name: string
|
||||
system: string
|
||||
server_zone: string
|
||||
series: string
|
||||
diamond_level: number
|
||||
brilliance_count: number
|
||||
seller_game_id: string
|
||||
seller_payment_info: string
|
||||
seller_phone: string
|
||||
contact_info: string
|
||||
content_json: string | Record<string, unknown>
|
||||
screenshots_json: string | unknown[]
|
||||
asking_price_fen: number
|
||||
status: 'pending_review' | 'approved' | 'rejected'
|
||||
review_note: string
|
||||
reviewer_name: string
|
||||
reviewed_at: string | null
|
||||
result_inventory_id: string | number | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
category_name?: string
|
||||
link_title?: string
|
||||
}
|
||||
|
||||
export type ConsignmentStatsSummary = {
|
||||
totalInStockCount: number
|
||||
totalInStockCostFen: number
|
||||
totalConsumedCount: number
|
||||
totalConsumedCostFen: number
|
||||
byBuyer: Array<{
|
||||
buyer_name: string
|
||||
count: number
|
||||
total_cost_fen: number
|
||||
consumed_count: number
|
||||
}>
|
||||
byShop: Array<{ shop_name: string; count: number }>
|
||||
byBatch: Array<{ inbound_batch: string; count: number; total_cost_fen: number }>
|
||||
bySeries: Array<{ series: string; diamond_level: number; count: number }>
|
||||
}
|
||||
|
||||
export async function listConsignmentInventories(filters: {
|
||||
status?: string | undefined
|
||||
buyerName?: string | undefined
|
||||
shopName?: string | undefined
|
||||
inboundBatch?: string | undefined
|
||||
categoryId?: number | undefined
|
||||
system?: string | undefined
|
||||
serverZone?: string | undefined
|
||||
series?: string | undefined
|
||||
diamondLevel?: number | undefined
|
||||
allowMatching?: boolean | undefined
|
||||
keyword?: string | undefined
|
||||
page?: number | undefined
|
||||
pageSize?: number | undefined
|
||||
}): Promise<{ items: ConsignmentInventoryRow[]; total: number }> {
|
||||
const conditions: string[] = []
|
||||
const values: unknown[] = []
|
||||
|
||||
if (filters.status) {
|
||||
values.push(filters.status)
|
||||
conditions.push(`ci.status = $${values.length}`)
|
||||
}
|
||||
if (filters.buyerName) {
|
||||
values.push(filters.buyerName)
|
||||
conditions.push(`ci.buyer_name = $${values.length}`)
|
||||
}
|
||||
if (filters.shopName) {
|
||||
values.push(filters.shopName)
|
||||
conditions.push(`ci.shop_name = $${values.length}`)
|
||||
}
|
||||
if (filters.inboundBatch) {
|
||||
values.push(filters.inboundBatch)
|
||||
conditions.push(`ci.inbound_batch = $${values.length}`)
|
||||
}
|
||||
if (filters.categoryId) {
|
||||
values.push(filters.categoryId)
|
||||
conditions.push(`ci.category_id = $${values.length}`)
|
||||
}
|
||||
if (filters.system) {
|
||||
values.push(filters.system)
|
||||
conditions.push(`ci.system = $${values.length}`)
|
||||
}
|
||||
if (filters.serverZone) {
|
||||
values.push(filters.serverZone)
|
||||
conditions.push(`ci.server_zone = $${values.length}`)
|
||||
}
|
||||
if (filters.series) {
|
||||
values.push(filters.series)
|
||||
conditions.push(`ci.series = $${values.length}`)
|
||||
}
|
||||
if (filters.diamondLevel !== undefined && filters.diamondLevel > 0) {
|
||||
values.push(filters.diamondLevel)
|
||||
conditions.push(`ci.diamond_level = $${values.length}`)
|
||||
}
|
||||
if (filters.allowMatching !== undefined) {
|
||||
values.push(filters.allowMatching)
|
||||
conditions.push(`ci.allow_matching = $${values.length}`)
|
||||
}
|
||||
if (filters.keyword) {
|
||||
values.push(`%${filters.keyword.trim()}%`)
|
||||
conditions.push(
|
||||
`(ci.item_name ILIKE $${values.length} OR ci.inventory_no ILIKE $${values.length} OR ci.bound_platform_order_id ILIKE $${values.length} OR ci.seller_contact ILIKE $${values.length} OR ci.seller_game_id ILIKE $${values.length} OR ci.seller_payment_info ILIKE $${values.length} OR ci.remark ILIKE $${values.length})`,
|
||||
)
|
||||
}
|
||||
|
||||
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
|
||||
const countSql = `SELECT COUNT(*)::integer AS total FROM consignment_inventories ci ${whereClause}`
|
||||
const countRes = await query<{ total: number }>(countSql, values)
|
||||
const total = countRes.rows[0]?.total || 0
|
||||
|
||||
const page = Math.max(1, filters.page || 1)
|
||||
const pageSize = Math.max(1, Math.min(100, filters.pageSize || 20))
|
||||
const offset = (page - 1) * pageSize
|
||||
|
||||
values.push(pageSize, offset)
|
||||
const listSql = `
|
||||
SELECT
|
||||
ci.*,
|
||||
wc.name AS category_name
|
||||
FROM consignment_inventories ci
|
||||
LEFT JOIN work_categories wc ON wc.id = ci.category_id
|
||||
${whereClause}
|
||||
ORDER BY ci.id DESC
|
||||
LIMIT $${values.length - 1} OFFSET $${values.length}
|
||||
`
|
||||
const listRes = await query<ConsignmentInventoryRow>(listSql, values)
|
||||
return { items: listRes.rows, total }
|
||||
}
|
||||
|
||||
export async function getConsignmentInventoryStats(): Promise<ConsignmentStatsSummary> {
|
||||
const totalsSql = `
|
||||
SELECT
|
||||
COALESCE(SUM(CASE WHEN status = 'in_stock' THEN 1 ELSE 0 END), 0)::integer AS in_stock_count,
|
||||
COALESCE(SUM(CASE WHEN status = 'in_stock' THEN cost_price_fen ELSE 0 END), 0)::integer AS in_stock_cost_fen,
|
||||
COALESCE(SUM(CASE WHEN status = 'consumed' THEN 1 ELSE 0 END), 0)::integer AS consumed_count,
|
||||
COALESCE(SUM(CASE WHEN status = 'consumed' THEN cost_price_fen ELSE 0 END), 0)::integer AS consumed_cost_fen
|
||||
FROM consignment_inventories
|
||||
`
|
||||
const totalsRes = await query<{
|
||||
in_stock_count: number
|
||||
in_stock_cost_fen: number
|
||||
consumed_count: number
|
||||
consumed_cost_fen: number
|
||||
}>(totalsSql)
|
||||
const totals = totalsRes.rows[0] || {
|
||||
in_stock_count: 0,
|
||||
in_stock_cost_fen: 0,
|
||||
consumed_count: 0,
|
||||
consumed_cost_fen: 0,
|
||||
}
|
||||
|
||||
const buyerSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(buyer_name, ''), '未知') AS buyer_name,
|
||||
COUNT(*)::integer AS count,
|
||||
SUM(cost_price_fen)::integer AS total_cost_fen,
|
||||
COUNT(CASE WHEN status = 'consumed' THEN 1 END)::integer AS consumed_count
|
||||
FROM consignment_inventories
|
||||
GROUP BY buyer_name
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const buyerRes = await query<{
|
||||
buyer_name: string
|
||||
count: number
|
||||
total_cost_fen: number
|
||||
consumed_count: number
|
||||
}>(buyerSql)
|
||||
|
||||
const shopSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(shop_name, ''), '其他') AS shop_name,
|
||||
COUNT(*)::integer AS count
|
||||
FROM consignment_inventories
|
||||
WHERE status = 'consumed' AND shop_name <> ''
|
||||
GROUP BY shop_name
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const shopRes = await query<{ shop_name: string; count: number }>(shopSql)
|
||||
|
||||
const batchSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(inbound_batch, ''), '未分类') AS inbound_batch,
|
||||
COUNT(*)::integer AS count,
|
||||
SUM(cost_price_fen)::integer AS total_cost_fen
|
||||
FROM consignment_inventories
|
||||
GROUP BY inbound_batch
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const batchRes = await query<{ inbound_batch: string; count: number; total_cost_fen: number }>(
|
||||
batchSql,
|
||||
)
|
||||
|
||||
const seriesSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(series, ''), '未注明') AS series,
|
||||
diamond_level,
|
||||
COUNT(*)::integer AS count
|
||||
FROM consignment_inventories
|
||||
WHERE status = 'in_stock'
|
||||
GROUP BY series, diamond_level
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const seriesRes = await query<{ series: string; diamond_level: number; count: number }>(seriesSql)
|
||||
|
||||
return {
|
||||
totalInStockCount: totals.in_stock_count,
|
||||
totalInStockCostFen: totals.in_stock_cost_fen,
|
||||
totalConsumedCount: totals.consumed_count,
|
||||
totalConsumedCostFen: totals.consumed_cost_fen,
|
||||
byBuyer: buyerRes.rows,
|
||||
byShop: shopRes.rows,
|
||||
byBatch: batchRes.rows,
|
||||
bySeries: seriesRes.rows,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getConsignmentInventoryById(
|
||||
id: number | string,
|
||||
): Promise<ConsignmentInventoryRow | null> {
|
||||
const sql = `
|
||||
SELECT
|
||||
ci.*,
|
||||
wc.name AS category_name
|
||||
FROM consignment_inventories ci
|
||||
LEFT JOIN work_categories wc ON wc.id = ci.category_id
|
||||
WHERE ci.id = $1
|
||||
LIMIT 1
|
||||
`
|
||||
const res = await query<ConsignmentInventoryRow>(sql, [id])
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
export async function createConsignmentInventory(input: {
|
||||
inventoryNo: string
|
||||
gameName?: string
|
||||
categoryId?: number | null
|
||||
itemName: string
|
||||
system?: string
|
||||
serverZone?: string
|
||||
series?: string
|
||||
diamondLevel?: number
|
||||
brillianceCount?: number
|
||||
costPriceFen?: number
|
||||
buyerName?: string
|
||||
inboundBatch?: string
|
||||
sellerContact?: string
|
||||
sellerGameId?: string
|
||||
sellerPaymentInfo?: string
|
||||
sellerPhone?: string
|
||||
credentialsJson?: Record<string, unknown>
|
||||
screenshotsJson?: unknown[]
|
||||
remark?: string
|
||||
allowMatching?: boolean
|
||||
sourceType?: 'manual' | 'collection_link' | 'recycle'
|
||||
sourceSubmissionId?: number | null
|
||||
now: string
|
||||
}): Promise<ConsignmentInventoryRow> {
|
||||
const sql = `
|
||||
INSERT INTO consignment_inventories (
|
||||
inventory_no, game_name, category_id, item_name, system, server_zone,
|
||||
series, diamond_level, brilliance_count, cost_price_fen, buyer_name,
|
||||
inbound_batch, seller_contact, seller_game_id, seller_payment_info,
|
||||
seller_phone, credentials_json, screenshots_json, remark,
|
||||
allow_matching, status, allocation_status, source_type,
|
||||
source_submission_id, inbound_at, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10, $11,
|
||||
$12, $13, $14, $15,
|
||||
$16, $17::jsonb, $18::jsonb, $19,
|
||||
$20, 'in_stock', '待安排', $21,
|
||||
$22, $23, $23, $23
|
||||
)
|
||||
RETURNING *
|
||||
`
|
||||
const values = [
|
||||
input.inventoryNo,
|
||||
input.gameName || '和平精英',
|
||||
input.categoryId || null,
|
||||
input.itemName,
|
||||
input.system || '',
|
||||
input.serverZone || '',
|
||||
input.series || '',
|
||||
input.diamondLevel || 1,
|
||||
input.brillianceCount || 0,
|
||||
input.costPriceFen || 0,
|
||||
input.buyerName || '',
|
||||
input.inboundBatch || '',
|
||||
input.sellerContact || '',
|
||||
input.sellerGameId || '',
|
||||
input.sellerPaymentInfo || '',
|
||||
input.sellerPhone || '',
|
||||
toJsonString(input.credentialsJson || {}),
|
||||
toJsonString(input.screenshotsJson || []),
|
||||
input.remark || '',
|
||||
input.allowMatching !== false,
|
||||
input.sourceType || 'manual',
|
||||
input.sourceSubmissionId || null,
|
||||
input.now,
|
||||
]
|
||||
const res = await query<ConsignmentInventoryRow>(sql, values)
|
||||
return res.rows[0]!
|
||||
}
|
||||
|
||||
export async function updateConsignmentInventory(
|
||||
id: number | string,
|
||||
input: {
|
||||
itemName?: string | undefined
|
||||
system?: string | undefined
|
||||
serverZone?: string | undefined
|
||||
series?: string | undefined
|
||||
diamondLevel?: number | undefined
|
||||
brillianceCount?: number | undefined
|
||||
costPriceFen?: number | undefined
|
||||
buyerName?: string | undefined
|
||||
inboundBatch?: string | undefined
|
||||
sellerContact?: string | undefined
|
||||
sellerGameId?: string | undefined
|
||||
sellerPaymentInfo?: string | undefined
|
||||
sellerPhone?: string | undefined
|
||||
credentialsJson?: Record<string, unknown> | undefined
|
||||
screenshotsJson?: unknown[] | undefined
|
||||
remark?: string | undefined
|
||||
allowMatching?: boolean | undefined
|
||||
status?: 'in_stock' | 'consumed' | 'archived' | undefined
|
||||
allocationStatus?: string | undefined
|
||||
now: string
|
||||
},
|
||||
): Promise<ConsignmentInventoryRow | null> {
|
||||
const updates: string[] = []
|
||||
const values: unknown[] = []
|
||||
|
||||
if (input.itemName !== undefined) {
|
||||
values.push(input.itemName)
|
||||
updates.push(`item_name = $${values.length}`)
|
||||
}
|
||||
if (input.system !== undefined) {
|
||||
values.push(input.system)
|
||||
updates.push(`system = $${values.length}`)
|
||||
}
|
||||
if (input.serverZone !== undefined) {
|
||||
values.push(input.serverZone)
|
||||
updates.push(`server_zone = $${values.length}`)
|
||||
}
|
||||
if (input.series !== undefined) {
|
||||
values.push(input.series)
|
||||
updates.push(`series = $${values.length}`)
|
||||
}
|
||||
if (input.diamondLevel !== undefined) {
|
||||
values.push(input.diamondLevel)
|
||||
updates.push(`diamond_level = $${values.length}`)
|
||||
}
|
||||
if (input.brillianceCount !== undefined) {
|
||||
values.push(input.brillianceCount)
|
||||
updates.push(`brilliance_count = $${values.length}`)
|
||||
}
|
||||
if (input.costPriceFen !== undefined) {
|
||||
values.push(input.costPriceFen)
|
||||
updates.push(`cost_price_fen = $${values.length}`)
|
||||
}
|
||||
if (input.buyerName !== undefined) {
|
||||
values.push(input.buyerName)
|
||||
updates.push(`buyer_name = $${values.length}`)
|
||||
}
|
||||
if (input.inboundBatch !== undefined) {
|
||||
values.push(input.inboundBatch)
|
||||
updates.push(`inbound_batch = $${values.length}`)
|
||||
}
|
||||
if (input.sellerContact !== undefined) {
|
||||
values.push(input.sellerContact)
|
||||
updates.push(`seller_contact = $${values.length}`)
|
||||
}
|
||||
if (input.sellerGameId !== undefined) {
|
||||
values.push(input.sellerGameId)
|
||||
updates.push(`seller_game_id = $${values.length}`)
|
||||
}
|
||||
if (input.sellerPaymentInfo !== undefined) {
|
||||
values.push(input.sellerPaymentInfo)
|
||||
updates.push(`seller_payment_info = $${values.length}`)
|
||||
}
|
||||
if (input.sellerPhone !== undefined) {
|
||||
values.push(input.sellerPhone)
|
||||
updates.push(`seller_phone = $${values.length}`)
|
||||
}
|
||||
if (input.credentialsJson !== undefined) {
|
||||
values.push(toJsonString(input.credentialsJson))
|
||||
updates.push(`credentials_json = $${values.length}::jsonb`)
|
||||
}
|
||||
if (input.screenshotsJson !== undefined) {
|
||||
values.push(toJsonString(input.screenshotsJson))
|
||||
updates.push(`screenshots_json = $${values.length}::jsonb`)
|
||||
}
|
||||
if (input.remark !== undefined) {
|
||||
values.push(input.remark)
|
||||
updates.push(`remark = $${values.length}`)
|
||||
}
|
||||
if (input.allowMatching !== undefined) {
|
||||
values.push(input.allowMatching)
|
||||
updates.push(`allow_matching = $${values.length}`)
|
||||
}
|
||||
if (input.status !== undefined) {
|
||||
values.push(input.status)
|
||||
updates.push(`status = $${values.length}`)
|
||||
}
|
||||
if (input.allocationStatus !== undefined) {
|
||||
values.push(input.allocationStatus)
|
||||
updates.push(`allocation_status = $${values.length}`)
|
||||
}
|
||||
|
||||
if (updates.length === 0) return getConsignmentInventoryById(id)
|
||||
|
||||
values.push(input.now)
|
||||
updates.push(`updated_at = $${values.length}`)
|
||||
|
||||
values.push(id)
|
||||
const sql = `
|
||||
UPDATE consignment_inventories
|
||||
SET ${updates.join(', ')}
|
||||
WHERE id = $${values.length}
|
||||
RETURNING *
|
||||
`
|
||||
const res = await query<ConsignmentInventoryRow>(sql, values)
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
/** 退回复库(将已出库物品恢复为在库待匹配) */
|
||||
export async function returnConsignmentInventoryToStock(
|
||||
id: number | string,
|
||||
reason: string,
|
||||
now: string,
|
||||
): Promise<ConsignmentInventoryRow | null> {
|
||||
const sql = `
|
||||
UPDATE consignment_inventories
|
||||
SET
|
||||
status = 'in_stock',
|
||||
allocation_status = '待安排',
|
||||
bound_order_id = NULL,
|
||||
bound_work_order_id = NULL,
|
||||
bound_platform_order_id = '',
|
||||
shop_name = '',
|
||||
consumed_at = NULL,
|
||||
allow_matching = TRUE,
|
||||
remark = CASE
|
||||
WHEN $1 <> '' THEN remark || ' [退库原因: ' || $1 || ']'
|
||||
ELSE remark
|
||||
END,
|
||||
updated_at = $2
|
||||
WHERE id = $3 AND status = 'consumed'
|
||||
RETURNING *
|
||||
`
|
||||
const res = await query<ConsignmentInventoryRow>(sql, [reason, now, id])
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
/** 查询当前所有可供匹配的在库物品(status = 'in_stock' 且 allow_matching = TRUE) */
|
||||
export async function listActiveConsignmentInventoriesForMatching(options?: {
|
||||
gameName?: string
|
||||
categoryId?: number | null
|
||||
}): Promise<ConsignmentInventoryRow[]> {
|
||||
const conditions: string[] = ["status = 'in_stock'", 'allow_matching = TRUE']
|
||||
const values: unknown[] = []
|
||||
|
||||
if (options?.gameName) {
|
||||
values.push(options.gameName)
|
||||
conditions.push(`game_name = $${values.length}`)
|
||||
}
|
||||
if (options?.categoryId) {
|
||||
values.push(options.categoryId)
|
||||
conditions.push(`category_id = $${values.length}`)
|
||||
}
|
||||
|
||||
const sql = `
|
||||
SELECT *
|
||||
FROM consignment_inventories
|
||||
WHERE ${conditions.join(' AND ')}
|
||||
ORDER BY id ASC
|
||||
`
|
||||
const res = await query<ConsignmentInventoryRow>(sql, values)
|
||||
return res.rows
|
||||
}
|
||||
|
||||
// ---------------- 收集链接管理 ----------------
|
||||
|
||||
export async function listConsignmentCollectLinks(): Promise<ConsignmentCollectLinkRow[]> {
|
||||
const sql = `
|
||||
SELECT
|
||||
ccl.*,
|
||||
wc.name AS category_name,
|
||||
(SELECT COUNT(*)::integer FROM consignment_submissions cs WHERE cs.link_id = ccl.id) AS submission_count
|
||||
FROM consignment_collect_links ccl
|
||||
LEFT JOIN work_categories wc ON wc.id = ccl.category_id
|
||||
ORDER BY ccl.id DESC
|
||||
`
|
||||
const res = await query<ConsignmentCollectLinkRow>(sql)
|
||||
return res.rows
|
||||
}
|
||||
|
||||
export async function getConsignmentCollectLinkByToken(
|
||||
token: string,
|
||||
): Promise<ConsignmentCollectLinkRow | null> {
|
||||
const sql = `
|
||||
SELECT
|
||||
ccl.*,
|
||||
wc.name AS category_name
|
||||
FROM consignment_collect_links ccl
|
||||
LEFT JOIN work_categories wc ON wc.id = ccl.category_id
|
||||
WHERE ccl.token = $1 AND ccl.enabled = TRUE
|
||||
LIMIT 1
|
||||
`
|
||||
const res = await query<ConsignmentCollectLinkRow>(sql, [token])
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
export async function createConsignmentCollectLink(input: {
|
||||
token: string
|
||||
title: string
|
||||
gameName?: string
|
||||
categoryId?: number | null
|
||||
system?: string
|
||||
serverZone?: string
|
||||
series?: string
|
||||
diamondLevel?: number
|
||||
brillianceCount?: number
|
||||
presetPriceFen?: number
|
||||
inboundBatch?: string
|
||||
buyerName?: string
|
||||
templateJson?: Record<string, unknown>
|
||||
isMultiUse?: boolean
|
||||
expiresAt?: string | null
|
||||
createdBy?: string
|
||||
now: string
|
||||
}): Promise<ConsignmentCollectLinkRow> {
|
||||
const sql = `
|
||||
INSERT INTO consignment_collect_links (
|
||||
token, title, game_name, category_id, system, server_zone,
|
||||
series, diamond_level, brilliance_count, preset_price_fen,
|
||||
inbound_batch, buyer_name, template_json, is_multi_use,
|
||||
expires_at, enabled, created_by, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10,
|
||||
$11, $12, $13::jsonb, $14,
|
||||
$15, TRUE, $16, $17, $17
|
||||
)
|
||||
RETURNING *
|
||||
`
|
||||
const res = await query<ConsignmentCollectLinkRow>(sql, [
|
||||
input.token,
|
||||
input.title,
|
||||
input.gameName || '和平精英',
|
||||
input.categoryId || null,
|
||||
input.system || '',
|
||||
input.serverZone || '',
|
||||
input.series || '',
|
||||
input.diamondLevel || 1,
|
||||
input.brillianceCount || 0,
|
||||
input.presetPriceFen || 0,
|
||||
input.inboundBatch || '',
|
||||
input.buyerName || '',
|
||||
toJsonString(input.templateJson || {}),
|
||||
input.isMultiUse !== false,
|
||||
input.expiresAt || null,
|
||||
input.createdBy || '',
|
||||
input.now,
|
||||
])
|
||||
return res.rows[0]!
|
||||
}
|
||||
|
||||
export async function updateConsignmentCollectLink(
|
||||
id: number | string,
|
||||
input: {
|
||||
title?: string | undefined
|
||||
enabled?: boolean | undefined
|
||||
now: string
|
||||
},
|
||||
): Promise<ConsignmentCollectLinkRow | null> {
|
||||
const updates: string[] = []
|
||||
const values: unknown[] = []
|
||||
|
||||
if (input.title !== undefined) {
|
||||
values.push(input.title)
|
||||
updates.push(`title = $${values.length}`)
|
||||
}
|
||||
if (input.enabled !== undefined) {
|
||||
values.push(input.enabled)
|
||||
updates.push(`enabled = $${values.length}`)
|
||||
}
|
||||
|
||||
values.push(input.now)
|
||||
updates.push(`updated_at = $${values.length}`)
|
||||
|
||||
values.push(id)
|
||||
const sql = `
|
||||
UPDATE consignment_collect_links
|
||||
SET ${updates.join(', ')}
|
||||
WHERE id = $${values.length}
|
||||
RETURNING *
|
||||
`
|
||||
const res = await query<ConsignmentCollectLinkRow>(sql, values)
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
// ---------------- 玩家提报与审核 ----------------
|
||||
|
||||
export async function listConsignmentSubmissions(filters: {
|
||||
status?: string | undefined
|
||||
linkId?: number | undefined
|
||||
keyword?: string | undefined
|
||||
page?: number | undefined
|
||||
pageSize?: number | undefined
|
||||
}): Promise<{ items: ConsignmentSubmissionRow[]; total: number }> {
|
||||
const conditions: string[] = []
|
||||
const values: unknown[] = []
|
||||
|
||||
if (filters.status) {
|
||||
values.push(filters.status)
|
||||
conditions.push(`cs.status = $${values.length}`)
|
||||
}
|
||||
if (filters.linkId) {
|
||||
values.push(filters.linkId)
|
||||
conditions.push(`cs.link_id = $${values.length}`)
|
||||
}
|
||||
if (filters.keyword) {
|
||||
values.push(`%${filters.keyword.trim()}%`)
|
||||
conditions.push(
|
||||
`(cs.item_name ILIKE $${values.length} OR cs.submission_no ILIKE $${values.length} OR cs.contact_info ILIKE $${values.length} OR cs.seller_game_id ILIKE $${values.length} OR cs.seller_phone ILIKE $${values.length})`,
|
||||
)
|
||||
}
|
||||
|
||||
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
|
||||
const countSql = `SELECT COUNT(*)::integer AS total FROM consignment_submissions cs ${whereClause}`
|
||||
const countRes = await query<{ total: number }>(countSql, values)
|
||||
const total = countRes.rows[0]?.total || 0
|
||||
|
||||
const page = Math.max(1, filters.page || 1)
|
||||
const pageSize = Math.max(1, Math.min(100, filters.pageSize || 20))
|
||||
const offset = (page - 1) * pageSize
|
||||
|
||||
values.push(pageSize, offset)
|
||||
const listSql = `
|
||||
SELECT
|
||||
cs.*,
|
||||
wc.name AS category_name,
|
||||
ccl.title AS link_title
|
||||
FROM consignment_submissions cs
|
||||
LEFT JOIN work_categories wc ON wc.id = cs.category_id
|
||||
LEFT JOIN consignment_collect_links ccl ON ccl.id = cs.link_id
|
||||
${whereClause}
|
||||
ORDER BY cs.id DESC
|
||||
LIMIT $${values.length - 1} OFFSET $${values.length}
|
||||
`
|
||||
const listRes = await query<ConsignmentSubmissionRow>(listSql, values)
|
||||
return { items: listRes.rows, total }
|
||||
}
|
||||
|
||||
export async function getConsignmentSubmissionById(
|
||||
id: number | string,
|
||||
): Promise<ConsignmentSubmissionRow | null> {
|
||||
const sql = `
|
||||
SELECT
|
||||
cs.*,
|
||||
wc.name AS category_name,
|
||||
ccl.title AS link_title
|
||||
FROM consignment_submissions cs
|
||||
LEFT JOIN work_categories wc ON wc.id = cs.category_id
|
||||
LEFT JOIN consignment_collect_links ccl ON ccl.id = cs.link_id
|
||||
WHERE cs.id = $1
|
||||
LIMIT 1
|
||||
`
|
||||
const res = await query<ConsignmentSubmissionRow>(sql, [id])
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
export async function createConsignmentSubmission(input: {
|
||||
submissionNo: string
|
||||
linkId?: number | null
|
||||
gameName?: string
|
||||
categoryId?: number | null
|
||||
itemName: string
|
||||
system?: string
|
||||
serverZone?: string
|
||||
series?: string
|
||||
diamondLevel?: number
|
||||
brillianceCount?: number
|
||||
sellerGameId?: string
|
||||
sellerPaymentInfo?: string
|
||||
sellerPhone?: string
|
||||
contactInfo?: string
|
||||
contentJson?: Record<string, unknown>
|
||||
screenshotsJson?: unknown[]
|
||||
askingPriceFen?: number
|
||||
now: string
|
||||
}): Promise<ConsignmentSubmissionRow> {
|
||||
const sql = `
|
||||
INSERT INTO consignment_submissions (
|
||||
submission_no, link_id, game_name, category_id, item_name,
|
||||
system, server_zone, series, diamond_level, brilliance_count,
|
||||
seller_game_id, seller_payment_info, seller_phone, contact_info,
|
||||
content_json, screenshots_json, asking_price_fen,
|
||||
status, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, $9, $10,
|
||||
$11, $12, $13, $14,
|
||||
$15::jsonb, $16::jsonb, $17,
|
||||
'pending_review', $18, $18
|
||||
)
|
||||
RETURNING *
|
||||
`
|
||||
const res = await query<ConsignmentSubmissionRow>(sql, [
|
||||
input.submissionNo,
|
||||
input.linkId || null,
|
||||
input.gameName || '和平精英',
|
||||
input.categoryId || null,
|
||||
input.itemName,
|
||||
input.system || '',
|
||||
input.serverZone || '',
|
||||
input.series || '',
|
||||
input.diamondLevel || 1,
|
||||
input.brillianceCount || 0,
|
||||
input.sellerGameId || '',
|
||||
input.sellerPaymentInfo || '',
|
||||
input.sellerPhone || '',
|
||||
input.contactInfo || '',
|
||||
toJsonString(input.contentJson || {}),
|
||||
toJsonString(input.screenshotsJson || []),
|
||||
input.askingPriceFen || 0,
|
||||
input.now,
|
||||
])
|
||||
return res.rows[0]!
|
||||
}
|
||||
|
||||
export async function rejectConsignmentSubmission(input: {
|
||||
submissionId: number | string
|
||||
reviewNote: string
|
||||
reviewerName: string
|
||||
now: string
|
||||
}): Promise<ConsignmentSubmissionRow | null> {
|
||||
const sql = `
|
||||
UPDATE consignment_submissions
|
||||
SET
|
||||
status = 'rejected',
|
||||
review_note = $1,
|
||||
reviewer_name = $2,
|
||||
reviewed_at = $3,
|
||||
updated_at = $3
|
||||
WHERE id = $4 AND status = 'pending_review'
|
||||
RETURNING *
|
||||
`
|
||||
const res = await query<ConsignmentSubmissionRow>(sql, [
|
||||
input.reviewNote,
|
||||
input.reviewerName,
|
||||
input.now,
|
||||
input.submissionId,
|
||||
])
|
||||
return res.rows[0] || null
|
||||
}
|
||||
|
||||
/** 审核通过并入库(原子事务) */
|
||||
export async function approveConsignmentSubmissionWithInbound(input: {
|
||||
submissionId: number | string
|
||||
inventoryNo: string
|
||||
buyerName: string
|
||||
inboundBatch?: string | undefined
|
||||
itemName: string
|
||||
system?: string | undefined
|
||||
serverZone?: string | undefined
|
||||
series?: string | undefined
|
||||
diamondLevel?: number | undefined
|
||||
brillianceCount?: number | undefined
|
||||
costPriceFen: number
|
||||
sellerContact?: string | undefined
|
||||
sellerGameId?: string | undefined
|
||||
sellerPaymentInfo?: string | undefined
|
||||
sellerPhone?: string | undefined
|
||||
credentialsJson?: Record<string, unknown> | undefined
|
||||
screenshotsJson?: unknown[] | undefined
|
||||
reviewNote?: string | undefined
|
||||
now: string
|
||||
}): Promise<{ submission: ConsignmentSubmissionRow; inventory: ConsignmentInventoryRow }> {
|
||||
return withTransaction(async (client: PoolClient) => {
|
||||
const subRes = await client.query<ConsignmentSubmissionRow>(
|
||||
'SELECT * FROM consignment_submissions WHERE id = $1 LIMIT 1 FOR UPDATE',
|
||||
[input.submissionId],
|
||||
)
|
||||
const submission = subRes.rows[0]
|
||||
if (!submission) throw new Error('提报单不存在')
|
||||
if (submission.status !== 'pending_review') throw new Error('提报单已被处理')
|
||||
|
||||
const invSql = `
|
||||
INSERT INTO consignment_inventories (
|
||||
inventory_no, game_name, category_id, item_name, system, server_zone,
|
||||
series, diamond_level, brilliance_count, cost_price_fen, buyer_name,
|
||||
inbound_batch, seller_contact, seller_game_id, seller_payment_info,
|
||||
seller_phone, credentials_json, screenshots_json, remark,
|
||||
allow_matching, status, allocation_status, source_type,
|
||||
source_submission_id, inbound_at, created_at, updated_at
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10, $11,
|
||||
$12, $13, $14, $15,
|
||||
$16, $17::jsonb, $18::jsonb, $19,
|
||||
TRUE, 'in_stock', '待安排', 'collection_link',
|
||||
$20, $21, $21, $21
|
||||
)
|
||||
RETURNING *
|
||||
`
|
||||
const invValues = [
|
||||
input.inventoryNo,
|
||||
submission.game_name || '和平精英',
|
||||
submission.category_id,
|
||||
input.itemName || submission.item_name,
|
||||
input.system || submission.system,
|
||||
input.serverZone || submission.server_zone,
|
||||
input.series || submission.series,
|
||||
input.diamondLevel || submission.diamond_level || 1,
|
||||
input.brillianceCount ?? submission.brilliance_count ?? 0,
|
||||
input.costPriceFen,
|
||||
input.buyerName,
|
||||
input.inboundBatch || '',
|
||||
input.sellerContact || submission.contact_info,
|
||||
input.sellerGameId || submission.seller_game_id,
|
||||
input.sellerPaymentInfo || submission.seller_payment_info,
|
||||
input.sellerPhone || submission.seller_phone,
|
||||
toJsonString(input.credentialsJson || submission.content_json || {}),
|
||||
toJsonString(input.screenshotsJson || submission.screenshots_json || []),
|
||||
input.reviewNote || '',
|
||||
submission.id,
|
||||
input.now,
|
||||
]
|
||||
const invRes = await client.query<ConsignmentInventoryRow>(invSql, invValues)
|
||||
const inventory = invRes.rows[0]!
|
||||
|
||||
const updateSubSql = `
|
||||
UPDATE consignment_submissions
|
||||
SET
|
||||
status = 'approved',
|
||||
result_inventory_id = $1,
|
||||
reviewer_name = $2,
|
||||
review_note = $3,
|
||||
reviewed_at = $4,
|
||||
updated_at = $4
|
||||
WHERE id = $5
|
||||
RETURNING *
|
||||
`
|
||||
const updateSubRes = await client.query<ConsignmentSubmissionRow>(updateSubSql, [
|
||||
inventory.id,
|
||||
input.buyerName,
|
||||
input.reviewNote || '',
|
||||
input.now,
|
||||
submission.id,
|
||||
])
|
||||
|
||||
return {
|
||||
submission: updateSubRes.rows[0]!,
|
||||
inventory,
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user