feat: 接口调整
This commit is contained in:
parent
dab6c9317d
commit
2eca9f867c
@ -8,6 +8,8 @@ exports.main = async (event, mongodb) => {
|
|||||||
return await getCorpInfo(event);
|
return await getCorpInfo(event);
|
||||||
case "getCustomCorpInfo":
|
case "getCustomCorpInfo":
|
||||||
return await getCustomCorpInfo(event);
|
return await getCustomCorpInfo(event);
|
||||||
|
case "getCorpDiseaseList":
|
||||||
|
return await getCorpDiseaseList(event);
|
||||||
case "addCorpDisease":
|
case "addCorpDisease":
|
||||||
return await addCorpDisease(event);
|
return await addCorpDisease(event);
|
||||||
case "updateCorp":
|
case "updateCorp":
|
||||||
@ -97,6 +99,33 @@ async function getCustomCorpInfo(event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getCorpDiseaseList(event) {
|
||||||
|
const { corpId } = event;
|
||||||
|
if (typeof corpId !== "string" || !corpId.trim()) {
|
||||||
|
return { success: false, message: "参数错误", data: [] };
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const corp = await db.collection("corp").findOne(
|
||||||
|
{ corpId: corpId.trim() },
|
||||||
|
{ projection: { _id: 0, diseases: 1 } }
|
||||||
|
);
|
||||||
|
if (!corp) {
|
||||||
|
return { success: false, message: "未查询到机构", data: [] };
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "获取成功",
|
||||||
|
data: Array.isArray(corp.diseases) ? corp.diseases : [],
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: error.message || "获取失败",
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function addCorpDisease(event) {
|
async function addCorpDisease(event) {
|
||||||
let { corpId, disease, oldDisease } = event;
|
let { corpId, disease, oldDisease } = event;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -18,6 +18,7 @@ exports.main = async (event, db, req) => {
|
|||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case "getCorpInfo":
|
case "getCorpInfo":
|
||||||
case "getCustomCorpInfo":
|
case "getCustomCorpInfo":
|
||||||
|
case "getCorpDiseaseList":
|
||||||
case "addCorpDisease":
|
case "addCorpDisease":
|
||||||
case "updateCorp":
|
case "updateCorp":
|
||||||
case "addCorp":
|
case "addCorp":
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
const { ObjectId } = require("mongodb");
|
|
||||||
|
|
||||||
let ensureIndexesPromise = null;
|
let ensureIndexesPromise = null;
|
||||||
|
|
||||||
async function ensureApplicableDiagnosisIndexes(db) {
|
async function ensureApplicableDiagnosisIndexes(db) {
|
||||||
if (!ensureIndexesPromise) {
|
if (!ensureIndexesPromise) {
|
||||||
ensureIndexesPromise = Promise.all([
|
ensureIndexesPromise = Promise.all([
|
||||||
db.collection("drug-info").createIndex({ applicableDiagnosisIds: 1 }),
|
db.collection("drug-info").createIndex({ applicableDiagnosisCodes: 1 }),
|
||||||
db.collection("online-drug-info").createIndex({ applicableDiagnosisIds: 1 }),
|
db.collection("online-drug-info").createIndex({ applicableDiagnosisCodes: 1 }),
|
||||||
]).catch((error) => {
|
]).catch((error) => {
|
||||||
ensureIndexesPromise = null;
|
ensureIndexesPromise = null;
|
||||||
throw error;
|
throw error;
|
||||||
@ -15,43 +13,45 @@ async function ensureApplicableDiagnosisIndexes(db) {
|
|||||||
await ensureIndexesPromise;
|
await ensureIndexesPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function validateApplicableDiagnosisIds(params, db) {
|
async function validateApplicableDiagnosisCodes(params, db) {
|
||||||
if (!Object.prototype.hasOwnProperty.call(params, "applicableDiagnosisIds")) {
|
if (!Object.prototype.hasOwnProperty.call(params, "applicableDiagnosisCodes")) {
|
||||||
return { present: false, value: [] };
|
return { present: false, value: [] };
|
||||||
}
|
}
|
||||||
|
|
||||||
const rawIds = params.applicableDiagnosisIds;
|
const rawCodes = params.applicableDiagnosisCodes;
|
||||||
if (!Array.isArray(rawIds)) {
|
if (!Array.isArray(rawCodes)) {
|
||||||
throw new Error("适应诊断格式错误");
|
throw new Error("适应诊断格式错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
const uniqueIds = [];
|
const uniqueCodes = [];
|
||||||
const seen = new Set();
|
const seen = new Set();
|
||||||
for (const rawId of rawIds) {
|
for (const rawCode of rawCodes) {
|
||||||
if (typeof rawId !== "string" || !ObjectId.isValid(rawId.trim())) {
|
if (typeof rawCode !== "string" || !rawCode.trim()) {
|
||||||
throw new Error("适应诊断包含无效ID");
|
throw new Error("适应诊断包含无效编码");
|
||||||
}
|
}
|
||||||
const id = rawId.trim();
|
const code = rawCode.trim();
|
||||||
if (!seen.has(id)) {
|
if (!seen.has(code)) {
|
||||||
seen.add(id);
|
seen.add(code);
|
||||||
uniqueIds.push(new ObjectId(id));
|
uniqueCodes.push(code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uniqueIds.length) {
|
if (uniqueCodes.length) {
|
||||||
const diagnosisCount = await db.collection("hlw-diagnosis").countDocuments({
|
const diagnoses = await db.collection("hlw-diagnosis").find(
|
||||||
_id: { $in: uniqueIds },
|
{ code: { $in: uniqueCodes } },
|
||||||
});
|
{ projection: { _id: 0, code: 1 } }
|
||||||
if (diagnosisCount !== uniqueIds.length) {
|
).toArray();
|
||||||
|
const existingCodes = new Set(diagnoses.map(item => item.code));
|
||||||
|
if (uniqueCodes.some(code => !existingCodes.has(code))) {
|
||||||
throw new Error("部分适应诊断不存在,请重新选择");
|
throw new Error("部分适应诊断不存在,请重新选择");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await ensureApplicableDiagnosisIndexes(db);
|
await ensureApplicableDiagnosisIndexes(db);
|
||||||
return { present: true, value: uniqueIds };
|
return { present: true, value: uniqueCodes };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ensureApplicableDiagnosisIndexes,
|
ensureApplicableDiagnosisIndexes,
|
||||||
validateApplicableDiagnosisIds,
|
validateApplicableDiagnosisCodes,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,7 +2,7 @@ const dayjs = require("dayjs");
|
|||||||
const { ObjectId } = require("mongodb");
|
const { ObjectId } = require("mongodb");
|
||||||
|
|
||||||
const { getDrugList } = require('./verify');
|
const { getDrugList } = require('./verify');
|
||||||
const { validateApplicableDiagnosisIds } = require('./applicable-diagnosis');
|
const { validateApplicableDiagnosisCodes } = require('./applicable-diagnosis');
|
||||||
let db = "";
|
let db = "";
|
||||||
|
|
||||||
function getConfigOptionNames(list) {
|
function getConfigOptionNames(list) {
|
||||||
@ -248,8 +248,8 @@ async function addHlwDrugInfo(ctx) {
|
|||||||
creatorId: typeof ctx.operatorId === "string" ? ctx.operatorId : "",
|
creatorId: typeof ctx.operatorId === "string" ? ctx.operatorId : "",
|
||||||
createTime: Date.now()
|
createTime: Date.now()
|
||||||
}
|
}
|
||||||
const diagnosisIds = await validateApplicableDiagnosisIds(params, db);
|
const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db);
|
||||||
const { onSale, applicableDiagnosisIds, ...data } = params;
|
const { onSale, applicableDiagnosisCodes, ...data } = params;
|
||||||
let newDrug = {};
|
let newDrug = {};
|
||||||
if (Object.keys(data).length) {
|
if (Object.keys(data).length) {
|
||||||
const [item] = getDrugList([data], true, peopleInfo, 'hlw', optionLists);
|
const [item] = getDrugList([data], true, peopleInfo, 'hlw', optionLists);
|
||||||
@ -262,8 +262,8 @@ async function addHlwDrugInfo(ctx) {
|
|||||||
if (typeof onSale === "boolean") {
|
if (typeof onSale === "boolean") {
|
||||||
newDrug.onSale = onSale;
|
newDrug.onSale = onSale;
|
||||||
}
|
}
|
||||||
if (diagnosisIds.present) {
|
if (diagnosisCodes.present) {
|
||||||
newDrug.applicableDiagnosisIds = diagnosisIds.value;
|
newDrug.applicableDiagnosisCodes = diagnosisCodes.value;
|
||||||
}
|
}
|
||||||
if (!Object.prototype.hasOwnProperty.call(newDrug, "antibiotic_level")) {
|
if (!Object.prototype.hasOwnProperty.call(newDrug, "antibiotic_level")) {
|
||||||
newDrug.antibiotic_level = 0;
|
newDrug.antibiotic_level = 0;
|
||||||
@ -286,8 +286,8 @@ async function editHlwDrugInfo(ctx) {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const optionLists = await getHlwDrugOptionLists(ctx.corpId);
|
const optionLists = await getHlwDrugOptionLists(ctx.corpId);
|
||||||
const diagnosisIds = await validateApplicableDiagnosisIds(params, db);
|
const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db);
|
||||||
const { onSale, applicableDiagnosisIds, ...data } = params;
|
const { onSale, applicableDiagnosisCodes, ...data } = params;
|
||||||
const currentDrug = await db.collection("drug-info").findOne(
|
const currentDrug = await db.collection("drug-info").findOne(
|
||||||
{ _id: new ObjectId(id) },
|
{ _id: new ObjectId(id) },
|
||||||
{ projection: { erpId: 1, product_id: 1, product_id_str: 1 } }
|
{ projection: { erpId: 1, product_id: 1, product_id_str: 1 } }
|
||||||
@ -319,8 +319,8 @@ async function editHlwDrugInfo(ctx) {
|
|||||||
if (typeof onSale === "boolean") {
|
if (typeof onSale === "boolean") {
|
||||||
updateData.onSale = onSale;
|
updateData.onSale = onSale;
|
||||||
}
|
}
|
||||||
if (diagnosisIds.present) {
|
if (diagnosisCodes.present) {
|
||||||
updateData.applicableDiagnosisIds = diagnosisIds.value;
|
updateData.applicableDiagnosisCodes = diagnosisCodes.value;
|
||||||
}
|
}
|
||||||
if (Object.keys(updateData).length) {
|
if (Object.keys(updateData).length) {
|
||||||
updateData.updateTime = Date.now();
|
updateData.updateTime = Date.now();
|
||||||
@ -473,18 +473,21 @@ async function getDrugMatchedDisease(ctx) {
|
|||||||
if (drugIds.length === 0) {
|
if (drugIds.length === 0) {
|
||||||
return { success: false, message: "参数错误" }
|
return { success: false, message: "参数错误" }
|
||||||
}
|
}
|
||||||
const list = await db.collection("drug-info").find({ _id: { $in: drugIds.map(i => new ObjectId(i)) } }, { projection: { _id: 1, applicableDiagnosisIds: 1 } }).toArray();
|
const list = await db.collection("drug-info").find({ _id: { $in: drugIds.map(i => new ObjectId(i)) } }, { projection: { _id: 1, applicableDiagnosisCodes: 1 } }).toArray();
|
||||||
const ids = [];
|
const codes = [];
|
||||||
list.forEach(i => {
|
list.forEach(i => {
|
||||||
if (i && Array.isArray(i.applicableDiagnosisIds)) {
|
if (i && Array.isArray(i.applicableDiagnosisCodes)) {
|
||||||
ids.push(...i.applicableDiagnosisIds);
|
codes.push(...i.applicableDiagnosisCodes.filter(code => typeof code === 'string' && code.trim()).map(code => code.trim()));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (ids.length === 0) {
|
const uniqueCodes = [...new Set(codes)];
|
||||||
|
if (uniqueCodes.length === 0) {
|
||||||
return { success: true, message: "查询成功", list: [] }
|
return { success: true, message: "查询成功", list: [] }
|
||||||
}
|
}
|
||||||
const diseases = await db.collection("hlw-diagnosis").find({ _id: { $in: ids} }, { projection: { _id: 1, name: 1 } }).toArray();
|
const diseases = await db.collection("hlw-diagnosis").find({ code: { $in: uniqueCodes } }, { projection: { _id: 1, code: 1, name: 1 } }).toArray();
|
||||||
return { success: true, message: "查询成功", list: diseases.map(i => i.name) }
|
const diseaseMap = new Map(diseases.map(item => [item.code, item.name]));
|
||||||
|
const names = uniqueCodes.map(code => diseaseMap.get(code)).filter(name => typeof name === 'string' && name.trim());
|
||||||
|
return { success: true, message: "查询成功", list: [...new Set(names)] }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return { success: false, message: e.message }
|
return { success: false, message: e.message }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,6 +37,13 @@ async function getDiagnosisList(item) {
|
|||||||
}
|
}
|
||||||
query._id = { $in: ids.map(id => new ObjectId(id)) };
|
query._id = { $in: ids.map(id => new ObjectId(id)) };
|
||||||
}
|
}
|
||||||
|
if (Array.isArray(item.codes)) {
|
||||||
|
if (item.codes.some(code => typeof code !== 'string' || !code.trim())) {
|
||||||
|
return { success: false, message: '诊断编码格式错误' };
|
||||||
|
}
|
||||||
|
const codes = [...new Set(item.codes.map(code => code.trim()))];
|
||||||
|
query.code = { $in: codes };
|
||||||
|
}
|
||||||
if (name && typeof name === "string") {
|
if (name && typeof name === "string") {
|
||||||
query.name = new RegExp(name.trim(), "i");
|
query.name = new RegExp(name.trim(), "i");
|
||||||
}
|
}
|
||||||
@ -57,7 +64,7 @@ async function getDiagnosisList(item) {
|
|||||||
const res = await db
|
const res = await db
|
||||||
.collection("hlw-diagnosis")
|
.collection("hlw-diagnosis")
|
||||||
.find(query)
|
.find(query)
|
||||||
.sort({ createTime: -1 })
|
.sort({ _id: -1 })
|
||||||
.skip((page - 1) * pageSize)
|
.skip((page - 1) * pageSize)
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.toArray();
|
.toArray();
|
||||||
@ -88,10 +95,17 @@ async function deleteHlwDiagnosis(item) {
|
|||||||
return { success: false, message: '诊断ID格式错误' }
|
return { success: false, message: '诊断ID格式错误' }
|
||||||
}
|
}
|
||||||
const diagnosisId = new ObjectId(id.trim());
|
const diagnosisId = new ObjectId(id.trim());
|
||||||
|
const diagnosis = await db.collection("hlw-diagnosis").findOne(
|
||||||
|
{ _id: diagnosisId },
|
||||||
|
{ projection: { _id: 1, code: 1 } }
|
||||||
|
);
|
||||||
|
if (!diagnosis) {
|
||||||
|
return { success: false, message: '未查询到诊断' }
|
||||||
|
}
|
||||||
await ensureApplicableDiagnosisIndexes(db);
|
await ensureApplicableDiagnosisIndexes(db);
|
||||||
const [hlwDrugCount, onlineDrugCount] = await Promise.all([
|
const [hlwDrugCount, onlineDrugCount] = await Promise.all([
|
||||||
db.collection("drug-info").countDocuments({ applicableDiagnosisIds: diagnosisId }, { limit: 1 }),
|
db.collection("drug-info").countDocuments({ applicableDiagnosisCodes: diagnosis.code }, { limit: 1 }),
|
||||||
db.collection("online-drug-info").countDocuments({ applicableDiagnosisIds: diagnosisId }, { limit: 1 }),
|
db.collection("online-drug-info").countDocuments({ applicableDiagnosisCodes: diagnosis.code }, { limit: 1 }),
|
||||||
]);
|
]);
|
||||||
if (hlwDrugCount > 0 || onlineDrugCount > 0) {
|
if (hlwDrugCount > 0 || onlineDrugCount > 0) {
|
||||||
return { success: false, message: '该诊断已被药品引用,请先解除关联后再删除' }
|
return { success: false, message: '该诊断已被药品引用,请先解除关联后再删除' }
|
||||||
@ -122,11 +136,17 @@ async function updateHlwDiagnosis(item) {
|
|||||||
// 调用mongo 数据库更新
|
// 调用mongo 数据库更新
|
||||||
const [valid, data] = verifyDisease(item);
|
const [valid, data] = verifyDisease(item);
|
||||||
if (!valid) return { success: false, message: data }
|
if (!valid) return { success: false, message: data }
|
||||||
|
const diagnosisId = new ObjectId(item.id);
|
||||||
|
const duplicate = await db.collection("hlw-diagnosis").findOne({
|
||||||
|
code: data.code,
|
||||||
|
_id: { $ne: diagnosisId },
|
||||||
|
}, { projection: { _id: 1 } });
|
||||||
|
if (duplicate) return { success: false, message: '诊断编码已存在' }
|
||||||
data.updateTime = Date.now()
|
data.updateTime = Date.now()
|
||||||
const res = await db
|
const res = await db
|
||||||
.collection("hlw-diagnosis")
|
.collection("hlw-diagnosis")
|
||||||
.updateOne(
|
.updateOne(
|
||||||
{ _id: new ObjectId(item.id) },
|
{ _id: diagnosisId },
|
||||||
{ $set: data }
|
{ $set: data }
|
||||||
);
|
);
|
||||||
if (res.matchedCount === 0) {
|
if (res.matchedCount === 0) {
|
||||||
@ -150,6 +170,11 @@ async function addHlwDiagnosis(item) {
|
|||||||
// 调用mongo 数据库新增
|
// 调用mongo 数据库新增
|
||||||
const [valid, data] = verifyDisease(item);
|
const [valid, data] = verifyDisease(item);
|
||||||
if (!valid) return { success: false, message: data }
|
if (!valid) return { success: false, message: data }
|
||||||
|
const duplicate = await db.collection("hlw-diagnosis").findOne(
|
||||||
|
{ code: data.code },
|
||||||
|
{ projection: { _id: 1 } }
|
||||||
|
);
|
||||||
|
if (duplicate) return { success: false, message: '诊断编码已存在' }
|
||||||
data.createTime = Date.now()
|
data.createTime = Date.now()
|
||||||
const res = await db.collection("hlw-diagnosis").insertOne(data);
|
const res = await db.collection("hlw-diagnosis").insertOne(data);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
const { ObjectId } = require("mongodb");
|
const { ObjectId } = require("mongodb");
|
||||||
const { getDrugList } = require("../drug-info/verify");
|
const { getDrugList } = require("../drug-info/verify");
|
||||||
const { validateApplicableDiagnosisIds } = require("../drug-info/applicable-diagnosis");
|
const { validateApplicableDiagnosisCodes } = require("../drug-info/applicable-diagnosis");
|
||||||
const validator = require("../../utils/validator");
|
const validator = require("../../utils/validator");
|
||||||
|
|
||||||
exports.main = async (item, db) => {
|
exports.main = async (item, db) => {
|
||||||
@ -170,8 +170,8 @@ async function addOnlineDrugInfo(ctx, db) {
|
|||||||
typeof ctx.operatorId === "string" ? ctx.operatorId : "",
|
typeof ctx.operatorId === "string" ? ctx.operatorId : "",
|
||||||
createTime: Date.now(),
|
createTime: Date.now(),
|
||||||
};
|
};
|
||||||
const diagnosisIds = await validateApplicableDiagnosisIds(params, db);
|
const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db);
|
||||||
const { onSale, applicableDiagnosisIds, ...data } = params;
|
const { onSale, applicableDiagnosisCodes, ...data } = params;
|
||||||
let newDrug = {};
|
let newDrug = {};
|
||||||
if (Object.keys(data).length) {
|
if (Object.keys(data).length) {
|
||||||
if (!data.his_drug_code || !String(data.his_drug_code).trim()) {
|
if (!data.his_drug_code || !String(data.his_drug_code).trim()) {
|
||||||
@ -193,8 +193,8 @@ async function addOnlineDrugInfo(ctx, db) {
|
|||||||
if (typeof onSale === "boolean") {
|
if (typeof onSale === "boolean") {
|
||||||
newDrug.onSale = onSale;
|
newDrug.onSale = onSale;
|
||||||
}
|
}
|
||||||
if (diagnosisIds.present) {
|
if (diagnosisCodes.present) {
|
||||||
newDrug.applicableDiagnosisIds = diagnosisIds.value;
|
newDrug.applicableDiagnosisCodes = diagnosisCodes.value;
|
||||||
}
|
}
|
||||||
if (!Object.prototype.hasOwnProperty.call(newDrug, "antibiotic_level")) {
|
if (!Object.prototype.hasOwnProperty.call(newDrug, "antibiotic_level")) {
|
||||||
newDrug.antibiotic_level = 0;
|
newDrug.antibiotic_level = 0;
|
||||||
@ -229,8 +229,8 @@ async function editOnlineDrugInfo(ctx, db) {
|
|||||||
return { success: false, message: "参数错误" };
|
return { success: false, message: "参数错误" };
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const diagnosisIds = await validateApplicableDiagnosisIds(params, db);
|
const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db);
|
||||||
const { onSale, applicableDiagnosisIds, ...data } = params;
|
const { onSale, applicableDiagnosisCodes, ...data } = params;
|
||||||
let updateData = {};
|
let updateData = {};
|
||||||
if (Object.keys(data).length) {
|
if (Object.keys(data).length) {
|
||||||
if ("his_drug_code" in data && !String(data.his_drug_code).trim()) {
|
if ("his_drug_code" in data && !String(data.his_drug_code).trim()) {
|
||||||
@ -258,8 +258,8 @@ async function editOnlineDrugInfo(ctx, db) {
|
|||||||
if (typeof onSale === "boolean") {
|
if (typeof onSale === "boolean") {
|
||||||
updateData.onSale = onSale;
|
updateData.onSale = onSale;
|
||||||
}
|
}
|
||||||
if (diagnosisIds.present) {
|
if (diagnosisCodes.present) {
|
||||||
updateData.applicableDiagnosisIds = diagnosisIds.value;
|
updateData.applicableDiagnosisCodes = diagnosisCodes.value;
|
||||||
}
|
}
|
||||||
if (Object.keys(updateData).length) {
|
if (Object.keys(updateData).length) {
|
||||||
updateData.updateTime = Date.now();
|
updateData.updateTime = Date.now();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user