gljModel.js 12 KB

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