searchModel.js 10 KB

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