feat(hospital): 新增医院基础信息接口与复旦排名
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import mongoose from "mongoose";
|
||||
import { DBModel } from "../models/index.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
|
||||
/**
|
||||
* 医院基础信息:维护名称、主体名称、省市、地址、电话、科室、等级。
|
||||
* 分院/院区作为独立医院记录,不设上下级关系;名称约定为「主体名称-分院名称」。
|
||||
*/
|
||||
class HandlerHospital {
|
||||
// 单页最大条数,避免 pageSize 传超大值一次拖库
|
||||
static MAX_PAGE_SIZE = 200;
|
||||
|
||||
// 可写字段白名单(pinyin / pinyinFL 由服务端自动生成,不接受手写)
|
||||
static FIELDS = ["name", "entityName", "province", "city", "address", "phone", "departments", "level"];
|
||||
|
||||
// 解析分页参数并夹紧上限
|
||||
static parsePaging(query = {}) {
|
||||
const page = Math.max(1, parseInt(query.page) || 1);
|
||||
const rawSize = parseInt(query.pageSize) || 20;
|
||||
const pageSize = Math.min(Math.max(1, rawSize), HandlerHospital.MAX_PAGE_SIZE);
|
||||
return { page, pageSize };
|
||||
}
|
||||
|
||||
// 按白名单挑选请求体字段,避免 schema 之外的字段被写入
|
||||
static pickFields(body = {}) {
|
||||
const picked = {};
|
||||
for (const key of Object.keys(body || {})) {
|
||||
if (HandlerHospital.FIELDS.includes(key)) {
|
||||
picked[key] = body[key];
|
||||
}
|
||||
}
|
||||
return picked;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并规整字段(只处理请求中出现的字段,未出现的沿用原值)
|
||||
* @param {Object} fields 已按白名单挑出的字段
|
||||
* @param {boolean} requireName 新增时要求名称必填
|
||||
* @returns {string|null} 不合规时返回提示文案
|
||||
*/
|
||||
static validateFields(fields, { requireName = false } = {}) {
|
||||
if (requireName || fields.name !== undefined) {
|
||||
const name = String(fields.name ?? "").trim();
|
||||
if (!name) return "请填写医院名称";
|
||||
fields.name = name;
|
||||
}
|
||||
|
||||
for (const key of ["entityName", "province", "city", "address", "phone", "level"]) {
|
||||
if (fields[key] !== undefined) {
|
||||
fields[key] = String(fields[key] ?? "").trim();
|
||||
}
|
||||
}
|
||||
|
||||
if (fields.departments !== undefined) {
|
||||
if (!Array.isArray(fields.departments)) return "科室需为字符串数组";
|
||||
fields.departments = fields.departments
|
||||
.map((item) => String(item ?? "").trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// 校验失败统一转成 400,避免把 mongoose 字段细节抛给前端
|
||||
static handleWriteError(ctx, err) {
|
||||
if (err?.code === 11000) {
|
||||
return ResponseUtil.badRequest(ctx, "医院名称已存在,请使用「主体名称-分院名称」格式避免重名");
|
||||
}
|
||||
if (err?.name === "ValidationError") {
|
||||
return ResponseUtil.badRequest(ctx, "医院信息不合法,请检查名称、科室等字段");
|
||||
}
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
|
||||
// 医院列表(公开):支持分页、关键词搜索与按主体名称列分院
|
||||
async getHospitals(ctx) {
|
||||
try {
|
||||
const keyword = String(ctx.request.query.keyword || "").trim();
|
||||
const entityName = String(ctx.request.query.entityName || "").trim();
|
||||
const { page, pageSize } = HandlerHospital.parsePaging(ctx.request.query);
|
||||
const result = await DBModel.Hospital.findHospitals({ page, pageSize, keyword, entityName });
|
||||
return ResponseUtil.success(ctx, result, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 医院详情(公开)
|
||||
async getHospitalById(ctx) {
|
||||
try {
|
||||
const id = ctx.params.id;
|
||||
|
||||
// 非法 ObjectId 会让 mongoose 抛 CastError,先挡掉
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
return ResponseUtil.badRequest(ctx, "医院ID不合法");
|
||||
}
|
||||
|
||||
const hospital = await DBModel.Hospital.findHospitalById(id);
|
||||
if (!hospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { hospital }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 新增医院(管理员)
|
||||
async createHospital(ctx) {
|
||||
try {
|
||||
const fields = HandlerHospital.pickFields(ctx.request.body);
|
||||
const invalid = HandlerHospital.validateFields(fields, { requireName: true });
|
||||
if (invalid) {
|
||||
return ResponseUtil.badRequest(ctx, invalid);
|
||||
}
|
||||
|
||||
const hospital = await DBModel.Hospital.createHospital(fields);
|
||||
return ResponseUtil.success(ctx, { hospital }, "创建成功");
|
||||
} catch (err) {
|
||||
return HandlerHospital.handleWriteError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改医院(管理员)
|
||||
async updateHospital(ctx) {
|
||||
try {
|
||||
const id = ctx.params.id;
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
return ResponseUtil.badRequest(ctx, "医院ID不合法");
|
||||
}
|
||||
|
||||
const fields = HandlerHospital.pickFields(ctx.request.body);
|
||||
if (Object.keys(fields).length === 0) {
|
||||
return ResponseUtil.badRequest(ctx, "没有可更新的字段");
|
||||
}
|
||||
|
||||
const invalid = HandlerHospital.validateFields(fields);
|
||||
if (invalid) {
|
||||
return ResponseUtil.badRequest(ctx, invalid);
|
||||
}
|
||||
|
||||
const hospital = await DBModel.Hospital.updateHospitalById(id, fields);
|
||||
if (!hospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { hospital }, "更新成功");
|
||||
} catch (err) {
|
||||
return HandlerHospital.handleWriteError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除医院(管理员)
|
||||
async deleteHospital(ctx) {
|
||||
try {
|
||||
const id = ctx.params.id;
|
||||
if (!mongoose.Types.ObjectId.isValid(id)) {
|
||||
return ResponseUtil.badRequest(ctx, "医院ID不合法");
|
||||
}
|
||||
|
||||
const hospital = await DBModel.Hospital.deleteHospitalById(id);
|
||||
if (!hospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { hospital }, "删除成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { HandlerHospital };
|
||||
Reference in New Issue
Block a user