gljModel.js 29 KB

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