40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const riderSchema = new mongoose.Schema({
|
|
// 基本信息
|
|
riderId: { type: String, required: true, unique: true }, // 骑手编号
|
|
name: { type: String, required: true }, // 姓名
|
|
phone: { type: String, required: true }, // 手机号
|
|
password: { type: String, required: true }, // 密码(简单位符串存储)
|
|
|
|
// 接单状态
|
|
status: {
|
|
type: String,
|
|
enum: ['active', 'inactive'],
|
|
default: 'inactive'
|
|
},
|
|
|
|
// 统计数据
|
|
rating: { type: Number, default: 5.0 }, // 评分
|
|
totalOrders: { type: Number, default: 0 }, // 总订单数
|
|
totalIncome: { type: Number, default: 0 }, // 总收入
|
|
|
|
// 时间戳
|
|
createdAt: { type: Date, default: Date.now }
|
|
});
|
|
|
|
// 生成骑手编号
|
|
riderSchema.pre('save', function(next) {
|
|
if (!this.riderId) {
|
|
const date = new Date();
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
|
|
this.riderId = `RIDER${year}${month}${day}${random}`;
|
|
}
|
|
next();
|
|
});
|
|
|
|
module.exports = mongoose.model('Rider', riderSchema);
|