From 2eca9f867cba11329a03d6850195b236a7cfadf9 Mon Sep 17 00:00:00 2001 From: huxuejian Date: Tue, 25 Aug 2026 13:39:09 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=20=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- corp/corp-base/index.js | 29 +++++++++++++++++ corp/index.js | 1 + hlw/drug-info/applicable-diagnosis.js | 46 +++++++++++++-------------- hlw/drug-info/index.js | 35 ++++++++++---------- hlw/hlw-diagnosis/index.js | 33 ++++++++++++++++--- hlw/online-drug-info/index.js | 18 +++++------ 6 files changed, 110 insertions(+), 52 deletions(-) diff --git a/corp/corp-base/index.js b/corp/corp-base/index.js index 4ccf51a..ccc048d 100644 --- a/corp/corp-base/index.js +++ b/corp/corp-base/index.js @@ -8,6 +8,8 @@ exports.main = async (event, mongodb) => { return await getCorpInfo(event); case "getCustomCorpInfo": return await getCustomCorpInfo(event); + case "getCorpDiseaseList": + return await getCorpDiseaseList(event); case "addCorpDisease": return await addCorpDisease(event); 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) { let { corpId, disease, oldDisease } = event; try { diff --git a/corp/index.js b/corp/index.js index 6629942..8a233e3 100644 --- a/corp/index.js +++ b/corp/index.js @@ -18,6 +18,7 @@ exports.main = async (event, db, req) => { switch (event.type) { case "getCorpInfo": case "getCustomCorpInfo": + case "getCorpDiseaseList": case "addCorpDisease": case "updateCorp": case "addCorp": diff --git a/hlw/drug-info/applicable-diagnosis.js b/hlw/drug-info/applicable-diagnosis.js index 705f9f5..f6a21eb 100644 --- a/hlw/drug-info/applicable-diagnosis.js +++ b/hlw/drug-info/applicable-diagnosis.js @@ -1,12 +1,10 @@ -const { ObjectId } = require("mongodb"); - let ensureIndexesPromise = null; async function ensureApplicableDiagnosisIndexes(db) { if (!ensureIndexesPromise) { ensureIndexesPromise = Promise.all([ - db.collection("drug-info").createIndex({ applicableDiagnosisIds: 1 }), - db.collection("online-drug-info").createIndex({ applicableDiagnosisIds: 1 }), + db.collection("drug-info").createIndex({ applicableDiagnosisCodes: 1 }), + db.collection("online-drug-info").createIndex({ applicableDiagnosisCodes: 1 }), ]).catch((error) => { ensureIndexesPromise = null; throw error; @@ -15,43 +13,45 @@ async function ensureApplicableDiagnosisIndexes(db) { await ensureIndexesPromise; } -async function validateApplicableDiagnosisIds(params, db) { - if (!Object.prototype.hasOwnProperty.call(params, "applicableDiagnosisIds")) { +async function validateApplicableDiagnosisCodes(params, db) { + if (!Object.prototype.hasOwnProperty.call(params, "applicableDiagnosisCodes")) { return { present: false, value: [] }; } - const rawIds = params.applicableDiagnosisIds; - if (!Array.isArray(rawIds)) { + const rawCodes = params.applicableDiagnosisCodes; + if (!Array.isArray(rawCodes)) { throw new Error("适应诊断格式错误"); } - const uniqueIds = []; + const uniqueCodes = []; const seen = new Set(); - for (const rawId of rawIds) { - if (typeof rawId !== "string" || !ObjectId.isValid(rawId.trim())) { - throw new Error("适应诊断包含无效ID"); + for (const rawCode of rawCodes) { + if (typeof rawCode !== "string" || !rawCode.trim()) { + throw new Error("适应诊断包含无效编码"); } - const id = rawId.trim(); - if (!seen.has(id)) { - seen.add(id); - uniqueIds.push(new ObjectId(id)); + const code = rawCode.trim(); + if (!seen.has(code)) { + seen.add(code); + uniqueCodes.push(code); } } - if (uniqueIds.length) { - const diagnosisCount = await db.collection("hlw-diagnosis").countDocuments({ - _id: { $in: uniqueIds }, - }); - if (diagnosisCount !== uniqueIds.length) { + if (uniqueCodes.length) { + const diagnoses = await db.collection("hlw-diagnosis").find( + { code: { $in: uniqueCodes } }, + { projection: { _id: 0, code: 1 } } + ).toArray(); + const existingCodes = new Set(diagnoses.map(item => item.code)); + if (uniqueCodes.some(code => !existingCodes.has(code))) { throw new Error("部分适应诊断不存在,请重新选择"); } } await ensureApplicableDiagnosisIndexes(db); - return { present: true, value: uniqueIds }; + return { present: true, value: uniqueCodes }; } module.exports = { ensureApplicableDiagnosisIndexes, - validateApplicableDiagnosisIds, + validateApplicableDiagnosisCodes, }; diff --git a/hlw/drug-info/index.js b/hlw/drug-info/index.js index d9cd5bf..55d52d7 100644 --- a/hlw/drug-info/index.js +++ b/hlw/drug-info/index.js @@ -2,7 +2,7 @@ const dayjs = require("dayjs"); const { ObjectId } = require("mongodb"); const { getDrugList } = require('./verify'); -const { validateApplicableDiagnosisIds } = require('./applicable-diagnosis'); +const { validateApplicableDiagnosisCodes } = require('./applicable-diagnosis'); let db = ""; function getConfigOptionNames(list) { @@ -248,8 +248,8 @@ async function addHlwDrugInfo(ctx) { creatorId: typeof ctx.operatorId === "string" ? ctx.operatorId : "", createTime: Date.now() } - const diagnosisIds = await validateApplicableDiagnosisIds(params, db); - const { onSale, applicableDiagnosisIds, ...data } = params; + const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db); + const { onSale, applicableDiagnosisCodes, ...data } = params; let newDrug = {}; if (Object.keys(data).length) { const [item] = getDrugList([data], true, peopleInfo, 'hlw', optionLists); @@ -262,8 +262,8 @@ async function addHlwDrugInfo(ctx) { if (typeof onSale === "boolean") { newDrug.onSale = onSale; } - if (diagnosisIds.present) { - newDrug.applicableDiagnosisIds = diagnosisIds.value; + if (diagnosisCodes.present) { + newDrug.applicableDiagnosisCodes = diagnosisCodes.value; } if (!Object.prototype.hasOwnProperty.call(newDrug, "antibiotic_level")) { newDrug.antibiotic_level = 0; @@ -286,8 +286,8 @@ async function editHlwDrugInfo(ctx) { } try { const optionLists = await getHlwDrugOptionLists(ctx.corpId); - const diagnosisIds = await validateApplicableDiagnosisIds(params, db); - const { onSale, applicableDiagnosisIds, ...data } = params; + const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db); + const { onSale, applicableDiagnosisCodes, ...data } = params; const currentDrug = await db.collection("drug-info").findOne( { _id: new ObjectId(id) }, { projection: { erpId: 1, product_id: 1, product_id_str: 1 } } @@ -319,8 +319,8 @@ async function editHlwDrugInfo(ctx) { if (typeof onSale === "boolean") { updateData.onSale = onSale; } - if (diagnosisIds.present) { - updateData.applicableDiagnosisIds = diagnosisIds.value; + if (diagnosisCodes.present) { + updateData.applicableDiagnosisCodes = diagnosisCodes.value; } if (Object.keys(updateData).length) { updateData.updateTime = Date.now(); @@ -473,18 +473,21 @@ async function getDrugMatchedDisease(ctx) { if (drugIds.length === 0) { 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 ids = []; + const list = await db.collection("drug-info").find({ _id: { $in: drugIds.map(i => new ObjectId(i)) } }, { projection: { _id: 1, applicableDiagnosisCodes: 1 } }).toArray(); + const codes = []; list.forEach(i => { - if (i && Array.isArray(i.applicableDiagnosisIds)) { - ids.push(...i.applicableDiagnosisIds); + if (i && Array.isArray(i.applicableDiagnosisCodes)) { + 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: [] } } - const diseases = await db.collection("hlw-diagnosis").find({ _id: { $in: ids} }, { projection: { _id: 1, name: 1 } }).toArray(); - return { success: true, message: "查询成功", list: diseases.map(i => i.name) } + const diseases = await db.collection("hlw-diagnosis").find({ code: { $in: uniqueCodes } }, { projection: { _id: 1, code: 1, name: 1 } }).toArray(); + 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) { return { success: false, message: e.message } } diff --git a/hlw/hlw-diagnosis/index.js b/hlw/hlw-diagnosis/index.js index ccea1d4..ea3137b 100644 --- a/hlw/hlw-diagnosis/index.js +++ b/hlw/hlw-diagnosis/index.js @@ -37,6 +37,13 @@ async function getDiagnosisList(item) { } 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") { query.name = new RegExp(name.trim(), "i"); } @@ -57,7 +64,7 @@ async function getDiagnosisList(item) { const res = await db .collection("hlw-diagnosis") .find(query) - .sort({ createTime: -1 }) + .sort({ _id: -1 }) .skip((page - 1) * pageSize) .limit(pageSize) .toArray(); @@ -88,10 +95,17 @@ async function deleteHlwDiagnosis(item) { return { success: false, message: '诊断ID格式错误' } } 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); const [hlwDrugCount, onlineDrugCount] = await Promise.all([ - db.collection("drug-info").countDocuments({ applicableDiagnosisIds: diagnosisId }, { limit: 1 }), - db.collection("online-drug-info").countDocuments({ applicableDiagnosisIds: diagnosisId }, { limit: 1 }), + db.collection("drug-info").countDocuments({ applicableDiagnosisCodes: diagnosis.code }, { limit: 1 }), + db.collection("online-drug-info").countDocuments({ applicableDiagnosisCodes: diagnosis.code }, { limit: 1 }), ]); if (hlwDrugCount > 0 || onlineDrugCount > 0) { return { success: false, message: '该诊断已被药品引用,请先解除关联后再删除' } @@ -122,11 +136,17 @@ async function updateHlwDiagnosis(item) { // 调用mongo 数据库更新 const [valid, data] = verifyDisease(item); 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() const res = await db .collection("hlw-diagnosis") .updateOne( - { _id: new ObjectId(item.id) }, + { _id: diagnosisId }, { $set: data } ); if (res.matchedCount === 0) { @@ -150,6 +170,11 @@ async function addHlwDiagnosis(item) { // 调用mongo 数据库新增 const [valid, data] = verifyDisease(item); 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() const res = await db.collection("hlw-diagnosis").insertOne(data); return { diff --git a/hlw/online-drug-info/index.js b/hlw/online-drug-info/index.js index d3cb597..f29985e 100644 --- a/hlw/online-drug-info/index.js +++ b/hlw/online-drug-info/index.js @@ -5,7 +5,7 @@ const dayjs = require("dayjs"); const { ObjectId } = require("mongodb"); 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"); exports.main = async (item, db) => { @@ -170,8 +170,8 @@ async function addOnlineDrugInfo(ctx, db) { typeof ctx.operatorId === "string" ? ctx.operatorId : "", createTime: Date.now(), }; - const diagnosisIds = await validateApplicableDiagnosisIds(params, db); - const { onSale, applicableDiagnosisIds, ...data } = params; + const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db); + const { onSale, applicableDiagnosisCodes, ...data } = params; let newDrug = {}; if (Object.keys(data).length) { if (!data.his_drug_code || !String(data.his_drug_code).trim()) { @@ -193,8 +193,8 @@ async function addOnlineDrugInfo(ctx, db) { if (typeof onSale === "boolean") { newDrug.onSale = onSale; } - if (diagnosisIds.present) { - newDrug.applicableDiagnosisIds = diagnosisIds.value; + if (diagnosisCodes.present) { + newDrug.applicableDiagnosisCodes = diagnosisCodes.value; } if (!Object.prototype.hasOwnProperty.call(newDrug, "antibiotic_level")) { newDrug.antibiotic_level = 0; @@ -229,8 +229,8 @@ async function editOnlineDrugInfo(ctx, db) { return { success: false, message: "参数错误" }; } try { - const diagnosisIds = await validateApplicableDiagnosisIds(params, db); - const { onSale, applicableDiagnosisIds, ...data } = params; + const diagnosisCodes = await validateApplicableDiagnosisCodes(params, db); + const { onSale, applicableDiagnosisCodes, ...data } = params; let updateData = {}; if (Object.keys(data).length) { 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") { updateData.onSale = onSale; } - if (diagnosisIds.present) { - updateData.applicableDiagnosisIds = diagnosisIds.value; + if (diagnosisCodes.present) { + updateData.applicableDiagnosisCodes = diagnosisCodes.value; } if (Object.keys(updateData).length) { updateData.updateTime = Date.now();