增加健康档案api
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { EscortRecordSchema } from "./schema/escort_record.js"
|
||||
import { OrganizationSchema } from "./schema/org.js"
|
||||
import { HealthProfileSchema } from "./schema/health_profile.js"
|
||||
import config from '../conf.json' with { type: 'json' };
|
||||
import logger from '../utils/logger.js';
|
||||
|
||||
@@ -12,6 +13,7 @@ class MongoDBSchema {
|
||||
this.User = null;
|
||||
this.EscortRecord = null;
|
||||
this.Organization = null;
|
||||
this.HealthProfile = null;
|
||||
}
|
||||
|
||||
init() {
|
||||
@@ -38,6 +40,7 @@ class MongoDBSchema {
|
||||
);
|
||||
|
||||
this.EscortRecord = this.dbConnection.model('escort_record', EscortRecordSchema)
|
||||
this.HealthProfile = this.dbConnection.model('health_profile', HealthProfileSchema)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 };
|
||||
Reference in New Issue
Block a user