searchModel.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. //@param {Object}skip({std: Number, comple: Number})
  63. async findRation(userId, compilationId, rationRepId, keyword, skip, callback){
  64. //每次限制结果数
  65. const limit = 50;
  66. //结果数
  67. let resultCount = 0,
  68. rst = {data: [], count: null};
  69. try{
  70. //是否需要查找补充定额
  71. let findCompleRtion = rationRepId.length > 0 && rationRepId.includes(compleRationLib) ? true : false;
  72. //剔除补充定额库id
  73. if (rationRepId.includes(compleRationLib)) {
  74. rationRepId.splice(rationRepId.indexOf(compleRationLib), 1);
  75. }
  76. let filter = {
  77. 'rationRepId': {$in: rationRepId},
  78. '$and': [{
  79. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  80. }, {
  81. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
  82. }]
  83. };
  84. let compleFilter = {
  85. userId: userId,
  86. compilationId: compilationId,
  87. '$and': [{
  88. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  89. }, {
  90. '$or': [{deleteInfo: null}, {'deleteInfo.deleted': false}]
  91. }]
  92. };
  93. //结果数
  94. if (skip && skip.std === 0 && skip.comple === 0) {
  95. resultCount += rationRepId.length === 0 ? 0 : await stdRationModel.find(filter).count();
  96. resultCount += findCompleRtion ? await compleRationModel.find(compleFilter).count() : 0;
  97. rst.count = resultCount;
  98. }
  99. //搜索定额
  100. let stdGljIds = [],
  101. comGljIds = [];
  102. let stdRations = rationRepId.length === 0 ? [] : await stdRationModel.find(filter).sort({code: 1}).skip(skip.std).limit(limit);
  103. for(let i = 0, len = stdRations.length; i < len; i++){
  104. stdRations[i]._doc.type = 'std';
  105. for(let glj of stdRations[i].rationGljList){
  106. stdGljIds.push(glj.gljId);
  107. }
  108. }
  109. let compleRations = [];
  110. let residueLimit = limit - stdRations.length;
  111. if (residueLimit > 0) {
  112. compleRations = findCompleRtion ? await compleRationModel.find(compleFilter).sort({code: 1}).skip(skip.comple).limit(residueLimit) : [];
  113. for(let i = 0, len = compleRations.length; i <len; i++){
  114. compleRations[i]._doc.type = 'complementary';
  115. for(let glj of compleRations[i].rationGljList){
  116. if(glj.type === 'std'){
  117. stdGljIds.push(glj.gljId);
  118. }
  119. else {
  120. comGljIds.push(glj.gljId);
  121. }
  122. }
  123. }
  124. }
  125. //设置悬浮信息
  126. stdGljIds = Array.from(new Set(stdGljIds));
  127. comGljIds = Array.from(new Set(comGljIds));
  128. let gljIDMapping = {};
  129. if(stdGljIds.length > 0){
  130. let stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}, '-_id ID code name specs unit gljType');
  131. for(let stdGlj of stdGljs){
  132. gljIDMapping[stdGlj.ID] = stdGlj;
  133. }
  134. }
  135. if(comGljIds.length > 0){
  136. let comGljs = await complementaryGljModel.find({ID: {$in: stdGljIds}});
  137. for(let comGlj of comGljs){
  138. gljIDMapping[comGlj.ID] = comGlj;
  139. }
  140. }
  141. for(let ration of stdRations){
  142. let hintsArr = [];
  143. //对人材机进行排序
  144. ration.rationGljList.sort(function (a, b) {
  145. let gljA = gljIDMapping[a.gljId],
  146. gljB = gljIDMapping[b.gljId];
  147. if(gljA && gljB){
  148. let aV = gljA.gljType + gljA.code,
  149. bV = gljB.gljType + gljB.code;
  150. if(aV > bV) {
  151. return 1;
  152. } else if(aV < bV) {
  153. return -1;
  154. }
  155. }
  156. return 0;
  157. });
  158. for(let rationGlj of ration.rationGljList){
  159. let glj = gljIDMapping[rationGlj.gljId];
  160. if(glj){
  161. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  162. }
  163. }
  164. hintsArr.push(`基价 元 ${ration.basePrice}`);
  165. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  166. hintsArr.push(`工作内容:`);
  167. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  168. }
  169. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  170. hintsArr.push(`附注:`);
  171. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  172. }
  173. ration._doc.hint = hintsArr.join('<br>');
  174. }
  175. for(let ration of compleRations){
  176. let hintsArr = [];
  177. for(let rationGlj of ration.rationGljList){
  178. let glj = gljIDMapping[rationGlj.gljId];
  179. if(glj){
  180. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  181. }
  182. }
  183. hintsArr.push(`基价 元 ${ration.basePrice}`);
  184. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  185. hintsArr.push(`工作内容:`);
  186. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  187. }
  188. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  189. hintsArr.push(`附注:`);
  190. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  191. }
  192. ration._doc.hint = hintsArr.join('<br>');
  193. }
  194. rst.data = stdRations.concat(compleRations);
  195. callback(0, rst);
  196. }
  197. catch(err){
  198. console.log(err);
  199. callback(err, null);
  200. }
  201. }
  202. }
  203. function isDef(v){
  204. return v !== undefined && v !== null;
  205. }
  206. export default SearchDao;