gljModel.js 22 KB

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