来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
147 lines
6.2 KiB
JavaScript
147 lines
6.2 KiB
JavaScript
/**
|
|
* Diagnose why SKU ranking margins are negative on prod.
|
|
* Usage: node --env-file=.env scripts/prod-diagnose-sku-margin.cjs [cycleCode]
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
async function main() {
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
const cycle = process.argv[2] || '202607';
|
|
|
|
// Single-row purchase costs attributed to one SKU (legacy model)
|
|
const legacy = await pool.query(
|
|
`WITH purchase_rows AS (
|
|
SELECT order_code, sku_code, amount,
|
|
COUNT(*) OVER (PARTITION BY order_code) AS rows_per_order
|
|
FROM cost_details
|
|
WHERE cost_type = 'purchase' AND cycle_code = $1
|
|
)
|
|
SELECT COUNT(*) FILTER (WHERE rows_per_order = 1) AS single_row_orders,
|
|
COUNT(*) FILTER (WHERE rows_per_order > 1) AS multi_row_sku_lines,
|
|
ROUND(SUM(amount) FILTER (WHERE rows_per_order = 1)::numeric, 2) AS single_row_amt,
|
|
ROUND(SUM(amount) FILTER (WHERE rows_per_order > 1)::numeric, 2) AS multi_row_amt
|
|
FROM purchase_rows`,
|
|
[cycle]
|
|
);
|
|
console.log('purchase row model', legacy.rows[0]);
|
|
|
|
// SKU ranking like API: sales vs purchase cost by sku
|
|
const ranking = await pool.query(
|
|
`WITH sales AS (
|
|
SELECT oi.sku_code,
|
|
MAX(COALESCE(NULLIF(TRIM(oi.item_name), ''), oi.sku_code)) AS sku_name,
|
|
ROUND(SUM(COALESCE(oi.amount, 0))::numeric, 2) AS sales_amount,
|
|
ROUND(SUM(COALESCE(oi.quantity, 0))::numeric, 2) AS quantity
|
|
FROM order_items oi
|
|
JOIN orders o ON o.order_code = oi.order_code
|
|
WHERE o.cycle_code = $1
|
|
AND oi.sku_code IS NOT NULL AND TRIM(oi.sku_code) <> ''
|
|
GROUP BY oi.sku_code
|
|
),
|
|
costs AS (
|
|
SELECT cd.sku_code,
|
|
ROUND(SUM(CASE WHEN cd.cost_type = 'purchase' THEN cd.amount ELSE 0 END)::numeric, 2) AS purchase_cost,
|
|
ROUND(SUM(CASE WHEN cd.cost_type ILIKE '%loss%' THEN cd.amount ELSE 0 END)::numeric, 2) AS loss_cost,
|
|
COUNT(*) FILTER (WHERE cd.cost_type = 'purchase') AS purchase_lines,
|
|
COUNT(DISTINCT cd.order_code) FILTER (WHERE cd.cost_type = 'purchase') AS purchase_orders
|
|
FROM cost_details cd
|
|
JOIN orders o ON o.order_code = cd.order_code AND o.cycle_code = cd.cycle_code
|
|
WHERE cd.cycle_code = $1
|
|
AND cd.sku_code IS NOT NULL AND TRIM(cd.sku_code) <> ''
|
|
AND LOWER(COALESCE(cd.cost_type, '')) <> 'purchase_applied'
|
|
GROUP BY cd.sku_code
|
|
)
|
|
SELECT s.sku_code, s.sku_name, s.sales_amount, s.quantity,
|
|
COALESCE(c.purchase_cost, 0) AS purchase_cost,
|
|
COALESCE(c.loss_cost, 0) AS loss_cost,
|
|
COALESCE(c.purchase_lines, 0) AS purchase_lines,
|
|
COALESCE(c.purchase_orders, 0) AS purchase_orders,
|
|
ROUND((COALESCE(c.purchase_cost,0) + COALESCE(c.loss_cost,0))::numeric, 2) AS total_direct,
|
|
CASE WHEN s.sales_amount > 0
|
|
THEN ROUND(((s.sales_amount - COALESCE(c.purchase_cost,0) - COALESCE(c.loss_cost,0)) / s.sales_amount * 100)::numeric, 1)
|
|
ELSE NULL END AS margin_pct
|
|
FROM sales s
|
|
LEFT JOIN costs c ON c.sku_code = s.sku_code
|
|
WHERE COALESCE(c.purchase_cost, 0) + COALESCE(c.loss_cost, 0) > 0
|
|
ORDER BY (COALESCE(c.purchase_cost,0) + COALESCE(c.loss_cost,0)) DESC
|
|
LIMIT 30`,
|
|
[cycle]
|
|
);
|
|
|
|
const neg = ranking.rows.filter((r) => Number(r.margin_pct) < 0);
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
cycle,
|
|
top30: ranking.rows.length,
|
|
negativeInTop30: neg.length,
|
|
top15: ranking.rows.slice(0, 15),
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
|
|
// Sample one worst SKU: show order-level attribution
|
|
if (ranking.rows[0]) {
|
|
const sku = ranking.rows[0].sku_code;
|
|
const detail = await pool.query(
|
|
`SELECT cd.order_code, cd.amount, cd.cost_subtype,
|
|
(SELECT COUNT(*) FROM cost_details x
|
|
WHERE x.order_code = cd.order_code AND x.cost_type = 'purchase') AS purchase_rows_on_order,
|
|
(SELECT ROUND(SUM(oi.amount)::numeric,2) FROM order_items oi
|
|
WHERE oi.order_code = cd.order_code AND oi.sku_code = cd.sku_code) AS sku_sell_on_order,
|
|
(SELECT ROUND(SUM(oi.amount)::numeric,2) FROM order_items oi
|
|
WHERE oi.order_code = cd.order_code) AS order_sell
|
|
FROM cost_details cd
|
|
JOIN orders o ON o.order_code = cd.order_code AND o.cycle_code = cd.cycle_code
|
|
WHERE cd.cycle_code = $1 AND cd.sku_code = $2 AND cd.cost_type = 'purchase'
|
|
ORDER BY cd.amount DESC
|
|
LIMIT 10`,
|
|
[cycle, sku]
|
|
);
|
|
console.log('\nworst sku detail', sku, detail.rows);
|
|
}
|
|
|
|
// Correct per-SKU from inspections vs ledger
|
|
const drift = await pool.query(
|
|
`WITH latest AS (
|
|
SELECT order_code, sku_code, amt FROM (
|
|
SELECT DISTINCT ON (i.order_code, i.sku_code)
|
|
i.order_code, i.sku_code,
|
|
ROUND((COALESCE(i.actual_qty,0)*COALESCE(i.purchase_price,0))::numeric,2) AS amt
|
|
FROM inspections i
|
|
JOIN orders o ON o.order_code = i.order_code AND o.cycle_code = $1
|
|
WHERE i.sku_code IS NOT NULL AND TRIM(i.sku_code) <> ''
|
|
AND COALESCE(i.actual_qty,0) > 0 AND COALESCE(i.purchase_price,0) > 0
|
|
ORDER BY i.order_code, i.sku_code, i.inspection_time DESC NULLS LAST, i.created_at DESC, i.id DESC
|
|
) t
|
|
),
|
|
ledger AS (
|
|
SELECT order_code, sku_code, ROUND(SUM(amount)::numeric,2) AS amt, COUNT(*) AS cnt
|
|
FROM cost_details
|
|
WHERE cost_type = 'purchase' AND cycle_code = $1
|
|
AND sku_code IS NOT NULL AND TRIM(sku_code) <> ''
|
|
GROUP BY order_code, sku_code
|
|
),
|
|
single AS (
|
|
SELECT order_code FROM cost_details
|
|
WHERE cost_type = 'purchase' AND cycle_code = $1
|
|
GROUP BY order_code HAVING COUNT(*) = 1
|
|
)
|
|
SELECT
|
|
(SELECT COUNT(*) FROM single) AS legacy_single_orders,
|
|
(SELECT ROUND(SUM(l.amt)::numeric,2) FROM ledger l JOIN single s ON s.order_code = l.order_code) AS legacy_ledger_amt,
|
|
(SELECT ROUND(SUM(lat.amt)::numeric,2) FROM latest lat JOIN single s ON s.order_code = lat.order_code) AS legacy_correct_sku_sum`,
|
|
[cycle]
|
|
);
|
|
console.log('\nlegacy single-row vs correct per-sku sum', drift.rows[0]);
|
|
|
|
await pool.end();
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|