2025-09-18 17:13:28 +08:00
|
|
|
const { sequelize } = require('./models');
|
2025-09-19 00:42:14 +08:00
|
|
|
const { Admin, Order } = require('./models');
|
2025-09-18 17:13:28 +08:00
|
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
|
|
|
|
|
|
// 演示账号数据
|
|
|
|
|
const demoUsers = [
|
|
|
|
|
{
|
|
|
|
|
username: 'admin',
|
|
|
|
|
password: 'admin123',
|
|
|
|
|
phone: '13800138001',
|
|
|
|
|
email: 'admin@niumall.com',
|
|
|
|
|
user_type: 'admin',
|
|
|
|
|
status: 'active'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
username: 'buyer',
|
|
|
|
|
password: 'buyer123',
|
|
|
|
|
phone: '13800138002',
|
|
|
|
|
email: 'buyer@niumall.com',
|
|
|
|
|
user_type: 'client',
|
|
|
|
|
status: 'active'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
username: 'trader',
|
|
|
|
|
password: 'trader123',
|
|
|
|
|
phone: '13800138003',
|
|
|
|
|
email: 'trader@niumall.com',
|
|
|
|
|
user_type: 'supplier',
|
|
|
|
|
status: 'active'
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 演示订单数据
|
|
|
|
|
const demoOrders = [
|
|
|
|
|
{
|
|
|
|
|
orderNo: 'ORD20240520001',
|
|
|
|
|
buyerId: 2, // buyer用户
|
|
|
|
|
buyerName: '采购商',
|
|
|
|
|
supplierId: 3, // trader用户
|
|
|
|
|
supplierName: '供应商',
|
|
|
|
|
traderId: null,
|
|
|
|
|
traderName: null,
|
|
|
|
|
cattleBreed: '西门塔尔牛',
|
|
|
|
|
cattleCount: 10,
|
|
|
|
|
expectedWeight: 5000.00,
|
|
|
|
|
actualWeight: null,
|
|
|
|
|
unitPrice: 35.00,
|
|
|
|
|
totalAmount: 175000.00,
|
|
|
|
|
paidAmount: 50000.00,
|
|
|
|
|
remainingAmount: 125000.00,
|
|
|
|
|
status: 'pending',
|
|
|
|
|
deliveryAddress: '北京市朝阳区某某路123号',
|
|
|
|
|
expectedDeliveryDate: new Date('2024-06-01'),
|
|
|
|
|
actualDeliveryDate: null,
|
|
|
|
|
notes: '请按时交货,质量要保证'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
orderNo: 'ORD20240520002',
|
|
|
|
|
buyerId: 2, // buyer用户
|
|
|
|
|
buyerName: '采购商',
|
|
|
|
|
supplierId: 3, // trader用户
|
|
|
|
|
supplierName: '供应商',
|
|
|
|
|
traderId: null,
|
|
|
|
|
traderName: null,
|
|
|
|
|
cattleBreed: '夏洛来牛',
|
|
|
|
|
cattleCount: 5,
|
|
|
|
|
expectedWeight: 2500.00,
|
|
|
|
|
actualWeight: null,
|
|
|
|
|
unitPrice: 38.00,
|
|
|
|
|
totalAmount: 95000.00,
|
|
|
|
|
paidAmount: 95000.00,
|
|
|
|
|
remainingAmount: 0.00,
|
|
|
|
|
status: 'confirmed',
|
|
|
|
|
deliveryAddress: '北京市海淀区某某路456号',
|
|
|
|
|
expectedDeliveryDate: new Date('2024-05-28'),
|
|
|
|
|
actualDeliveryDate: null,
|
|
|
|
|
notes: '加急订单'
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 初始化数据库
|
|
|
|
|
const initDatabase = async () => {
|
|
|
|
|
try {
|
|
|
|
|
console.log('开始初始化数据库...');
|
|
|
|
|
|
|
|
|
|
// 测试数据库连接
|
|
|
|
|
await sequelize.authenticate();
|
|
|
|
|
console.log('✅ 数据库连接成功');
|
|
|
|
|
|
|
|
|
|
// 删除并重新创建表结构
|
|
|
|
|
console.log('正在创建表结构...');
|
|
|
|
|
await sequelize.sync({ force: true });
|
|
|
|
|
console.log('✅ 表结构创建完成');
|
|
|
|
|
|
|
|
|
|
// 添加演示用户
|
|
|
|
|
console.log('正在添加演示用户...');
|
|
|
|
|
for (const userData of demoUsers) {
|
|
|
|
|
// 加密密码
|
|
|
|
|
const salt = await bcrypt.genSalt(10);
|
|
|
|
|
const passwordHash = await bcrypt.hash(userData.password, salt);
|
|
|
|
|
|
|
|
|
|
try {
|
2025-09-19 00:42:14 +08:00
|
|
|
await Admin.create({
|
2025-09-18 17:13:28 +08:00
|
|
|
...userData,
|
|
|
|
|
password_hash: passwordHash
|
|
|
|
|
});
|
|
|
|
|
console.log(`✅ 成功创建用户: ${userData.username} (${userData.user_type})`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`❌ 创建用户 ${userData.username} 时出错:`, error.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加演示订单
|
|
|
|
|
console.log('正在添加演示订单...');
|
|
|
|
|
for (const orderData of demoOrders) {
|
|
|
|
|
try {
|
|
|
|
|
await Order.create(orderData);
|
|
|
|
|
console.log(`✅ 成功创建订单: ${orderData.orderNo}`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`❌ 创建订单 ${orderData.orderNo} 时出错:`, error.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('✅ 数据库初始化完成');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('❌ 数据库初始化失败:', error.message);
|
|
|
|
|
console.error(error);
|
|
|
|
|
} finally {
|
|
|
|
|
// 关闭数据库连接
|
|
|
|
|
await sequelize.close();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 执行初始化
|
|
|
|
|
initDatabase();
|