1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /**
- * 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, 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;
|