gljModel.js 29 KB

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