29 lines
1017 B
JavaScript
29 lines
1017 B
JavaScript
const { ObjectId } = require('mongodb');
|
|
|
|
exports.getConsultWines = async function (wines, db) {
|
|
if (!Array.isArray(wines) || wines.length === 0) {
|
|
throw new Error('浸酒参数错误')
|
|
}
|
|
const wineIds = wines.map(i => ObjectId.isValid(i._id) ? new ObjectId(i._id) : null).filter(Boolean);
|
|
const wineDocs = await db.collection('chinese-medicine').find({ _id: { $in: wineIds } }).toArray();
|
|
let result = [];
|
|
for (let wine of wines) {
|
|
const doc = wineDocs.find(i => i._id.toString() === wine._id);
|
|
if (!doc) {
|
|
throw new Error(`浸酒${wine.name}不存在`)
|
|
}
|
|
if (!(wine.quantity > 0 && Number.isInteger(wine.quantity))) {
|
|
throw new Error(`浸酒${wine.name}数量错误`)
|
|
}
|
|
result.push({
|
|
id: doc._id.toString(),
|
|
cate: doc.cate || '',
|
|
name: doc.name,
|
|
quantity: wine.quantity,
|
|
efficacy: wine.efficacy || '',
|
|
unit: doc.unit || '',
|
|
isFirst: typeof wine.isFirst === 'boolean' ? wine.isFirst : false
|
|
})
|
|
}
|
|
return result;
|
|
} |