/** * Created by Zhong on 2018/1/24. */ const mongoose = require('mongoose'); const rationItemModel = mongoose.model('std_ration_lib_ration_items'); const installFeeItemModel = mongoose.model('std_ration_lib_installation'); const installSectionModel = mongoose.model('std_ration_lib_installationSection'); class InstallationDao{ async getInstallation(rationRepId, callback){ try { const feeItems = await installFeeItemModel.find({ rationRepId }).lean(); const feeItemMap = {}; const sectionIds = []; feeItems.forEach(item => { feeItemMap[item.ID] = item; item.section.forEach(s => sectionIds.push(s.ID)); item.section = []; }); const sections = await installSectionModel.find({ID: {$in: sectionIds}}); sections.forEach(section => { const matchFeeItem = feeItemMap[section.feeItemId]; if (matchFeeItem) { matchFeeItem.section.push(section); } }); if (!callback) { return feeItems; } callback(0, feeItems); } catch(err){ if (!callback) { return []; } callback(err, null); } } /*async getInstallation(rationRepId, callback){ try { let feeItems = await installFeeItemModel.find({rationRepId: rationRepId}); for(let feeItem of feeItems){ let sids = []; for(let sec of feeItem.section){ sids.push(sec.ID); } if(sids.length > 0){ let sections = await installSectionModel.find({ID: {$in: sids}}); feeItem._doc.section = sections; } } callback(0, feeItems); } catch(err){ callback(err, null); } }*/ async updateSection(updateData, callback){ try{ for(let data of updateData){ if(data.updateType === 'new'){ await installSectionModel.create(data.updateData); } else if(data.updateType === 'update' && !data.updateData.deleted){ await installSectionModel.update({ID: data.updateData.ID}, data.updateData); } else { await installSectionModel.remove({ID: data.updateData.ID}); } } callback(0, null); } catch(err){ callback(err, null); } } async updateFeeItem(updateData, callback){ try{ for(let data of updateData){ if(data.updateType === 'new'){ await installFeeItemModel.create(data.updateData); } else if(data.updateType === 'update' && !data.updateData.deleted){ await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData); } else{ await installFeeItemModel.remove({ID: data.updateData.ID}); } } callback(0, null); } catch(err){ callback(err, null); } } async batchUpdateInst(rationSection, inst, callback){ try{ for(let sectionId of rationSection){ await rationItemModel.update({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]}, {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true}); } callback(0, null); } catch(err){ callback(err, null); } } } export default InstallationDao;