gljModel.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 rationMapModel = mongoose.model('std_ration_lib_map');
  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 = gljLib.rationLibs.map(lib => lib.ID);
  21. const rationLibs = await rationMapModel.find({ID: {$in: rationLibIds}}, '-_id ID dispName');
  22. const rationLibNameMapping = {};
  23. rationLibs.forEach(item => {
  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. async getGljTreeSync(gljLibId) {
  45. return await gljClassModel.find({repositoryId: gljLibId});
  46. }
  47. getGljTypes (gljLibId, callback){
  48. gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false}, {deleted: false} ]},
  49. '-_id', {lean: true}, function(err,data){
  50. if(err) callback("获取工料机类型错误!",false)
  51. else {
  52. callback(0, data);
  53. }
  54. });
  55. }
  56. _exist(data, attr){
  57. return data && data[attr] !== 'undefined' && data[attr];
  58. }
  59. sortToNumber(datas){
  60. for(let i = 0, len = datas.length; i < len; i++){
  61. let data = datas[i];
  62. if(this._exist(data, 'basePrice')){
  63. data['basePrice'] = parseFloat(data['basePrice']);
  64. }
  65. if(this._exist(data, 'component')){
  66. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  67. let comGljObj = data['component'][j];
  68. if(this._exist(comGljObj, 'consumeAmt')){
  69. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. async getGljItemsSync(gljLibId) {
  76. return await gljModel.find({repositoryId: gljLibId}, '-_id', {lean: true});
  77. }
  78. async getGljItemsByRep(repositoryId,callback = null){
  79. try {
  80. let rst = await gljModel.find({repositoryId: repositoryId}).lean();
  81. // test-- 删除重复编码数据
  82. /*const map = {};
  83. rst.forEach(glj => {
  84. if (glj.code === '016100400') {
  85. console.log(glj);
  86. }
  87. const obj = {code: glj.code, ID: glj.ID};
  88. if (!map[glj.code]) {
  89. map[glj.code] = [obj];
  90. } else {
  91. map[glj.code].push(obj);
  92. }
  93. });
  94. const IDs = [];
  95. for (let code in map) {
  96. if (map[code].length > 1) {
  97. map[code].sort((a, b) => a.ID - b.ID);
  98. console.log(map[code]);
  99. let hasUsed = false;
  100. const removeIDs = [];
  101. const tempIDs = [];
  102. for (let i = 0; i < map[code].length; i++) {
  103. const glj = map[code][i];
  104. if (i !== 0) {
  105. tempIDs.push(glj.ID);
  106. }
  107. const isUsed = await rationModel.findOne({'rationGljList.gljId': glj.ID});
  108. if (isUsed) {
  109. hasUsed = true;
  110. } else {
  111. removeIDs.push(glj.ID);
  112. }
  113. }
  114. if (hasUsed) {
  115. IDs.push(...removeIDs);
  116. } else {
  117. IDs.push(...tempIDs);
  118. }
  119. //console.log(map[code]);
  120. }
  121. }
  122. if (IDs.length) {
  123. await gljModel.deleteMany({ID: {$in: IDs}});
  124. }
  125. console.log(IDs);*/
  126. // test--
  127. this.sortToNumber(rst);
  128. callback(0, rst);
  129. } catch (err) {
  130. callback(1, null);
  131. }
  132. }
  133. getGljItemByType (repositoryId, type, callback){
  134. let me = this;
  135. gljModel.find({"repositoryId": repositoryId, "gljType": type}, '-_id', {lean: true}, function(err,data){
  136. if(err) callback(true, "");
  137. else {
  138. me.sortToNumber(data);
  139. callback(false, data);
  140. }
  141. })
  142. };
  143. getGljItem (repositoryId, code, callback){
  144. let me = this;
  145. gljModel.find({"repositoryId": repositoryId, "code": code}, '-_id', {lean: true}, function(err,data){
  146. if(err) callback(true, "")
  147. else {
  148. me.sortToNumber(data);
  149. callback(false, data);
  150. }
  151. })
  152. };
  153. getGljItems (gljIds, callback){
  154. let me = this;
  155. gljModel.find({"ID": {"$in": gljIds}}, '-_id', {lean: true}, function(err,data){
  156. if(err) callback(true, "")
  157. else {
  158. me.sortToNumber(data);
  159. callback(false, data);
  160. }
  161. })
  162. };
  163. getGljItemsByCode (repositoryId, codes, callback){
  164. let me = this;
  165. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}}, '-_id', {lean: true}, function(err,data){
  166. if(err) callback(true, "");
  167. else {
  168. me.sortToNumber(data);
  169. callback(false, data);
  170. }
  171. })
  172. };
  173. updateComponent(libId, oprtor, updateArr, callback){
  174. let parallelFucs = [];
  175. for(let i = 0; i < updateArr.length; i++){
  176. parallelFucs.push((function(obj){
  177. return function (cb) {
  178. if(typeof obj.component === 'undefined'){
  179. obj.component = [];
  180. }
  181. gljModel.update({repositoryId: libId, ID: obj.ID}, obj, function (err, result) {
  182. if(err){
  183. cb(err);
  184. }
  185. else{
  186. cb(null, obj);
  187. }
  188. })
  189. }
  190. })(updateArr[i]));
  191. }
  192. parallelFucs.push((function () {
  193. return function (cb) {
  194. GljDao.updateOprArr({ID: libId}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  195. if(err){
  196. cb(err);
  197. }
  198. else{
  199. cb(null);
  200. }
  201. })
  202. }
  203. })());
  204. async.parallel(parallelFucs, function (err, result) {
  205. if(err){
  206. callback(err, '更新组成物错误!', null);
  207. }
  208. else{
  209. callback(null, '成功!', result);
  210. }
  211. });
  212. }
  213. mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  214. if (updateItems.length == 0 && rIds.length == 0) {
  215. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  216. }
  217. else if(rIds.length > 0 && updateItems.length > 0){
  218. async.parallel([
  219. function (cb) {
  220. GljDao.removeGljItems(repId, lastOpr, rIds, cb);
  221. },
  222. function (cb) {
  223. GljDao.updateGljItems(repId, lastOpr, updateItems, cb);
  224. }
  225. ], function (err) {
  226. if(err){
  227. callback(true, "Fail to update and delete", false)
  228. }
  229. else{
  230. callback(false, "Save successfully", false);
  231. }
  232. })
  233. }
  234. else if (rIds.length > 0 && updateItems.length === 0) {
  235. GljDao.removeGljItems(repId, lastOpr, rIds, callback);
  236. }
  237. else if(updateItems.length > 0 || addItems.length > 0){
  238. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  239. if (err) {
  240. callback(true, "Fail to update", false);
  241. } else {
  242. if (addItems && addItems.length > 0) {
  243. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  244. } else {
  245. callback(false, "Save successfully", results);
  246. }
  247. }
  248. });
  249. }
  250. }
  251. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  252. if (updateItems.length == 0 && rIds.length == 0) {
  253. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  254. } else if (rIds.length > 0) {
  255. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  256. });
  257. }else{
  258. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  259. if (err) {
  260. callback(true, "Fail to update", false);
  261. } else {
  262. if (addItems && addItems.length > 0) {
  263. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  264. } else {
  265. callback(false, "Save successfully", results);
  266. }
  267. }
  268. });
  269. }
  270. };*/
  271. static removeGljItems (repId, lastOpr, rIds, callback) {
  272. if (rIds && rIds.length > 0) {
  273. gljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  274. if (err) {
  275. callback(true, "Fail to remove", false);
  276. } else {
  277. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  278. if(err){
  279. callback(true, "Fail to update operator", false);
  280. }
  281. else{
  282. callback(false, "Remove successfully", docs);
  283. }
  284. });
  285. }
  286. })
  287. } else {
  288. callback(false, "No records were deleted!", null);
  289. }
  290. }
  291. static addGljItems (repId, lastOpr, items, callback) {
  292. if (items && items.length > 0) {
  293. const codes = [];
  294. items.forEach(item => codes.push(item.code));
  295. gljModel.find({repositoryId: repId, code: {$in: codes}}, '-_id code', {lean: true}, (err, codeData) => {
  296. if (err) {
  297. callback(true, '判断编码唯一性失败', false);
  298. return;
  299. }
  300. const insertData = [];
  301. const failCode = [];
  302. items.forEach(item => {
  303. const matchData = codeData.find(codeItem => codeItem.code === item.code);
  304. if (!matchData) {
  305. insertData.push(item);
  306. } else {
  307. failCode.push(item.code);
  308. }
  309. });
  310. if (!insertData.length) {
  311. callback(false, 'empty data', {insertData, failCode});
  312. return;
  313. }
  314. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, (counterErr, counterData) => {
  315. if (counterErr) {
  316. callback(true, '获取人材机ID失败', false);
  317. return;
  318. }
  319. const maxId = counterData.sequence_value;
  320. for (let i = 0; i < insertData.length; i++) {
  321. insertData[i].ID = (maxId - (insertData.length - 1) + i);
  322. insertData[i].repositoryId = repId;
  323. }
  324. const task = [];
  325. insertData.forEach(item => {
  326. task.push({
  327. insertOne: {document: item}
  328. });
  329. });
  330. gljModel.bulkWrite(task, (insertErr, rst) => {
  331. if (insertErr) {
  332. callback(true, '新增数据失败', false);
  333. return;
  334. }
  335. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  336. if(err){
  337. callback(true, "Fail to update Operator", false);
  338. } else{
  339. callback(false, "Add successfully", {insertData, failCode});
  340. }
  341. });
  342. });
  343. });
  344. });
  345. /*counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  346. const maxId = result.sequence_value;
  347. const arr = [];
  348. for (let i = 0; i < items.length; i++) {
  349. const obj = new gljModel(items[i]);
  350. obj.ID = (maxId - (items.length - 1) + i);
  351. obj.repositoryId = repId;
  352. arr.push(obj);
  353. }
  354. gljModel.collection.insert(arr, null, function(err, docs){
  355. if (err) {
  356. callback(true, "Fail to add", false);
  357. } else {
  358. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  359. if(err){
  360. callback(true, "Fail to update Operator", false);
  361. }
  362. else{
  363. callback(false, "Add successfully", docs);
  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;