const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/e-scooter-rental') .then(async () => { console.log('✅ MongoDB 连接成功'); const Store = require('./server/models/Store'); const Vehicle = require('./server/models/Vehicle'); const Customer = require('./server/models/Customer'); const Order = require('./server/models/Order'); const Payment = require('./server/models/Payment'); // 清空数据 await Vehicle.deleteMany({}); await Customer.deleteMany({}); await Order.deleteMany({}); console.log('🧹 已清空车辆、客户、订单数据'); // ===== 1. 门店数据 (10个) ===== const stores = await Store.insertMany([ { storeId: 'STORE001', name: '朝阳区总店', address: '北京市朝阳区建国路88号', phone: '010-12345678', manager: '王店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE002', name: '海淀区一分店', address: '北京市海淀区中关村大街1号', phone: '010-23456789', manager: '李店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE003', name: '西城区二分店', address: '北京市西城区西单北大街120号', phone: '010-34567890', manager: '张店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE004', name: '东城区三分店', address: '北京市东城区王府井大街138号', phone: '010-45678901', manager: '赵店长', status: '装修中', approvalStatus: '已通过' }, { storeId: 'STORE005', name: '丰台区四分店', address: '北京市丰台区南三环西路16号', phone: '010-56789012', manager: '钱店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE006', name: '通州区分店', address: '北京市通州区运河西大街128号', phone: '010-67890123', manager: '孙店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE007', name: '昌平区分店', address: '北京市昌平区西关环岛附近', phone: '010-78901234', manager: '周店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE008', name: '大兴区分店', address: '北京市大兴区黄村镇兴政街', phone: '010-89012345', manager: '吴店长', status: '已停业', approvalStatus: '已通过' }, { storeId: 'STORE009', name: '顺义区分店', address: '北京市顺义区府前中街', phone: '010-90123456', manager: '郑店长', status: '营业中', approvalStatus: '已通过' }, { storeId: 'STORE010', name: '房山区分店', address: '北京市房山区良乡镇拱辰大街', phone: '010-01234567', manager: '冯店长', status: '营业中', approvalStatus: '待审批' } ]); console.log('✅ 门店: 10 个'); // ===== 2. 客户/骑手数据 (15个) ===== const customers = await Customer.insertMany([ { customerId: 'CUST001', name: '张三', phone: '13800138001', idCard: '110101199001011234', address: '北京市朝阳区', email: 'zhangsan@email.com', creditScore: 95, creditLevel: '优秀', accountStatus: '正常' }, { customerId: 'CUST002', name: '李四', phone: '13800138002', idCard: '110102199002022345', address: '北京市海淀区', email: 'lisi@email.com', creditScore: 88, creditLevel: '优秀', accountStatus: '正常' }, { customerId: 'CUST003', name: '王五', phone: '13800138003', idCard: '110103199003033456', address: '北京市西城区', email: 'wangwu@email.com', creditScore: 75, creditLevel: '良好', accountStatus: '正常' }, { customerId: 'CUST004', name: '赵六', phone: '13800138004', idCard: '110104199004044567', address: '北京市东城区', email: 'zhaoliu@email.com', creditScore: 92, creditLevel: '优秀', accountStatus: '正常' }, { customerId: 'CUST005', name: '钱七', phone: '13800138005', idCard: '110105199005055678', address: '北京市丰台区', email: 'qianqi@email.com', creditScore: 65, creditLevel: '一般', accountStatus: '正常' }, { customerId: 'CUST006', name: '孙八', phone: '13800138006', idCard: '110106199006066789', address: '北京市通州区', email: 'sunba@email.com', creditScore: 80, creditLevel: '良好', accountStatus: '正常' }, { customerId: 'CUST007', name: '周九', phone: '13800138007', idCard: '110107199007077890', address: '北京市昌平区', email: 'zhoujiu@email.com', creditScore: 85, creditLevel: '优秀', accountStatus: '正常' }, { customerId: 'CUST008', name: '吴十', phone: '13800138008', idCard: '110108199008088901', address: '北京市大兴区', email: 'wushi@email.com', creditScore: 70, creditLevel: '良好', accountStatus: '正常' }, { customerId: 'CUST009', name: '郑十一', phone: '13800138009', idCard: '110109199009099012', address: '北京市顺义区', email: 'zhengshiyi@email.com', creditScore: 90, creditLevel: '优秀', accountStatus: '正常' }, { customerId: 'CUST010', name: '冯十二', phone: '13800138010', idCard: '110110199010101123', address: '北京市房山区', email: 'fengershi@email.com', creditScore: 55, creditLevel: '较差', accountStatus: '正常' }, { customerId: 'CUST011', name: '陈十三', phone: '13800138011', idCard: '110111199011112234', address: '北京市朝阳区', email: 'chenshisan@email.com', creditScore: 82, creditLevel: '良好', accountStatus: '正常' }, { customerId: 'CUST012', name: '刘十四', phone: '13800138012', idCard: '110112199012123345', address: '北京市海淀区', email: 'liushisi@email.com', creditScore: 78, creditLevel: '良好', accountStatus: '正常' }, { customerId: 'CUST013', name: '杨十五', phone: '13800138013', idCard: '110113199101011456', address: '北京市西城区', email: 'yangshiwu@email.com', creditScore: 95, creditLevel: '优秀', accountStatus: '正常' }, { customerId: 'CUST014', name: '黄十六', phone: '13800138014', idCard: '110114199102022567', address: '北京市东城区', email: 'huangshiliu@email.com', creditScore: 60, creditLevel: '一般', accountStatus: '冻结' }, { customerId: 'CUST015', name: '林十七', phone: '13800138015', idCard: '110115199103033678', address: '北京市丰台区', email: 'linshiqi@email.com', creditScore: 88, creditLevel: '优秀', accountStatus: '正常' } ]); console.log('✅ 客户/骑手: 15 个'); // ===== 3. 车辆数据 (25辆) ===== const vehicleData = []; const models = ['雅迪DT3', '爱玛A500', '台铃TL1', '新日XC1', '绿源L6', '小牛MQi2', '九号E25', '雅马哈E01']; const colors = ['黑色', '白色', '红色', '蓝色', '绿色', '灰色', '金色']; const storeIds = stores.map(s => s._id); for (let i = 1; i <= 25; i++) { const storeIndex = (i - 1) % storeIds.length; const statusOptions = ['空闲', '空闲', '空闲', '在租', '在租', '维修中', '空闲']; const status = statusOptions[Math.floor(Math.random() * statusOptions.length)]; vehicleData.push({ vehicleId: `VEH${String(i).padStart(4, '0')}`, model: models[Math.floor(Math.random() * models.length)], color: colors[Math.floor(Math.random() * colors.length)], batteryType: '锂电池', batteryCapacity: [48, 60, 72][Math.floor(Math.random() * 3)], batteryStatus: ['正常', '正常', '正常', '老化'][Math.floor(Math.random() * 4)], status: status, location: { type: 'Point', coordinates: [116.2 + Math.random() * 0.5, 39.8 + Math.random() * 0.5] }, totalRentDays: Math.floor(Math.random() * 500), purchaseDate: new Date(2023, Math.floor(Math.random() * 12), Math.floor(Math.random() * 28) + 1), purchasePrice: [2999, 3999, 4999, 5999][Math.floor(Math.random() * 4)], purchaseSupplier: ['雅迪集团', '爱玛集团', '台铃集团', '新日集团'][Math.floor(Math.random() * 4)], notes: i <= 2 ? '新车,暂无记录' : '' }); } const vehicles = await Vehicle.insertMany(vehicleData); console.log('✅ 车辆: 25 辆'); // ===== 4. 订单数据 (20个) - 不同状态 ===== const orderData = []; const now = new Date(); const oneDay = 24 * 60 * 60 * 1000; // 已完成订单 (8个) for (let i = 1; i <= 8; i++) { const customer = customers[Math.floor(Math.random() * customers.length)]; const vehicle = vehicles[Math.floor(Math.random() * vehicles.length)]; const days = Math.floor(Math.random() * 10) + 1; const startDate = new Date(now.getTime() - (Math.floor(Math.random() * 30) + days) * oneDay); const endDate = new Date(startDate.getTime() + days * oneDay); const dailyRate = [30, 50, 80, 100][Math.floor(Math.random() * 4)]; const total = dailyRate * days; orderData.push({ orderNumber: `ORD${String(now.getFullYear())}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(i).padStart(4, '0')}`, customer: customer._id, vehicle: vehicle._id, startDate: startDate, endDate: endDate, actualEndDate: endDate, rentalFee: dailyRate * days, deposit: 200, totalAmount: total + 200, paidAmount: total + 200, status: '已完成', paymentMethod: ['微信', '支付宝', '现金'][Math.floor(Math.random() * 3)], paymentDate: startDate, contractSigned: true, notes: '' }); } // 进行中订单 (6个) for (let i = 9; i <= 14; i++) { const customer = customers[Math.floor(Math.random() * customers.length)]; const vehicle = vehicles[Math.floor(Math.random() * vehicles.length)]; const days = Math.floor(Math.random() * 15) + 1; const startDate = new Date(now.getTime() - Math.floor(Math.random() * 5) * oneDay); const endDate = new Date(startDate.getTime() + days * oneDay); const dailyRate = [30, 50, 80, 100][Math.floor(Math.random() * 4)]; orderData.push({ orderNumber: `ORD${String(now.getFullYear())}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(i).padStart(4, '0')}`, customer: customer._id, vehicle: vehicle._id, startDate: startDate, endDate: endDate, rentalFee: dailyRate * days, deposit: 200, totalAmount: dailyRate * days + 200, paidAmount: dailyRate * days + 200, status: '进行中', paymentMethod: ['微信', '支付宝', '现金'][Math.floor(Math.random() * 3)], paymentDate: startDate, contractSigned: true, notes: '' }); } // 待支付订单 (2个) for (let i = 15; i <= 16; i++) { const customer = customers[Math.floor(Math.random() * customers.length)]; const vehicle = vehicles[Math.floor(Math.random() * vehicles.length)]; const days = Math.floor(Math.random() * 7) + 1; const startDate = new Date(now.getTime() + oneDay); const endDate = new Date(startDate.getTime() + days * oneDay); const dailyRate = [30, 50, 80, 100][Math.floor(Math.random() * 4)]; orderData.push({ orderNumber: `ORD${String(now.getFullYear())}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(i).padStart(4, '0')}`, customer: customer._id, vehicle: vehicle._id, startDate: startDate, endDate: endDate, rentalFee: dailyRate * days, deposit: 200, totalAmount: dailyRate * days + 200, paidAmount: 0, status: '待支付', contractSigned: false, notes: '订单已创建,等待支付' }); } // 已取消订单 (2个) for (let i = 17; i <= 18; i++) { const customer = customers[Math.floor(Math.random() * customers.length)]; const vehicle = vehicles[Math.floor(Math.random() * vehicles.length)]; const days = Math.floor(Math.random() * 5) + 1; const startDate = new Date(now.getTime() - Math.floor(Math.random() * 10) * oneDay); const endDate = new Date(startDate.getTime() + days * oneDay); const dailyRate = [30, 50, 80, 100][Math.floor(Math.random() * 4)]; orderData.push({ orderNumber: `ORD${String(now.getFullYear())}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(i).padStart(4, '0')}`, customer: customer._id, vehicle: vehicle._id, startDate: startDate, endDate: endDate, rentalFee: dailyRate * days, deposit: 200, totalAmount: dailyRate * days + 200, paidAmount: 200, status: '已取消', paymentMethod: ['微信', '支付宝'][Math.floor(Math.random() * 2)], paymentDate: startDate, contractSigned: true, notes: '客户主动取消订单' }); } // 逾期订单 (2个) for (let i = 19; i <= 20; i++) { const customer = customers[Math.floor(Math.random() * customers.length)]; const vehicle = vehicles[Math.floor(Math.random() * vehicles.length)]; const days = 3; const startDate = new Date(now.getTime() - 10 * oneDay); const endDate = new Date(startDate.getTime() + days * oneDay); const dailyRate = [30, 50, 80, 100][Math.floor(Math.random() * 4)]; const overdueDays = Math.floor((now.getTime() - endDate.getTime()) / oneDay); orderData.push({ orderNumber: `ORD${String(now.getFullYear())}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(i).padStart(4, '0')}`, customer: customer._id, vehicle: vehicle._id, startDate: startDate, endDate: endDate, actualEndDate: now, rentalFee: dailyRate * days, deposit: 200, totalAmount: dailyRate * days + 200 + overdueDays * 20, paidAmount: dailyRate * days + 200, status: '逾期', overdueDays: overdueDays, overdueFee: overdueDays * 20, paymentMethod: ['微信', '支付宝'][Math.floor(Math.random() * 2)], paymentDate: startDate, contractSigned: true, notes: '逾期未归还,需缴纳逾期费用' }); } const orders = await Order.insertMany(orderData); console.log('✅ 订单: 20 个 (已完成: 8, 进行中: 6, 待支付: 2, 已取消: 2, 逾期: 2)'); // 更新客户的总租赁次数和消费 for (const customer of customers) { const customerOrders = await Order.find({ customer: customer._id, status: '已完成' }); await Customer.findByIdAndUpdate(customer._id, { totalRentals: customerOrders.length, totalSpent: customerOrders.reduce((sum, o) => sum + o.totalAmount, 0), currentRentals: await Order.countDocuments({ customer: customer._id, status: '进行中' }) }); } console.log('✅ 客户数据已更新'); console.log('\n🎉 测试数据创建完成!'); console.log('📊 数据汇总:'); console.log(' - 门店: 10 个'); console.log(' - 车辆: 25 辆'); console.log(' - 客户/骑手: 15 个'); console.log(' - 订单: 20 个'); console.log(' - 订单状态分布: 已完成(8), 进行中(6), 待支付(2), 已取消(2), 逾期(2)'); mongoose.disconnect(); process.exit(0); }) .catch(err => console.error('❌ 错误:', err));