gljModel.js 30 KB

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