searchModel.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Created by Zhong on 2018/1/9.
  3. */
  4. const mongoose = require('mongoose');
  5. const compleRationModel = mongoose.model('complementary_ration_items');
  6. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  7. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  8. const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree');
  9. let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model;
  10. let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
  11. class SearchDao{
  12. async getRationItem(userId, rationRepId, code, callback){
  13. let ration = null;
  14. try{
  15. let stdRation = await stdRationModel.findOne({rationRepId: rationRepId, code: code, $or: [{isDeleted: null}, {isDeleted: false}]});
  16. if(isDef(stdRation)){
  17. ration = stdRation._doc;
  18. ration.type = 'std';
  19. }
  20. else{
  21. let compleRation = await compleRationModel.findOne({userId: userId, rationRepId: rationRepId, code: code, deleteInfo: null});
  22. if(isDef(compleRation)){
  23. ration = compleRation._doc;
  24. ration.type = 'complementary';
  25. }
  26. }
  27. if(isDef(ration)){
  28. let stdChapter = await stdSectionTreeModel.findOne({rationRepId: ration.rationRepId, ID: ration.sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  29. if(isDef(stdChapter)){
  30. ration.chapter = stdChapter._doc;
  31. }
  32. else{
  33. let compleChapter = await compleRationSectionTreeModel.findOne({userId: userId, ID: ration.sectionId, deleteInfo: null});
  34. if(isDef(compleChapter)){
  35. ration.chapter = compleChapter._doc;
  36. }
  37. }
  38. }
  39. if(callback){
  40. callback(0, ration);
  41. }
  42. }
  43. catch(err){
  44. if(callback){
  45. callback(err, null);
  46. }
  47. }
  48. return ration;
  49. }
  50. async findRation(userId, rationRepId, keyword, callback){
  51. try{
  52. let filter = {
  53. 'rationRepId': rationRepId,
  54. '$and': [{
  55. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  56. }, {
  57. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
  58. }]
  59. };
  60. let stdRations = await stdRationModel.find(filter);
  61. for(let i = 0, len = stdRations.length; i < len; i++){
  62. stdRations[i]._doc.type = 'std';
  63. }
  64. filter.userId = userId;
  65. let compleRations = await compleRationModel.find(filter);
  66. for(let i = 0, len = compleRations.length; i <len; i++){
  67. compleRations[i]._doc.type = 'complementary';
  68. }
  69. callback(0, stdRations.concat(compleRations));
  70. }
  71. catch(err){
  72. callback(err, null);
  73. }
  74. }
  75. }
  76. function isDef(v){
  77. return v !== undefined && v !== null;
  78. }
  79. export default SearchDao;