gljModel.js 27 KB

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