gljModel.js 14 KB

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