gljModel.js 15 KB

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