/** * Created by Zhong on 2018/1/24. */ import {rationItemModel, installFeeItemModel, installSectionModel} from '../models/schemas'; class InstallationDao{ async getInstallation(rationRepId, callback){ try { let feeItems = await installFeeItemModel.find({rationRepId: rationRepId, $or: [{deleted: false}, {deleted: null}]}); 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}, $or: [{deleted: false}, {deleted: null}]}); 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'){ await installSectionModel.update({ID: data.updateData.ID}, data.updateData); } } 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'){ await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData); } } 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;