searchModel.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, rationRepIds, code, ID, callback){
  13. let ration = null;
  14. try{
  15. let stdQuery = {rationRepId: {$in: rationRepIds}, code: code, $or: [{isDeleted: null}, {isDeleted: false}]};
  16. if(ID){
  17. stdQuery = {ID: ID, $or: [{isDeleted: null}, {isDeleted: false}]};
  18. }
  19. //let stdRation = await stdRationModel.findOne({rationRepId: {$in: rationRepIds}, code: code, $or: [{isDeleted: null}, {isDeleted: false}]});
  20. let stdRation = await stdRationModel.findOne(stdQuery);
  21. if(isDef(stdRation)){
  22. ration = stdRation._doc;
  23. ration.type = 'std';
  24. }
  25. else{
  26. let compleQuery = {userId: userId, rationRepId: {$in: rationRepIds}, code: code, deleteInfo: null};
  27. if(ID){
  28. compleQuery = {ID: ID, deleteInfo: null};
  29. }
  30. //let compleRation = await compleRationModel.findOne({userId: userId, rationRepId: {$in: rationRepIds}, code: code, deleteInfo: null});
  31. let compleRation = await compleRationModel.findOne(compleQuery);
  32. if(isDef(compleRation)){
  33. ration = compleRation._doc;
  34. ration.type = 'complementary';
  35. }
  36. }
  37. if(isDef(ration)){
  38. let stdChapter = await stdSectionTreeModel.findOne({rationRepId: ration.rationRepId, ID: ration.sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  39. if(isDef(stdChapter)){
  40. ration.chapter = stdChapter._doc;
  41. }
  42. else{
  43. let compleChapter = await compleRationSectionTreeModel.findOne({userId: userId, ID: ration.sectionId, deleteInfo: null});
  44. if(isDef(compleChapter)){
  45. ration.chapter = compleChapter._doc;
  46. }
  47. }
  48. }
  49. if(callback){
  50. callback(0, ration);
  51. }
  52. }
  53. catch(err){
  54. if(callback){
  55. callback(err, null);
  56. }
  57. }
  58. return ration;
  59. }
  60. async findRation(userId, rationRepId, keyword, callback){
  61. try{
  62. let filter = {
  63. 'rationRepId': rationRepId,
  64. '$and': [{
  65. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  66. }, {
  67. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
  68. }]
  69. };
  70. let stdRations = await stdRationModel.find(filter);
  71. for(let i = 0, len = stdRations.length; i < len; i++){
  72. stdRations[i]._doc.type = 'std';
  73. }
  74. filter.userId = userId;
  75. let compleRations = await compleRationModel.find(filter);
  76. for(let i = 0, len = compleRations.length; i <len; i++){
  77. compleRations[i]._doc.type = 'complementary';
  78. }
  79. callback(0, stdRations.concat(compleRations));
  80. }
  81. catch(err){
  82. callback(err, null);
  83. }
  84. }
  85. }
  86. function isDef(v){
  87. return v !== undefined && v !== null;
  88. }
  89. export default SearchDao;