gljModel.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. const materialTypes = [202, 203, 204, 205, 206];
  134. query.gljType = materialTypes.includes(replace.gljType) ? {$in: materialTypes} : replace.gljType;
  135. }
  136. // 添加组成物时的查询扩展
  137. if (queryExtend) {
  138. Object.assign(query, queryExtend);
  139. }
  140. // 搜索关键字
  141. if (search) {
  142. query.$or = [{code: {'$regex': search, $options: '$i'}}, {name: {'$regex': search, $options: '$i'}}];
  143. }
  144. countQuery = {...query};
  145. // 上一次分页的最末人材机编码
  146. if (code) {
  147. query.code = {$gt: code};
  148. }
  149. return {
  150. model,
  151. query,
  152. countQuery,
  153. matchLocation
  154. };
  155. }
  156. async getGLJPaging (condition) {
  157. const queryData = await this.getQueryByType(condition);
  158. // 定位(替换初始化)
  159. // 替换触发的人材机选择界面,只有在初始化时才定位,其他操作下是正常的分页
  160. if (queryData.matchLocation) {
  161. // 返回的数据,为编码小于等于替换的编码,再加附加条数
  162. const limitQuery = _.cloneDeep(queryData.query);
  163. limitQuery.code = {$lte: condition.replace.code};
  164. const lteLimit = await queryData.model.count(limitQuery);
  165. const additionalLimit = 10;
  166. const limit = lteLimit + additionalLimit;
  167. // 显示的数据不能太少,否则数据没有占满整个表格区域(实际上数据条目应该超过表格显示区域),让人误以为数据只有那么点,不再滚动触发分页
  168. condition.limit = limit > condition.limit ? limit : condition.limit;
  169. }
  170. //const gljs = await queryData.model.find(queryData.query).lean().sort({code: 1}).skip(condition.index).limit(condition.limit);
  171. // 分页使用skip的话,跳过的条数多的话性能会下降,因此不使用
  172. const gljs = await queryData.model.find(queryData.query).lean().sort({code: 1}).limit(condition.limit);
  173. const total = await queryData.model.find(queryData.countQuery).count();
  174. return condition.type === libType.stdGLJ
  175. ? {stdGLJ: gljs, complementaryGLJs: [], total: total}
  176. : {stdGLJ: [], complementaryGLJs: gljs, total: total}
  177. }
  178. async getGLJDataSync (gljLibId, userId, compilationId) {
  179. const stdGljs = await stdGljModel.find({repositoryId: gljLibId}).lean();
  180. const complementaryGljs = await complementaryGljModel.find({userId, compilationId}).lean();
  181. return {
  182. stdGljs,
  183. complementaryGljs
  184. }
  185. }
  186. //获得用户的补充工料机和用户当前所在编办的标准工料机
  187. async getGljItems(stdGljLibId, userId, compilationId, projection, callback) {
  188. let me = this;
  189. let rst = { stdGljs: [], complementaryGljs: [] };
  190. //批量获取异步
  191. async.parallel([
  192. async function (cb) {
  193. try {
  194. let stdGljs = stdGljLibId ? await stdGljModel.find({ repositoryId: stdGljLibId }, projection).lean() : [];
  195. me.sortToNumber(stdGljs);
  196. rst.stdGljs = stdGljs;
  197. cb(null);
  198. }
  199. catch (err) {
  200. cb(err);
  201. }
  202. },
  203. function (cb) {
  204. if (!userId || !compilationId) {
  205. rst.complementaryGljs = [];
  206. cb(null);
  207. return;
  208. }
  209. complementaryGljModel.find({ userId: userId, compilationId: compilationId }, '-_id', { lean: true }, function (err, complementaryGljs) {
  210. if (err) {
  211. cb(err);
  212. }
  213. else {
  214. me.sortToNumber(complementaryGljs);
  215. rst.complementaryGljs = complementaryGljs;
  216. cb(null);
  217. }
  218. });
  219. }
  220. ], function (err) {
  221. if (err) {
  222. callback(err, null);
  223. }
  224. else {
  225. callback(0, rst);
  226. }
  227. })
  228. }
  229. async getGLJItemsSync(stdGljLibId, userId, compilationId, projection) {
  230. const rst = { stdGljs: [], complementaryGljs: [] };
  231. const task = [
  232. stdGljModel.find({ repositoryId: stdGljLibId }, projection).lean(),
  233. complementaryGljModel.find({ userId: userId, compilationId: compilationId }, '-_id').lean()
  234. ];
  235. const [ stdRst, cptRst ] = await Promise.all(task);
  236. this.sortToNumber(stdRst);
  237. rst.stdGljs = stdRst;
  238. this.sortToNumber(cptRst);
  239. rst.complementaryGljs = cptRst;
  240. return rst;
  241. }
  242. async getStdItems (stdGljLibId, projection, callback) {
  243. try {
  244. let stdItems = await stdGljModel.find({repositoryId: stdGljLibId}, projection).lean();
  245. callback(0, stdItems);
  246. } catch (err) {
  247. callback(1, null);
  248. }
  249. }
  250. getGljItemsByCode (repositoryId, codes, callback){
  251. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  252. if(err) callback(true, "")
  253. else callback(false, data);
  254. })
  255. };
  256. updateComponent(userId, updateArr, callback){
  257. let parallelFucs = [];
  258. for(let i = 0; i < updateArr.length; i++){
  259. parallelFucs.push((function(obj){
  260. return function (cb) {
  261. if(typeof obj.component === 'undefined'){
  262. obj.component = [];
  263. }
  264. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  265. if(err){
  266. cb(err);
  267. }
  268. else{
  269. cb(null, obj);
  270. }
  271. })
  272. }
  273. })(updateArr[i]));
  274. }
  275. async.parallel(parallelFucs, function (err, result) {
  276. if(err){
  277. callback(err, '更新组成物错误!', null);
  278. }
  279. else{
  280. callback(0, '成功!', result);
  281. }
  282. });
  283. }
  284. //-oprtor
  285. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  286. if (updateItems.length == 0 && rIds.length == 0) {
  287. GljDao.addGljItems(userId, compilationId, addItems, callback);
  288. }
  289. else if(rIds.length > 0 && updateItems.length > 0){
  290. async.parallel([
  291. function (cb) {
  292. GljDao.removeGljItems(rIds, cb);
  293. },
  294. function (cb) {
  295. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  296. }
  297. ], function (err) {
  298. if(err){
  299. callback(true, "Fail to update and delete", false)
  300. }
  301. else{
  302. callback(false, "Save successfully", false);
  303. }
  304. })
  305. }
  306. else if (rIds.length > 0 && updateItems.length === 0) {
  307. GljDao.removeGljItems(rIds, callback);
  308. }
  309. else if(updateItems.length > 0 || addItems.length > 0){
  310. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  311. if (err) {
  312. callback(true, "Fail to update", false);
  313. } else {
  314. if (addItems && addItems.length > 0) {
  315. GljDao.addGljItems(userId, compilationId, addItems, callback);
  316. } else {
  317. callback(false, "Save successfully", results);
  318. }
  319. }
  320. });
  321. }
  322. }
  323. static removeGljItems (rIds, callback) {
  324. if (rIds && rIds.length > 0) {
  325. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  326. if (err) {
  327. callback(true, "Fail to remove", false);
  328. } else {
  329. callback(false, "Remove successfully", docs);
  330. }
  331. })
  332. } else {
  333. callback(false, "No records were deleted!", null);
  334. }
  335. }
  336. static addGljItems (userId, compilationId, items, callback) {
  337. if (items && items.length > 0) {
  338. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  339. var maxId = result.sequence_value;
  340. var arr = [];
  341. for (var i = 0; i < items.length; i++) {
  342. var obj = new complementaryGljModel(items[i]);
  343. obj.ID = (maxId - (items.length - 1) + i);
  344. obj.userId = userId;
  345. obj.compilationId = compilationId;
  346. arr.push(obj);
  347. }
  348. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  349. if (err) {
  350. callback(true, "Fail to add", false);
  351. } else {
  352. callback(false, "Add successfully", docs);
  353. }
  354. });
  355. });
  356. } else {
  357. callback(true, "No source", false);
  358. }
  359. }
  360. static updateGljItems(userId, compilationId, items, callback) {
  361. var functions = [];
  362. for (var i=0; i < items.length; i++) {
  363. functions.push((function(doc) {
  364. return function(cb) {
  365. var filter = {};
  366. if (doc.ID) {
  367. filter.ID = doc.ID;
  368. } else {
  369. filter.userId = userId;
  370. filter.compilationId = compilationId;
  371. filter.code = doc.code;
  372. }
  373. complementaryGljModel.update(filter, doc, cb);
  374. };
  375. })(items[i]));
  376. }
  377. async.parallel(functions, function(err, results) {
  378. callback(err, results);
  379. });
  380. }
  381. /**
  382. * 获取组成物数据
  383. *
  384. * @param {Number} gljId
  385. * @return {Promise}
  386. */
  387. async getComponent(gljId) {
  388. let result = [];
  389. let libGljData = await complementaryGljModel.findOne({ID: gljId});
  390. if (libGljData === null || libGljData.component.length <= 0) {
  391. return result;
  392. }
  393. // 标准工料机库
  394. let componentIdListStd = [];
  395. // 补充工料机库
  396. let componentIdListCpt = [];
  397. let componentConsume = {};
  398. // 整理数据
  399. for (let component of libGljData.component) {
  400. if (component.isStd) {
  401. componentIdListStd.push(component.ID);
  402. } else {
  403. componentIdListCpt.push(component.ID);
  404. }
  405. let isStdFlag = component.isStd ? 'std' : 'cpt';
  406. if(component.consumeAmt != undefined && component.consumeAmt != null) {
  407. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  408. }else if(component.consumeAmtProperty){
  409. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmtProperty;
  410. }
  411. }
  412. // 查找标准库数据
  413. let condition = {};
  414. let componentStdGljData = [];
  415. if (componentIdListStd.length > 0) {
  416. let gljListModel = new STDGLJLibGLJListModel();
  417. condition = {ID: {$in: componentIdListStd}};
  418. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  419. }
  420. // 查找补充库数据
  421. let componentCptGljData = [];
  422. if (componentIdListCpt.length > 0) {
  423. condition = {ID: {$in: componentIdListCpt}};
  424. componentCptGljData = await complementaryGljModel.find(condition);
  425. }
  426. if (componentCptGljData === null && componentStdGljData === null) {
  427. return result;
  428. }
  429. // 整理数据
  430. if (componentStdGljData.length > 0) {
  431. componentStdGljData = JSON.stringify(componentStdGljData);
  432. componentStdGljData = JSON.parse(componentStdGljData);
  433. for(let gljData of componentStdGljData) {
  434. gljData.connectCode = libGljData.code;
  435. gljData.consumption = componentConsume[gljData.ID + '_std'];
  436. gljData.from='std';
  437. result.push(gljData);
  438. }
  439. }
  440. if (componentCptGljData.length > 0) {
  441. componentCptGljData = JSON.stringify(componentCptGljData);
  442. componentCptGljData = JSON.parse(componentCptGljData);
  443. for(let gljData of componentCptGljData) {
  444. gljData.connectCode = libGljData.code;
  445. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  446. gljData.from='cpt';
  447. result.push(gljData);
  448. }
  449. }
  450. return result;
  451. }
  452. async getMixedTree(gljLibId, userId, compilationId){
  453. let rst = {std: [], comple: []};
  454. if (gljLibId) {
  455. rst.std = await gljClassModel.find({repositoryId: gljLibId}).lean();
  456. }
  457. if (userId && compilationId) {
  458. rst.comple = await compleClassModel.find({userId: userId, compilationId: compilationId}).lean();
  459. }
  460. return rst;
  461. }
  462. }
  463. module.exports = GljDao;