#!/bin/bash # 生产库导入业务演示数据(看板 + 订单录入) set -euo pipefail cd /opt/ai-fresh-delivery/server node <<'NODE' const { Pool } = require('pg'); require('dotenv').config(); (async () => { const pool = new Pool({ connectionString: process.env.DATABASE_URL }); const today = new Date().toISOString().split('T')[0]; const weekAgo = new Date(Date.now() - 5 * 86400000).toISOString().split('T')[0]; console.log('Seeding categories...'); await pool.query(` INSERT INTO categories (category_code, category_name, level, status) VALUES ('VEG001', '蔬菜', 1, 1), ('MEAT001', '肉类', 1, 1), ('FRUIT001', '水果', 1, 1) ON CONFLICT (category_code) DO NOTHING `).catch((e) => console.warn('categories:', e.message)); console.log('Seeding SKUs...'); await pool.query(` INSERT INTO skus (sku_code, sku_name, category_code, unit, status) VALUES ('SKU001', '红富士苹果', 'FRUIT001', '斤', 1), ('SKU002', '土鸡蛋', 'MEAT001', '个', 1), ('SKU003', '土豆', 'VEG001', '斤', 1), ('SKU004', '赣南橙', 'FRUIT001', '箱', 1), ('SKU005', '大白菜', 'VEG001', '斤', 1) ON CONFLICT (sku_code) DO NOTHING `); console.log('Seeding customers...'); await pool.query(` INSERT INTO customers (customer_code, company_name, contact_name, contact_phone, address, status, branch_code) VALUES ('CUST001', '阳光食堂', '张经理', '13800000001', '北京市朝阳区示范路1号', 10, 'BR001'), ('CUST002', '绿野学校食堂', '李老师', '13800000002', '北京市海淀区学院路88号', 10, 'BR001'), ('CUST003', '华东示范食堂', '周经理', '13800000003', '上海市浦东新区示例路66号', 10, 'BR002') ON CONFLICT (customer_code) DO UPDATE SET branch_code = EXCLUDED.branch_code `); console.log('Seeding suppliers...'); await pool.query(` INSERT INTO suppliers (supplier_code, company_name, contact_name, contact_phone, rating, quota_percent, settlement_days, status) VALUES ('SUP001', '鲜丰果蔬供应链', '王总', '13900000001', 'A', 100, 30, 10), ('SUP002', '绿源禽蛋批发中心', '刘经理', '13900000002', 'A', 100, 15, 10), ('SUP003', '华北土豆种植基地', '赵场长', '13900000003', 'B', 80, 30, 10), ('SUP004', '赣南果业直销', '陈老板', '13900000004', 'B', 90, 20, 10) ON CONFLICT (supplier_code) DO NOTHING `).catch((e) => console.warn('suppliers:', e.message)); console.log('Seeding customer_skus...'); await pool.query(` INSERT INTO customer_skus (customer_code, sku_code, customer_sku_name, customer_unit, usage_count, is_favorite) SELECT v.* FROM (VALUES ('CUST001', 'SKU001', '苹果', '斤', 12, true), ('CUST001', 'SKU002', '鸡蛋', '个', 8, false), ('CUST001', 'SKU003', '土豆', '斤', 5, false), ('CUST002', 'SKU001', '红富士', '斤', 6, true), ('CUST002', 'SKU004', '橙子', '箱', 3, false), ('CUST003', 'SKU005', '白菜', '斤', 10, true) ) AS v(customer_code, sku_code, customer_sku_name, customer_unit, usage_count, is_favorite) WHERE NOT EXISTS ( SELECT 1 FROM customer_skus cs WHERE cs.customer_code = v.customer_code AND cs.sku_code = v.sku_code ) `).catch((e) => console.warn('customer_skus:', e.message)); console.log('Seeding pricing cycle & bid...'); await pool.query(` INSERT INTO pricing_cycles (cycle_code, cycle_name, start_date, end_date, status) VALUES ('CYC001', '演示周期', CURRENT_DATE - 7, CURRENT_DATE + 7, 1) ON CONFLICT (cycle_code) DO NOTHING `).catch((e) => console.warn('pricing_cycles:', e.message)); await pool.query(` INSERT INTO bid_prices (bid_code, cycle_code, sku_code, price, status) SELECT 'BID001', 'CYC001', sku_code, 10.00, 1 FROM skus ON CONFLICT DO NOTHING `).catch((e) => console.warn('bid_prices:', e.message)); console.log('Seeding sample orders...'); await pool.query(` INSERT INTO orders (order_code, cycle_code, bid_code, customer_code, order_status, total_amount, delivery_address, delivery_date, branch_code, created_at, updated_at) VALUES ('ORD-TODAY-001', 'CYC001', 'BID001', 'CUST001', 30, 1280.50, '北京市朝阳区示范路1号', $1, 'BR001', NOW() - INTERVAL '2 hours', NOW()), ('ORD-TODAY-002', 'CYC001', 'BID001', 'CUST002', 20, 860.00, '北京市海淀区学院路88号', $1, 'BR001', NOW() - INTERVAL '1 hour', NOW()), ('ORD-WEEK-001', 'CYC001', 'BID001', 'CUST003', 90, 2350.00, '上海市浦东新区示例路66号', $2, 'BR002', NOW() - INTERVAL '3 days', NOW() - INTERVAL '2 days'), ('ORD-WEEK-002', 'CYC001', 'BID001', 'CUST001', 50, 560.00, '北京市朝阳区示范路1号', $2, 'BR001', NOW() - INTERVAL '2 days', NOW() - INTERVAL '1 day'), ('ORD-WEEK-003', 'CYC001', 'BID001', 'CUST002', 40, 420.00, '北京市海淀区学院路88号', $3, 'BR001', NOW() - INTERVAL '4 days', NOW() - INTERVAL '3 days') ON CONFLICT (order_code) DO UPDATE SET branch_code = EXCLUDED.branch_code `, [today, weekAgo, new Date(Date.now() - 86400000).toISOString().split('T')[0]]); console.log('Seeding order items...'); await pool.query(` INSERT INTO order_items (order_code, sku_code, quantity, unit, price, amount, supplier_code, item_status, branch_code) SELECT v.* FROM (VALUES ('ORD-TODAY-001', 'SKU001', 100, '斤', 5.50, 550.00, 'SUP001', 30, 'BR001'), ('ORD-TODAY-001', 'SKU002', 200, '个', 1.20, 240.00, 'SUP002', 30, 'BR001'), ('ORD-TODAY-001', 'SKU003', 245, '斤', 2.00, 490.00, 'SUP003', 30, 'BR001'), ('ORD-TODAY-002', 'SKU001', 80, '斤', 5.50, 440.00, 'SUP001', 20, 'BR001'), ('ORD-TODAY-002', 'SKU004', 10, '箱', 38.00, 380.00, 'SUP004', 20, 'BR001'), ('ORD-TODAY-002', 'SKU005', 22, '斤', 1.80, 40.00, 'SUP001', 20, 'BR001'), ('ORD-WEEK-001', 'SKU004', 50, '箱', 38.00, 1900.00, 'SUP004', 90, 'BR002'), ('ORD-WEEK-001', 'SKU005', 250, '斤', 1.80, 450.00, 'SUP003', 90, 'BR002'), ('ORD-WEEK-002', 'SKU002', 300, '个', 1.20, 360.00, 'SUP002', 50, 'BR001'), ('ORD-WEEK-002', 'SKU003', 100, '斤', 2.00, 200.00, 'SUP003', 50, 'BR001'), ('ORD-WEEK-003', 'SKU001', 60, '斤', 5.50, 330.00, 'SUP001', 40, 'BR001'), ('ORD-WEEK-003', 'SKU005', 50, '斤', 1.80, 90.00, 'SUP001', 40, 'BR001') ) AS v(order_code, sku_code, quantity, unit, price, amount, supplier_code, item_status, branch_code) WHERE NOT EXISTS ( SELECT 1 FROM order_items oi WHERE oi.order_code = v.order_code AND oi.sku_code = v.sku_code ) `).catch((e) => console.warn('order_items:', e.message)); await pool.query(` UPDATE order_items oi SET branch_code = o.branch_code FROM orders o WHERE oi.order_code = o.order_code `).catch(() => {}); console.log('Seeding inventory...'); const wh = await pool.query(`SELECT warehouse_code FROM warehouses LIMIT 1`); const warehouseCode = wh.rows[0]?.warehouse_code || 'WH001'; if (!wh.rows[0]) { await pool.query(` INSERT INTO warehouses (warehouse_code, warehouse_name, status) VALUES ('WH001', '主仓库', 1) ON CONFLICT (warehouse_code) DO NOTHING `).catch(() => undefined); } await pool.query(` INSERT INTO inventory (sku_code, warehouse_code, quantity, available_qty, expiry_date, batch_no, status) SELECT v.sku_code, v.warehouse_code, v.qty, v.qty, v.expiry, v.batch, 1 FROM (VALUES ('SKU001', $1, 120, (CURRENT_DATE + INTERVAL '30 days')::date, 'B001'), ('SKU002', $1, 8, (CURRENT_DATE + INTERVAL '5 days')::date, 'B002'), ('SKU003', $1, 200, (CURRENT_DATE + INTERVAL '15 days')::date, 'B003'), ('SKU004', $1, 5, (CURRENT_DATE + INTERVAL '3 days')::date, 'B004'), ('SKU005', $1, 50, (CURRENT_DATE + INTERVAL '7 days')::date, 'B005') ) AS v(sku_code, warehouse_code, qty, expiry, batch) WHERE NOT EXISTS ( SELECT 1 FROM inventory i WHERE i.sku_code = v.sku_code AND i.warehouse_code = v.warehouse_code ) `, [warehouseCode]).catch((e) => console.warn('inventory:', e.message)); const counts = await pool.query(` SELECT (SELECT COUNT(*) FROM customers) AS customers, (SELECT COUNT(*) FROM orders) AS orders, (SELECT COUNT(*)::int FROM order_items) AS order_items, (SELECT COUNT(*) FROM skus) AS skus, (SELECT COUNT(*) FROM suppliers) AS suppliers, (SELECT COUNT(*) FROM inventory) AS inventory `); console.log('Done:', counts.rows[0]); await pool.end(); })().catch((e) => { console.error(e); process.exit(1); }); NODE