feat(fulfillment): 支持删除待分类和强制删除渠道规则
This commit is contained in:
@@ -902,6 +902,7 @@ export type PendingGroupRow = {
|
||||
sku_name: string
|
||||
providers: string[]
|
||||
shop_ids: string[]
|
||||
shops: Array<{ id: string; name: string }>
|
||||
sku_codes: string[]
|
||||
pending_reasons: string[]
|
||||
item_count: number
|
||||
@@ -910,6 +911,16 @@ export type PendingGroupRow = {
|
||||
last_seen_at: string
|
||||
}
|
||||
|
||||
type PendingShopNameRow = {
|
||||
internal_sku: string
|
||||
provider: string
|
||||
shop_id: string
|
||||
shop_name: string
|
||||
usage_count: number
|
||||
}
|
||||
|
||||
type PendingGroupDbRow = Omit<PendingGroupRow, 'shops'>
|
||||
|
||||
/** 待分类队列:按全局内部 SKU 聚合(跨平台、跨店铺合并为一行)。 */
|
||||
export async function listPendingChannelGroups(input: {
|
||||
page?: number
|
||||
@@ -943,7 +954,7 @@ export async function listPendingChannelGroups(input: {
|
||||
)
|
||||
|
||||
params.push(pageSize, (page - 1) * pageSize)
|
||||
const rows = await query<PendingGroupRow>(
|
||||
const rows = await query<PendingGroupDbRow>(
|
||||
`
|
||||
SELECT
|
||||
${INTERNAL_SKU_SQL} AS internal_sku,
|
||||
@@ -966,7 +977,213 @@ export async function listPendingChannelGroups(input: {
|
||||
params,
|
||||
)
|
||||
|
||||
return { items: rows.rows, total: total.rows[0]?.count || 0, page, pageSize }
|
||||
const internalSkus = rows.rows.map((row) => row.internal_sku)
|
||||
const shopsBySku = new Map<string, Array<{ id: string; name: string }>>()
|
||||
if (internalSkus.length > 0) {
|
||||
const shopRows = await query<PendingShopNameRow>(
|
||||
`
|
||||
SELECT
|
||||
${INTERNAL_SKU_SQL} AS internal_sku,
|
||||
o.provider,
|
||||
o.shop_id,
|
||||
COALESCE(NULLIF(BTRIM(o.shop_name), ''), o.shop_id) AS shop_name,
|
||||
COUNT(*)::int AS usage_count
|
||||
FROM order_items oi
|
||||
JOIN orders o ON o.id = oi.order_id
|
||||
WHERE oi.fulfillment_status = 'pending'
|
||||
AND ${INTERNAL_SKU_SQL} = ANY($1::text[])
|
||||
AND BTRIM(o.shop_id) <> ''
|
||||
GROUP BY ${INTERNAL_SKU_SQL}, o.provider, o.shop_id, o.shop_name
|
||||
ORDER BY
|
||||
internal_sku,
|
||||
provider,
|
||||
shop_id,
|
||||
CASE
|
||||
WHEN BTRIM(o.shop_name) = '' OR BTRIM(o.shop_name) = o.shop_id THEN 3
|
||||
WHEN BTRIM(o.shop_name) = '91卡券' OR BTRIM(o.shop_name) LIKE '快手店铺-%' THEN 2
|
||||
WHEN BTRIM(o.shop_name) ~ '[一-龥]' THEN 0
|
||||
ELSE 1
|
||||
END,
|
||||
usage_count DESC,
|
||||
shop_name
|
||||
`,
|
||||
[internalSkus],
|
||||
)
|
||||
|
||||
const seen = new Set<string>()
|
||||
for (const row of shopRows.rows) {
|
||||
const shopKey = `${row.internal_sku}\u0000${row.shop_id}`
|
||||
if (seen.has(shopKey)) continue
|
||||
seen.add(shopKey)
|
||||
const shops = shopsBySku.get(row.internal_sku) || []
|
||||
shops.push({ id: row.shop_id, name: row.shop_name || row.shop_id })
|
||||
shopsBySku.set(row.internal_sku, shops)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
items: rows.rows.map((row) => ({
|
||||
...row,
|
||||
shops:
|
||||
shopsBySku.get(row.internal_sku) ||
|
||||
row.shop_ids.map((shopId) => ({ id: shopId, name: shopId })),
|
||||
})),
|
||||
total: total.rows[0]?.count || 0,
|
||||
page,
|
||||
pageSize,
|
||||
}
|
||||
}
|
||||
|
||||
export type DeletePendingChannelGroupResult = {
|
||||
archivedItems: number
|
||||
preservedOrders: number
|
||||
deletedCostSnapshots: number
|
||||
resolvedExceptions: number
|
||||
refreshedDays: number
|
||||
skippedClosedDays: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除待分类 SKU:只归档订单项并清理其成本/异常,不删除原始订单。
|
||||
* 已关账月份拒绝操作,避免已固化财务口径与底层数据不一致。
|
||||
*/
|
||||
export async function deletePendingChannelGroup(input: {
|
||||
internalSku: string
|
||||
adminUserId: number | null
|
||||
}): Promise<DeletePendingChannelGroupResult> {
|
||||
const internalSku = canonicalizeInternalSku(input.internalSku)
|
||||
if (!internalSku || !containsCjk(internalSku)) {
|
||||
throw createHttpError('必须使用包含中文的内部 SKU', {
|
||||
statusCode: 400,
|
||||
errorCode: 'internal_sku_required',
|
||||
})
|
||||
}
|
||||
|
||||
const result = await withTransaction(async (client) => {
|
||||
const items = await client.query<{ id: number; order_id: number; basis_date: string }>(
|
||||
`
|
||||
SELECT
|
||||
oi.id,
|
||||
oi.order_id,
|
||||
(COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date::text
|
||||
AS basis_date
|
||||
FROM order_items oi
|
||||
JOIN orders o ON o.id = oi.order_id
|
||||
WHERE oi.fulfillment_status = 'pending'
|
||||
AND ${INTERNAL_SKU_SQL} = $1
|
||||
ORDER BY oi.id
|
||||
FOR UPDATE OF oi
|
||||
`,
|
||||
[internalSku],
|
||||
)
|
||||
const itemIds = items.rows.map((row) => Number(row.id))
|
||||
if (itemIds.length === 0) {
|
||||
return {
|
||||
archivedItems: 0,
|
||||
preservedOrders: 0,
|
||||
deletedCostSnapshots: 0,
|
||||
resolvedExceptions: 0,
|
||||
dates: [] as string[],
|
||||
}
|
||||
}
|
||||
|
||||
const periodKeys = [
|
||||
...new Set(items.rows.map((row) => String(row.basis_date || '').slice(0, 7)).filter(Boolean)),
|
||||
]
|
||||
const closedPeriods = await client.query<{ period_key: string }>(
|
||||
`
|
||||
SELECT period_key
|
||||
FROM finance_periods
|
||||
WHERE status = 'closed'
|
||||
AND period_key = ANY($1::text[])
|
||||
ORDER BY period_key
|
||||
`,
|
||||
[periodKeys],
|
||||
)
|
||||
if (closedPeriods.rows.length > 0) {
|
||||
throw createHttpError(
|
||||
`包含已关账期间(${closedPeriods.rows.map((row) => row.period_key).join('、')}),不能删除`,
|
||||
{
|
||||
statusCode: 409,
|
||||
errorCode: 'finance_period_closed',
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
const snapshots = await client.query(
|
||||
`DELETE FROM cost_snapshots WHERE order_item_id = ANY($1::bigint[])`,
|
||||
[itemIds],
|
||||
)
|
||||
|
||||
const removedLinks = await client.query<{ exception_id: number }>(
|
||||
`
|
||||
DELETE FROM cost_exception_items
|
||||
WHERE order_item_id = ANY($1::bigint[])
|
||||
RETURNING exception_id
|
||||
`,
|
||||
[itemIds],
|
||||
)
|
||||
const exceptionIds = [
|
||||
...new Set(removedLinks.rows.map((row) => Number(row.exception_id)).filter(Boolean)),
|
||||
]
|
||||
const resolved = await client.query(
|
||||
`
|
||||
UPDATE cost_exceptions ce
|
||||
SET status = 'resolved',
|
||||
resolved_at = NOW(),
|
||||
item_count = 0,
|
||||
last_seen_at = NOW()
|
||||
WHERE (
|
||||
ce.order_item_id = ANY($1::bigint[])
|
||||
OR ce.id = ANY($2::bigint[])
|
||||
)
|
||||
AND ce.status <> 'resolved'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM cost_exception_items i
|
||||
WHERE i.exception_id = ce.id
|
||||
)
|
||||
`,
|
||||
[itemIds, exceptionIds],
|
||||
)
|
||||
|
||||
const archived = await client.query(
|
||||
`
|
||||
UPDATE order_items
|
||||
SET fulfillment_channel = NULL,
|
||||
fulfillment_status = 'archived',
|
||||
fulfillment_source = 'cutover_archive',
|
||||
fulfillment_locked = TRUE,
|
||||
fulfillment_reason = '运营从待分类队列删除',
|
||||
fulfillment_pending_reason = NULL,
|
||||
fulfillment_pending_detail = NULL,
|
||||
fulfillment_decided_by = $2,
|
||||
fulfillment_decided_at = NOW(),
|
||||
updated_at = NOW()
|
||||
WHERE id = ANY($1::bigint[])
|
||||
AND fulfillment_status = 'pending'
|
||||
`,
|
||||
[itemIds, input.adminUserId],
|
||||
)
|
||||
|
||||
return {
|
||||
archivedItems: archived.rowCount || 0,
|
||||
preservedOrders: new Set(items.rows.map((row) => Number(row.order_id))).size,
|
||||
deletedCostSnapshots: snapshots.rowCount || 0,
|
||||
resolvedExceptions: resolved.rowCount || 0,
|
||||
dates: [...new Set(items.rows.map((row) => row.basis_date).filter(Boolean))],
|
||||
}
|
||||
})
|
||||
|
||||
const refreshed = await refreshFactsForDates(result.dates)
|
||||
return {
|
||||
archivedItems: result.archivedItems,
|
||||
preservedOrders: result.preservedOrders,
|
||||
deletedCostSnapshots: result.deletedCostSnapshots,
|
||||
resolvedExceptions: result.resolvedExceptions,
|
||||
refreshedDays: refreshed.days,
|
||||
skippedClosedDays: refreshed.skippedClosed,
|
||||
}
|
||||
}
|
||||
|
||||
/** 按内部 SKU 批量指定(同一中文 SKU 跨平台码一起分流)。 */
|
||||
@@ -1340,6 +1557,25 @@ export async function deleteChannelRules(idsInput: number[]): Promise<{
|
||||
return { deletedCount: result.deletedIds.length, ...result }
|
||||
}
|
||||
|
||||
/**
|
||||
* 运营强制删除:跳过系统来源保护,只删除规则本身,不改历史订单渠道。
|
||||
* 对应来源的同步任务后续仍可能重新生成规则。
|
||||
*/
|
||||
export async function forceDeleteChannelRules(idsInput: number[]): Promise<{
|
||||
deletedCount: number
|
||||
deletedIds: number[]
|
||||
missingIds: number[]
|
||||
}> {
|
||||
if (!Array.isArray(idsInput) || idsInput.length === 0) {
|
||||
throw createHttpError('请选择要强制删除的渠道规则', {
|
||||
statusCode: 400,
|
||||
errorCode: 'channel_rule_ids_required',
|
||||
})
|
||||
}
|
||||
const result = await deleteInternalSkuChannelsByIds(idsInput)
|
||||
return { deletedCount: result.deletedIds.length, ...result }
|
||||
}
|
||||
|
||||
async function assertRulesDeletable(idsInput: number[]): Promise<void> {
|
||||
const ids = [...new Set(idsInput.map(Number).filter((id) => Number.isInteger(id) && id > 0))]
|
||||
if (ids.length === 0) return
|
||||
|
||||
Reference in New Issue
Block a user