gljModel.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * @Descripttion:
  3. * @Author: vian
  4. * @Date: 2020-12-16 17:48:47
  5. */
  6. /*
  7. * @Descripttion:
  8. * @Author: vian
  9. * @Date: 2020-12-16 17:48:47
  10. */
  11. /*
  12. * @Descripttion:
  13. * @Author: vian
  14. * @Date: 2020-12-16 17:48:47
  15. */
  16. /*
  17. * @Descripttion:
  18. * @Author: vian
  19. * @Date: 2020-12-16 17:48:47
  20. */
  21. /*
  22. * @Descripttion:
  23. * @Author: vian
  24. * @Date: 2020-12-16 17:48:47
  25. */
  26. /*
  27. * @Descripttion:
  28. * @Author: vian
  29. * @Date: 2020-12-16 17:48:47
  30. */
  31. /**
  32. * Created by Zhong on 2017/8/22.
  33. */
  34. const mongoose = require('mongoose');
  35. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  36. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  37. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  38. const compleClassModel = mongoose.model('complementary_glj_section');
  39. const _ = require('lodash');
  40. const counter = require("../../../public/counter/counter");
  41. const async = require("async");
  42. const STDGLJLibGLJListModel = require("../../common/std/std_glj_lib_glj_list_model");
  43. const libType = {
  44. stdGLJ: 1,
  45. complementaryGLJs: 2
  46. };
  47. const stdgljutil = require("../../../public/cache/std_glj_type_util");
  48. const { ShareLibType } = require('../../../public/common_constants');
  49. class GljDao {
  50. async prepareInitData(gljLibID, userID, sessionUserID, compilationID, projection) {
  51. const pmFacade = require('../../pm/facade/pm_facade');
  52. const receiveLibs = await pmFacade.getReceiveLibList(sessionUserID, compilationID, ShareLibType.GLJ_LIB);
  53. const shareLibs = sessionUserID === userID ? await pmFacade.getLibShareList(sessionUserID, compilationID, ShareLibType.GLJ_LIB) : [];
  54. const gljItems = await this.getGLJItemsSync(gljLibID, userID, compilationID, projection);
  55. const mixTree = await this.getMixedTree(gljLibID, userID, compilationID);
  56. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  57. return {
  58. receiveLibs,
  59. shareLibs,
  60. gljItems,
  61. mixTree,
  62. distTypeData: gljDistTypeCache,
  63. };
  64. }
  65. getGljTypes (gljLibId, callback){
  66. gljClassModel.find({"repositoryId": gljLibId},function(err,data){
  67. if(data.length) {
  68. callback(0,data);
  69. }
  70. else if(err) callback("获取人材机类型错误!",false);
  71. })
  72. }
  73. _exist(data, attr){
  74. return data && data[attr] !== 'undefined' && data[attr];
  75. }
  76. sortToNumber(datas){
  77. for(let i = 0, len = datas.length; i < len; i++){
  78. let data = datas[i]._doc;
  79. if(this._exist(data, 'basePrice')){
  80. data['basePrice'] = parseFloat(data['basePrice']);
  81. }
  82. if(this._exist(data, 'component')){
  83. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  84. let comGljObj = data['component'][j]._doc;
  85. if(this._exist(comGljObj, 'consumeAmt')){
  86. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. async getQueryByType (condition) {
  93. const {
  94. userID,
  95. compilationId,
  96. gljLibId, code,
  97. type,
  98. queryExtend,
  99. replace,
  100. location,
  101. classList,
  102. search
  103. } = condition;
  104. let model = null,
  105. query = {},
  106. countQuery = {},
  107. matchLocation = false;
  108. if (type === libType.stdGLJ) {
  109. model = stdGljModel;
  110. query.repositoryId = gljLibId;
  111. } else {
  112. model = complementaryGljModel;
  113. query.userId = userID;
  114. query.compilationId = compilationId;
  115. }
  116. // 分类树
  117. if (classList.length) {
  118. query.gljClass = {$in: classList};
  119. }
  120. // 定位
  121. if (location) {
  122. // 替换定位,则先看看能不能找到替换的人材机,能找到的话就能定位要被替换人材机所在分类树
  123. const replaceQuery = {...query, ...replace};
  124. const replaceData = await model.findOne(replaceQuery);
  125. if (replaceData) {
  126. query.gljClass = replaceData.gljClass;
  127. matchLocation = true;
  128. }
  129. }
  130. // 替换过滤类型
  131. if (replace) {
  132. // 人材机类型是“混凝土、砂浆、配合比、商品混凝土、商品砂浆”时,筛选的可替换的人材机类型应是“混凝土、或砂浆、或配合比、或商品混凝土、或商品砂浆”。
  133. //2021-03-17 zhang 需求更改 当前是材料、主材、设备,可以替换为材料、主材、设备。(因有些混凝土在定额书中未给单价,所以是主材,实际使用时 需要替换为“砼”或“商砼”等)
  134. //const materialTypes = [202, 203, 204, 205, 206];
  135. const materialTypes = [201,202, 203, 204, 205, 206,4,5];
  136. query.gljType = materialTypes.includes(replace.gljType) ? {$in: materialTypes} : replace.gljType;
  137. }
  138. // 添加组成物时的查询扩展
  139. if (queryExtend) {
  140. Object.assign(query, queryExtend);
  141. }
  142. // 搜索关键字
  143. if (search) {
  144. query.$or = [{code: {'$regex': search, $options: '$i'}}, {name: {'$regex': search, $options: '$i'}}];
  145. }
  146. countQuery = {...query};
  147. // 上一次分页的最末人材机编码
  148. if (code) {
  149. query.code = {$gt: code};
  150. }
  151. return {
  152. model,
  153. query,
  154. countQuery,
  155. matchLocation
  156. };
  157. }
  158. async getGLJPaging (condition) {
  159. const queryData = await this.getQueryByType(condition);
  160. // 定位(替换初始化)
  161. // 替换触发的人材机选择界面,只有在初始化时才定位,其他操作下是正常的分页
  162. if (queryData.matchLocation) {
  163. // 返回的数据,为编码小于等于替换的编码,再加附加条数
  164. const limitQuery = _.cloneDeep(queryData.query);
  165. limitQuery.code = {$lte: condition.replace.code};
  166. const lteLimit = await queryData.model.count(limitQuery);
  167. const additionalLimit = 10;
  168. const limit = lteLimit + additionalLimit;
  169. // 显示的数据不能太少,否则数据没有占满整个表格区域(实际上数据条目应该超过表格显示区域),让人误以为数据只有那么点,不再滚动触发分页
  170. condition.limit = limit > condition.limit ? limit : condition.limit;
  171. }
  172. //const gljs = await queryData.model.find(queryData.query).lean().sort({code: 1}).skip(condition.index).limit(condition.limit);
  173. // 分页使用skip的话,跳过的条数多的话性能会下降,因此不使用
  174. const gljs = await queryData.model.find(queryData.query).lean().sort({code: 1}).limit(condition.limit);
  175. const total = await queryData.model.find(queryData.countQuery).count();
  176. return condition.type === libType.stdGLJ
  177. ? {stdGLJ: gljs, complementaryGLJs: [], total: total}
  178. : {stdGLJ: [], complementaryGLJs: gljs, total: total}
  179. }
  180. async getGLJDataSync (gljLibId, userId, compilationId) {
  181. const stdGljs = await stdGljModel.find({repositoryId: gljLibId}).lean();
  182. const complementaryGljs = await complementaryGljModel.find({userId, compilationId}).lean();
  183. return {
  184. stdGljs,
  185. complementaryGljs
  186. }
  187. }
  188. //获得用户的补充工料机和用户当前所在编办的标准工料机
  189. async getGljItems(stdGljLibId, userId, compilationId, projection, callback) {
  190. let me = this;
  191. let rst = { stdGljs: [], complementaryGljs: [] };
  192. //批量获取异步
  193. async.parallel([
  194. async function (cb) {
  195. try {
  196. let stdGljs = stdGljLibId ? await stdGljModel.find({ repositoryId: stdGljLibId }, projection).lean() : [];
  197. me.sortToNumber(stdGljs);
  198. rst.stdGljs = stdGljs;
  199. cb(null);
  200. }
  201. catch (err) {
  202. cb(err);
  203. }
  204. },
  205. function (cb) {
  206. if (!userId || !compilationId) {
  207. rst.complementaryGljs = [];
  208. cb(null);
  209. return;
  210. }
  211. complementaryGljModel.find({ userId: userId, compilationId: compilationId }, '-_id', { lean: true }, function (err, complementaryGljs) {
  212. if (err) {
  213. cb(err);
  214. }
  215. else {
  216. me.sortToNumber(complementaryGljs);
  217. rst.complementaryGljs = complementaryGljs;
  218. cb(null);
  219. }
  220. });
  221. }
  222. ], function (err) {
  223. if (err) {
  224. callback(err, null);
  225. }
  226. else {
  227. callback(0, rst);
  228. }
  229. })
  230. }
  231. async getGLJItemsSync(stdGljLibId, userId, compilationId, projection) {
  232. const rst = { stdGljs: [], complementaryGljs: [] };
  233. const task = [
  234. stdGljModel.find({ repositoryId: stdGljLibId }, projection).lean(),
  235. complementaryGljModel.find({ userId: userId, compilationId: compilationId }, '-_id').lean()
  236. ];
  237. const [ stdRst, cptRst ] = await Promise.all(task);
  238. this.sortToNumber(stdRst);
  239. rst.stdGljs = stdRst;
  240. this.sortToNumber(cptRst);
  241. rst.complementaryGljs = cptRst;
  242. return rst;
  243. }
  244. async getStdItems (stdGljLibId, projection, callback) {
  245. try {
  246. let stdItems = await stdGljModel.find({repositoryId: stdGljLibId}, projection).lean();
  247. callback(0, stdItems);
  248. } catch (err) {
  249. callback(1, null);
  250. }
  251. }
  252. getGljItemsByCode (repositoryId, codes, callback){
  253. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  254. if(err) callback(true, "")
  255. else callback(false, data);
  256. })
  257. };
  258. updateComponent(userId, updateArr, callback){
  259. let parallelFucs = [];
  260. for(let i = 0; i < updateArr.length; i++){
  261. parallelFucs.push((function(obj){
  262. return function (cb) {
  263. if(typeof obj.component === 'undefined'){
  264. obj.component = [];
  265. }
  266. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  267. if(err){
  268. cb(err);
  269. }
  270. else{
  271. cb(null, obj);
  272. }
  273. })
  274. }
  275. })(updateArr[i]));
  276. }
  277. async.parallel(parallelFucs, function (err, result) {
  278. if(err){
  279. callback(err, '更新组成物错误!', null);
  280. }
  281. else{
  282. callback(0, '成功!', result);
  283. }
  284. });
  285. }
  286. //-oprtor
  287. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  288. if (updateItems.length == 0 && rIds.length == 0) {
  289. GljDao.addGljItems(userId, compilationId, addItems, callback);
  290. }
  291. else if(rIds.length > 0 && updateItems.length > 0){
  292. async.parallel([
  293. function (cb) {
  294. GljDao.removeGljItems(rIds, cb);
  295. },
  296. function (cb) {
  297. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  298. }
  299. ], function (err) {
  300. if(err){
  301. callback(true, "Fail to update and delete", false)
  302. }
  303. else{
  304. callback(false, "Save successfully", false);
  305. }
  306. })
  307. }
  308. else if (rIds.length > 0 && updateItems.length === 0) {
  309. GljDao.removeGljItems(rIds, callback);
  310. }
  311. else if(updateItems.length > 0 || addItems.length > 0){
  312. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  313. if (err) {
  314. callback(true, "Fail to update", false);
  315. } else {
  316. if (addItems && addItems.length > 0) {
  317. GljDao.addGljItems(userId, compilationId, addItems, callback);
  318. } else {
  319. callback(false, "Save successfully", results);
  320. }
  321. }
  322. });
  323. }
  324. }
  325. static removeGljItems (rIds, callback) {
  326. if (rIds && rIds.length > 0) {
  327. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  328. if (err) {
  329. callback(true, "Fail to remove", false);
  330. } else {
  331. callback(false, "Remove successfully", docs);
  332. }
  333. })
  334. } else {
  335. callback(false, "No records were deleted!", null);
  336. }
  337. }
  338. static addGljItems (userId, compilationId, items, callback) {
  339. if (items && items.length > 0) {
  340. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  341. var maxId = result.sequence_value;
  342. var arr = [];
  343. for (var i = 0; i < items.length; i++) {
  344. var obj = new complementaryGljModel(items[i]);
  345. obj.ID = (maxId - (items.length - 1) + i);
  346. obj.userId = userId;
  347. obj.compilationId = compilationId;
  348. arr.push(obj);
  349. }
  350. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  351. if (err) {
  352. callback(true, "Fail to add", false);
  353. } else {
  354. callback(false, "Add successfully", docs);
  355. }
  356. });
  357. });
  358. } else {
  359. callback(true, "No source", false);
  360. }
  361. }
  362. static updateGljItems(userId, compilationId, items, callback) {
  363. var functions = [];
  364. for (var i=0; i < items.length; i++) {
  365. functions.push((function(doc) {
  366. return function(cb) {
  367. var filter = {};
  368. if (doc.ID) {
  369. filter.ID = doc.ID;
  370. } else {
  371. filter.userId = userId;
  372. filter.compilationId = compilationId;
  373. filter.code = doc.code;
  374. }
  375. complementaryGljModel.update(filter, doc, cb);
  376. };
  377. })(items[i]));
  378. }
  379. async.parallel(functions, function(err, results) {
  380. callback(err, results);
  381. });
  382. }
  383. /**
  384. * 获取组成物数据
  385. *
  386. * @param {Number} gljId
  387. * @return {Promise}
  388. */
  389. async getComponent(gljId) {
  390. let result = [];
  391. let libGljData = await complementaryGljModel.findOne({ID: gljId});
  392. if (libGljData === null || libGljData.component.length <= 0) {
  393. return result;
  394. }
  395. // 标准工料机库
  396. let componentIdListStd = [];
  397. // 补充工料机库
  398. let componentIdListCpt = [];
  399. let componentConsume = {};
  400. // 整理数据
  401. for (let component of libGljData.component) {
  402. if (component.isStd) {
  403. componentIdListStd.push(component.ID);
  404. } else {
  405. componentIdListCpt.push(component.ID);
  406. }
  407. let isStdFlag = component.isStd ? 'std' : 'cpt';
  408. if(component.consumeAmt != undefined && component.consumeAmt != null) {
  409. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  410. }else if(component.consumeAmtProperty){
  411. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmtProperty;
  412. }
  413. }
  414. // 查找标准库数据
  415. let condition = {};
  416. let componentStdGljData = [];
  417. if (componentIdListStd.length > 0) {
  418. let gljListModel = new STDGLJLibGLJListModel();
  419. condition = {ID: {$in: componentIdListStd}};
  420. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  421. }
  422. // 查找补充库数据
  423. let componentCptGljData = [];
  424. if (componentIdListCpt.length > 0) {
  425. condition = {ID: {$in: componentIdListCpt}};
  426. componentCptGljData = await complementaryGljModel.find(condition);
  427. }
  428. if (componentCptGljData === null && componentStdGljData === null) {
  429. return result;
  430. }
  431. // 整理数据
  432. if (componentStdGljData.length > 0) {
  433. componentStdGljData = JSON.stringify(componentStdGljData);
  434. componentStdGljData = JSON.parse(componentStdGljData);
  435. for(let gljData of componentStdGljData) {
  436. gljData.connectCode = libGljData.code;
  437. gljData.consumption = componentConsume[gljData.ID + '_std'];
  438. gljData.from='std';
  439. result.push(gljData);
  440. }
  441. }
  442. if (componentCptGljData.length > 0) {
  443. componentCptGljData = JSON.stringify(componentCptGljData);
  444. componentCptGljData = JSON.parse(componentCptGljData);
  445. for(let gljData of componentCptGljData) {
  446. gljData.connectCode = libGljData.code;
  447. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  448. gljData.from='cpt';
  449. result.push(gljData);
  450. }
  451. }
  452. return result;
  453. }
  454. async getMixedTree(gljLibId, userId, compilationId){
  455. let rst = {std: [], comple: []};
  456. if (gljLibId) {
  457. rst.std = await gljClassModel.find({repositoryId: gljLibId}).lean();
  458. }
  459. if (userId && compilationId) {
  460. rst.comple = await compleClassModel.find({userId: userId, compilationId: compilationId}).lean();
  461. }
  462. return rst;
  463. }
  464. }
  465. module.exports = GljDao;