const mongoose = require('mongoose'); const vehicleSchema = new mongoose.Schema({ // 门店关联 storeId: { type: String, required: true, index: true }, // 车辆基本信息 frameNumber: { type: String, required: true, unique: true }, // 车架号 plateNumber: { type: String }, // 车牌号 // 品牌与车型 brand: { type: String }, // 品牌 vehicleType: { type: String }, // 车型(对应 vehicleTypes 的 name) color: { type: String }, // 颜色 // 电池信息 batteryType: { type: String }, // 电池类型 batteryCapacity: { type: Number }, // 电池容量 (Ah) batteryStatus: { type: String, enum: ['正常', '老化', '待更换'], default: '正常' }, // 状态信息 status: { type: String, enum: ['空闲', '在租', '维修中', '已报废', '待回收'], default: '空闲' }, isRented: { type: Boolean, default: false }, // 是否在租(冗余字段,方便查询) // 位置信息 location: { type: { type: String, default: 'Point' }, coordinates: { type: [Number], default: [0, 0] } // [经度, 纬度] }, lastLocationUpdate: { type: Date }, // 租赁信息 currentOrderId: { type: mongoose.Schema.Types.ObjectId, ref: 'Order' }, totalRentDays: { type: Number, default: 0 }, // 累计租赁天数 // 维护信息 lastMaintenanceDate: { type: Date }, nextMaintenanceDate: { type: Date }, maintenanceHistory: [{ date: { type: Date }, type: { type: String }, // 保养类型 description: { type: String }, cost: { type: Number } }], // 购买信息 purchaseDate: { type: Date }, purchasePrice: { type: Number }, purchaseSupplier: { type: String }, // 备注 notes: { type: String }, // 时间戳 createdAt: { type: Date, default: Date.now }, updatedAt: { type: Date, default: Date.now } }); // 更新时自动更新 updatedAt vehicleSchema.pre('save', function(next) { this.updatedAt = new Date(); next(); }); // 创建地理位置索引 vehicleSchema.index({ location: '2dsphere' }); // 按门店索引,方便查询某门店的车辆 vehicleSchema.index({ storeId: 1 }); module.exports = mongoose.model('Vehicle', vehicleSchema);