searchModel.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. let ration = null;
  12. try{
  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. if(callback){
  38. callback(0, ration);
  39. }
  40. }
  41. catch(err){
  42. if(callback){
  43. callback(err, null);
  44. }
  45. }
  46. return ration;
  47. }
  48. async findRation(userId, rationRepId, keyword, callback){
  49. try{
  50. let filter = {
  51. 'rationRepId': rationRepId,
  52. '$and': [{
  53. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  54. }, {
  55. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
  56. }]
  57. };
  58. let stdRations = await stdRationModel.find(filter);
  59. for(let i = 0, len = stdRations.length; i < len; i++){
  60. stdRations[i]._doc.type = 'std';
  61. }
  62. filter.userId = userId;
  63. let compleRations = await compleRationModel.find(filter);
  64. for(let i = 0, len = compleRations.length; i <len; i++){
  65. compleRations[i]._doc.type = 'complementary';
  66. }
  67. callback(0, stdRations.concat(compleRations));
  68. }
  69. catch(err){
  70. callback(err, null);
  71. }
  72. }
  73. }
  74. function isDef(v){
  75. return v !== undefined && v !== null;
  76. }
  77. export default SearchDao;