const dayjs = require("dayjs"); const validator = require('../../utils/validator.js'); const logger = require('../../utils/logger'); let db = ""; module.exports = async (item, mongodb) => { db = mongodb; switch (item.type) { case "addLackMedicineReg": return await addLackMedicineReg(item); case "getLackMedicineList": return await getLackMedicineList(item); case "batchAddLackMedicineReg": return await batchAddLackMedicineReg(item); } }; async function addLackMedicineReg(ctx) { try { const inventory = ['online', 'store'].includes(ctx.inventory) ? ctx.inventory : 'online'; const accountId = typeof ctx.accountId === 'string' ? ctx.accountId.trim() : ''; const drugName = typeof ctx.drugName === 'string' ? ctx.drugName.trim() : ''; const spec = typeof ctx.spec === 'string' ? ctx.spec.trim() : ''; const manufacturer = typeof ctx.manufacturer === 'string' ? ctx.manufacturer.trim() : ''; const insuranceCode = typeof ctx.insuranceCode === 'string' ? ctx.insuranceCode.trim() : ''; const images = Array.isArray(ctx.images) ? ctx.images.filter(i => typeof i === 'string' && i.trim() !== '') : []; const description = typeof ctx.description === 'string' ? ctx.description.trim() : ''; const patientName = typeof ctx.patientName === 'string' ? ctx.patientName.trim() : ''; const phone = typeof ctx.phone === 'string' ? ctx.phone.trim() : ''; const patientId = typeof ctx.patientId === 'string' ? ctx.patientId.trim() : ''; if (patientId.length === 0) { return { success: false, message: "患者id不能为空" } } if (patientId.length > 50) { return { success: false, message: "患者id不能超过50个字" } } if (accountId.length === 0) { return { success: false, message: "账号id不能为空" } } if (accountId.length > 50) { return { success: false, message: "账号id不能超过50个字" } } if (drugName.length > 100) { return { success: false, message: "药品名称不能超过100个字" } } if (spec.length > 100) { return { success: false, message: "规格不能超过100个字" } } if (manufacturer.length > 200) { return { success: false, message: "生产厂家不能超过200个字" } } if (insuranceCode.length > 100) { return { success: false, message: "医保编码不能超过100个字" } } if (images.length > 5) { return { success: false, message: "图片不能超过5张" } } if (description.length > 500) { return { success: false, message: "描述不能超过500个字" } } if (patientName.length === 0) { return { success: false, message: "患者姓名不能为空" } } if (patientName.length > 30) { return { success: false, message: "患者姓名不能超过30个字" } } if (phone.length === 0) { return { success: false, message: "患者手机号不能为空" } } if (!validator.isMobile(phone)) { return { success: false, message: "患者手机号格式不正确" } } const data = { accountId, corpId: typeof ctx.corpId === 'string' ? ctx.corpId.trim() : undefined, createTime: Date.now(), description, drugName, images, insuranceCode, inventory, manufacturer, patientName, phone, patientId, regType: 'report', spec } const res = await db.collection("lack-medicine-reg").insertOne(data); return { success: true, message: "新增成功", id: res.insertedId, }; } catch (e) { return { success: false, message: e.message } } } async function batchAddLackMedicineReg(ctx) { try { const regType = ['outOfStock', 'lowStock'].includes(ctx.regType) ? ctx.regType : ''; const inventory = ['online', 'store'].includes(ctx.inventory) ? ctx.inventory : 'online'; const accountId = typeof ctx.accountId === 'string' ? ctx.accountId.trim() : ''; const patientId = typeof ctx.patientId === 'string' ? ctx.patientId.trim() : ''; const description = typeof ctx.description === 'string' ? ctx.description.trim() : ''; const patientName = typeof ctx.patientName === 'string' ? ctx.patientName.trim() : ''; const phone = typeof ctx.phone === 'string' ? ctx.phone.trim() : ''; const drugs = Array.isArray(ctx.drugs) ? ctx.drugs.filter(Boolean) : []; if (regType === '') { return { success: false, message: "登记类型不能为空" } } if (accountId.length === 0) { return { success: false, message: "账号id不能为空" } } if (accountId.length > 50) { return { success: false, message: "账号id不能超过50个字" } } if (patientId.length > 50) { return { success: false, message: "患者id不能超过50个字" } } if (description.length > 500) { return { success: false, message: "描述不能超过500个字" } } if (patientName.length === 0) { return { success: false, message: "患者姓名不能为空" } } if (patientName.length > 30) { return { success: false, message: "患者姓名不能超过30个字" } } // 手机号验证:如果提供了手机号则必须格式正确 if (phone.length > 0 && !validator.isMobile(phone)) { return { success: false, message: "患者手机号格式不正确" } } // 注意:批量自动登记允许手机号为空,因为从购药记录导入时可能没有手机号 if (phone.length === 0) { logger.warn(`[缺货登记警告] 批量登记缺少患者手机号 - patientName: ${patientName}, patientId: ${patientId}`); } const validDrugs = drugs.map(i => { const drugName = typeof i.drugName === 'string' ? i.drugName.trim() : ''; const spec = typeof i.spec === 'string' ? i.spec.trim() : ''; const manufacturer = typeof i.manufacturer === 'string' ? i.manufacturer.trim() : ''; const insuranceCode = typeof i.insurance_code === 'string' ? i.insurance_code.trim() : ''; return { drugName: drugName.slice(0, 100), spec: spec.slice(0, 100), manufacturer: manufacturer.slice(0, 200), insuranceCode: insuranceCode.slice(0, 100) } }) const data = { accountId, patientId, corpId: typeof ctx.corpId === 'string' ? ctx.corpId.trim() : undefined, createTime: Date.now(), description, images: [], inventory, patientName, phone, regType } let res if (validDrugs.length) { res = await db.collection("lack-medicine-reg").insertMany(validDrugs.map(i => ({ ...i, ...data }))); } return { success: true, res, message: "新增成功", }; } catch (e) { return { success: false, message: e.message } } } async function getLackMedicineList(ctx) { try { const page = Number.isInteger(ctx.page) && ctx.page > 0 ? ctx.page : 1; const pageSize = Number.isInteger(ctx.pageSize) && ctx.pageSize > 0 ? ctx.pageSize : 10; const query = {}; if (typeof ctx.corpId === 'string' && ctx.corpId.trim()) { query.corpId = ctx.corpId.trim(); } if(Array.isArray(ctx.regTypes) && ctx.regTypes.length > 0) { query.regType = { $in: ctx.regTypes } } // 缺货药品库筛选:online=配送药品库,store=药店药品库 if (typeof ctx.inventory === 'string') { const inventory = ctx.inventory.trim(); if (['online', 'store'].includes(inventory)) { query.inventory = inventory; } } if (typeof ctx.drugName === 'string' && ctx.drugName.trim()) { query.drugName = new RegExp(ctx.drugName.trim(), 'i'); } if (ctx.startTime && dayjs(ctx.startTime).isValid()) { query.createTime = { $gte: dayjs(ctx.startTime).startOf('day').valueOf() }; } if (ctx.endTime && dayjs(ctx.endTime).isValid()) { query.createTime = query.createTime || {}; query.createTime['$lte'] = dayjs(ctx.endTime).endOf('day').valueOf(); } const list = await db.collection("lack-medicine-reg").find(query, { projection: { _id: 1, drugName: 1, spec: 1, manufacturer: 1, insuranceCode: 1, images: 1, description: 1, patientName: 1, phone: 1, regType: 1, createTime: 1, inventory: 1, // 将账号 id 和患者 id 一并返回,便于前端通过 patientId 或 accountId 做患者关联 accountId: 1, patientId: 1, } }).sort({ createTime: -1 }).skip((page - 1) * pageSize).limit(pageSize).toArray(); const total = await db.collection("lack-medicine-reg").countDocuments(query); return { success: true, message: "查询成功", data: list, total: total, page: page, pages: Math.ceil(total / pageSize), pageSize: pageSize, }; } catch (e) { return { success: false, message: e.message } } }