gljModel.js 24 KB

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