searchModel.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. } else{
  25. let compleQuery = {userId: userId, rationRepId: {$in: rationRepIds}, code: code, deleteInfo: null};
  26. if(ID){
  27. compleQuery = {ID: ID, deleteInfo: null};
  28. }
  29. //let compleRation = await compleRationModel.findOne({userId: userId, rationRepId: {$in: rationRepIds}, code: code, deleteInfo: null});
  30. let compleRation = await compleRationModel.findOne(compleQuery);
  31. if(isDef(compleRation)){
  32. ration = compleRation._doc;
  33. ration.type = 'complementary';
  34. }
  35. }
  36. if(isDef(ration)){
  37. let stdChapter = await stdSectionTreeModel.findOne({rationRepId: ration.rationRepId, ID: ration.sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  38. if(isDef(stdChapter)){
  39. ration.chapter = stdChapter._doc;
  40. }
  41. else{
  42. let compleChapter = await compleRationSectionTreeModel.findOne({userId: userId, ID: ration.sectionId, deleteInfo: null});
  43. if(isDef(compleChapter)){
  44. ration.chapter = compleChapter._doc;
  45. }
  46. }
  47. }
  48. if(callback){
  49. callback(0, ration);
  50. }
  51. }
  52. catch(err){
  53. if(callback){
  54. callback(err, null);
  55. }
  56. }
  57. return ration;
  58. }
  59. async findRation(userId, rationRepId, keyword, callback){
  60. try{
  61. let filter = {
  62. 'rationRepId': {$in: rationRepId},
  63. '$and': [{
  64. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  65. }, {
  66. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
  67. }]
  68. };
  69. let stdGljIds = [],
  70. comGljIds = [];
  71. let stdRations = await stdRationModel.find(filter);
  72. for(let i = 0, len = stdRations.length; i < len; i++){
  73. stdRations[i]._doc.type = 'std';
  74. for(let glj of stdRations[i].rationGljList){
  75. stdGljIds.push(glj.gljId);
  76. }
  77. }
  78. filter.userId = userId;
  79. let compleRations = await compleRationModel.find(filter);
  80. for(let i = 0, len = compleRations.length; i <len; i++){
  81. compleRations[i]._doc.type = 'complementary';
  82. for(let glj of stdRations[i].rationGljList){
  83. if(glj.type === 'std'){
  84. stdGljIds.push(glj.gljId);
  85. }
  86. else {
  87. comGljIds.push(glj.gljId);
  88. }
  89. }
  90. }
  91. //设置悬浮信息
  92. stdGljIds = Array.from(new Set(stdGljIds));
  93. comGljIds = Array.from(new Set(comGljIds));
  94. let gljIDMapping = {};
  95. if(stdGljIds.length > 0){
  96. let stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}, '-_id ID code name specs unit gljType');
  97. for(let stdGlj of stdGljs){
  98. gljIDMapping[stdGlj.ID] = stdGlj;
  99. }
  100. }
  101. if(comGljIds.length > 0){
  102. let comGljs = await complementaryGljModel.find({ID: {$in: stdGljIds}});
  103. for(let comGlj of comGljs){
  104. gljIDMapping[comGlj.ID] = comGlj;
  105. }
  106. }
  107. for(let ration of stdRations){
  108. let hintsArr = [];
  109. //对人材机进行排序
  110. ration.rationGljList.sort(function (a, b) {
  111. let gljA = gljIDMapping[a.gljId],
  112. gljB = gljIDMapping[b.gljId];
  113. if(gljA && gljB){
  114. let aV = gljA.gljType + gljA.code,
  115. bV = gljB.gljType + gljB.code;
  116. if(aV > bV) {
  117. return 1;
  118. } else if(aV < bV) {
  119. return -1;
  120. }
  121. }
  122. return 0;
  123. });
  124. for(let rationGlj of ration.rationGljList){
  125. let glj = gljIDMapping[rationGlj.gljId];
  126. if(glj){
  127. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  128. }
  129. }
  130. hintsArr.push(`基价 元 ${ration.basePrice}`);
  131. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  132. hintsArr.push(`工作内容:`);
  133. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  134. }
  135. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  136. hintsArr.push(`附注:`);
  137. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  138. }
  139. ration._doc.hint = hintsArr.join('<br>');
  140. }
  141. for(let ration of compleRations){
  142. let hintsArr = [];
  143. for(let rationGlj of ration.rationGljList){
  144. let glj = gljIDMapping[rationGlj.gljId];
  145. if(glj){
  146. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  147. }
  148. }
  149. hintsArr.push(`基价 元 ${ration.basePrice}`);
  150. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  151. hintsArr.push(`工作内容:`);
  152. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  153. }
  154. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  155. hintsArr.push(`附注:`);
  156. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  157. }
  158. ration._doc.hint = hintsArr.join('<br>');
  159. }
  160. callback(0, stdRations.concat(compleRations));
  161. }
  162. catch(err){
  163. callback(err, null);
  164. }
  165. }
  166. }
  167. function isDef(v){
  168. return v !== undefined && v !== null;
  169. }
  170. export default SearchDao;