gljModel.js 14 KB

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