analysis_excel.js 19 KB

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