searchModel.js 8.3 KB

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