searchModel.js 2.9 KB

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