tmp
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
import mongoose from "mongoose";
|
||||
import { pinyin } from "pinyin-pro";
|
||||
|
||||
/**
|
||||
* Employee Schema
|
||||
@@ -8,8 +9,7 @@ import mongoose from "mongoose";
|
||||
*/
|
||||
const EmployeeSchema = mongoose.Schema(
|
||||
{
|
||||
// 基础信息
|
||||
name: { type: String, required: true, comment: "姓名" },
|
||||
// 工号(业务雇佣标识)
|
||||
employeeNo: { type: String, default: "", unique: true, sparse: true, comment: "工号" },
|
||||
|
||||
// 登录账号(关联用户体系)
|
||||
@@ -38,6 +38,9 @@ const EmployeeSchema = mongoose.Schema(
|
||||
|
||||
// 个人信息
|
||||
profile: {
|
||||
name: { type: String, required: true, comment: "姓名" },
|
||||
pinyin: { type: String, default: "", index: true, comment: "姓名的拼音,用于搜索" },
|
||||
pinyinFL: { type: String, default: "", index: true, comment: "姓名拼音的首字母,用于搜索" },
|
||||
sex: { type: String, enum: ["male", "female", "other", ""], default: "", comment: "性别" },
|
||||
birthday: { type: Date, comment: "出生日期" },
|
||||
idNumber: { type: String, default: "", comment: "身份证号" },
|
||||
@@ -98,6 +101,31 @@ const EmployeeSchema = mongoose.Schema(
|
||||
}
|
||||
);
|
||||
|
||||
// ==================== 拼音生成 ====================
|
||||
|
||||
/**
|
||||
* 姓名 → 全拼(小写、无分隔,如 "张三" → "zhangsan")
|
||||
*/
|
||||
const genPinyin = (name) =>
|
||||
pinyin(String(name), { toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
|
||||
|
||||
/**
|
||||
* 姓名 → 拼音首字母(如 "张三" → "zs")
|
||||
*/
|
||||
const genPinyinFL = (name) =>
|
||||
pinyin(String(name), { pattern: "first", toneType: "none", nonZh: "consecutive" }).toLowerCase().replace(/[^a-z0-9]/g, "");
|
||||
|
||||
// 保存时自动生成/同步拼音字段
|
||||
EmployeeSchema.pre("save", function (next) {
|
||||
const name = this.profile?.name;
|
||||
if (!name) return next();
|
||||
if (this.isModified("profile.name") || !this.profile.pinyin || !this.profile.pinyinFL) {
|
||||
this.profile.pinyin = genPinyin(name);
|
||||
this.profile.pinyinFL = genPinyinFL(name);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
// ==================== 静态方法 ====================
|
||||
|
||||
/**
|
||||
@@ -156,6 +184,13 @@ EmployeeSchema.statics.createEmployee = async function (data) {
|
||||
* 更新员工
|
||||
*/
|
||||
EmployeeSchema.statics.updateEmployee = async function (id, update) {
|
||||
const newName = update.name || update["profile.name"];
|
||||
if (newName) {
|
||||
update["profile.name"] = newName;
|
||||
delete update.name;
|
||||
update["profile.pinyin"] = genPinyin(newName);
|
||||
update["profile.pinyinFL"] = genPinyinFL(newName);
|
||||
}
|
||||
update["meta.updatetime"] = Date.now();
|
||||
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec();
|
||||
};
|
||||
@@ -179,7 +214,7 @@ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
|
||||
|
||||
// ==================== 索引定义 ====================
|
||||
|
||||
EmployeeSchema.index({ name: 1 });
|
||||
EmployeeSchema.index({ "profile.name": 1 });
|
||||
EmployeeSchema.index({ employeeNo: 1 });
|
||||
EmployeeSchema.index({ userId: 1 });
|
||||
EmployeeSchema.index({ orgId: 1, role: 1, status: 1 });
|
||||
|
||||
Reference in New Issue
Block a user