gljModel.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. import counter from "../../../public/counter/counter";
  11. import async from "async";
  12. import STDGLJLibGLJListModel from "../../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 = 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. complementaryGljModel.find({userId: userId, compilationId: compilationId}, '-_id', {lean: true}, function (err, complementaryGljs) {
  158. if(err){
  159. cb(err);
  160. }
  161. else{
  162. me.sortToNumber(complementaryGljs);
  163. rst.complementaryGljs = complementaryGljs;
  164. cb(null);
  165. }
  166. });
  167. }
  168. ], function (err) {
  169. if(err){
  170. callback(err, null);
  171. }
  172. else{
  173. callback(0, rst);
  174. }
  175. })
  176. };
  177. async getStdItems (stdGljLibId, projection, callback) {
  178. try {
  179. let stdItems = await stdGljModel.find({repositoryId: stdGljLibId}, projection).lean();
  180. callback(0, stdItems);
  181. } catch (err) {
  182. callback(1, null);
  183. }
  184. }
  185. getGljItemsByCode (repositoryId, codes, callback){
  186. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  187. if(err) callback(true, "")
  188. else callback(false, data);
  189. })
  190. };
  191. updateComponent(userId, updateArr, callback){
  192. let parallelFucs = [];
  193. for(let i = 0; i < updateArr.length; i++){
  194. parallelFucs.push((function(obj){
  195. return function (cb) {
  196. if(typeof obj.component === 'undefined'){
  197. obj.component = [];
  198. }
  199. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  200. if(err){
  201. cb(err);
  202. }
  203. else{
  204. cb(null, obj);
  205. }
  206. })
  207. }
  208. })(updateArr[i]));
  209. }
  210. async.parallel(parallelFucs, function (err, result) {
  211. if(err){
  212. callback(err, '更新组成物错误!', null);
  213. }
  214. else{
  215. callback(0, '成功!', result);
  216. }
  217. });
  218. }
  219. //-oprtor
  220. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  221. if (updateItems.length == 0 && rIds.length == 0) {
  222. GljDao.addGljItems(userId, compilationId, addItems, callback);
  223. }
  224. else if(rIds.length > 0 && updateItems.length > 0){
  225. async.parallel([
  226. function (cb) {
  227. GljDao.removeGljItems(rIds, cb);
  228. },
  229. function (cb) {
  230. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  231. }
  232. ], function (err) {
  233. if(err){
  234. callback(true, "Fail to update and delete", false)
  235. }
  236. else{
  237. callback(false, "Save successfully", false);
  238. }
  239. })
  240. }
  241. else if (rIds.length > 0 && updateItems.length === 0) {
  242. GljDao.removeGljItems(rIds, callback);
  243. }
  244. else if(updateItems.length > 0 || addItems.length > 0){
  245. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  246. if (err) {
  247. callback(true, "Fail to update", false);
  248. } else {
  249. if (addItems && addItems.length > 0) {
  250. GljDao.addGljItems(userId, compilationId, addItems, callback);
  251. } else {
  252. callback(false, "Save successfully", results);
  253. }
  254. }
  255. });
  256. }
  257. }
  258. static removeGljItems (rIds, callback) {
  259. if (rIds && rIds.length > 0) {
  260. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  261. if (err) {
  262. callback(true, "Fail to remove", false);
  263. } else {
  264. callback(false, "Remove successfully", docs);
  265. }
  266. })
  267. } else {
  268. callback(false, "No records were deleted!", null);
  269. }
  270. }
  271. static addGljItems (userId, compilationId, items, callback) {
  272. if (items && items.length > 0) {
  273. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  274. var maxId = result.sequence_value;
  275. var arr = [];
  276. for (var i = 0; i < items.length; i++) {
  277. var obj = new complementaryGljModel(items[i]);
  278. obj.ID = (maxId - (items.length - 1) + i);
  279. obj.userId = userId;
  280. obj.compilationId = compilationId;
  281. arr.push(obj);
  282. }
  283. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  284. if (err) {
  285. callback(true, "Fail to add", false);
  286. } else {
  287. callback(false, "Add successfully", docs);
  288. }
  289. });
  290. });
  291. } else {
  292. callback(true, "No source", false);
  293. }
  294. }
  295. static updateGljItems(userId, compilationId, items, callback) {
  296. var functions = [];
  297. for (var i=0; i < items.length; i++) {
  298. functions.push((function(doc) {
  299. return function(cb) {
  300. var filter = {};
  301. if (doc.ID) {
  302. filter.ID = doc.ID;
  303. } else {
  304. filter.userId = userId;
  305. filter.compilationId = compilationId;
  306. filter.code = doc.code;
  307. }
  308. complementaryGljModel.update(filter, doc, cb);
  309. };
  310. })(items[i]));
  311. }
  312. async.parallel(functions, function(err, results) {
  313. callback(err, results);
  314. });
  315. }
  316. /**
  317. * 获取组成物数据
  318. *
  319. * @param {Number} gljId
  320. * @return {Promise}
  321. */
  322. async getComponent(gljId) {
  323. let result = [];
  324. let libGljData = await complementaryGljModel.findOne({ID: gljId});
  325. if (libGljData === null || libGljData.component.length <= 0) {
  326. return result;
  327. }
  328. // 标准工料机库
  329. let componentIdListStd = [];
  330. // 补充工料机库
  331. let componentIdListCpt = [];
  332. let componentConsume = {};
  333. // 整理数据
  334. for (let component of libGljData.component) {
  335. if (component.isStd) {
  336. componentIdListStd.push(component.ID);
  337. } else {
  338. componentIdListCpt.push(component.ID);
  339. }
  340. let isStdFlag = component.isStd ? 'std' : 'cpt';
  341. //对于补充人材机的组成物,不会有多单价的情况
  342. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  343. }
  344. // 查找标准库数据
  345. let condition = {};
  346. let componentStdGljData = [];
  347. if (componentIdListStd.length > 0) {
  348. let gljListModel = new STDGLJLibGLJListModel();
  349. condition = {ID: {$in: componentIdListStd}};
  350. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  351. }
  352. // 查找补充库数据
  353. let componentCptGljData = [];
  354. if (componentIdListCpt.length > 0) {
  355. condition = {ID: {$in: componentIdListCpt}};
  356. componentCptGljData = await complementaryGljModel.find(condition);
  357. }
  358. if (componentCptGljData === null && componentStdGljData === null) {
  359. return result;
  360. }
  361. // 整理数据
  362. if (componentStdGljData.length > 0) {
  363. componentStdGljData = JSON.stringify(componentStdGljData);
  364. componentStdGljData = JSON.parse(componentStdGljData);
  365. for(let gljData of componentStdGljData) {
  366. gljData.connectCode = libGljData.code;
  367. gljData.consumption = componentConsume[gljData.ID + '_std'];
  368. gljData.from='std';
  369. result.push(gljData);
  370. }
  371. }
  372. if (componentCptGljData.length > 0) {
  373. componentCptGljData = JSON.stringify(componentCptGljData);
  374. componentCptGljData = JSON.parse(componentCptGljData);
  375. for(let gljData of componentCptGljData) {
  376. gljData.connectCode = libGljData.code;
  377. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  378. gljData.from='cpt';
  379. result.push(gljData);
  380. }
  381. }
  382. return result;
  383. }
  384. async getMixedTree(gljLibId, userId, compilationId){
  385. let rst = {std: [], comple: []};
  386. rst.std = await gljClassModel.find({repositoryId: gljLibId}).lean();
  387. rst.comple = await compleClassModel.find({userId: userId, compilationId: compilationId}).lean();
  388. return rst;
  389. }
  390. }
  391. export default GljDao;