"use strict"; import mongoose from "mongoose"; /** * Organization Schema * 组织/机构基础信息,支持医院、诊所、体检中心等多种机构类型 */ const OrganizationSchema = mongoose.Schema( { // 基础信息 name: { type: String, required: true, index: true, comment: "机构名称" }, shortName: { type: String, default: "", comment: "机构简称" }, code: { type: String, default: "", unique: true, sparse: true, comment: "机构编码" }, // 机构类型 type: { type: String, enum: ["hospital", "clinic", "checkup_center", "other"], default: "hospital", comment: "机构类型:医院、诊所、体检中心、其他", }, // 等级/性质 level: { type: String, enum: ["三级甲等", "三级乙等", "二级甲等", "二级乙等", "一级", "未定级", ""], default: "", comment: "医院等级", }, nature: { type: String, enum: ["public", "private", "joint", "other", ""], default: "", comment: "机构性质:公立、私立、合资、其他", }, // 联系方式 contact: { phone: { type: String, default: "", comment: "联系电话" }, email: { type: String, default: "", comment: "联系邮箱" }, website: { type: String, default: "", comment: "官方网站" }, }, // 地址信息 address: { province: { type: String, default: "", comment: "省份" }, city: { type: String, default: "", comment: "城市" }, district: { type: String, default: "", comment: "区县" }, detail: { type: String, default: "", comment: "详细地址" }, longitude: { type: Number, default: 0, comment: "经度" }, latitude: { type: Number, default: 0, comment: "纬度" }, }, // 状态 status: { type: String, enum: ["active", "inactive", "suspended"], default: "active", comment: "机构状态:正常、停用、暂停", }, // 简介/备注 description: { type: String, default: "", comment: "机构简介" }, remark: { type: String, default: "", comment: "备注" }, // 元数据 meta: { createtime: { type: Date, default: Date.now, comment: "创建时间" }, updatetime: { type: Date, default: Date.now, comment: "更新时间" }, }, }, { minimize: false, strict: false, collection: "organization", timestamps: false, } ); // ==================== 静态方法 ==================== /** * 根据名称模糊查询机构 */ OrganizationSchema.statics.findByName = async function (name, options = {}) { const { page = 1, pageSize = 20 } = options; const skip = (page - 1) * pageSize; return await this.find({ name: { $regex: name, $options: "i" } }) .skip(skip) .limit(pageSize) .exec(); }; /** * 根据城市查询机构列表 */ OrganizationSchema.statics.findByCity = async function (city, options = {}) { const { page = 1, pageSize = 20 } = options; const skip = (page - 1) * pageSize; return await this.find({ "address.city": city }) .skip(skip) .limit(pageSize) .exec(); }; /** * 根据ID查找机构 */ OrganizationSchema.statics.findByOrgId = async function (id) { return await this.findById(id).exec(); }; /** * 创建机构 */ OrganizationSchema.statics.createOrg = async function (data) { data.meta = { createtime: Date.now(), updatetime: Date.now() }; const doc = new this(data); return await doc.save(); }; /** * 更新机构 */ OrganizationSchema.statics.updateOrg = async function (id, update) { update["meta.updatetime"] = Date.now(); return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec(); }; // ==================== 索引定义 ==================== OrganizationSchema.index({ name: 1 }); OrganizationSchema.index({ code: 1 }); OrganizationSchema.index({ type: 1, status: 1 }); OrganizationSchema.index({ "address.city": 1, "address.district": 1 }); OrganizationSchema.index({ status: 1 }); export { OrganizationSchema };