/** * Created by Zhong on 2018/1/9. */ const mongoose = require('mongoose'); const compleRationModel = mongoose.model('complementary_ration_items'); const complementaryGljModel = mongoose.model('complementary_glj_lib'); const stdGljModel = mongoose.model('std_glj_lib_gljList'); const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree'); let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model; let stdRationModel = require ('../../ration_repository/models/ration_item').Model; class SearchDao{ async getRationItem(userId, rationRepIds, code, ID, callback){ let ration = null; try{ let stdQuery = {rationRepId: {$in: rationRepIds}, code: code, $or: [{isDeleted: null}, {isDeleted: false}]}; if(ID){ stdQuery = {ID: ID, $or: [{isDeleted: null}, {isDeleted: false}]}; } //let stdRation = await stdRationModel.findOne({rationRepId: {$in: rationRepIds}, code: code, $or: [{isDeleted: null}, {isDeleted: false}]}); let stdRation = await stdRationModel.findOne(stdQuery); if(isDef(stdRation)){ ration = stdRation._doc; ration.type = 'std'; } else{ let compleQuery = {userId: userId, rationRepId: {$in: rationRepIds}, code: code, deleteInfo: null}; if(ID){ compleQuery = {ID: ID, deleteInfo: null}; } //let compleRation = await compleRationModel.findOne({userId: userId, rationRepId: {$in: rationRepIds}, code: code, deleteInfo: null}); let compleRation = await compleRationModel.findOne(compleQuery); if(isDef(compleRation)){ ration = compleRation._doc; ration.type = 'complementary'; } } if(isDef(ration)){ let stdChapter = await stdSectionTreeModel.findOne({rationRepId: ration.rationRepId, ID: ration.sectionId, $or: [{isDeleted: null}, {isDeleted: false}]}); if(isDef(stdChapter)){ ration.chapter = stdChapter._doc; } else{ let compleChapter = await compleRationSectionTreeModel.findOne({userId: userId, ID: ration.sectionId, deleteInfo: null}); if(isDef(compleChapter)){ ration.chapter = compleChapter._doc; } } } if(callback){ callback(0, ration); } } catch(err){ if(callback){ callback(err, null); } } return ration; } async findRation(userId, rationRepId, keyword, callback){ try{ let filter = { 'rationRepId': rationRepId, '$and': [{ '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}] }, { '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}] }] }; let stdGljIds = [], comGljIds = []; let stdRations = await stdRationModel.find(filter); for(let i = 0, len = stdRations.length; i < len; i++){ stdRations[i]._doc.type = 'std'; for(let glj of stdRations[i].rationGljList){ stdGljIds.push(glj.gljId); } } filter.userId = userId; let compleRations = await compleRationModel.find(filter); for(let i = 0, len = compleRations.length; i 0){ let stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}, '-_id ID code name specs unit'); for(let stdGlj of stdGljs){ gljIDMapping[stdGlj.ID] = stdGlj; } } if(comGljIds.length > 0){ let comGljs = await complementaryGljModel.find({ID: {$in: stdGljIds}}); for(let comGlj of comGljs){ gljIDMapping[comGlj.ID] = comGlj; } } for(let ration of stdRations){ let hintsArr = []; for(let rationGlj of ration.rationGljList){ let glj = gljIDMapping[rationGlj.gljId]; if(glj){ hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`); } } hintsArr.push(`基价 元 ${ration.basePrice}`); if(ration.jobContent && ration.jobContent.toString().trim() !== ''){ hintsArr.push(`工作内容:`); hintsArr = hintsArr.concat(ration.jobContent.split('\n')); } if(ration.annotation && ration.annotation.toString().trim() !== ''){ hintsArr.push(`附注:`); hintsArr = hintsArr.concat(ration.annotation.split('\n')); } ration._doc.hint = hintsArr.join('
'); } for(let ration of compleRations){ let hintsArr = []; for(let rationGlj of ration.rationGljList){ let glj = gljIDMapping[rationGlj.gljId]; if(glj){ hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`); } } hintsArr.push(`基价 元 ${ration.basePrice}`); if(ration.jobContent && ration.jobContent.toString().trim() !== ''){ hintsArr.push(`工作内容:`); hintsArr = hintsArr.concat(ration.jobContent.split('\n')); } if(ration.annotation && ration.annotation.toString().trim() !== ''){ hintsArr.push(`附注:`); hintsArr = hintsArr.concat(ration.annotation.split('\n')); } ration._doc.hint = hintsArr.join('
'); } callback(0, stdRations.concat(compleRations)); } catch(err){ callback(err, null); } } } function isDef(v){ return v !== undefined && v !== null; } export default SearchDao;