gljModel.js 16 KB

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