/** * 诊断生产环境分类分配为何未生效 */ const API = (process.env.API_BASE_URL || 'https://www.xdjchuanhui.com/api').replace(/\/+$/, ''); async function login() { const res = await fetch(`${API}/auth/token`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: process.env.REPROCESS_EMAIL || 'superadmin@meoo.local', password: process.env.REPROCESS_PASSWORD || 'SuperAdmin@123', }), }); const data = await res.json(); if (!res.ok) throw new Error(JSON.stringify(data)); return data.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), }); const payload = await res.json(); if (payload.error) throw new Error(payload.error.message || JSON.stringify(payload.error)); return payload.data; } async function main() { const token = await login(); const [supplierCategories, suppliers, catalogCats, legacyCats, orderItems, skuSample] = await Promise.all([ query(token, { action: 'select', table: 'supplier_categories', select: 'supplier_code, category_code, is_primary', }), query(token, { action: 'select', table: 'suppliers', select: 'supplier_code, company_name, status', filters: [], }), query(token, { action: 'select', table: 'product_catalog', select: 'code, name, parent_code, item_type', filters: [{ op: 'eq', column: 'item_type', value: 'category' }], }), query(token, { action: 'select', table: 'categories', select: 'category_code, category_name, parent_code', }), 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 }], limit: 30, }), query(token, { action: 'select', table: 'skus', select: 'sku_code, sku_name, category_code', limit: 20, }), ]); const catalogCodes = new Set((catalogCats || []).map((c) => c.code)); const legacyCodes = new Set((legacyCats || []).map((c) => c.category_code)); const activeSuppliers = new Set( (suppliers || []).filter((s) => [10, 20].includes(Number(s.status))).map((s) => s.supplier_code) ); console.log('\n=== 供应商经营品类 ==='); console.log('绑定数:', supplierCategories?.length || 0); const scIssues = []; for (const sc of supplierCategories || []) { const inCatalog = catalogCodes.has(sc.category_code); const inLegacy = legacyCodes.has(sc.category_code); const supplierActive = activeSuppliers.has(sc.supplier_code); if (!inCatalog && !inLegacy) scIssues.push({ ...sc, issue: 'category_code 不存在' }); if (!supplierActive) scIssues.push({ ...sc, issue: '供应商非启用状态' }); } console.log('异常绑定:', scIssues.length); scIssues.slice(0, 10).forEach((x) => console.log(` ${x.supplier_code} -> ${x.category_code} (${x.issue})`) ); console.log('\n=== 分类来源 ==='); console.log('product_catalog 分类数:', catalogCodes.size); console.log('categories 表分类数:', legacyCodes.size); const onlyLegacy = [...legacyCodes].filter((c) => !catalogCodes.has(c)).slice(0, 5); const onlyCatalog = [...catalogCodes].filter((c) => !legacyCodes.has(c)).slice(0, 5); console.log('仅在 categories:', onlyLegacy.join(', ') || '(无)'); console.log('仅在 product_catalog:', onlyCatalog.join(', ') || '(无)'); console.log('\n=== 最近订单明细 ==='); const skuMap = Object.fromEntries((skuSample || []).map((s) => [s.sku_code, s])); const supplierMap = Object.fromEntries((suppliers || []).map((s) => [s.supplier_code, s])); const bindingsByCat = {}; for (const sc of supplierCategories || []) { if (!bindingsByCat[sc.category_code]) bindingsByCat[sc.category_code] = []; bindingsByCat[sc.category_code].push(sc); } let mismatch = 0; let noCategory = 0; let noBinding = 0; for (const item of orderItems || []) { const cat = item.category_code || skuMap[item.sku_code]?.category_code || null; if (!cat) { noCategory++; continue; } const bindings = bindingsByCat[cat] || []; if (!bindings.length) { noBinding++; console.log( ` [无绑定] ${item.order_code} ${item.item_name || item.sku_code} cat=${cat} supplier=${item.supplier_code || '-'}` ); continue; } const expected = bindings.find((b) => b.is_primary) || bindings[0]; if (item.supplier_code && item.supplier_code !== expected.supplier_code) { mismatch++; const expName = supplierMap[expected.supplier_code]?.company_name || expected.supplier_code; const actName = supplierMap[item.supplier_code]?.company_name || item.supplier_code; console.log( ` [不匹配] ${item.order_code} ${item.item_name || item.sku_code} cat=${cat} 期望=${expName} 实际=${actName}` ); } } console.log(`统计: 无分类=${noCategory}, 分类无供应商绑定=${noBinding}, 供应商不匹配=${mismatch}`); } main().catch((e) => { console.error(e); process.exit(1); });