analysis_excel.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const _ = require('lodash');
  10. const aeUtils = {
  11. toNumber: function (value) {
  12. let num = _.toNumber(value);
  13. return _.isNaN(num) ? null : num;
  14. },
  15. checkColHeader: function (row, colHeaderMatch) {
  16. const colsDef = {};
  17. for (const iCol in row) {
  18. const text = row[iCol];
  19. for (const head in colHeaderMatch) {
  20. const match = colHeaderMatch[head];
  21. if (match.indexOf(text) >= 0) {
  22. colsDef[head] = iCol;
  23. }
  24. }
  25. }
  26. return colsDef;
  27. }
  28. };
  29. class ImportBaseTree {
  30. /**
  31. * 构造函数
  32. * @param {Array} tempData - 清单模板数据
  33. */
  34. constructor (tempData, ctx) {
  35. this.ctx = ctx;
  36. // 常量
  37. this.splitChar = '-';
  38. // 索引
  39. // 以code为索引
  40. this.codeNodes = {};
  41. this.items = [];
  42. this.roots = [];
  43. this.pos = [];
  44. this.tempData = [];
  45. // 缓存
  46. this.finalNode = null;
  47. this.finalPrecision = null;
  48. this.finalXmjNode = null;
  49. this.keyNodeId = 1;
  50. this._loadTemplateTree(tempData);
  51. }
  52. /**
  53. * 加载 清单模板
  54. * @param {Array} data - 模板数据
  55. * @private
  56. */
  57. _loadTemplateTree(data) {
  58. let loadCodeNodes = true;
  59. for (const node of data) {
  60. node.ledger_id = node.template_id;
  61. node.ledger_pid = node.pid;
  62. node.id = this.ctx.app.uuid.v4();
  63. delete node.pid;
  64. if (node.code && loadCodeNodes) {
  65. this.codeNodes[node.code] = node;
  66. }
  67. if (node.code === '3') {
  68. loadCodeNodes = false;
  69. }
  70. this.items.push(node);
  71. if (node.ledger_pid === -1) {
  72. this.roots.push(node);
  73. }
  74. if (node.ledger_id >= this.keyNodeId) {
  75. this.keyNodeId = node.ledger_id + 1;
  76. }
  77. this.tempData.push(node);
  78. }
  79. for (const node of this.items) {
  80. node.tender_id = this.ctx.tender.id;
  81. node.children = this.items.filter(function (i) {
  82. return i.ledger_pid === node.ledger_id;
  83. });
  84. }
  85. }
  86. /**
  87. * 根据 编号、名称 查找模板节点
  88. * @param {Object} node - 要查找的节点
  89. * @returns {*}
  90. */
  91. findTempData(node) {
  92. return this.tempData.find(function (td) {
  93. return td.code === node.code && td.name === node.name;
  94. });
  95. }
  96. /**
  97. * 根据 编号 查找 父项项目节
  98. * @param {String} code - 子项编号
  99. * @returns {*}
  100. */
  101. findXmjParent(code) {
  102. const codePath = code.split(this.splitChar);
  103. if (codePath.length > 1) {
  104. codePath.splice(codePath.length - 1, 1);
  105. return this.codeNodes[codePath.join(this.splitChar)];
  106. }
  107. }
  108. getPosterity(node) {
  109. let posterity = [].concat(node.children);
  110. for (const c of node.children) {
  111. posterity = posterity.concat(this.getPosterity(c));
  112. }
  113. return posterity;
  114. }
  115. findGclParent(code, xmj) {
  116. let parent;
  117. const codePath = code.split(this.splitChar);
  118. if (codePath.length > 1) {
  119. codePath.splice(codePath.length - 1, 1);
  120. const parentCode = codePath.join(this.splitChar);
  121. const posterity = this.getPosterity(xmj);
  122. parent = posterity.find(function (x) {
  123. return x.b_code === parentCode;
  124. })
  125. }
  126. return parent ? parent : xmj;
  127. }
  128. /**
  129. * 添加 树节点 并完善该节点的树结构
  130. * @param {Object} node - 添加节点
  131. * @param {Object} parent - 父项
  132. * @returns {*}
  133. */
  134. addNodeWithParent(node, parent) {
  135. node.id = this.ctx.app.uuid.v4();
  136. node.ledger_id = this.keyNodeId;
  137. this.keyNodeId += 1;
  138. node.ledger_pid = parent ? parent.ledger_id : -1;
  139. node.level = parent ? parent.level + 1 : 1;
  140. node.order = parent ? parent.children.length + 1 : this.roots.length + 1;
  141. node.full_path = parent ? parent.full_path + '-' + node.ledger_id : '' + node.ledger_id;
  142. if (parent) {
  143. parent.children.push(node);
  144. } else {
  145. this.roots.push(node);
  146. }
  147. this.items.push(node);
  148. this.codeNodes[node.code] = node;
  149. this.defineCacheData(node);
  150. return node;
  151. }
  152. /**
  153. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  154. * @param {Object} node - 当前添加的节点
  155. */
  156. defineCacheData(node) {
  157. this.finalNode = node;
  158. this.finalPrecision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  159. if (node.code) {
  160. this.finalXmjNode = node;
  161. }
  162. }
  163. /**
  164. * 添加 项目节
  165. * @param {Object} node - 项目节
  166. * @returns {*}
  167. */
  168. addXmjNode(node) {
  169. node.id = this.ctx.app.uuid.v4();
  170. node.tender_id = this.ctx.tender.id;
  171. node.children = [];
  172. if (node.code.split(this.splitChar).length > 1) {
  173. const temp = this.findTempData(node);
  174. if (temp) {
  175. this.defineCacheData(temp);
  176. return temp;
  177. } else {
  178. const parent = this.findXmjParent(node.code);
  179. return this.addNodeWithParent(node, parent);
  180. }
  181. } else {
  182. const n = this.codeNodes[node.code];
  183. if (!n) {
  184. return this.addNodeWithParent(node, null);
  185. } else {
  186. this.defineCacheData(n);
  187. return n;
  188. }
  189. }
  190. }
  191. /**
  192. * 添加 工程量清单
  193. * @param {Object} node - 工程量清单
  194. */
  195. addGclNode(node) {
  196. node.id = this.ctx.app.uuid.v4();
  197. node.tender_id = this.ctx.tender.id;
  198. node.pos = [];
  199. node.children = [];
  200. if (this.finalXmjNode) {
  201. const parent = node.b_code ? this.findGclParent(node.b_code, this.finalXmjNode) : this.finalXmjNode;
  202. return this.addNodeWithParent(node, parent);
  203. }
  204. }
  205. /**
  206. * 添加 部位明细
  207. * @param {object} pos - 部位明细
  208. * @returns {*}
  209. */
  210. addPos (pos){
  211. if (this.finalNode && this.finalNode.pos) {
  212. pos.id = this.ctx.app.uuid.v4();
  213. pos.lid = this.finalNode.id;
  214. pos.tid = this.ctx.tender.id;
  215. pos.add_stage = 0;
  216. pos.add_times = 0;
  217. pos.in_time = new Date();
  218. pos.porder = this.finalNode.pos.length + 1;
  219. pos.add_user = this.ctx.session.sessionUser.accountId;
  220. this.finalNode.pos.push(pos);
  221. this.pos.push(pos);
  222. pos.quantity = this.ctx.helper.round(pos.quantity, this.finalPrecision.value);
  223. return pos;
  224. }
  225. }
  226. /**
  227. * 第一部分的子节点,顺序重排
  228. */
  229. resortFirstPartChildren () {
  230. const splitChar = this.splitChar;
  231. const firstPart = this.roots[0];
  232. firstPart.children.sort(function (a, b) {
  233. if (a.code === '') {
  234. return 1;
  235. } else if (b.code === '') {
  236. return -1;
  237. }
  238. const codeA = a.code.split(splitChar);
  239. const numA = _.toNumber(codeA[codeA.length -1]);
  240. const codeB = b.code.split(splitChar);
  241. const numB = _.toNumber(codeB[codeB.length -1]);
  242. return numA - numB;
  243. });
  244. for (const [i, c] of firstPart.children.entries()) {
  245. c.order = i + 1;
  246. }
  247. }
  248. calculateLeafWithPos () {
  249. for (const node of this.items) {
  250. if (node.children && node.children.length > 0) { continue; }
  251. if (!node.pos || node.pos.length === 0) { continue; }
  252. node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  253. if (node.quantity && node.unit_price) {
  254. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  255. } else {
  256. node.total_price = null;
  257. }
  258. }
  259. }
  260. }
  261. class AnalysisExcelTree {
  262. /**
  263. * 构造函数
  264. */
  265. constructor(ctx) {
  266. this.ctx = ctx;
  267. this.colsDef = null;
  268. this.colHeaderMatch = {
  269. code: ['项目节编号', '预算项目节'],
  270. b_code: ['清单子目号', '清单编号', '子目号'],
  271. pos: ['部位明细'],
  272. name: ['名称'],
  273. unit: ['单位'],
  274. quantity: ['清单数量'],
  275. dgn_qty1: ['设计数量1'],
  276. dgn_qty2: ['设计数量2'],
  277. unit_price: ['单价'],
  278. drawing_code: ['图号'],
  279. memo: ['备注'],
  280. };
  281. }
  282. /**
  283. * 读取项目节节点
  284. * @param {Array} row - excel行数据
  285. * @returns {*}
  286. * @private
  287. */
  288. _loadXmjNode(row) {
  289. const node = {};
  290. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  291. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  292. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  293. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  294. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  295. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  296. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  297. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  298. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  299. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  300. if (node.quantity && node.unit_price) {
  301. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  302. } else {
  303. node.total_price = null;
  304. }
  305. return this.cacheTree.addXmjNode(node);
  306. }
  307. /**
  308. * 读取工程量清单数据
  309. * @param {Array} row - excel行数据
  310. * @returns {*}
  311. * @private
  312. */
  313. _loadGclNode(row) {
  314. const node = {};
  315. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  316. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  317. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  318. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  319. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  320. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  321. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  322. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  323. if (node.quantity && node.unit_price) {
  324. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  325. } else {
  326. node.total_price = null;
  327. }
  328. return this.cacheTree.addGclNode(node);
  329. }
  330. /**
  331. * 读取部位明细数据
  332. * @param {Array} row - excel行数据
  333. * @returns {*}
  334. * @private
  335. */
  336. _loadPos(row) {
  337. const pos = {};
  338. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  339. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  340. pos.sgfh_qty = pos.quantity;
  341. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  342. return this.cacheTree.addPos(pos);
  343. }
  344. /**
  345. * 读取数据行
  346. * @param {Array} row - excel数据行
  347. * @param {Number} index - 行索引号
  348. */
  349. loadRowData(row, index) {
  350. let result;
  351. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  352. if (row[this.colsDef.code]) {
  353. // 第三部分(编号为'3')及之后的数据不读取
  354. if (row[this.colsDef.code] === '3') {
  355. this.loadEnd = true;
  356. return;
  357. }
  358. result = this._loadXmjNode(row)
  359. } else if (row[this.colsDef.pos]) {
  360. result = this._loadPos(row);
  361. } else {
  362. result = this._loadGclNode(row);
  363. }
  364. // 读取失败则写入错误数据 todo 返回前端告知用户?
  365. if (!result) {
  366. this.errorData.push({
  367. serialNo: index,
  368. data: row,
  369. });
  370. }
  371. }
  372. /**
  373. * 读取表头并检查
  374. * @param {Number} row - Excel数据行
  375. */
  376. checkColHeader(row) {
  377. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  378. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  379. this.colsDef = colsDef;
  380. }
  381. }
  382. /**
  383. * 将excel清单 平面数据 解析为 树结构数据
  384. * @param {object} sheet - excel清单数据
  385. * @param {Array} tempData - 新建项目使用的清单模板
  386. * @returns {ImportBaseTree}
  387. */
  388. analysisData(sheet, tempData) {
  389. this.colsDef = null;
  390. this.cacheTree = new ImportBaseTree(tempData, this.ctx);
  391. this.errorData = [];
  392. this.loadEnd = false;
  393. for (const iRow in sheet.rows) {
  394. const row = sheet.rows[iRow];
  395. if (this.colsDef && !this.loadEnd) {
  396. this.loadRowData(row, iRow);
  397. } else {
  398. this.checkColHeader(row);
  399. }
  400. }
  401. this.cacheTree.resortFirstPartChildren();
  402. this.cacheTree.calculateLeafWithPos();
  403. return this.cacheTree;
  404. }
  405. }
  406. class ImportGclBaseTree {
  407. /**
  408. * 构造函数
  409. * @param {Array} tempData - 清单模板数据
  410. */
  411. constructor (ctx, parent, maxId, defaultData) {
  412. this.ctx = ctx;
  413. this.parent = parent;
  414. this.defaultData = defaultData;
  415. // 常量
  416. this.splitChar = '-';
  417. // 索引
  418. // 以code为索引
  419. this.codeNodes = {};
  420. this.items = [];
  421. // 缓存
  422. this.keyNodeId = maxId ? maxId + 1 : 1;
  423. this.blankParent = null;
  424. }
  425. /**
  426. * 根据 编号 查找 父项项目节
  427. * @param {String} code - 子项编号
  428. * @returns {*}
  429. */
  430. findParent(code) {
  431. const codePath = code.split(this.splitChar);
  432. if (codePath.length > 1) {
  433. codePath.splice(codePath.length - 1, 1);
  434. return this.codeNodes[codePath.join(this.splitChar)];
  435. }
  436. }
  437. /**
  438. * 添加 树节点 并完善该节点的树结构
  439. * @param {Object} node - 添加节点
  440. * @param {Object} parent - 父项
  441. * @returns {*}
  442. */
  443. addNodeWithParent(node, parent) {
  444. parent = parent ? parent : this.parent;
  445. if (!parent.children) parent.children = [];
  446. node.id = this.ctx.app.uuid.v4();
  447. node.tender_id = this.ctx.tender.id;
  448. node.ledger_id = this.keyNodeId;
  449. this.keyNodeId += 1;
  450. node.ledger_pid = parent.ledger_id;
  451. node.level = parent.level + 1;
  452. node.order = parent.children.length + 1;
  453. node.full_path = parent.full_path + '-' + node.ledger_id;
  454. parent.children.push(node);
  455. node.children = [];
  456. if (this.defaultData) _.assignIn(node, this.defaultData);
  457. this.items.push(node);
  458. if (!_.isNil(node.b_code) && node.b_code !== '') {
  459. this.codeNodes[node.b_code] = node;
  460. } else {
  461. this.blankParent = node;
  462. }
  463. return node;
  464. }
  465. /**
  466. * 添加 项目节
  467. * @param {Object} node - 项目节
  468. * @returns {*}
  469. */
  470. addNode(node) {
  471. if (_.isNil(node.b_code) || node.b_code === '') {
  472. return this.addNodeWithParent(node, null);
  473. } else if (node.b_code.split(this.splitChar).length > 1) {
  474. const parent = this.findParent(node.b_code);
  475. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  476. } else {
  477. return this.addNodeWithParent(node, this.blankParent);
  478. }
  479. }
  480. }
  481. class AnalysisGclExcelTree {
  482. /**
  483. * 构造函数
  484. */
  485. constructor(ctx) {
  486. this.ctx = ctx;
  487. this.colsDef = null;
  488. this.colHeaderMatch = {
  489. b_code: ['编号', '清单编号', '子目号', '子目编号', '清单号'],
  490. name: ['名称', '清单名称', '子目名称'],
  491. unit: ['单位'],
  492. quantity: ['清单数量'], // 施工图复核数量
  493. unit_price: ['单价'],
  494. };
  495. }
  496. /**
  497. * 读取表头并检查
  498. * @param {Number} row - Excel数据行
  499. */
  500. checkColHeader(row) {
  501. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  502. if (colsDef.b_code) {
  503. this.colsDef = colsDef;
  504. }
  505. }
  506. loadRowData(row) {
  507. const node = {};
  508. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  509. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  510. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  511. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  512. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  513. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  514. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  515. if (node.quantity && node.unit_price) {
  516. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  517. } else {
  518. node.total_price = null;
  519. }
  520. return this.cacheTree.addNode(node);
  521. }
  522. /**
  523. * 将excel清单 平面数据 解析为 树结构数据
  524. * @param {object} sheet - excel清单数据
  525. * @param {Array} parentId - 导入至的节点id
  526. * @returns {ImportBaseTree}
  527. */
  528. analysisData(sheet, parent, maxId, defaultData) {
  529. try {
  530. this.colsDef = null;
  531. this.cacheTree = new ImportGclBaseTree(this.ctx, parent, maxId, defaultData);
  532. this.errorData = [];
  533. this.loadEnd = false;
  534. // 识别表头导入
  535. for (const iRow in sheet.rows) {
  536. const row = sheet.rows[iRow];
  537. if (this.colsDef && !this.loadEnd) {
  538. const result = this.loadRowData(row);
  539. // 读取失败则写入错误数据 todo 返回前端告知用户?
  540. if (!result) {
  541. this.errorData.push({
  542. serialNo: iRow,
  543. data: row,
  544. });
  545. }
  546. } else {
  547. this.checkColHeader(row);
  548. }
  549. }
  550. // 固定列导入
  551. // this.colsDef = {
  552. // b_code: 0,
  553. // name: 1,
  554. // unit: 2,
  555. // quantity: 3,
  556. // unit_price: 4
  557. // };
  558. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  559. // const row = sheet.rows[iRow];
  560. // const result = this.loadRowData(row);
  561. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  562. // if (!result) {
  563. // this.errorData.push({
  564. // serialNo: iRow,
  565. // data: row,
  566. // });
  567. // }
  568. // }
  569. return this.cacheTree;
  570. } catch(err) {
  571. this.ctx.helper.log(err);
  572. }
  573. }
  574. }
  575. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree };