增加健康档案api

This commit is contained in:
lik
2026-08-27 21:54:51 +08:00
parent 1f10eb3c92
commit 644f4bc7cb
4 changed files with 255 additions and 0 deletions
+148
View File
@@ -0,0 +1,148 @@
"use strict";
import mongoose from "mongoose";
/**
* HealthProfile Schema
* 健康档案 - 记录患者的基本健康信息,内容保持简单
*/
const HealthProfileSchema = mongoose.Schema(
{
// 关联用户ID(可为空)
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
index: { unique: false, sparse: true },
comment: "所属用户ID",
},
// 患者信息
profile: {
name: { type: String, default: "", comment: "患者姓名" },
mobile: { type: String, default: "", index: true, comment: "患者电话" },
sex: { type: String, enum: ["male", "female", ""], default: "", comment: "性别" },
age: { type: Number, default: 0, comment: "年龄" },
idnumber: { type: String, default: "", comment: "身份证号" },
},
// 健康信息
health: {
height: { type: Number, default: 0, comment: "身高(cm" },
weight: { type: Number, default: 0, comment: "体重(kg" },
bloodType: { 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: "health_profile",
timestamps: false,
}
);
// ==================== 静态方法 ====================
/**
* 根据用户ID查找健康档案
*/
HealthProfileSchema.statics.findByUserId = async function (userId) {
return await this.findOne({ userId }).exec();
};
/**
* 查询健康档案列表(支持分页与筛选)
*
* @param {Object} options - 查询选项
* @param {number} [options.page=1] - 页码
* @param {number} [options.pageSize=20] - 每页数量
* @param {ObjectId} [options.userId] - 所属用户ID筛选(可选)
* @param {string} [options.name] - 患者姓名模糊筛选(可选)
* @param {string} [options.mobile] - 患者电话筛选(可选)
* @param {string} [options.sortBy="createtime"] - 排序字段:createtime | updatetime(可选)
* @returns {Promise<Object>} 返回 `{ list, total, page, pageSize }`
*/
HealthProfileSchema.statics.findProfiles = async function (options = {}) {
const { page = 1, pageSize = 20, userId, name, mobile, sortBy = "createtime" } = options;
const filter = {};
if (userId) {
filter.userId = userId;
}
if (name) {
filter["profile.name"] = { $regex: name, $options: "i" };
}
if (mobile) {
filter["profile.mobile"] = mobile;
}
const sortField = sortBy === "updatetime" ? "meta.updatetime" : "meta.createtime";
const skip = (page - 1) * pageSize;
const [list, total] = await Promise.all([
this.find(filter)
.sort({ [sortField]: -1 })
.skip(skip)
.limit(pageSize)
.exec(),
this.countDocuments(filter).exec(),
]);
return { list, total, page: parseInt(page), pageSize: parseInt(pageSize) };
};
/**
* 根据ID查找健康档案
*/
HealthProfileSchema.statics.findProfileById = async function (id) {
return await this.findById(id).exec();
};
/**
* 创建健康档案
*/
HealthProfileSchema.statics.createProfile = async function (data) {
data.meta = { createtime: Date.now(), updatetime: Date.now() };
const doc = new this(data);
return await doc.save();
};
/**
* 根据ID更新健康档案
*/
HealthProfileSchema.statics.updateProfileById = async function (id, update) {
update["meta.updatetime"] = Date.now();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }).exec();
};
/**
* 根据用户ID更新健康档案
*/
HealthProfileSchema.statics.updateProfile = async function (userId, update) {
update["meta.updatetime"] = Date.now();
return await this.findOneAndUpdate({ userId }, { $set: update }, { new: true }).exec();
};
/**
* 根据ID删除健康档案
*/
HealthProfileSchema.statics.deleteProfileById = async function (id) {
try {
return await this.findByIdAndDelete(id).exec();
} catch (error) {
console.error("删除健康档案失败:", error);
return null;
}
};
// ==================== 索引定义 ====================
HealthProfileSchema.index({ userId: 1 }, { sparse: true });
HealthProfileSchema.index({ "profile.mobile": 1 });
export { HealthProfileSchema };