start
This commit is contained in:
170
handler/org.js
Normal file
170
handler/org.js
Normal file
@@ -0,0 +1,170 @@
|
||||
import { DBModel } from "../models/index.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
|
||||
class HandlerHospital {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
async searchHospitalByName(ctx) {
|
||||
try {
|
||||
const { name, page = 1, pageSize = 20 } = ctx.request.query;
|
||||
if (!name) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少搜索关键词");
|
||||
}
|
||||
|
||||
const hospitals = await DBModel.Hospital.findByName(name, {
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
});
|
||||
|
||||
return ResponseUtil.success(ctx, { hospitals }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getHospitalsByCity(ctx) {
|
||||
try {
|
||||
const { city, page = 1, pageSize = 20, level, type } = ctx.request.query;
|
||||
if (!city) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少城市参数");
|
||||
}
|
||||
|
||||
const hospitals = await DBModel.Hospital.findByCity(city, {
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
level,
|
||||
type,
|
||||
});
|
||||
|
||||
return ResponseUtil.success(ctx, { hospitals }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getHospitalSelector(ctx) {
|
||||
try {
|
||||
const { city, level, type } = ctx.request.query;
|
||||
const hospitals = await DBModel.Hospital.getHospitalSelector({
|
||||
city,
|
||||
level,
|
||||
type,
|
||||
});
|
||||
|
||||
return ResponseUtil.success(ctx, { hospitals }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getHospitalById(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!id) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少医院ID");
|
||||
}
|
||||
|
||||
const hospital = await DBModel.Hospital.findById(id);
|
||||
if (!hospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { hospital }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async createHospital(ctx) {
|
||||
try {
|
||||
const hospital = ctx.request.body;
|
||||
if (!hospital.basic?.name) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少医院名称");
|
||||
}
|
||||
|
||||
const newHospital = await DBModel.Hospital.createHospital(hospital);
|
||||
return ResponseUtil.success(ctx, { hospital: newHospital }, "创建成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async updateHospital(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
const update = ctx.request.body;
|
||||
|
||||
if (!id) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少医院ID");
|
||||
}
|
||||
|
||||
const updatedHospital = await DBModel.Hospital.updateHospital(id, update);
|
||||
if (!updatedHospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { hospital: updatedHospital }, "更新成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async setHospitalStatus(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
const { isEnabled } = ctx.request.body;
|
||||
|
||||
if (!id) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少医院ID");
|
||||
}
|
||||
|
||||
const updatedHospital = await DBModel.Hospital.setHospitalStatus(id, isEnabled);
|
||||
if (!updatedHospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, { hospital: updatedHospital }, "状态更新成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getAllHospitals(ctx) {
|
||||
try {
|
||||
const { page = 1, pageSize = 20 } = ctx.request.query;
|
||||
const skip = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
|
||||
const hospitals = await DBModel.Hospital.find({})
|
||||
.sort({ "service.sortOrder": 1, "basic.name": 1 })
|
||||
.skip(skip)
|
||||
.limit(parseInt(pageSize));
|
||||
|
||||
const total = await DBModel.Hospital.countDocuments({});
|
||||
|
||||
return ResponseUtil.success(ctx, { hospitals, total }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteHospital(ctx) {
|
||||
try {
|
||||
const { id } = ctx.params;
|
||||
if (!id) {
|
||||
return ResponseUtil.badRequest(ctx, "缺少医院ID");
|
||||
}
|
||||
|
||||
const deletedHospital = await DBModel.Hospital.findByIdAndDelete(id);
|
||||
if (!deletedHospital) {
|
||||
return ResponseUtil.notFound(ctx, "医院不存在");
|
||||
}
|
||||
|
||||
return ResponseUtil.success(ctx, null, "删除成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { HandlerHospital };
|
||||
Reference in New Issue
Block a user