searchModel.js 11 KB

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