searchModel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. const rationLibModel = mongoose.model('std_ration_lib_map');
  10. let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model;
  11. let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
  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. // 设置定额所属定额库数据
  93. async setRationLibName(stdRations, compleRations) {
  94. const rationLibIDSet = new Set();
  95. stdRations.forEach(ration => {
  96. rationLibIDSet.add(ration.rationRepId);
  97. });
  98. const rationLibNameList = await rationLibModel.find({ ID: { $in: [...rationLibIDSet] } }, '-_id dispName ID').lean();
  99. const rationLibNameMap = {};
  100. rationLibNameList.forEach(lib => rationLibNameMap[lib.ID] = lib.dispName);
  101. stdRations.forEach(ration => {
  102. ration.rationLibName = rationLibNameMap[ration.rationRepId];
  103. });
  104. compleRations.forEach(ration => ration.rationLibName = '我的补充定额');
  105. }
  106. //@param {Object}skip({std: Number, comple: Number})
  107. async findRation(userId, compilationId, rationRepId, keyword, skip, callback){
  108. //每次限制结果数
  109. const limit = 50;
  110. //结果数
  111. let resultCount = 0,
  112. rst = {data: [], count: null};
  113. try{
  114. let compleLibUsers = new Set();
  115. const stdRationLibs = [];
  116. rationRepId.forEach(libItem => {
  117. const [libID, owner] = String(libItem).split('*');
  118. if (libID === compleRationLib) {
  119. compleLibUsers.add(owner || userId);
  120. } else {
  121. stdRationLibs.push(libID);
  122. }
  123. });
  124. compleLibUsers = [...compleLibUsers];
  125. //是否需要查找补充定额
  126. let findCompleRtion = compleLibUsers.length > 0;
  127. let filter = {
  128. 'rationRepId': {$in: stdRationLibs},
  129. '$and': [{
  130. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  131. }, {
  132. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
  133. }]
  134. };
  135. let compleFilter = {
  136. userId: { $in: compleLibUsers },
  137. compilationId: compilationId,
  138. '$and': [{
  139. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  140. }, {
  141. '$or': [{deleteInfo: null}, {'deleteInfo.deleted': false}]
  142. }]
  143. };
  144. //结果数
  145. if (skip && skip.std === 0 && skip.comple === 0) {
  146. resultCount += stdRationLibs.length === 0 ? 0 : await stdRationModel.find(filter).count();
  147. resultCount += findCompleRtion ? await compleRationModel.find(compleFilter).count() : 0;
  148. rst.count = resultCount;
  149. }
  150. //搜索定额
  151. let stdGljIds = [],
  152. comGljIds = [];
  153. let stdRations = stdRationLibs.length === 0 ? [] : await stdRationModel.find(filter).lean().sort({code: 1}).skip(skip.std).limit(limit);
  154. for(let i = 0, len = stdRations.length; i < len; i++){
  155. stdRations[i].type = 'std';
  156. for(let glj of stdRations[i].rationGljList){
  157. stdGljIds.push(glj.gljId);
  158. }
  159. }
  160. let compleRations = [];
  161. let residueLimit = limit - stdRations.length;
  162. if (residueLimit > 0) {
  163. compleRations = findCompleRtion ? await compleRationModel.find(compleFilter).lean().sort({code: 1}).skip(skip.comple).limit(residueLimit) : [];
  164. for(let i = 0, len = compleRations.length; i <len; i++){
  165. compleRations[i].type = 'complementary';
  166. for(let glj of compleRations[i].rationGljList){
  167. if(glj.type === 'std'){
  168. stdGljIds.push(glj.gljId);
  169. }
  170. else {
  171. comGljIds.push(glj.gljId);
  172. }
  173. }
  174. }
  175. }
  176. // 设置定额所属定额库名称
  177. await this.setRationLibName(stdRations, compleRations);
  178. //设置悬浮信息
  179. stdGljIds = Array.from(new Set(stdGljIds));
  180. comGljIds = Array.from(new Set(comGljIds));
  181. let gljIDMapping = {};
  182. if(stdGljIds.length > 0){
  183. let stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}, '-_id ID code name specs unit gljType');
  184. for(let stdGlj of stdGljs){
  185. gljIDMapping[stdGlj.ID] = stdGlj;
  186. }
  187. }
  188. if(comGljIds.length > 0){
  189. let comGljs = await complementaryGljModel.find({ID: {$in: stdGljIds}});
  190. for(let comGlj of comGljs){
  191. gljIDMapping[comGlj.ID] = comGlj;
  192. }
  193. }
  194. for(let ration of stdRations){
  195. let hintsArr = [];
  196. //对人材机进行排序
  197. ration.rationGljList.sort(function (a, b) {
  198. let gljA = gljIDMapping[a.gljId],
  199. gljB = gljIDMapping[b.gljId];
  200. if(gljA && gljB){
  201. let aV = gljA.gljType + gljA.code,
  202. bV = gljB.gljType + gljB.code;
  203. if(aV > bV) {
  204. return 1;
  205. } else if(aV < bV) {
  206. return -1;
  207. }
  208. }
  209. return 0;
  210. });
  211. for(let rationGlj of ration.rationGljList){
  212. let glj = gljIDMapping[rationGlj.gljId];
  213. if(glj){
  214. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  215. }
  216. }
  217. hintsArr.push(`基价 元 ${ration.basePrice}`);
  218. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  219. hintsArr.push(`工作内容:`);
  220. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  221. }
  222. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  223. hintsArr.push(`附注:`);
  224. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  225. }
  226. ration.hint = hintsArr.join('<br>');
  227. }
  228. for(let ration of compleRations){
  229. let hintsArr = [];
  230. for(let rationGlj of ration.rationGljList){
  231. let glj = gljIDMapping[rationGlj.gljId];
  232. if(glj){
  233. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? ' ' + glj.specs : ''} ${glj.unit} ${rationGlj.consumeAmt}`);
  234. }
  235. }
  236. hintsArr.push(`基价 元 ${ration.basePrice}`);
  237. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  238. hintsArr.push(`工作内容:`);
  239. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  240. }
  241. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  242. hintsArr.push(`附注:`);
  243. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  244. }
  245. ration.hint = hintsArr.join('<br>');
  246. }
  247. rst.data = stdRations.concat(compleRations);
  248. callback(0, rst);
  249. }
  250. catch(err){
  251. console.log(err);
  252. callback(err, null);
  253. }
  254. }
  255. }
  256. function isDef(v){
  257. return v !== undefined && v !== null;
  258. }
  259. module.exports = SearchDao;