ai review

This commit is contained in:
lik
2026-09-01 21:49:17 +08:00
parent 390c871d35
commit 98c9026485
14 changed files with 288 additions and 262 deletions
+6 -7
View File
@@ -2,7 +2,6 @@
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';
@@ -21,22 +20,22 @@ class MongoDBSchema {
this.dbConnection = mongoose.createConnection(config.mongodb.str, config.mongodb.option);
this.dbConnection.on("error", () => {
logger.error.bind(logger, "...mongodb connect error ...")
logger.error("...mongodb connect error ...");
});
this.dbConnection.on("connected", async () => {
logger.info("Mongodb: " + config.mongodb.str + " connected");
logger.info(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} connected`);
});
this.dbConnection.on("disconnected", () =>
logger.warn("Mongodb: " + config.mongodb.str + " disconnected")
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} disconnected`)
);
this.dbConnection.on("reconnected", () =>
logger.info("Mongodb: " + config.mongodb.str + " reconnected")
logger.info(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} reconnected`)
);
this.dbConnection.on("disconnecting", () =>
logger.warn("Mongodb: " + config.mongodb.str + " disconnecting")
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} disconnecting`)
);
this.dbConnection.on("close", () =>
logger.warn("Mongodb: " + config.mongodb.str + " closed")
logger.warn(`Mongodb: ${config.mongodb.host}/${config.mongodb.option.dbName} closed`)
);
this.EscortRecord = this.dbConnection.model('escort_record', EscortRecordSchema)
+33 -15
View File
@@ -7,7 +7,7 @@ import mongoose from "mongoose";
*
* 用于记录和管理陪诊服务全流程数据,包含以下主要分类:
* - 基础信息:userId(订单提交用户)、healthProfileId(关联健康档案ID
* - 患者信息:patient(姓名、电话、性别、年龄、身份证号)
* - 患者信息:patient(姓名、电话、性别、出生年月、身份证号)
* - 陪诊服务:escort(服务ID、服务名称)
* - 就诊信息:hospital(医院省份、名称、地址、科室、医生、病历号)
* - 时间安排:schedule(预约时间、开始时间、结束时间、时长)
@@ -50,7 +50,7 @@ const EscortRecordSchema = mongoose.Schema(
name: { type: String, default: "", comment: "患者姓名" },
mobile: { type: String, default: "", index: true, comment: "患者联系电话" },
sex: { type: String, enum: ["male", "female"], comment: "患者性别" },
age: { type: Number, default: 0, comment: "患者年龄" },
birth: { type: String, default: "", comment: "患者出生年月(YYYY-MM-DD" },
weight: { type: Number, default: 0, comment: "患者体重(kg" },
height: { type: Number, default: 0, comment: "患者身高(cm" },
idnumber: { type: String, default: "", comment: "患者身份证号" },
@@ -193,6 +193,34 @@ EscortRecordSchema.statics.findRecords = async function (options = {}, cb) {
.exec(cb);
};
/**
* 查找陪诊员的陪诊记录
*
* @param {ObjectId|string} attendantId - 陪诊员用户ID
* @param {Object} options - 查询选项
* @param {number} [options.page=1] - 页码
* @param {number} [options.pageSize=20] - 每页数量
* @param {string} [options.status] - 状态筛选(可选)
* @param {Function} [cb] - 可选的回调函数
* @returns {Promise<Array>} 陪诊记录列表(按预约日期倒序)
*/
EscortRecordSchema.statics.findRecordsByAttendant = async function (attendantId, options = {}, cb) {
const { page = 1, pageSize = 20, status } = options;
const filter = { "attendant.id": attendantId };
if (status) {
filter.status = status;
}
const skip = (page - 1) * pageSize;
return await this.find(filter)
.sort({ "schedule.date": -1 })
.skip(skip)
.limit(pageSize)
.exec(cb);
};
/**
* 创建陪诊记录
*
@@ -219,13 +247,8 @@ EscortRecordSchema.statics.createRecord = async function (record, cb) {
* @returns {Promise<Object|null>} 更新后的记录,失败返回null
*/
EscortRecordSchema.statics.updateRecord = async function (id, update, cb) {
try {
update["meta.updatetime"] = Date.now();
return await this.findByIdAndUpdate(id, { $set: update }, { new: true }, cb);
} catch (error) {
console.error("更新陪诊记录失败:", error);
return null;
}
const $set = { ...update, "meta.updatetime": Date.now() };
return await this.findByIdAndUpdate(id, { $set }, { new: true }, cb);
};
/**
@@ -268,12 +291,7 @@ EscortRecordSchema.statics.findRecordsByStatus = async function (status, options
* @returns {Promise<Object|null>} 删除的记录,失败返回null
*/
EscortRecordSchema.statics.deleteRecord = async function (id, cb) {
try {
return await this.findByIdAndDelete(id, cb);
} catch (error) {
console.error("删除陪诊记录失败:", error);
return null;
}
return await this.findByIdAndDelete(id, cb);
};
// ==================== 索引定义 ====================
+5 -8
View File
@@ -21,7 +21,7 @@ const HealthProfileSchema = mongoose.Schema(
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: "年龄" },
birth: { type: String, default: "", comment: "出生年月(YYYY-MM-DD" },
idnumber: { type: String, default: "", comment: "身份证号" },
},
@@ -76,7 +76,9 @@ HealthProfileSchema.statics.findByUserId = async function (userId) {
filter.userId = userId;
}
if (name) {
filter["profile.name"] = { $regex: name, $options: "i" };
// 转义正则特殊字符,避免查询报错或 ReDoS
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
filter["profile.name"] = { $regex: escaped, $options: "i" };
}
if (mobile) {
filter["profile.mobile"] = mobile;
@@ -132,12 +134,7 @@ HealthProfileSchema.statics.updateProfile = async function (userId, update) {
* 根据ID删除健康档案
*/
HealthProfileSchema.statics.deleteProfileById = async function (id) {
try {
return await this.findByIdAndDelete(id).exec();
} catch (error) {
console.error("删除健康档案失败:", error);
return null;
}
return await this.findByIdAndDelete(id).exec();
};
// ==================== 索引定义 ====================