e-scooter-rental-system/server/models/Rider.js

45 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 }, // 手机号
// 密码bcrypt 哈希存储,查询时默认不返回)
password: { type: String, required: true, select: false },
// 角色customer=普通用户, admin=管理员, store=门店管理员)
role: { type: String, enum: ['customer', 'admin', 'store'], default: 'customer' },
// 接单状态
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);