gljModel.js 13 KB

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