analysis_excel.js 21 KB

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