diff --git a/corp/corp-account/account-utils.js b/corp/corp-account/account-utils.js index ef3848f..1f98a4b 100644 --- a/corp/corp-account/account-utils.js +++ b/corp/corp-account/account-utils.js @@ -17,15 +17,14 @@ const rules = [ { key: 'phone', validate: val => { - if (typeof val === 'number') val = String(val); - if (typeof val !== 'string' || val.trim() === '') { - return [false, '手机号不能为空']; + const valStr = ['number', 'string'].includes(typeof val) ? String(val).trim() : null; + if (valStr) { + if (!validator.isMobile(valStr)) { + return [false, '手机号格式不正确']; + } + return [true, valStr]; } - const s = val.trim(); - if (!validator.isMobile(s)) { - return [false, '手机号格式不正确']; - } - return [true, s]; + return [true, ''] } }, { diff --git a/corp/corp-account/index.js b/corp/corp-account/index.js index 2f5de78..6009968 100644 --- a/corp/corp-account/index.js +++ b/corp/corp-account/index.js @@ -482,28 +482,26 @@ async function addCorpAccount(ctx) { if (/\W/.test(accountData.mobile)) { return { success: false, message: "登录账号不能包含特殊字符" }; } - - // 校验 phone 字段是否存在 - if (!accountData.phone || typeof accountData.phone !== 'string' || accountData.phone.trim() === '') { - return { success: false, message: "手机号不能为空" }; + const orList = [ + { corpId: data.corpId, mobile: data.mobile } // 账号相同 + ]; + if (accountData.phone) { + orList.push({ corpId: data.corpId, phone: data.phone }); // 手机号相同 + } + if (relatedHlwPeople) { + orList.push({ corpId: data.corpId, relatedHlwPeople }); // 关联人员相同 } - - const sameMobile = { corpId: data.corpId, mobile: data.mobile }; // 账号相同 - const samePhone = { corpId: data.corpId, phone: data.phone }; // 手机号相同 - const sameRelatedPeople = relatedHlwPeople - ? [{ corpId: data.corpId, relatedHlwPeople }] - : []; // 关联人员相同 const account = await db .collection("corp-member") .findOne( - { $or: [sameMobile, samePhone, ...sameRelatedPeople] }, + { $or: orList }, { projection: { mobile: 1, phone: 1, relatedHlwPeople: 1 } } ); if (account && account.mobile === data.mobile) { return { success: false, message: "该账号已存在" }; - } else if (account && account.phone === data.phone) { + } else if (account && account.phone && account.phone === data.phone) { return { success: false, message: "该手机号已被使用" }; - } else if (account) { + } else if (account && account.relatedHlwPeople && account.relatedHlwPeople === relatedHlwPeople) { return { success: false, message: "该人员已关联其他账号" }; } if ( @@ -582,28 +580,22 @@ async function editCorpAccount(ctx) { ) { return { success: false, message: "登录账号不能修改" }; } - - const sameMobile = { corpId: data.corpId, mobile: data.mobile }; // 账号相同 - const samePhone = accountData.phone ? { corpId: data.corpId, phone: accountData.phone } : null; // 手机号相同 - const sameRelatedPeople = - typeof relatedHlwPeople == "string" && relatedHlwPeople - ? [{ corpId: data.corpId, relatedHlwPeople }] - : []; // 关联人员相同 - - const queryConditions = [sameMobile, ...sameRelatedPeople]; - if (samePhone) { - queryConditions.push(samePhone); + const orList = [{ corpId: data.corpId, mobile: data.mobile, _id: { $ne: record._id } }]; + if (accountData.phone) { + orList.push({ corpId: data.corpId, phone: accountData.phone, _id: { $ne: record._id } }); // 手机号相同 + } + if (relatedHlwPeople) { + orList.push({ corpId: data.corpId, relatedHlwPeople, _id: { $ne: record._id } }); // 关联人员相同 } - const account = await db.collection("corp-member").findOne( - { $or: queryConditions, _id: { $ne: record._id } }, // 排除当前账号 + { $or: orList }, { projection: { mobile: 1, phone: 1, doctorNo: 1, workNo: 1 } } ); if (account && account.mobile === data.mobile) { return { success: false, message: "该账号已存在" }; } else if (account && accountData.phone && account.phone === accountData.phone) { return { success: false, message: "该手机号已被使用" }; - } else if (account) { + } else if (account && account.relatedHlwPeople && account.relatedHlwPeople === relatedHlwPeople) { return { success: false, message: "该人员已关联其他账号" }; } if ( diff --git a/hlw/drug-info/applicable-diagnosis.js b/hlw/drug-info/applicable-diagnosis.js new file mode 100644 index 0000000..705f9f5 --- /dev/null +++ b/hlw/drug-info/applicable-diagnosis.js @@ -0,0 +1,57 @@ +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 }), + ]).catch((error) => { + ensureIndexesPromise = null; + throw error; + }); + } + await ensureIndexesPromise; +} + +async function validateApplicableDiagnosisIds(params, db) { + if (!Object.prototype.hasOwnProperty.call(params, "applicableDiagnosisIds")) { + return { present: false, value: [] }; + } + + const rawIds = params.applicableDiagnosisIds; + if (!Array.isArray(rawIds)) { + throw new Error("适应诊断格式错误"); + } + + const uniqueIds = []; + const seen = new Set(); + for (const rawId of rawIds) { + if (typeof rawId !== "string" || !ObjectId.isValid(rawId.trim())) { + throw new Error("适应诊断包含无效ID"); + } + const id = rawId.trim(); + if (!seen.has(id)) { + seen.add(id); + uniqueIds.push(new ObjectId(id)); + } + } + + if (uniqueIds.length) { + const diagnosisCount = await db.collection("hlw-diagnosis").countDocuments({ + _id: { $in: uniqueIds }, + }); + if (diagnosisCount !== uniqueIds.length) { + throw new Error("部分适应诊断不存在,请重新选择"); + } + } + + await ensureApplicableDiagnosisIndexes(db); + return { present: true, value: uniqueIds }; +} + +module.exports = { + ensureApplicableDiagnosisIndexes, + validateApplicableDiagnosisIds, +}; diff --git a/hlw/drug-info/index.js b/hlw/drug-info/index.js index b784440..787e789 100644 --- a/hlw/drug-info/index.js +++ b/hlw/drug-info/index.js @@ -2,6 +2,7 @@ const dayjs = require("dayjs"); const { ObjectId } = require("mongodb"); const { getDrugList } = require('./verify'); +const { validateApplicableDiagnosisIds } = require('./applicable-diagnosis'); let db = ""; module.exports = async (item, mongodb) => { @@ -219,7 +220,8 @@ async function addHlwDrugInfo(ctx) { creatorId: typeof ctx.operatorId === "string" ? ctx.operatorId : "", createTime: Date.now() } - const { onSale, ...data } = params; + const diagnosisIds = await validateApplicableDiagnosisIds(params, db); + const { onSale, applicableDiagnosisIds, ...data } = params; let newDrug = {}; if (Object.keys(data).length) { const [item] = getDrugList([data], true, peopleInfo); @@ -232,6 +234,9 @@ async function addHlwDrugInfo(ctx) { if (typeof onSale === "boolean") { newDrug.onSale = onSale; } + if (diagnosisIds.present) { + newDrug.applicableDiagnosisIds = diagnosisIds.value; + } newDrug.createTime = Date.now(); const res = await db.collection("drug-info").insertOne(newDrug); return { success: true, message: "新增药品成功", id: res.insertedId } @@ -246,7 +251,8 @@ async function editHlwDrugInfo(ctx) { return { success: false, message: "参数错误" } } try { - const { onSale, ...data } = params; + const diagnosisIds = await validateApplicableDiagnosisIds(params, db); + const { onSale, applicableDiagnosisIds, ...data } = params; const currentDrug = await db.collection("drug-info").findOne( { _id: new ObjectId(id) }, { projection: { erpId: 1, product_id: 1, product_id_str: 1 } } @@ -278,6 +284,9 @@ async function editHlwDrugInfo(ctx) { if (typeof onSale === "boolean") { updateData.onSale = onSale; } + if (diagnosisIds.present) { + updateData.applicableDiagnosisIds = diagnosisIds.value; + } if (Object.keys(updateData).length) { updateData.updateTime = Date.now(); const res = await db.collection("drug-info").updateOne({ _id: new ObjectId(id) }, { $set: updateData }); diff --git a/hlw/hlw-diagnosis/index.js b/hlw/hlw-diagnosis/index.js index f910199..ccea1d4 100644 --- a/hlw/hlw-diagnosis/index.js +++ b/hlw/hlw-diagnosis/index.js @@ -1,6 +1,7 @@ let db = ""; const { ObjectId } = require("mongodb"); const validator = require('../../utils/validator.js') +const { ensureApplicableDiagnosisIndexes } = require('../drug-info/applicable-diagnosis'); module.exports = async (item, mongodb) => { db = mongodb; switch (item.type) { @@ -20,10 +21,22 @@ module.exports = async (item, mongodb) => { // 获取诊断列表 async function getDiagnosisList(item) { - const { page, pageSize, name, code, keyword, category } = item; + const { name, code, keyword, category } = item; try { + const page = Number.isInteger(item.page) && item.page > 0 ? item.page : 1; + const pageSize = Number.isInteger(item.pageSize) && item.pageSize > 0 ? item.pageSize : 30; // 调用mongo 数据库查询 let query = {}; + if (Array.isArray(item.ids)) { + if (item.ids.some(id => typeof id !== 'string')) { + return { success: false, message: '诊断ID格式错误' }; + } + const ids = [...new Set(item.ids.map(id => id.trim()))]; + if (ids.some(id => !ObjectId.isValid(id))) { + return { success: false, message: '诊断ID格式错误' }; + } + query._id = { $in: ids.map(id => new ObjectId(id)) }; + } if (name && typeof name === "string") { query.name = new RegExp(name.trim(), "i"); } @@ -71,9 +84,21 @@ async function deleteHlwDiagnosis(item) { return { success: false, message: 'id不能为空' } } try { + if (!ObjectId.isValid(id.trim())) { + return { success: false, message: '诊断ID格式错误' } + } + const diagnosisId = new ObjectId(id.trim()); + 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 }), + ]); + if (hlwDrugCount > 0 || onlineDrugCount > 0) { + return { success: false, message: '该诊断已被药品引用,请先解除关联后再删除' } + } // 调用mongo 数据库删除 const res = await db.collection("hlw-diagnosis").deleteOne({ - _id: new ObjectId(id), + _id: diagnosisId, }); return { success: true, diff --git a/hlw/online-drug-info/index.js b/hlw/online-drug-info/index.js index c29e73b..2cea081 100644 --- a/hlw/online-drug-info/index.js +++ b/hlw/online-drug-info/index.js @@ -5,6 +5,7 @@ const dayjs = require("dayjs"); const { ObjectId } = require("mongodb"); const { getDrugList } = require("../drug-info/verify"); +const { validateApplicableDiagnosisIds } = require("../drug-info/applicable-diagnosis"); const validator = require("../../utils/validator"); exports.main = async (item, db) => { @@ -169,7 +170,8 @@ async function addOnlineDrugInfo(ctx, db) { typeof ctx.operatorId === "string" ? ctx.operatorId : "", createTime: Date.now(), }; - const { onSale, ...data } = params; + const diagnosisIds = await validateApplicableDiagnosisIds(params, db); + const { onSale, applicableDiagnosisIds, ...data } = params; let newDrug = {}; if (Object.keys(data).length) { if (!data.his_drug_code || !String(data.his_drug_code).trim()) { @@ -191,6 +193,9 @@ async function addOnlineDrugInfo(ctx, db) { if (typeof onSale === "boolean") { newDrug.onSale = onSale; } + if (diagnosisIds.present) { + newDrug.applicableDiagnosisIds = diagnosisIds.value; + } newDrug.createTime = Date.now(); const res = await db .collection("online-drug-info") @@ -218,7 +223,8 @@ async function editOnlineDrugInfo(ctx, db) { return { success: false, message: "参数错误" }; } try { - const { onSale, ...data } = params; + const diagnosisIds = await validateApplicableDiagnosisIds(params, db); + const { onSale, applicableDiagnosisIds, ...data } = params; let updateData = {}; if (Object.keys(data).length) { if ("his_drug_code" in data && !String(data.his_drug_code).trim()) { @@ -246,6 +252,9 @@ async function editOnlineDrugInfo(ctx, db) { if (typeof onSale === "boolean") { updateData.onSale = onSale; } + if (diagnosisIds.present) { + updateData.applicableDiagnosisIds = diagnosisIds.value; + } if (Object.keys(updateData).length) { updateData.updateTime = Date.now(); const res = await db