58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const customerSchema = new mongoose.Schema({
|
|
// 基本信息
|
|
customerId: { type: String, required: true, unique: true }, // 客户编号
|
|
name: { type: String, required: true }, // 姓名
|
|
phone: { type: String, required: true }, // 手机号
|
|
idCard: { type: String }, // 身份证号
|
|
|
|
// 联系信息
|
|
address: { type: String }, // 地址
|
|
email: { type: String }, // 邮箱
|
|
|
|
// 租赁信息
|
|
totalRentals: { type: Number, default: 0 }, // 总租赁次数
|
|
totalSpent: { type: Number, default: 0 }, // 总消费金额
|
|
currentRentals: { type: Number, default: 0 }, // 当前租赁数量
|
|
|
|
// 信用信息
|
|
creditScore: { type: Number, default: 100 }, // 信用评分 (0-100)
|
|
creditLevel: { type: String, enum: ['优秀', '良好', '一般', '较差'], default: '优秀' },
|
|
|
|
// 账户信息
|
|
accountStatus: {
|
|
type: String,
|
|
enum: ['正常', '冻结', '注销'],
|
|
default: '正常'
|
|
},
|
|
|
|
// 备注
|
|
notes: { type: String },
|
|
|
|
// 时间戳
|
|
createdAt: { type: Date, default: Date.now },
|
|
updatedAt: { type: Date, default: Date.now }
|
|
});
|
|
|
|
// 更新时自动更新 updatedAt
|
|
customerSchema.pre('save', function(next) {
|
|
this.updatedAt = new Date();
|
|
next();
|
|
});
|
|
|
|
// 生成客户编号
|
|
customerSchema.pre('save', function(next) {
|
|
if (!this.customerId) {
|
|
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.customerId = `CUST${year}${month}${day}${random}`;
|
|
}
|
|
next();
|
|
});
|
|
|
|
module.exports = mongoose.model('Customer', customerSchema);
|