| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 | /** * Created by Zhong on 2018/1/9. */import {compleRationModel} from './schemas';import {complementaryGljModel, stdGljModel} from '../../complementary_glj_lib/models/schemas';import {compleRationSectionTreeModel} from './schemas';let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model;let stdRationModel = require ('../../ration_repository/models/ration_item').Model;class SearchDao{    async getRationItem(userId, rationRepId, code, callback){        let ration = null;        try{            let stdRation = await stdRationModel.findOne({rationRepId: rationRepId, code: code, $or: [{isDeleted: null}, {isDeleted: false}]});            if(isDef(stdRation)){                ration = stdRation._doc;                ration.type = 'std';            }            else{                let compleRation = await compleRationModel.findOne({userId: userId, rationRepId: rationRepId, code: code, deleteInfo: null});                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 stdRations = await stdRationModel.find(filter);            for(let i = 0, len = stdRations.length; i < len; i++){                stdRations[i]._doc.type = 'std';            }            filter.userId = userId;            let compleRations = await compleRationModel.find(filter);            for(let i = 0, len = compleRations.length; i <len; i++){                compleRations[i]._doc.type = 'complementary';            }            callback(0, stdRations.concat(compleRations));        }        catch(err){            callback(err, null);        }    }}function isDef(v){    return v !== undefined && v !== null;}export default SearchDao;
 |