analysis_excel.js 19 KB

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