searchModel.js 9.9 KB

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