/** * 诊断剩余未完成自采:为何规则仍判 purchaser */ const path = require('path'); const serverRoot = process.env.SERVER_ROOT || process.cwd(); process.chdir(serverRoot); const { pool } = require(path.join(serverRoot, 'dist/db')); const { resolveItemAllocation, DEFAULT_ALLOCATION_RULES } = require( path.join(serverRoot, 'dist/services/allocationRules') ); const { fetchCategoryRows } = require(path.join(serverRoot, 'dist/services/categoryCatalogService')); async function main() { const [suppliers, supplierCategories, skuSuppliers] = await Promise.all([ pool.query(`SELECT supplier_code, company_name, rating, status FROM suppliers WHERE status IN (10,20,30,40)`), pool.query(`SELECT supplier_code, category_code, is_primary FROM supplier_categories`), pool.query( `SELECT sku_code, supplier_code, supply_price, is_preferred, in_stock, status, quality_rating FROM sku_suppliers WHERE status = 1` ), ]); const rows = ( await pool.query(` SELECT o.order_code, o.branch_code, oi.id, oi.sku_code, oi.item_name, COALESCE(NULLIF(TRIM(oi.category_code),''), NULLIF(TRIM(s.category_code),'')) AS category_code, oi.category_name, oi.quantity, oi.price, oi.unit, po.purchase_code, po.source_type FROM purchase_orders po JOIN orders o ON o.order_code = po.order_code LEFT JOIN order_items oi ON oi.id::text = po.source_id LEFT JOIN skus s ON s.sku_code = po.sku_code WHERE po.purchase_status IN (10, 32) AND o.order_status = ANY(ARRAY[20,30]) ORDER BY o.order_code, oi.item_name `) ).rows; const byReason = new Map(); const byCategory = new Map(); const categoryRowsCache = new Map(); for (const row of rows) { const branch = row.branch_code || null; if (!categoryRowsCache.has(branch)) { categoryRowsCache.set(branch, await fetchCategoryRows(branch)); } const resolved = resolveItemAllocation({ orderCode: row.order_code, item: { id: String(row.id || row.purchase_code), sku_code: String(row.sku_code || ''), sku_name: String(row.item_name || row.sku_code || ''), category_code: row.category_code || undefined, category_name: row.category_name || undefined, quantity: Number(row.quantity) || 0, price: Number(row.price) || 0, unit: String(row.unit || '斤'), }, rules: DEFAULT_ALLOCATION_RULES, supplierCategories: supplierCategories.rows, suppliers: suppliers.rows, skuSuppliers: skuSuppliers.rows, categoryRows: categoryRowsCache.get(branch), }); const key = `${resolved?.target}|${resolved?.allocation_source}|${resolved?.reason || ''}`; byReason.set(key, (byReason.get(key) || 0) + 1); const cat = row.category_code || '(无分类)'; const catStat = byCategory.get(cat) || { category: cat, count: 0, target: resolved?.target, reason: resolved?.reason }; catStat.count += 1; byCategory.set(cat, catStat); } console.log('=== remaining open PO resolve summary ==='); console.log('total', rows.length); for (const [k, v] of [...byReason.entries()].sort((a, b) => b[1] - a[1])) { console.log(`${v}\t${k}`); } console.log('=== by category (top 20) ==='); console.table( [...byCategory.values()] .sort((a, b) => b.count - a.count) .slice(0, 20) .map((r) => ({ category: r.category, count: r.count, target: r.target, reason: String(r.reason || '').slice(0, 60) })) ); // show supplier_categories for veg/poultry related names const bound = await pool.query(` SELECT sc.category_code, c.category_name, sc.supplier_code, s.company_name, sc.is_primary FROM supplier_categories sc LEFT JOIN categories c ON c.category_code = sc.category_code JOIN suppliers s ON s.supplier_code = sc.supplier_code WHERE s.status IN (10,20) ORDER BY c.category_name NULLS LAST, sc.category_code LIMIT 80 `); console.log('=== sample supplier_categories (available suppliers) ==='); console.table( bound.rows.map((r) => ({ category_code: r.category_code, category_name: r.category_name, supplier: r.company_name, primary: r.is_primary, })) ); await pool.end(); } main().catch(async (e) => { console.error(e); try { await pool.end(); } catch (_) {} process.exit(1); });