gljModel.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. * Created by Zhong on 2017/8/11.
  3. */
  4. const mongoose = require('mongoose');
  5. const gljMapModel = mongoose.model('std_glj_lib_gljList');
  6. const gljModel = mongoose.model('std_glj_lib_gljList');
  7. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  8. const gljClassTemplateModel = mongoose.model('std_glj_lib_gljClassTemplate');
  9. import {OprDao} from "./gljMapModel";
  10. import moment from "moment";
  11. import counter from "../../../public/counter/counter";
  12. import async from "async";
  13. class GljDao extends OprDao{
  14. getGljTypes (gljLibId, callback){
  15. gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false}, {deleted: false} ]},function(err,data){
  16. if(data.length) {
  17. callback(0, data);
  18. }
  19. else if(err) callback("获取工料机类型错误!",false)
  20. else {
  21. gljClassTemplateModel.find({'$or': [{isDeleted: null}, {isDeleted: false}, {deleted: false}]}, function (err, datas) {
  22. if(err){
  23. callback("获取工料机类型错误!", false);
  24. }
  25. else{
  26. let rst = [];
  27. async.each(datas, function (data, cb) {
  28. let newClassObj = {};
  29. newClassObj.ID = data.ID;
  30. newClassObj.ParentID = data.ParentID;
  31. newClassObj.NextSiblingID = data.NextSiblingID;
  32. newClassObj.Name = data.Name;
  33. newClassObj.repositoryId = gljLibId;
  34. newClassObj.deleted = false;
  35. gljClassModel.create(newClassObj, function(err){
  36. if(err)cb(err);
  37. else{
  38. rst.push(newClassObj);
  39. cb(null);
  40. }
  41. });
  42. }, function (err) {
  43. if(err) callback("新增工料机类型错误!", false);
  44. else callback(0, rst);
  45. });
  46. }
  47. });
  48. }
  49. })
  50. }
  51. _exist(data, attr){
  52. return data && data[attr] !== 'undefined' && data[attr];
  53. }
  54. sortToNumber(datas){
  55. for(let i = 0, len = datas.length; i < len; i++){
  56. let data = datas[i]._doc;
  57. if(this._exist(data, 'basePrice')){
  58. data['basePrice'] = parseFloat(data['basePrice']);
  59. }
  60. if(this._exist(data, 'component')){
  61. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  62. let comGljObj = data['component'][j]._doc;
  63. if(this._exist(comGljObj, 'consumeAmt')){
  64. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. async getGljItemsByRep(repositoryId,callback = null){
  71. /* let me = this;
  72. if (callback === null) {
  73. return gljModel.find({"repositoryId": repositoryId});
  74. } else {
  75. gljModel.find({"repositoryId": repositoryId},function(err,data){
  76. if(err) callback(true, "")
  77. else {
  78. me.sortToNumber(data);
  79. callback(false,data);
  80. }
  81. })
  82. }*/
  83. let me = this;
  84. let rst = [];
  85. //批量获取异步
  86. let functions = [];
  87. let count = await gljModel.find({repositoryId: repositoryId, $or: [{deleted: null}, {deleted: false}]}).count();
  88. let findCount = Math.ceil(count/500);
  89. for(let i = 0, len = findCount; i < len; i++){
  90. functions.push((function(flag) {
  91. return function (cb) {
  92. gljModel.find({repositoryId: repositoryId, deleted: null}, cb).skip(flag).sort({ID: 1}).limit(500);
  93. }
  94. })(i*500));
  95. }
  96. async.parallel(functions, function (err, results) {
  97. if(err){
  98. callback(err, null);
  99. }
  100. else{
  101. for(let stdGljs of results){
  102. rst = rst.concat(stdGljs);
  103. }
  104. me.sortToNumber(rst);
  105. callback(0, rst);
  106. }
  107. });
  108. }
  109. getGljItemByType (repositoryId, type, callback){
  110. let me = this;
  111. gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  112. if(err) callback(true, "");
  113. else {
  114. me.sortToNumber(data);
  115. callback(false, data);
  116. }
  117. })
  118. };
  119. getGljItem (repositoryId, code, callback){
  120. let me = this;
  121. gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  122. if(err) callback(true, "")
  123. else {
  124. me.sortToNumber(data);
  125. callback(false, data);
  126. }
  127. })
  128. };
  129. getGljItems (gljIds, callback){
  130. let me = this;
  131. gljModel.find({"ID": {"$in": gljIds}},function(err,data){
  132. if(err) callback(true, "")
  133. else {
  134. me.sortToNumber(data);
  135. callback(false, data);
  136. }
  137. })
  138. };
  139. getGljItemsByCode (repositoryId, codes, callback){
  140. let me = this;
  141. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  142. if(err) callback(true, "");
  143. else {
  144. me.sortToNumber(data);
  145. callback(false, data);
  146. }
  147. })
  148. };
  149. updateComponent(libId, oprtor, updateArr, callback){
  150. let parallelFucs = [];
  151. for(let i = 0; i < updateArr.length; i++){
  152. parallelFucs.push((function(obj){
  153. return function (cb) {
  154. if(typeof obj.component === 'undefined'){
  155. obj.component = [];
  156. }
  157. gljModel.update({repositoryId: libId, ID: obj.ID}, obj, function (err, result) {
  158. if(err){
  159. cb(err);
  160. }
  161. else{
  162. cb(null, obj);
  163. }
  164. })
  165. }
  166. })(updateArr[i]));
  167. }
  168. parallelFucs.push((function () {
  169. return function (cb) {
  170. GljDao.updateOprArr({ID: libId}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  171. if(err){
  172. cb(err);
  173. }
  174. else{
  175. cb(null);
  176. }
  177. })
  178. }
  179. })());
  180. async.parallel(parallelFucs, function (err, result) {
  181. if(err){
  182. callback(err, '更新组成物错误!', null);
  183. }
  184. else{
  185. callback(null, '成功!', result);
  186. }
  187. });
  188. }
  189. mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  190. if (updateItems.length == 0 && rIds.length == 0) {
  191. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  192. }
  193. else if(rIds.length > 0 && updateItems.length > 0){
  194. async.parallel([
  195. function (cb) {
  196. GljDao.removeGljItems(repId, lastOpr, rIds, cb);
  197. },
  198. function (cb) {
  199. GljDao.updateGljItems(repId, lastOpr, updateItems, cb);
  200. }
  201. ], function (err) {
  202. if(err){
  203. callback(true, "Fail to update and delete", false)
  204. }
  205. else{
  206. callback(false, "Save successfully", false);
  207. }
  208. })
  209. }
  210. else if (rIds.length > 0 && updateItems.length === 0) {
  211. GljDao.removeGljItems(repId, lastOpr, rIds, callback);
  212. }
  213. else if(updateItems.length > 0 || addItems.length > 0){
  214. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  215. if (err) {
  216. callback(true, "Fail to update", false);
  217. } else {
  218. if (addItems && addItems.length > 0) {
  219. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  220. } else {
  221. callback(false, "Save successfully", results);
  222. }
  223. }
  224. });
  225. }
  226. }
  227. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  228. if (updateItems.length == 0 && rIds.length == 0) {
  229. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  230. } else if (rIds.length > 0) {
  231. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  232. });
  233. }else{
  234. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  235. if (err) {
  236. callback(true, "Fail to update", false);
  237. } else {
  238. if (addItems && addItems.length > 0) {
  239. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  240. } else {
  241. callback(false, "Save successfully", results);
  242. }
  243. }
  244. });
  245. }
  246. };*/
  247. static removeGljItems (repId, lastOpr, rIds, callback) {
  248. if (rIds && rIds.length > 0) {
  249. gljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  250. if (err) {
  251. callback(true, "Fail to remove", false);
  252. } else {
  253. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  254. if(err){
  255. callback(true, "Fail to update operator", false);
  256. }
  257. else{
  258. callback(false, "Remove successfully", docs);
  259. }
  260. });
  261. }
  262. })
  263. } else {
  264. callback(false, "No records were deleted!", null);
  265. }
  266. }
  267. static addGljItems (repId, lastOpr, items, callback) {
  268. if (items && items.length > 0) {
  269. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  270. var maxId = result.sequence_value;
  271. var arr = [];
  272. for (var i = 0; i < items.length; i++) {
  273. var obj = new gljModel(items[i]);
  274. obj.ID = (maxId - (items.length - 1) + i);
  275. obj.repositoryId = repId;
  276. arr.push(obj);
  277. }
  278. gljModel.collection.insert(arr, null, function(err, docs){
  279. if (err) {
  280. callback(true, "Fail to add", false);
  281. } else {
  282. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  283. if(err){
  284. callback(true, "Fail to update Operator", false);
  285. }
  286. else{
  287. callback(false, "Add successfully", docs);
  288. }
  289. });
  290. }
  291. })
  292. });
  293. } else {
  294. callback(true, "No source", false);
  295. }
  296. }
  297. static updateGljItems(repId, lastOpr, items, callback) {
  298. var functions = [];
  299. for (var i=0; i < items.length; i++) {
  300. functions.push((function(doc) {
  301. return function(cb) {
  302. var filter = {};
  303. if (doc.ID) {
  304. filter.ID = doc.ID;
  305. } else {
  306. filter.repositoryId = repId;
  307. filter.code = doc.code;
  308. }
  309. gljModel.update(filter, doc, cb);
  310. };
  311. })(items[i]));
  312. }
  313. functions.push((function () {
  314. return function (cb) {
  315. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  316. if(err){
  317. cb(err);
  318. }
  319. else{
  320. cb(null);
  321. }
  322. })
  323. }
  324. })());
  325. async.parallel(functions, function(err, results) {
  326. callback(err, results);
  327. });
  328. }
  329. getRationGljIds(rationLibs, callback){
  330. }
  331. updateNodes (updateData, lastOpr, callback) {
  332. let functions = [];
  333. for (let i = 0, len = updateData.length; i < len; i++) {
  334. functions.push((function(doc) {
  335. return function(cb) {
  336. if(doc.updateType === 'update'){
  337. gljClassModel.update({repositoryId: doc.updateData.repositoryId, ID: doc.updateData.ID}, doc.updateData, function (err) {
  338. if(err){
  339. cb(err);
  340. }
  341. else {
  342. cb(null);
  343. }
  344. });
  345. }
  346. else if(doc.updateType === 'new'){
  347. gljClassModel.create(doc.updateData, function (err) {
  348. if(err){
  349. cb(err);
  350. }
  351. else {
  352. cb(null);
  353. }
  354. });
  355. }
  356. };
  357. })(updateData[i]));
  358. }
  359. if(updateData.length > 0){
  360. functions.push((function () {
  361. return function (cb) {
  362. GljDao.updateOprArr({ID: updateData[0].updateData.rationRepId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  363. if(err){
  364. cb(err);
  365. }
  366. else{
  367. cb(null);
  368. }
  369. })
  370. }
  371. })());
  372. }
  373. async.parallel(functions, function(err, results) {
  374. if(!err){
  375. err = 0;
  376. }
  377. callback(err, results);
  378. });
  379. }
  380. /* updateNodes (repId, lastOpr, nodes, callback) {
  381. var functions = [];
  382. for (var i=0; i < nodes.length; i++) {
  383. functions.push((function(doc) {
  384. return function(cb) {
  385. gljClassModel.update({ID: doc.ID}, doc, cb);
  386. };
  387. })(nodes[i]));
  388. }
  389. functions.push((function () {
  390. return function (cb) {
  391. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  392. if(err){
  393. cb(err);
  394. }
  395. else{
  396. cb(null);
  397. }
  398. })
  399. }
  400. })());
  401. async.parallel(functions, function(err, results) {
  402. callback(err, results);
  403. });
  404. }*/
  405. removeNodes (repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  406. var functions = [];
  407. if (preNodeId != -1) {
  408. functions.push((function(nodeId, nextId) {
  409. return function(cb) {
  410. gljClassModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  411. };
  412. })(preNodeId, preNodeNextId));
  413. }
  414. for (var i=0; i < nodeIds.length; i++) {
  415. functions.push((function(nodeId) {
  416. return function(cb) {
  417. gljClassModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  418. };
  419. })(nodeIds[i]));
  420. }
  421. functions.push((function () {
  422. return function (cb) {
  423. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  424. if(err){
  425. cb(err);
  426. }
  427. else{
  428. cb(null);
  429. }
  430. })
  431. }
  432. })());
  433. async.parallel(functions, function(err, results) {
  434. callback(err, results);
  435. });
  436. }
  437. createNewNode(repId, lastOpr, lastNodeId, nodeData, callback) {
  438. return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){
  439. nodeData.repositoryId = repId;
  440. nodeData.ID = result.sequence_value;
  441. var node = new gljModel(nodeData);
  442. async.parallel([
  443. function (cb) {
  444. node.save(function (err, result) {
  445. if (err) {
  446. cb("章节树ID错误!", false);
  447. } else {
  448. if (lastNodeId > 0) {
  449. gljClassModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  450. if (err) {
  451. cb("章节树ID错误!", false);
  452. } else {
  453. cb(false, result);
  454. }
  455. });
  456. } else cb(false, result);
  457. }
  458. });
  459. },
  460. function (cb) {
  461. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  462. if(err){
  463. cb(err);
  464. }
  465. else{
  466. cb(null);
  467. }
  468. })
  469. }
  470. ], function (err, result) {
  471. if(err){
  472. callback(true, "章节树错误!", false);
  473. }
  474. else{
  475. callback(false, '', result[0]);
  476. }
  477. })
  478. });
  479. }
  480. getGljItemsOccupied(repId, occupation, callback){
  481. gljModel.find({repositoryId: repId}, occupation, function (err, result) {
  482. if(err) callback(true, 'fail', null);
  483. else callback(false, 'sc', result);
  484. });
  485. }
  486. async getGljItemsByRepId(repositoryId){
  487. return gljModel.find({"repositoryId": repositoryId});
  488. }
  489. }
  490. export default GljDao;