searchModel.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 stdGljIds = [],
  71. comGljIds = [];
  72. let stdRations = await stdRationModel.find(filter);
  73. for(let i = 0, len = stdRations.length; i < len; i++){
  74. stdRations[i]._doc.type = 'std';
  75. for(let glj of stdRations[i].rationGljList){
  76. stdGljIds.push(glj.gljId);
  77. }
  78. }
  79. filter.userId = userId;
  80. let compleRations = await compleRationModel.find(filter);
  81. for(let i = 0, len = compleRations.length; i <len; i++){
  82. compleRations[i]._doc.type = 'complementary';
  83. for(let glj of stdRations[i].rationGljList){
  84. if(glj.type === 'std'){
  85. stdGljIds.push(glj.gljId);
  86. }
  87. else {
  88. comGljIds.push(glj.gljId);
  89. }
  90. }
  91. }
  92. //设置悬浮信息
  93. stdGljIds = Array.from(new Set(stdGljIds));
  94. comGljIds = Array.from(new Set(comGljIds));
  95. let gljIDMapping = {};
  96. if(stdGljIds.length > 0){
  97. let stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}, '-_id ID code name specs unit');
  98. for(let stdGlj of stdGljs){
  99. gljIDMapping[stdGlj.ID] = stdGlj;
  100. }
  101. }
  102. if(comGljIds.length > 0){
  103. let comGljs = await complementaryGljModel.find({ID: {$in: stdGljIds}});
  104. for(let comGlj of comGljs){
  105. gljIDMapping[comGlj.ID] = comGlj;
  106. }
  107. }
  108. for(let ration of stdRations){
  109. let hintsArr = [];
  110. for(let rationGlj of ration.rationGljList){
  111. let glj = gljIDMapping[rationGlj.gljId];
  112. if(glj){
  113. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  114. }
  115. }
  116. hintsArr.push(`基价 元 ${ration.basePrice}`);
  117. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  118. hintsArr.push(`工作内容:`);
  119. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  120. }
  121. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  122. hintsArr.push(`附注:`);
  123. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  124. }
  125. ration._doc.hint = hintsArr.join('<br>');
  126. }
  127. for(let ration of compleRations){
  128. let hintsArr = [];
  129. for(let rationGlj of ration.rationGljList){
  130. let glj = gljIDMapping[rationGlj.gljId];
  131. if(glj){
  132. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  133. }
  134. }
  135. hintsArr.push(`基价 元 ${ration.basePrice}`);
  136. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  137. hintsArr.push(`工作内容:`);
  138. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  139. }
  140. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  141. hintsArr.push(`附注:`);
  142. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  143. }
  144. ration._doc.hint = hintsArr.join('<br>');
  145. }
  146. callback(0, stdRations.concat(compleRations));
  147. }
  148. catch(err){
  149. callback(err, null);
  150. }
  151. }
  152. }
  153. function isDef(v){
  154. return v !== undefined && v !== null;
  155. }
  156. export default SearchDao;