gljModel.js 27 KB

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