searchModel.js 11 KB

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