gljModel.js 28 KB

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