296 lines
7.3 KiB
JavaScript
296 lines
7.3 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const Vehicle = require('./models/Vehicle');
|
|
const Customer = require('./models/Customer');
|
|
const Order = require('./models/Order');
|
|
const Store = require('./models/Store');
|
|
const Rider = require('./models/Rider');
|
|
|
|
// 连接 MongoDB
|
|
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/e-scooter-rental')
|
|
.then(() => console.log('✅ MongoDB 连接成功'))
|
|
.catch(err => console.error('❌ MongoDB 连接失败:', err));
|
|
|
|
// 清空数据
|
|
async function clearData() {
|
|
console.log('🧹 清空数据...');
|
|
await Vehicle.deleteMany({});
|
|
await Customer.deleteMany({});
|
|
await Order.deleteMany({});
|
|
await Store.deleteMany({});
|
|
await Rider.deleteMany({});
|
|
console.log('✅ 数据已清空');
|
|
}
|
|
|
|
// 创建示例车辆
|
|
async function createVehicles() {
|
|
const vehicles = [
|
|
{
|
|
vehicleId: 'SCOOTER001',
|
|
model: '黑骑士',
|
|
color: '黑色',
|
|
batteryType: '锂电池',
|
|
batteryCapacity: 20,
|
|
batteryStatus: '正常',
|
|
status: '空闲',
|
|
purchaseDate: new Date('2024-01-15'),
|
|
purchasePrice: 3500,
|
|
purchaseSupplier: '小牛电动车'
|
|
},
|
|
{
|
|
vehicleId: 'SCOOTER002',
|
|
model: '黑骑士',
|
|
color: '白色',
|
|
batteryType: '锂电池',
|
|
batteryCapacity: 20,
|
|
batteryStatus: '正常',
|
|
status: '空闲',
|
|
purchaseDate: new Date('2024-02-20'),
|
|
purchasePrice: 3500,
|
|
purchaseSupplier: '小牛电动车'
|
|
},
|
|
{
|
|
vehicleId: 'SCOOTER003',
|
|
model: '电动车',
|
|
color: '蓝色',
|
|
batteryType: '铅酸电池',
|
|
batteryCapacity: 24,
|
|
batteryStatus: '正常',
|
|
status: '空闲',
|
|
purchaseDate: new Date('2024-03-10'),
|
|
purchasePrice: 2800,
|
|
purchaseSupplier: '雅迪电动车'
|
|
},
|
|
{
|
|
vehicleId: 'SCOOTER004',
|
|
model: '高端豪车',
|
|
color: '红色',
|
|
batteryType: '锂电池',
|
|
batteryCapacity: 30,
|
|
batteryStatus: '正常',
|
|
status: '空闲',
|
|
purchaseDate: new Date('2024-04-05'),
|
|
purchasePrice: 8000,
|
|
purchaseSupplier: '特斯拉'
|
|
},
|
|
{
|
|
vehicleId: 'SCOOTER005',
|
|
model: '普通标准套餐',
|
|
color: '绿色',
|
|
batteryType: '铅酸电池',
|
|
batteryCapacity: 20,
|
|
batteryStatus: '正常',
|
|
status: '空闲',
|
|
purchaseDate: new Date('2024-05-01'),
|
|
purchasePrice: 2500,
|
|
purchaseSupplier: '爱玛电动车'
|
|
}
|
|
];
|
|
|
|
console.log('🚗 创建示例车辆...');
|
|
await Vehicle.insertMany(vehicles);
|
|
console.log(`✅ 创建了 ${vehicles.length} 辆车`);
|
|
}
|
|
|
|
// 创建示例客户
|
|
async function createCustomers() {
|
|
const customers = [
|
|
{
|
|
customerId: 'CUST001',
|
|
name: '张三',
|
|
phone: '13800138000',
|
|
idCard: '110101199001011234',
|
|
address: '北京市朝阳区',
|
|
email: 'zhangsan@example.com'
|
|
},
|
|
{
|
|
customerId: 'CUST002',
|
|
name: '李四',
|
|
phone: '13800138001',
|
|
idCard: '110101199002022345',
|
|
address: '北京市海淀区',
|
|
email: 'lisi@example.com'
|
|
},
|
|
{
|
|
customerId: 'CUST003',
|
|
name: '王五',
|
|
phone: '13800138002',
|
|
idCard: '110101199003033456',
|
|
address: '北京市西城区',
|
|
email: 'wangwu@example.com'
|
|
},
|
|
{
|
|
customerId: 'CUST004',
|
|
name: '赵六',
|
|
phone: '13800138003',
|
|
idCard: '110101199004044567',
|
|
address: '北京市东城区',
|
|
email: 'zhaoliu@example.com'
|
|
},
|
|
{
|
|
customerId: 'CUST005',
|
|
name: '钱七',
|
|
phone: '13800138004',
|
|
idCard: '110101199005055678',
|
|
address: '北京市丰台区',
|
|
email: 'qianqi@example.com'
|
|
}
|
|
];
|
|
|
|
console.log('👥 创建示例客户...');
|
|
await Customer.insertMany(customers);
|
|
console.log(`✅ 创建了 ${customers.length} 个客户`);
|
|
}
|
|
|
|
// 创建示例订单
|
|
async function createOrders() {
|
|
const vehicles = await Vehicle.find().limit(3);
|
|
const customers = await Customer.find().limit(3);
|
|
|
|
const orders = [
|
|
{
|
|
orderNumber: 'ORDER001',
|
|
customer: customers[0]._id,
|
|
vehicle: vehicles[0]._id,
|
|
startDate: new Date('2026-02-20'),
|
|
endDate: new Date('2026-03-20'),
|
|
rentalFee: 50,
|
|
deposit: 200,
|
|
totalAmount: 300,
|
|
status: '进行中'
|
|
},
|
|
{
|
|
orderNumber: 'ORDER002',
|
|
customer: customers[1]._id,
|
|
vehicle: vehicles[1]._id,
|
|
startDate: new Date('2026-02-15'),
|
|
endDate: new Date('2026-03-15'),
|
|
rentalFee: 50,
|
|
deposit: 200,
|
|
totalAmount: 300,
|
|
status: '进行中'
|
|
},
|
|
{
|
|
orderNumber: 'ORDER003',
|
|
customer: customers[2]._id,
|
|
vehicle: vehicles[2]._id,
|
|
startDate: new Date('2026-01-10'),
|
|
endDate: new Date('2026-02-10'),
|
|
actualEndDate: new Date('2026-02-10'),
|
|
rentalFee: 40,
|
|
deposit: 150,
|
|
totalAmount: 200,
|
|
paidAmount: 200,
|
|
status: '已完成'
|
|
}
|
|
];
|
|
|
|
console.log('📋 创建示例订单...');
|
|
await Order.insertMany(orders);
|
|
console.log(`✅ 创建了 ${orders.length} 个订单`);
|
|
}
|
|
|
|
// 创建示例门店
|
|
async function createStores() {
|
|
const stores = [
|
|
{
|
|
storeId: 'STORE001',
|
|
name: '朝阳电动车门店',
|
|
address: '北京市朝阳区建国路88号',
|
|
phone: '010-12345678',
|
|
manager: '张三',
|
|
status: '营业中',
|
|
approvalStatus: '已通过',
|
|
images: []
|
|
},
|
|
{
|
|
storeId: 'STORE002',
|
|
name: '海淀电动车门店',
|
|
address: '北京市海淀区中关村大街100号',
|
|
phone: '010-87654321',
|
|
manager: '李四',
|
|
status: '营业中',
|
|
approvalStatus: '已通过',
|
|
images: []
|
|
},
|
|
{
|
|
storeId: 'STORE003',
|
|
name: '西城电动车门店',
|
|
address: '北京市西城区西单北大街120号',
|
|
phone: '010-11223344',
|
|
manager: '王五',
|
|
status: '营业中',
|
|
approvalStatus: '已通过',
|
|
images: []
|
|
}
|
|
];
|
|
|
|
console.log('🏪 创建示例门店...');
|
|
await Store.insertMany(stores);
|
|
console.log(`✅ 创建了 ${stores.length} 个门店`);
|
|
}
|
|
|
|
// 创建示例骑手
|
|
async function createRiders() {
|
|
const riders = [
|
|
{
|
|
riderId: 'RIDER001',
|
|
name: '张骑手',
|
|
phone: '13900139000',
|
|
password: '123456',
|
|
status: 'active',
|
|
rating: 4.8,
|
|
totalOrders: 120,
|
|
totalIncome: 15000
|
|
},
|
|
{
|
|
riderId: 'RIDER002',
|
|
name: '李骑手',
|
|
phone: '13900139001',
|
|
password: '123456',
|
|
status: 'active',
|
|
rating: 4.5,
|
|
totalOrders: 80,
|
|
totalIncome: 10000
|
|
},
|
|
{
|
|
riderId: 'RIDER003',
|
|
name: '王骑手',
|
|
phone: '13900139002',
|
|
password: '123456',
|
|
status: 'inactive',
|
|
rating: 4.9,
|
|
totalOrders: 200,
|
|
totalIncome: 25000
|
|
}
|
|
];
|
|
|
|
console.log('🛵 创建示例骑手...');
|
|
await Rider.insertMany(riders);
|
|
console.log(`✅ 创建了 ${riders.length} 个骑手`);
|
|
}
|
|
|
|
// 主函数
|
|
async function main() {
|
|
try {
|
|
await clearData();
|
|
await createVehicles();
|
|
await createCustomers();
|
|
await createOrders();
|
|
await createStores();
|
|
await createRiders();
|
|
console.log('\n🎉 示例数据创建完成!');
|
|
console.log('车辆: 5 辆');
|
|
console.log('客户: 5 个');
|
|
console.log('订单: 3 个');
|
|
console.log('门店: 3 个');
|
|
console.log('骑手: 3 个');
|
|
} catch (error) {
|
|
console.error('❌ 创建示例数据失败:', error);
|
|
} finally {
|
|
mongoose.disconnect();
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
main();
|