123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /**
- * 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 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;
|