feat(consignment): 寄售仓库按寄卖分类筛选并支持动态规格过滤
This commit is contained in:
@@ -150,6 +150,58 @@ export type ConsignmentStatsSummary = {
|
||||
bySeries: Array<{ series: string; diamond_level: number; count: number }>
|
||||
}
|
||||
|
||||
/** 寄售仓库按寄卖配置动态规格筛选:key 走白名单,值一律参数绑定。 */
|
||||
export type ConsignmentInventorySpecFilter = {
|
||||
key: string
|
||||
value: string
|
||||
/** exact = 等值(选项/数字/日期);contains = 模糊包含(文本) */
|
||||
match?: 'exact' | 'contains'
|
||||
}
|
||||
|
||||
/**
|
||||
* 内置规格 key → 库存旧列。
|
||||
* 老库存的 spec_values_json 可能为空(动态规格上线前入库),此时按旧列兜底,避免筛不到历史货。
|
||||
*/
|
||||
const LEGACY_SPEC_COLUMN_BY_KEY: Record<string, string> = {
|
||||
system: 'ci.system',
|
||||
serverZone: 'ci.server_zone',
|
||||
series: 'ci.series',
|
||||
diamondLevel: 'ci.diamond_level',
|
||||
brillianceCount: 'ci.brilliance_count',
|
||||
}
|
||||
|
||||
const SPEC_FILTER_KEY_PATTERN = /^[A-Za-z][A-Za-z0-9_]{0,39}$/
|
||||
|
||||
/** 把动态规格筛选追加到 conditions(values 同步 push,保持占位符顺序)。 */
|
||||
export function buildConsignmentSpecFilterConditions(
|
||||
specFilters: ConsignmentInventorySpecFilter[] | undefined,
|
||||
values: unknown[],
|
||||
conditions: string[],
|
||||
) {
|
||||
for (const filter of specFilters || []) {
|
||||
const key = String(filter?.key || '').trim()
|
||||
const value = String(filter?.value ?? '').trim()
|
||||
if (!key || !value || !SPEC_FILTER_KEY_PATTERN.test(key)) continue
|
||||
|
||||
values.push(key)
|
||||
const keyIndex = values.length
|
||||
if (filter.match === 'contains') {
|
||||
values.push(`%${value}%`)
|
||||
conditions.push(`ci.spec_values_json ->> $${keyIndex} ILIKE $${values.length}`)
|
||||
continue
|
||||
}
|
||||
|
||||
values.push(value)
|
||||
const valueIndex = values.length
|
||||
const legacyColumn = LEGACY_SPEC_COLUMN_BY_KEY[key]
|
||||
conditions.push(
|
||||
legacyColumn
|
||||
? `COALESCE(NULLIF(ci.spec_values_json ->> $${keyIndex}, ''), ${legacyColumn}::text) = $${valueIndex}`
|
||||
: `ci.spec_values_json ->> $${keyIndex} = $${valueIndex}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function listConsignmentInventories(filters: {
|
||||
status?: string | undefined
|
||||
buyerName?: string | undefined
|
||||
@@ -165,6 +217,8 @@ export async function listConsignmentInventories(filters: {
|
||||
keyword?: string | undefined
|
||||
dateFrom?: string | undefined
|
||||
dateTo?: string | undefined
|
||||
/** 按寄卖配置动态规格筛选(key -> 选中值);key 为内置规格时回退库存旧列 */
|
||||
specFilters?: ConsignmentInventorySpecFilter[] | undefined
|
||||
page?: number | undefined
|
||||
pageSize?: number | undefined
|
||||
}): Promise<{ items: ConsignmentInventoryRow[]; total: number }> {
|
||||
@@ -215,6 +269,7 @@ export async function listConsignmentInventories(filters: {
|
||||
values.push(filters.allowMatching)
|
||||
conditions.push(`ci.allow_matching = $${values.length}`)
|
||||
}
|
||||
buildConsignmentSpecFilterConditions(filters.specFilters, values, conditions)
|
||||
if (filters.keyword) {
|
||||
values.push(`%${filters.keyword.trim()}%`)
|
||||
conditions.push(
|
||||
@@ -290,21 +345,56 @@ export async function listConsignmentInventories(filters: {
|
||||
return { items: listRes.rows, total }
|
||||
}
|
||||
|
||||
export async function getConsignmentInventoryStats(): Promise<ConsignmentStatsSummary> {
|
||||
export async function getConsignmentInventoryStats(
|
||||
filters: {
|
||||
categoryId?: number | undefined
|
||||
specFilters?: ConsignmentInventorySpecFilter[] | undefined
|
||||
dateFrom?: string | undefined
|
||||
dateTo?: string | undefined
|
||||
} = {},
|
||||
): Promise<ConsignmentStatsSummary> {
|
||||
const conditions: string[] = []
|
||||
const values: unknown[] = []
|
||||
|
||||
if (filters.categoryId) {
|
||||
values.push(filters.categoryId)
|
||||
conditions.push(`ci.category_id = $${values.length}`)
|
||||
}
|
||||
buildConsignmentSpecFilterConditions(filters.specFilters, values, conditions)
|
||||
if (filters.dateFrom) {
|
||||
values.push(filters.dateFrom)
|
||||
conditions.push(
|
||||
`ci.inbound_at >= ($${values.length}::date::timestamp AT TIME ZONE 'Asia/Shanghai')`,
|
||||
)
|
||||
}
|
||||
if (filters.dateTo) {
|
||||
values.push(filters.dateTo)
|
||||
conditions.push(
|
||||
`ci.inbound_at < (($${values.length}::date + INTERVAL '1 day')::timestamp AT TIME ZONE 'Asia/Shanghai')`,
|
||||
)
|
||||
}
|
||||
|
||||
/** 统计口径跟着寄售仓库的筛选走:分类 + 动态规格 + 入库日期。 */
|
||||
const whereWith = (...extra: string[]) => {
|
||||
const all = [...conditions, ...extra]
|
||||
return all.length > 0 ? `WHERE ${all.join(' AND ')}` : ''
|
||||
}
|
||||
|
||||
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
|
||||
COALESCE(SUM(CASE WHEN ci.status = 'in_stock' THEN 1 ELSE 0 END), 0)::integer AS in_stock_count,
|
||||
COALESCE(SUM(CASE WHEN ci.status = 'in_stock' THEN ci.cost_price_fen ELSE 0 END), 0)::integer AS in_stock_cost_fen,
|
||||
COALESCE(SUM(CASE WHEN ci.status = 'consumed' THEN 1 ELSE 0 END), 0)::integer AS consumed_count,
|
||||
COALESCE(SUM(CASE WHEN ci.status = 'consumed' THEN ci.cost_price_fen ELSE 0 END), 0)::integer AS consumed_cost_fen
|
||||
FROM consignment_inventories ci
|
||||
${whereWith()}
|
||||
`
|
||||
const totalsRes = await query<{
|
||||
in_stock_count: number
|
||||
in_stock_cost_fen: number
|
||||
consumed_count: number
|
||||
consumed_cost_fen: number
|
||||
}>(totalsSql)
|
||||
}>(totalsSql, values)
|
||||
const totals = totalsRes.rows[0] || {
|
||||
in_stock_count: 0,
|
||||
in_stock_cost_fen: 0,
|
||||
@@ -314,12 +404,13 @@ export async function getConsignmentInventoryStats(): Promise<ConsignmentStatsSu
|
||||
|
||||
const buyerSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(buyer_name, ''), '未知') AS buyer_name,
|
||||
COALESCE(NULLIF(ci.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
|
||||
SUM(ci.cost_price_fen)::integer AS total_cost_fen,
|
||||
COUNT(CASE WHEN ci.status = 'consumed' THEN 1 END)::integer AS consumed_count
|
||||
FROM consignment_inventories ci
|
||||
${whereWith()}
|
||||
GROUP BY ci.buyer_name
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
@@ -328,46 +419,51 @@ export async function getConsignmentInventoryStats(): Promise<ConsignmentStatsSu
|
||||
count: number
|
||||
total_cost_fen: number
|
||||
consumed_count: number
|
||||
}>(buyerSql)
|
||||
}>(buyerSql, values)
|
||||
|
||||
const shopSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(shop_name, ''), '其他') AS shop_name,
|
||||
COALESCE(NULLIF(ci.shop_name, ''), '其他') AS shop_name,
|
||||
COUNT(*)::integer AS count
|
||||
FROM consignment_inventories
|
||||
WHERE status = 'consumed' AND shop_name <> ''
|
||||
GROUP BY shop_name
|
||||
FROM consignment_inventories ci
|
||||
${whereWith(`ci.status = 'consumed'`, `ci.shop_name <> ''`)}
|
||||
GROUP BY ci.shop_name
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const shopRes = await query<{ shop_name: string; count: number }>(shopSql)
|
||||
const shopRes = await query<{ shop_name: string; count: number }>(shopSql, values)
|
||||
|
||||
const batchSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(inbound_batch, ''), '未分类') AS inbound_batch,
|
||||
COALESCE(NULLIF(ci.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
|
||||
SUM(ci.cost_price_fen)::integer AS total_cost_fen
|
||||
FROM consignment_inventories ci
|
||||
${whereWith()}
|
||||
GROUP BY ci.inbound_batch
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const batchRes = await query<{ inbound_batch: string; count: number; total_cost_fen: number }>(
|
||||
batchSql,
|
||||
values,
|
||||
)
|
||||
|
||||
const seriesSql = `
|
||||
SELECT
|
||||
COALESCE(NULLIF(series, ''), '未注明') AS series,
|
||||
diamond_level,
|
||||
COALESCE(NULLIF(ci.series, ''), '未注明') AS series,
|
||||
ci.diamond_level,
|
||||
COUNT(*)::integer AS count
|
||||
FROM consignment_inventories
|
||||
WHERE status = 'in_stock'
|
||||
GROUP BY series, diamond_level
|
||||
FROM consignment_inventories ci
|
||||
${whereWith(`ci.status = 'in_stock'`)}
|
||||
GROUP BY ci.series, ci.diamond_level
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
`
|
||||
const seriesRes = await query<{ series: string; diamond_level: number; count: number }>(seriesSql)
|
||||
const seriesRes = await query<{ series: string; diamond_level: number; count: number }>(
|
||||
seriesSql,
|
||||
values,
|
||||
)
|
||||
|
||||
return {
|
||||
totalInStockCount: totals.in_stock_count,
|
||||
|
||||
Reference in New Issue
Block a user