gljModel.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. const codes = [];
  286. items.forEach(item => codes.push(item.code));
  287. gljModel.find({repositoryId: repId, code: {$in: codes}}, '-_id code', {lean: true}, (err, codeData) => {
  288. if (err) {
  289. callback(true, '判断编码唯一性失败', false);
  290. return;
  291. }
  292. const insertData = [];
  293. const failCode = [];
  294. items.forEach(item => {
  295. const matchData = codeData.find(codeItem => codeItem.code === item.code);
  296. if (!matchData) {
  297. insertData.push(item);
  298. } else {
  299. failCode.push(item.code);
  300. }
  301. });
  302. if (!insertData.length) {
  303. callback(false, 'empty data', {insertData, failCode});
  304. return;
  305. }
  306. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, (counterErr, counterData) => {
  307. if (counterErr) {
  308. callback(true, '获取人材机ID失败', false);
  309. return;
  310. }
  311. const maxId = counterData.sequence_value;
  312. for (let i = 0; i < insertData.length; i++) {
  313. insertData[i].ID = (maxId - (insertData.length - 1) + i);
  314. insertData[i].repositoryId = repId;
  315. }
  316. const task = [];
  317. insertData.forEach(item => {
  318. task.push({
  319. insertOne: {document: item}
  320. });
  321. });
  322. gljModel.bulkWrite(task, (insertErr, rst) => {
  323. if (insertErr) {
  324. callback(true, '新增数据失败', false);
  325. return;
  326. }
  327. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  328. if(err){
  329. callback(true, "Fail to update Operator", false);
  330. } else{
  331. callback(false, "Add successfully", {insertData, failCode});
  332. }
  333. });
  334. });
  335. });
  336. });
  337. } else {
  338. callback(true, "No source", false);
  339. }
  340. }
  341. static updateGljItems(repId, lastOpr, items, callback) {
  342. var functions = [];
  343. for (var i=0; i < items.length; i++) {
  344. functions.push((function(doc) {
  345. return function(cb) {
  346. var filter = {};
  347. if (doc.ID) {
  348. filter.ID = doc.ID;
  349. } else {
  350. filter.repositoryId = repId;
  351. filter.code = doc.code;
  352. }
  353. gljModel.update(filter, doc, cb);
  354. };
  355. })(items[i]));
  356. }
  357. functions.push((function () {
  358. return function (cb) {
  359. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  360. if(err){
  361. cb(err);
  362. }
  363. else{
  364. cb(null);
  365. }
  366. })
  367. }
  368. })());
  369. async.parallel(functions, function(err, results) {
  370. callback(err, results);
  371. });
  372. }
  373. getRationGljIds(rationLibs, callback){
  374. }
  375. updateNodes (updateData, lastOpr, callback) {
  376. let functions = [];
  377. for (let i = 0, len = updateData.length; i < len; i++) {
  378. functions.push((function(doc) {
  379. return function(cb) {
  380. if(doc.updateType === 'update' && !doc.updateData.deleted){
  381. gljClassModel.update({repositoryId: doc.updateData.repositoryId, ID: doc.updateData.ID}, doc.updateData, function (err) {
  382. if(err){
  383. cb(err);
  384. }
  385. else {
  386. cb(null);
  387. }
  388. });
  389. }
  390. else if(doc.updateType === 'update' && doc.updateData.deleted){
  391. gljClassModel.remove({repositoryId: doc.updateData.repositoryId, ID: doc.updateData.ID}, function (err) {
  392. if(err){
  393. cb(err);
  394. }
  395. else {
  396. gljModel.remove({repositoryId: doc.updateData.repositoryId, gljClass: doc.updateData.ID}, function (err) {
  397. if(err){
  398. cb(err);
  399. }
  400. else{
  401. cb(null);
  402. }
  403. });
  404. }
  405. });
  406. }
  407. else if(doc.updateType === 'new'){
  408. gljClassModel.create(doc.updateData, function (err) {
  409. if(err){
  410. cb(err);
  411. }
  412. else {
  413. cb(null);
  414. }
  415. });
  416. }
  417. };
  418. })(updateData[i]));
  419. }
  420. if(updateData.length > 0){
  421. functions.push((function () {
  422. return function (cb) {
  423. GljDao.updateOprArr({ID: updateData[0].updateData.rationRepId}, 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. }
  434. async.parallel(functions, function(err, results) {
  435. if(!err){
  436. err = 0;
  437. }
  438. callback(err, results);
  439. });
  440. }
  441. /* updateNodes (repId, lastOpr, nodes, callback) {
  442. var functions = [];
  443. for (var i=0; i < nodes.length; i++) {
  444. functions.push((function(doc) {
  445. return function(cb) {
  446. gljClassModel.update({ID: doc.ID}, doc, cb);
  447. };
  448. })(nodes[i]));
  449. }
  450. functions.push((function () {
  451. return function (cb) {
  452. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  453. if(err){
  454. cb(err);
  455. }
  456. else{
  457. cb(null);
  458. }
  459. })
  460. }
  461. })());
  462. async.parallel(functions, function(err, results) {
  463. callback(err, results);
  464. });
  465. }*/
  466. removeNodes (repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  467. var functions = [];
  468. if (preNodeId != -1) {
  469. functions.push((function(nodeId, nextId) {
  470. return function(cb) {
  471. gljClassModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  472. };
  473. })(preNodeId, preNodeNextId));
  474. }
  475. for (var i=0; i < nodeIds.length; i++) {
  476. functions.push((function(nodeId) {
  477. return function(cb) {
  478. gljClassModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  479. };
  480. })(nodeIds[i]));
  481. }
  482. functions.push((function () {
  483. return function (cb) {
  484. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  485. if(err){
  486. cb(err);
  487. }
  488. else{
  489. cb(null);
  490. }
  491. })
  492. }
  493. })());
  494. async.parallel(functions, function(err, results) {
  495. callback(err, results);
  496. });
  497. }
  498. createNewNode(repId, lastOpr, lastNodeId, nodeData, callback) {
  499. return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){
  500. nodeData.repositoryId = repId;
  501. nodeData.ID = result.sequence_value;
  502. var node = new gljModel(nodeData);
  503. async.parallel([
  504. function (cb) {
  505. node.save(function (err, result) {
  506. if (err) {
  507. cb("章节树ID错误!", false);
  508. } else {
  509. if (lastNodeId > 0) {
  510. gljClassModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  511. if (err) {
  512. cb("章节树ID错误!", false);
  513. } else {
  514. cb(false, result);
  515. }
  516. });
  517. } else cb(false, result);
  518. }
  519. });
  520. },
  521. function (cb) {
  522. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  523. if(err){
  524. cb(err);
  525. }
  526. else{
  527. cb(null);
  528. }
  529. })
  530. }
  531. ], function (err, result) {
  532. if(err){
  533. callback(true, "章节树错误!", false);
  534. }
  535. else{
  536. callback(false, '', result[0]);
  537. }
  538. })
  539. });
  540. }
  541. getGljItemsOccupied(repId, occupation, callback){
  542. gljModel.find({repositoryId: repId}, occupation, function (err, result) {
  543. if(err) callback(true, 'fail', null);
  544. else callback(false, 'sc', result);
  545. });
  546. }
  547. async getGljItemsByRepId(repositoryId, returnFields = ''){
  548. return gljModel.find({"repositoryId": repositoryId}, returnFields);
  549. }
  550. async batchUpdateGljPrice(gljLibId, sheetData){
  551. let gljLib = await gljMapModel.findOne({ID: gljLibId});
  552. if(!gljLib){
  553. throw '不存在此人材机库';
  554. }
  555. let compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(gljLib.compilationId)});
  556. if(!compilation){
  557. throw '不存在此费用定额';
  558. }
  559. let priceProperties = compilation.priceProperties ? compilation.priceProperties : [];
  560. //根据第一行数据,获取列下标与字段名映射
  561. let colMapping = {};
  562. for(let col = 0; col < sheetData[0].length; col++){
  563. let cData = sheetData[0][col];
  564. if(cData === '编码'){
  565. colMapping['code'] = col;
  566. }
  567. else {
  568. if(priceProperties.length === 0){
  569. if(cData === '定额价'){
  570. colMapping['basePrice'] = col;
  571. break;
  572. }
  573. }
  574. else {
  575. for(let priceProp of priceProperties){
  576. if(priceProp.price.dataName === cData){
  577. colMapping[priceProp.price.dataCode] = col;
  578. break;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. let colMappingKeys = Object.keys(colMapping);
  585. if(colMappingKeys.length < 2){
  586. throw 'excel数据不正确'
  587. }
  588. let updateBulk = [];
  589. //避免重复
  590. let updateCodes = [];
  591. //库中存在的人材机
  592. let dateA = Date.now();
  593. let existGljs = await gljModel.find({repositoryId: gljLibId}, '-_id code ID');
  594. let existMapping = {};
  595. for (let glj of existGljs) {
  596. existMapping[glj.code] = glj;
  597. }
  598. for(let row = 0; row < sheetData.length; row++){
  599. if(row === 0){
  600. continue;
  601. }
  602. let gljCode = sheetData[row][colMapping.code],
  603. existGlj = existMapping[gljCode];
  604. //更新多单价、不覆盖priceProperty字段,覆盖priceProperty下的子字段'priceProperty.x'
  605. if(gljCode && gljCode !== '' && !updateCodes.includes(gljCode) && existGlj){
  606. if(priceProperties.length > 0){
  607. for(let priceProp of priceProperties){
  608. let dataCode = priceProp.price.dataCode;
  609. let priceCellData = sheetData[row][colMapping[dataCode]];
  610. //Excel中没有这个单价则跳过
  611. if (!colMapping[dataCode]) {
  612. continue;
  613. }
  614. let updateSet = {};
  615. updateSet['priceProperty.' + dataCode] = priceCellData && !isNaN(priceCellData) ?
  616. scMathUtil.roundTo(parseFloat(priceCellData), -2) : 0;
  617. updateBulk.push({
  618. updateOne: {filter: {ID: existGlj.ID}, update: {$set: updateSet}}
  619. });
  620. }
  621. updateCodes.push(gljCode);
  622. }
  623. else {
  624. if(colMapping.basePrice){
  625. let priceCellData = sheetData[row][colMapping.basePrice];
  626. let basePrice = priceCellData && !isNaN(priceCellData) ?
  627. scMathUtil.roundTo(priceCellData, -2) : 0;
  628. updateCodes.push(gljCode);
  629. updateBulk.push({
  630. updateOne: {filter: {ID: existGlj.ID}, update: {$set: {basePrice: basePrice}}}
  631. });
  632. }
  633. }
  634. }
  635. }
  636. if(updateBulk.length > 0){
  637. while (updateBulk.length > 0) {
  638. let sliceBulk = updateBulk.splice(0, 1000);
  639. await gljModel.bulkWrite(sliceBulk);
  640. }
  641. }
  642. }
  643. async importComponents(gljLibId, sheetData) {
  644. const gljLib = await gljMapModel.findOne({ID: gljLibId});
  645. if (!gljLib) {
  646. throw '不存在此人材机库';
  647. }
  648. const compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(gljLib.compilationId)});
  649. if (!compilation) {
  650. throw '不存在此费用定额';
  651. }
  652. // 将所有人材机进行编码映射
  653. const allGLJs = await gljModel.find({repositoryId: gljLibId}, {ID: true, code: true}).lean();
  654. const codeMapping = {};
  655. allGLJs.forEach(glj => codeMapping[glj.code] = glj);
  656. // excel表格列号与字段的映射
  657. const colMapping = {
  658. // 材料编码
  659. code: 0,
  660. // 组成物编码
  661. componentCode: 1,
  662. // 组成物消耗量
  663. consumeAmt: 2
  664. };
  665. // 跳过列头
  666. for (let row = 1; row < sheetData.length; row++) {
  667. const rowData = sheetData[row];
  668. const code = rowData[colMapping.code];
  669. const componentCode = rowData[colMapping.componentCode];
  670. const consumeAmt = +rowData[colMapping.consumeAmt];
  671. const glj = codeMapping[code];
  672. const component = codeMapping[componentCode];
  673. if (!glj || !component) {
  674. continue;
  675. }
  676. if (!glj.component) {
  677. glj.component = [];
  678. }
  679. glj.component.push({
  680. ID: component.ID,
  681. consumeAmt: consumeAmt
  682. });
  683. }
  684. // 更新数据
  685. const tasks = [];
  686. allGLJs.filter(glj => glj.component && glj.component.length).forEach(glj => {
  687. tasks.push({
  688. updateOne: {
  689. filter: {
  690. ID: glj.ID
  691. },
  692. update: {$set: {component: glj.component}}
  693. }
  694. });
  695. });
  696. if (tasks.length) {
  697. await gljModel.bulkWrite(tasks);
  698. }
  699. }
  700. }
  701. export default GljDao;