gljModel.js 24 KB

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