This commit is contained in:
lik
2026-11-06 21:01:56 +08:00
parent af9a1543ae
commit adaf10376d
3 changed files with 43 additions and 1 deletions
+39
View File
@@ -114,6 +114,45 @@ class HandlerHealthProfile {
}
}
/**
* 管理端批量统计就诊人数量
*
* 用户列表页一屏最多 200 个用户,逐个查会产生 N+1;
* 这里按 userIds 一次聚合,未建档用户返回 0。
*/
async getProfileCounts(ctx) {
try {
const raw = String(ctx.request.query.userIds || "");
const ids = [...new Set(raw.split(",").map((id) => id.trim()).filter(Boolean))];
if (ids.length > HandlerHealthProfile.MAX_PAGE_SIZE) {
return ResponseUtil.badRequest(ctx, `单次最多查询 ${HandlerHealthProfile.MAX_PAGE_SIZE} 位用户`);
}
if (!ids.length) {
return ResponseUtil.success(ctx, { counts: {} }, "查询成功");
}
const invalid = ids.filter((id) => !mongoose.Types.ObjectId.isValid(id));
if (invalid.length) {
return ResponseUtil.badRequest(ctx, "用户ID不合法");
}
const rows = await DBModel.HealthProfile.aggregate([
{ $match: { userId: { $in: ids.map((id) => new mongoose.Types.ObjectId(id)) } } },
{ $group: { _id: "$userId", count: { $sum: 1 } } },
]).exec();
const counts = Object.fromEntries(ids.map((id) => [id, 0]));
for (const row of rows) {
counts[String(row._id)] = row.count;
}
return ResponseUtil.success(ctx, { counts }, "查询成功");
} catch (err) {
return ResponseUtil.internalError(ctx, err);
}
}
// 查询"我的"健康档案列表:强制以登录用户身份查询,忽略请求中的 userId
async getMyProfiles(ctx) {
try {