59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
|
|
const { getDatabase } = require("../../mongodb");
|
|||
|
|
const { getDrugList } = require("../drug-info/verify");
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* online-drug-info 批量导入工具
|
|||
|
|
* 逻辑与 hlw/drug-info/batch-upload 基本一致,仅集合名称不同
|
|||
|
|
*/
|
|||
|
|
async function getBatchInsertOnlineDrugFn() {
|
|||
|
|
const db = await getDatabase("Internet-hospital");
|
|||
|
|
if (!db) {
|
|||
|
|
throw new Error("数据库连接失败");
|
|||
|
|
}
|
|||
|
|
const drugCollection = db.collection("online-drug-info");
|
|||
|
|
const batch = [];
|
|||
|
|
let count = 0;
|
|||
|
|
const pendingList = [];
|
|||
|
|
|
|||
|
|
async function getCount() {
|
|||
|
|
await Promise.all(pendingList);
|
|||
|
|
return count;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function batchInsertDrug(row, extraInfo) {
|
|||
|
|
const rowData = {};
|
|||
|
|
for (const key of Object.keys(row)) {
|
|||
|
|
rowData[key.trim()] = row[key];
|
|||
|
|
}
|
|||
|
|
if (!rowData.his_drug_code || !String(rowData.his_drug_code).trim()) return;
|
|||
|
|
const [drugData] = getDrugList([rowData], true, extraInfo);
|
|||
|
|
const { msgs, ...drug } = drugData;
|
|||
|
|
if (msgs.length === 0) {
|
|||
|
|
batch.push(drug);
|
|||
|
|
}
|
|||
|
|
if (batch.length === 1000) {
|
|||
|
|
const insert = drugCollection.insertMany(batch.splice(0, 1000));
|
|||
|
|
pendingList.push(insert);
|
|||
|
|
const res = await insert;
|
|||
|
|
count += res.insertedCount;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function flushBatch() {
|
|||
|
|
if (batch.length > 0) {
|
|||
|
|
const insert = drugCollection.insertMany(batch.splice(0, 1000));
|
|||
|
|
pendingList.push(insert);
|
|||
|
|
const res = await insert;
|
|||
|
|
count += res.insertedCount;
|
|||
|
|
batch.length = 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return { batchInsertDrug, getCount, flushBatch };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
module.exports = {
|
|||
|
|
getBatchInsertOnlineDrugFn,
|
|||
|
|
};
|
|||
|
|
|