Browse Source

add searchModel

zhongzewei 7 years ago
parent
commit
3f9313205b
1 changed files with 76 additions and 0 deletions
  1. 76 0
      modules/complementary_ration_lib/models/searchModel.js

+ 76 - 0
modules/complementary_ration_lib/models/searchModel.js

@@ -0,0 +1,76 @@
+/**
+ * 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){
+        try{
+            let ration = null;
+            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;
+                    }
+                }
+            }
+            callback(0, ration);
+        }
+        catch(err){
+            callback(err, null);
+        }
+    }
+
+    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';
+            }
+            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;