重构:硬切履约路由唯一真源

This commit is contained in:
yml2213
2026-11-08 19:44:27 +08:00
parent f6e193e7f6
commit 6bff1971dd
31 changed files with 1029 additions and 1022 deletions
@@ -90,22 +90,6 @@ async function main() {
`,
)
const rules = await query<{
match_key_type: string
channel: string
scope: string
total: number
}>(
`
SELECT match_key_type, channel,
CASE WHEN provider = '' AND shop_id = '' THEN 'global' ELSE 'scoped' END AS scope,
count(*)::int AS total
FROM sku_channel_rules
GROUP BY 1, 2, 3
ORDER BY 1, 2, 3
`,
)
const duplicateProducts = await query<{ duplicate_names: number; duplicate_rows: number }>(
`
SELECT count(*)::int AS duplicate_names, COALESCE(sum(total), 0)::int AS duplicate_rows
@@ -119,43 +103,29 @@ async function main() {
`,
)
const capabilities = await query<{ channel: string; total: number }>(
`SELECT channel, count(*)::int AS total FROM product_channel_capabilities WHERE enabled GROUP BY 1 ORDER BY 2 DESC`,
)
const internalChannelConflicts = await query<{ internal_sku: string; total: number }>(
`SELECT internal_sku, count(*)::int AS total
FROM internal_sku_channels
WHERE enabled = TRUE AND is_fallback = FALSE
GROUP BY internal_sku
HAVING count(*) > 1
ORDER BY count(*) DESC, internal_sku`,
)
const internalChannels = await query<{
const routes = await query<{
channel: string
route_role: string
source: string
total: number
}>(
`SELECT channel, source, count(*)::int AS total
FROM internal_sku_channels
`SELECT channel, route_role, source, count(*)::int AS total
FROM sku_channel_routes
WHERE enabled = TRUE
GROUP BY channel, source
ORDER BY channel, source`,
GROUP BY channel, route_role, source
ORDER BY channel, route_role, source`,
)
const canonicalChannelDuplicates = await query<{
internal_sku: string
channel: string
const internalRouteConflicts = await query<{
internal_sku_id: number
total: number
}>(
`SELECT canonicalize_internal_sku(internal_sku) AS internal_sku,
channel, count(*)::int AS total
FROM internal_sku_channels
WHERE enabled = TRUE
GROUP BY canonicalize_internal_sku(internal_sku), channel
`SELECT internal_sku_id, count(*)::int AS total
FROM sku_channel_routes
WHERE enabled = TRUE AND route_role = 'primary'
GROUP BY internal_sku_id
HAVING count(*) > 1
ORDER BY count(*) DESC, internal_sku, channel`,
ORDER BY count(*) DESC, internal_sku_id`,
)
const assignedCarrierConflicts = await query<{
@@ -220,12 +190,9 @@ async function main() {
pending: pending.rows[0],
channelCarrier: channelCarrier.rows,
internalSku: internalSku.rows[0],
rules: rules.rows,
duplicateProducts: duplicateProducts.rows[0],
capabilities: capabilities.rows,
internalChannels: internalChannels.rows,
internalChannelConflicts: internalChannelConflicts.rows,
canonicalChannelDuplicates: canonicalChannelDuplicates.rows,
routes: routes.rows,
internalRouteConflicts: internalRouteConflicts.rows,
assignedCarrierConflicts: assignedCarrierConflicts.rows,
byProvider: byProvider.rows,
},