gljModel.js 29 KB

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