gljModel.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 counter = require("../../../public/counter/counter");
  10. const async = require("async");
  11. const STDGLJLibGLJListModel = require("../../common/std/std_glj_lib_glj_list_model");
  12. const stdgljutil = require("../../../public/cache/std_glj_type_util");
  13. const { ShareLibType } = require('../../../public/common_constants');
  14. class GljDao {
  15. async prepareInitData(gljLibID, userID, sessionUserID, compilationID, projection) {
  16. const pmFacade = require('../../pm/facade/pm_facade');
  17. const receiveLibs = await pmFacade.getReceiveLibList(sessionUserID, compilationID, ShareLibType.GLJ_LIB);
  18. const shareLibs = sessionUserID === userID ? await pmFacade.getLibShareList(sessionUserID, compilationID, ShareLibType.GLJ_LIB) : [];
  19. const gljItems = await this.getGLJItemsSync(gljLibID, userID, compilationID, projection);
  20. const mixTree = await this.getMixedTree(gljLibID, userID, compilationID);
  21. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  22. return {
  23. receiveLibs,
  24. shareLibs,
  25. gljItems,
  26. mixTree,
  27. distTypeData: gljDistTypeCache,
  28. };
  29. }
  30. getGljTypes (gljLibId, callback){
  31. gljClassModel.find({"repositoryId": gljLibId},function(err,data){
  32. if(data.length) {
  33. callback(0,data);
  34. }
  35. else if(err) callback("获取人材机类型错误!",false);
  36. })
  37. }
  38. _exist(data, attr){
  39. return data && data[attr] !== 'undefined' && data[attr];
  40. }
  41. sortToNumber(datas){
  42. for(let i = 0, len = datas.length; i < len; i++){
  43. let data = datas[i]._doc;
  44. if(this._exist(data, 'basePrice')){
  45. data['basePrice'] = parseFloat(data['basePrice']);
  46. }
  47. if(this._exist(data, 'component')){
  48. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  49. let comGljObj = data['component'][j]._doc;
  50. if(this._exist(comGljObj, 'consumeAmt')){
  51. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. async getGLJDataSync (gljLibId, userId, compilationId) {
  58. const stdGljs = await stdGljModel.find({repositoryId: gljLibId}).lean();
  59. const complementaryGljs = await complementaryGljModel.find({userId, compilationId}).lean();
  60. return {
  61. stdGljs,
  62. complementaryGljs
  63. }
  64. }
  65. //获得用户的补充工料机和用户当前所在编办的标准工料机
  66. async getGljItems(stdGljLibId, userId, compilationId, projection, callback) {
  67. let me = this;
  68. let rst = { stdGljs: [], complementaryGljs: [] };
  69. //批量获取异步
  70. async.parallel([
  71. async function (cb) {
  72. try {
  73. let stdGljs = stdGljLibId ? await stdGljModel.find({ repositoryId: stdGljLibId }, projection).lean() : [];
  74. me.sortToNumber(stdGljs);
  75. rst.stdGljs = stdGljs;
  76. cb(null);
  77. }
  78. catch (err) {
  79. cb(err);
  80. }
  81. },
  82. function (cb) {
  83. if (!userId || !compilationId) {
  84. rst.complementaryGljs = [];
  85. cb(null);
  86. return;
  87. }
  88. complementaryGljModel.find({ userId: userId, compilationId: compilationId }, '-_id', { lean: true }, function (err, complementaryGljs) {
  89. if (err) {
  90. cb(err);
  91. }
  92. else {
  93. me.sortToNumber(complementaryGljs);
  94. rst.complementaryGljs = complementaryGljs;
  95. cb(null);
  96. }
  97. });
  98. }
  99. ], function (err) {
  100. if (err) {
  101. callback(err, null);
  102. }
  103. else {
  104. callback(0, rst);
  105. }
  106. })
  107. }
  108. async getGLJItemsSync(stdGljLibId, userId, compilationId, projection) {
  109. const rst = { stdGljs: [], complementaryGljs: [] };
  110. const task = [
  111. stdGljModel.find({ repositoryId: stdGljLibId }, projection).lean(),
  112. complementaryGljModel.find({ userId: userId, compilationId: compilationId }, '-_id').lean()
  113. ];
  114. const [ stdRst, cptRst ] = await Promise.all(task);
  115. this.sortToNumber(stdRst);
  116. rst.stdGljs = stdRst;
  117. this.sortToNumber(cptRst);
  118. rst.complementaryGljs = cptRst;
  119. return rst;
  120. }
  121. async getStdItems (stdGljLibId, projection, callback) {
  122. try {
  123. let stdItems = await stdGljModel.find({repositoryId: stdGljLibId}, projection).lean();
  124. callback(0, stdItems);
  125. } catch (err) {
  126. callback(1, null);
  127. }
  128. }
  129. getGljItemsByCode (repositoryId, codes, callback){
  130. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  131. if(err) callback(true, "")
  132. else callback(false, data);
  133. })
  134. };
  135. updateComponent(userId, updateArr, callback){
  136. let parallelFucs = [];
  137. for(let i = 0; i < updateArr.length; i++){
  138. parallelFucs.push((function(obj){
  139. return function (cb) {
  140. if(typeof obj.component === 'undefined'){
  141. obj.component = [];
  142. }
  143. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  144. if(err){
  145. cb(err);
  146. }
  147. else{
  148. cb(null, obj);
  149. }
  150. })
  151. }
  152. })(updateArr[i]));
  153. }
  154. async.parallel(parallelFucs, function (err, result) {
  155. if(err){
  156. callback(err, '更新组成物错误!', null);
  157. }
  158. else{
  159. callback(0, '成功!', result);
  160. }
  161. });
  162. }
  163. //-oprtor
  164. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  165. if (updateItems.length == 0 && rIds.length == 0) {
  166. GljDao.addGljItems(userId, compilationId, addItems, callback);
  167. }
  168. else if(rIds.length > 0 && updateItems.length > 0){
  169. async.parallel([
  170. function (cb) {
  171. GljDao.removeGljItems(rIds, cb);
  172. },
  173. function (cb) {
  174. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  175. }
  176. ], function (err) {
  177. if(err){
  178. callback(true, "Fail to update and delete", false)
  179. }
  180. else{
  181. callback(false, "Save successfully", false);
  182. }
  183. })
  184. }
  185. else if (rIds.length > 0 && updateItems.length === 0) {
  186. GljDao.removeGljItems(rIds, callback);
  187. }
  188. else if(updateItems.length > 0 || addItems.length > 0){
  189. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  190. if (err) {
  191. callback(true, "Fail to update", false);
  192. } else {
  193. if (addItems && addItems.length > 0) {
  194. GljDao.addGljItems(userId, compilationId, addItems, callback);
  195. } else {
  196. callback(false, "Save successfully", results);
  197. }
  198. }
  199. });
  200. }
  201. }
  202. static removeGljItems (rIds, callback) {
  203. if (rIds && rIds.length > 0) {
  204. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  205. if (err) {
  206. callback(true, "Fail to remove", false);
  207. } else {
  208. callback(false, "Remove successfully", docs);
  209. }
  210. })
  211. } else {
  212. callback(false, "No records were deleted!", null);
  213. }
  214. }
  215. static addGljItems (userId, compilationId, items, callback) {
  216. if (items && items.length > 0) {
  217. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  218. var maxId = result.sequence_value;
  219. var arr = [];
  220. for (var i = 0; i < items.length; i++) {
  221. var obj = new complementaryGljModel(items[i]);
  222. obj.ID = (maxId - (items.length - 1) + i);
  223. obj.userId = userId;
  224. obj.compilationId = compilationId;
  225. arr.push(obj);
  226. }
  227. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  228. if (err) {
  229. callback(true, "Fail to add", false);
  230. } else {
  231. callback(false, "Add successfully", docs);
  232. }
  233. });
  234. });
  235. } else {
  236. callback(true, "No source", false);
  237. }
  238. }
  239. static updateGljItems(userId, compilationId, items, callback) {
  240. var functions = [];
  241. for (var i=0; i < items.length; i++) {
  242. functions.push((function(doc) {
  243. return function(cb) {
  244. var filter = {};
  245. if (doc.ID) {
  246. filter.ID = doc.ID;
  247. } else {
  248. filter.userId = userId;
  249. filter.compilationId = compilationId;
  250. filter.code = doc.code;
  251. }
  252. complementaryGljModel.update(filter, doc, cb);
  253. };
  254. })(items[i]));
  255. }
  256. async.parallel(functions, function(err, results) {
  257. callback(err, results);
  258. });
  259. }
  260. /**
  261. * 获取组成物数据
  262. *
  263. * @param {Number} gljId
  264. * @return {Promise}
  265. */
  266. async getComponent(gljId) {
  267. let result = [];
  268. let libGljData = await complementaryGljModel.findOne({ID: gljId});
  269. if (libGljData === null || libGljData.component.length <= 0) {
  270. return result;
  271. }
  272. // 标准工料机库
  273. let componentIdListStd = [];
  274. // 补充工料机库
  275. let componentIdListCpt = [];
  276. let componentConsume = {};
  277. // 整理数据
  278. for (let component of libGljData.component) {
  279. if (component.isStd) {
  280. componentIdListStd.push(component.ID);
  281. } else {
  282. componentIdListCpt.push(component.ID);
  283. }
  284. let isStdFlag = component.isStd ? 'std' : 'cpt';
  285. //对于补充人材机的组成物,不会有多单价的情况
  286. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  287. }
  288. // 查找标准库数据
  289. let condition = {};
  290. let componentStdGljData = [];
  291. if (componentIdListStd.length > 0) {
  292. let gljListModel = new STDGLJLibGLJListModel();
  293. condition = {ID: {$in: componentIdListStd}};
  294. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  295. }
  296. // 查找补充库数据
  297. let componentCptGljData = [];
  298. if (componentIdListCpt.length > 0) {
  299. condition = {ID: {$in: componentIdListCpt}};
  300. componentCptGljData = await complementaryGljModel.find(condition);
  301. }
  302. if (componentCptGljData === null && componentStdGljData === null) {
  303. return result;
  304. }
  305. // 整理数据
  306. if (componentStdGljData.length > 0) {
  307. componentStdGljData = JSON.stringify(componentStdGljData);
  308. componentStdGljData = JSON.parse(componentStdGljData);
  309. for(let gljData of componentStdGljData) {
  310. gljData.connectCode = libGljData.code;
  311. gljData.consumption = componentConsume[gljData.ID + '_std'];
  312. gljData.from='std';
  313. result.push(gljData);
  314. }
  315. }
  316. if (componentCptGljData.length > 0) {
  317. componentCptGljData = JSON.stringify(componentCptGljData);
  318. componentCptGljData = JSON.parse(componentCptGljData);
  319. for(let gljData of componentCptGljData) {
  320. gljData.connectCode = libGljData.code;
  321. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  322. gljData.from='cpt';
  323. result.push(gljData);
  324. }
  325. }
  326. return result;
  327. }
  328. async getMixedTree(gljLibId, userId, compilationId){
  329. let rst = {std: [], comple: []};
  330. if (gljLibId) {
  331. rst.std = await gljClassModel.find({repositoryId: gljLibId}).lean();
  332. }
  333. if (userId && compilationId) {
  334. rst.comple = await compleClassModel.find({userId: userId, compilationId: compilationId}).lean();
  335. }
  336. return rst;
  337. }
  338. }
  339. module.exports = GljDao;