merge: 合并 main 并保留寄卖内部匹配与客户退款

This commit is contained in:
2026-11-05 18:02:47 +08:00
105 changed files with 7248 additions and 665 deletions
@@ -28,6 +28,7 @@ export type OrderServiceProfileRow = {
export type OrderServicePublishedProfileRow = {
profile_id: number
category_id: number
source_category_id: number
version_id: number
version_no: number
config_json: Record<string, unknown>
@@ -221,12 +222,29 @@ export async function backfillPendingOrderServiceSnapshotsForCategory(
): Promise<number> {
const result = await client.query(
`
WITH affected_categories AS (
SELECT $1::bigint AS category_id
UNION
SELECT child.id
FROM work_categories child
WHERE child.parent_id = $1
AND NOT EXISTS (
SELECT 1
FROM order_service_profiles child_profile
JOIN order_service_profile_versions child_version
ON child_version.id = child_profile.current_version_id
WHERE child_profile.category_id = child.id
AND child_profile.status <> 'archived'
)
)
UPDATE work_orders wo
SET service_profile_id = $2,
service_profile_version_id = $3,
service_profile_snapshot_json = jsonb_build_object(
'profileId', p.id,
'categoryId', p.category_id,
'categoryId', wo.category_id,
'sourceCategoryId', p.category_id,
'inherited', wo.category_id <> p.category_id,
'versionId', v.id,
'versionNo', v.version_no,
'config', v.config_json
@@ -240,7 +258,7 @@ export async function backfillPendingOrderServiceSnapshotsForCategory(
WHERE p.id = $2
AND p.category_id = $1
AND v.id = $3
AND wo.category_id = p.category_id
AND wo.category_id IN (SELECT category_id FROM affected_categories)
AND wo.status = 'pending_material'
AND wo.assigned_worker_id IS NULL
AND NOT EXISTS (
@@ -295,6 +313,7 @@ export async function getPublishedOrderServiceProfileByCategory(
SELECT
p.id AS profile_id,
p.category_id,
p.category_id AS source_category_id,
v.id AS version_id,
v.version_no,
v.config_json
@@ -311,6 +330,57 @@ export async function getPublishedOrderServiceProfileByCategory(
? {
profile_id: Number(row.profile_id),
category_id: Number(row.category_id),
source_category_id: Number(row.source_category_id),
version_id: Number(row.version_id),
version_no: Number(row.version_no),
config_json: row.config_json,
}
: null
}
/**
* 分类配置解析:先从当前二级分类取已发布版本,没有再逐级继承父分类。
* 返回值中的 source_category_id 表示最终命中的配置所属分类。
*/
export async function getPublishedOrderServiceProfileByCategoryWithInheritance(
categoryId: number | string,
client?: Pick<PoolClient, 'query'>,
): Promise<OrderServicePublishedProfileRow | null> {
const db = client || { query }
const result = await db.query<OrderServicePublishedProfileRow>(
`
WITH RECURSIVE category_chain AS (
SELECT id, parent_id, 0 AS depth
FROM work_categories
WHERE id = $1
UNION ALL
SELECT parent.id, parent.parent_id, child.depth + 1
FROM work_categories parent
JOIN category_chain child ON parent.id = child.parent_id
WHERE child.depth < 8
)
SELECT
p.id AS profile_id,
p.category_id,
chain.id AS source_category_id,
v.id AS version_id,
v.version_no,
v.config_json
FROM category_chain chain
JOIN order_service_profiles p ON p.category_id = chain.id
JOIN order_service_profile_versions v ON v.id = p.current_version_id
WHERE p.status <> 'archived'
ORDER BY chain.depth ASC
LIMIT 1
`,
[Number(categoryId)],
)
const row = result.rows[0]
return row
? {
profile_id: Number(row.profile_id),
category_id: Number(row.category_id),
source_category_id: Number(row.source_category_id),
version_id: Number(row.version_id),
version_no: Number(row.version_no),
config_json: row.config_json,
@@ -298,7 +298,10 @@ export async function getWorkOrderByOrderItemId(
orderItemId: number | string,
): Promise<WorkOrderRow | null> {
const result = await query<WorkOrderRow>(
`${WORK_ORDER_SELECT} WHERE wo.order_item_id = $1 LIMIT 1`,
`${WORK_ORDER_SELECT}
WHERE wo.order_item_id = $1
ORDER BY (wo.status <> 'cancelled') DESC, wo.id DESC
LIMIT 1`,
[Number(orderItemId)],
)
return result.rows[0] || null