来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
102 lines
4.0 KiB
JavaScript
102 lines
4.0 KiB
JavaScript
/**
|
|
* Diagnose why SKU ranking margins are 0% on prod.
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
async function main() {
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
const cycle = process.argv[2] || '202607';
|
|
|
|
const ranking = await pool.query(
|
|
`WITH sales AS (
|
|
SELECT oi.sku_code,
|
|
ROUND(SUM(CASE WHEN COALESCE(oi.amount,0) > 0 THEN oi.amount
|
|
ELSE COALESCE(oi.price,0)*COALESCE(oi.quantity,0) END)::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 o.order_status >= 20
|
|
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 = 'loss' THEN cd.amount ELSE 0 END)::numeric,2) AS loss_cost
|
|
FROM cost_details cd
|
|
JOIN orders ox ON ox.order_code = cd.order_code AND ox.cycle_code = cd.cycle_code
|
|
WHERE cd.cycle_code = $1
|
|
AND cd.sku_code IS NOT NULL AND TRIM(cd.sku_code) <> ''
|
|
AND cd.cost_type IN ('purchase','loss')
|
|
GROUP BY cd.sku_code
|
|
)
|
|
SELECT s.sku_code,
|
|
COALESCE(sk.sku_name, s.sku_code) AS name,
|
|
s.sales_amount, s.quantity,
|
|
COALESCE(c.purchase_cost,0) AS purchase_cost,
|
|
COALESCE(c.loss_cost,0) AS loss_cost,
|
|
ROUND((s.sales_amount - COALESCE(c.purchase_cost,0) - COALESCE(c.loss_cost,0))::numeric,2) AS gross,
|
|
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
|
|
LEFT JOIN skus sk ON sk.sku_code = s.sku_code
|
|
WHERE s.quantity > 0 AND 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 40`,
|
|
[cycle]
|
|
);
|
|
|
|
const rows = ranking.rows;
|
|
const zero = rows.filter((r) => Number(r.margin_pct) === 0);
|
|
const pos = rows.filter((r) => Number(r.margin_pct) > 0);
|
|
const neg = rows.filter((r) => Number(r.margin_pct) < 0);
|
|
|
|
// Sample inspections: purchase_price vs sell price
|
|
const sample = await pool.query(
|
|
`WITH latest AS (
|
|
SELECT DISTINCT ON (i.order_code, i.sku_code)
|
|
i.order_code, i.sku_code, i.purchase_price, i.actual_qty,
|
|
oi.price AS sell_price, oi.amount AS sell_amt
|
|
FROM inspections i
|
|
JOIN orders o ON o.order_code = i.order_code AND o.cycle_code = $1
|
|
LEFT JOIN LATERAL (
|
|
SELECT price, amount FROM order_items oi
|
|
WHERE oi.order_code = i.order_code AND oi.sku_code = i.sku_code
|
|
ORDER BY oi.sort_order ASC NULLS LAST LIMIT 1
|
|
) oi ON TRUE
|
|
WHERE 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
|
|
)
|
|
SELECT COUNT(*)::int AS lines,
|
|
COUNT(*) FILTER (WHERE purchase_price IS NOT DISTINCT FROM sell_price)::int AS price_eq_sell,
|
|
COUNT(*) FILTER (WHERE purchase_price < sell_price)::int AS cheaper,
|
|
COUNT(*) FILTER (WHERE purchase_price > sell_price)::int AS dearer
|
|
FROM latest`,
|
|
[cycle]
|
|
);
|
|
|
|
// API formula check via same service query shape for eggs (known positive earlier)
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
cycle,
|
|
top40: { total: rows.length, zero: zero.length, positive: pos.length, negative: neg.length },
|
|
top15: rows.slice(0, 15),
|
|
positiveSamples: pos.slice(0, 5),
|
|
inspectionPriceCompare: sample.rows[0],
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
|
|
await pool.end();
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|