const API = 'https://www.xdjchuanhui.com/api'; async function login() { const res = await fetch(`${API}/auth/token`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: 'superadmin@meoo.local', password: 'SuperAdmin@123' }), }); return (await res.json()).access_token; } async function query(token, spec) { const res = await fetch(`${API}/data/query`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(spec), }); return (await res.json()).data; } function getCategoryChain(code, map) { const chain = []; let cur = code; const seen = new Set(); while (cur && !seen.has(cur)) { seen.add(cur); chain.push(cur); cur = map[cur]?.parent_code || null; } return chain; } async function main() { const token = await login(); const [supplierCategories, suppliers, catalogCats, orderItems, skus] = await Promise.all([ query(token, { action: 'select', table: 'supplier_categories', select: '*' }), query(token, { action: 'select', table: 'suppliers', select: 'supplier_code, company_name, status' }), query(token, { action: 'select', table: 'product_catalog', select: 'code, name, parent_code, item_type, level', filters: [{ op: 'eq', column: 'item_type', value: 'category' }], }), query(token, { action: 'select', table: 'order_items', select: 'id, order_code, sku_code, item_name, supplier_code, category_code', orders: [{ column: 'created_at', ascending: false }], }), query(token, { action: 'select', table: 'skus', select: 'sku_code, category_code, sku_name' }), ]); const catMap = Object.fromEntries( (catalogCats || []).map((c) => [c.code, { parent_code: c.parent_code, name: c.name, level: c.level }]) ); const skuMap = Object.fromEntries((skus || []).map((s) => [s.sku_code, s])); const supplierMap = Object.fromEntries((suppliers || []).map((s) => [s.supplier_code, s])); const items = (orderItems || []).slice(0, 20); console.log('订单明细样本:', items.length, '/ 总', orderItems?.length); for (const item of items) { const cat = item.category_code || skuMap[item.sku_code]?.category_code; if (!cat) { console.log(` NO_CAT: ${item.item_name || item.sku_code}`); continue; } const chain = getCategoryChain(cat, catMap); let matched = null; let matchedCode = null; for (const code of chain) { const hits = (supplierCategories || []).filter((sc) => sc.category_code === code); if (hits.length) { matched = hits.sort((a, b) => Number(b.is_primary) - Number(a.is_primary))[0]; matchedCode = code; break; } } const catName = catMap[cat]?.name || cat; if (matched) { const supName = supplierMap[matched.supplier_code]?.company_name || matched.supplier_code; const actual = item.supplier_code ? supplierMap[item.supplier_code]?.company_name || item.supplier_code : '(空)'; console.log( ` MATCH@${matchedCode}: ${item.item_name || item.sku_code} [${catName}] 期望=${supName} 实际=${actual}` ); } else { const chainStr = chain .map((c) => `${catMap[c]?.name || '?'}(${c},L${catMap[c]?.level ?? '?'})`) .join(' > '); console.log(` MISS: ${item.item_name || item.sku_code} [${catName}] 链=${chainStr}`); } } } main().catch(console.error);