analysis_excel.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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) { continue; }
  269. if (!node.pos || node.pos.length === 0) { continue; }
  270. node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  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. }
  277. }
  278. }
  279. class AnalysisExcelTree {
  280. /**
  281. * 构造函数
  282. */
  283. constructor(ctx) {
  284. this.ctx = ctx;
  285. this.colsDef = null;
  286. this.colHeaderMatch = {
  287. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  288. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  289. pos: {value: ['计量单元'], type: colDefineType.match},
  290. name: {value: ['名称'], type: colDefineType.match},
  291. unit: {value: ['单位'], type: colDefineType.match},
  292. quantity: {value: ['清单数量'], type: colDefineType.match},
  293. dgn_qty1: {value: ['设计数量1'], type: colDefineType.match},
  294. dgn_qty2: {value: ['设计数量2'], type: colDefineType.match},
  295. unit_price: {value: ['单价'], type: colDefineType.match},
  296. drawing_code: {value: ['图号'], type: colDefineType.match},
  297. memo: {value: ['备注'], type: colDefineType.match},
  298. };
  299. }
  300. /**
  301. * 读取项目节节点
  302. * @param {Array} row - excel行数据
  303. * @returns {*}
  304. * @private
  305. */
  306. _loadXmjNode(row) {
  307. const node = {};
  308. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  309. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  310. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  311. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  312. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  313. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  314. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  315. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  316. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  317. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  318. if (node.quantity && node.unit_price) {
  319. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  320. } else {
  321. node.total_price = null;
  322. }
  323. return this.cacheTree.addXmjNode(node);
  324. }
  325. /**
  326. * 读取工程量清单数据
  327. * @param {Array} row - excel行数据
  328. * @returns {*}
  329. * @private
  330. */
  331. _loadGclNode(row) {
  332. const node = {};
  333. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  334. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  335. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  336. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  337. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  338. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  339. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  340. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  341. if (node.quantity && node.unit_price) {
  342. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  343. } else {
  344. node.total_price = null;
  345. }
  346. return this.cacheTree.addGclNode(node);
  347. }
  348. /**
  349. * 读取部位明细数据
  350. * @param {Array} row - excel行数据
  351. * @returns {*}
  352. * @private
  353. */
  354. _loadPos(row) {
  355. const pos = {};
  356. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  357. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  358. pos.sgfh_qty = pos.quantity;
  359. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  360. return this.cacheTree.addPos(pos);
  361. }
  362. /**
  363. * 读取数据行
  364. * @param {Array} row - excel数据行
  365. * @param {Number} index - 行索引号
  366. */
  367. loadRowData(row, index) {
  368. let result;
  369. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  370. if (row[this.colsDef.code]) {
  371. // 第三部分(编号为'3')及之后的数据不读取
  372. if (row[this.colsDef.code] === '3') {
  373. this.loadEnd = true;
  374. return;
  375. }
  376. result = this._loadXmjNode(row)
  377. } else if (row[this.colsDef.pos]) {
  378. result = this._loadPos(row);
  379. } else {
  380. result = this._loadGclNode(row);
  381. }
  382. // 读取失败则写入错误数据 todo 返回前端告知用户?
  383. if (!result) {
  384. this.errorData.push({
  385. serialNo: index,
  386. data: row,
  387. });
  388. }
  389. }
  390. /**
  391. * 读取表头并检查
  392. * @param {Number} row - Excel数据行
  393. */
  394. checkColHeader(row) {
  395. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  396. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  397. this.colsDef = colsDef;
  398. }
  399. }
  400. /**
  401. * 将excel清单 平面数据 解析为 树结构数据
  402. * @param {object} sheet - excel清单数据
  403. * @param {Array} tempData - 新建项目使用的清单模板
  404. * @returns {ImportBaseTree}
  405. */
  406. analysisData(sheet, tempData) {
  407. this.colsDef = null;
  408. this.cacheTree = new ImportBaseTree(tempData, this.ctx);
  409. this.errorData = [];
  410. this.loadEnd = false;
  411. for (const iRow in sheet.rows) {
  412. const row = sheet.rows[iRow];
  413. if (this.colsDef && !this.loadEnd) {
  414. this.loadRowData(row, iRow);
  415. } else {
  416. this.checkColHeader(row);
  417. }
  418. }
  419. this.cacheTree.resortFirstPartChildren();
  420. this.cacheTree.calculateLeafWithPos();
  421. return this.cacheTree;
  422. }
  423. }
  424. class ImportGclBaseTree {
  425. /**
  426. * 构造函数
  427. * @param {Array} tempData - 清单模板数据
  428. */
  429. constructor (ctx, parent, maxId, defaultData) {
  430. this.ctx = ctx;
  431. this.parent = parent;
  432. this.defaultData = defaultData;
  433. // 常量
  434. this.splitChar = '-';
  435. // 索引
  436. // 以code为索引
  437. this.codeNodes = {};
  438. this.items = [];
  439. // 缓存
  440. this.keyNodeId = maxId ? maxId + 1 : 1;
  441. this.blankParent = null;
  442. }
  443. /**
  444. * 根据 编号 查找 父项项目节
  445. * @param {String} code - 子项编号
  446. * @returns {*}
  447. */
  448. findParent(code) {
  449. const codePath = code.split(this.splitChar);
  450. if (codePath.length > 1) {
  451. codePath.splice(codePath.length - 1, 1);
  452. return this.codeNodes[codePath.join(this.splitChar)];
  453. }
  454. }
  455. /**
  456. * 添加 树节点 并完善该节点的树结构
  457. * @param {Object} node - 添加节点
  458. * @param {Object} parent - 父项
  459. * @returns {*}
  460. */
  461. addNodeWithParent(node, parent) {
  462. parent = parent ? parent : this.parent;
  463. if (!parent.children) parent.children = [];
  464. node.id = this.ctx.app.uuid.v4();
  465. node.tender_id = this.ctx.tender.id;
  466. node.ledger_id = this.keyNodeId;
  467. this.keyNodeId += 1;
  468. node.ledger_pid = parent.ledger_id;
  469. node.level = parent.level + 1;
  470. node.order = parent.children.length + 1;
  471. node.full_path = parent.full_path + '-' + node.ledger_id;
  472. parent.children.push(node);
  473. node.children = [];
  474. if (this.defaultData) _.assignIn(node, this.defaultData);
  475. this.items.push(node);
  476. if (!_.isNil(node.b_code) && node.b_code !== '') {
  477. this.codeNodes[node.b_code] = node;
  478. } else {
  479. this.blankParent = node;
  480. }
  481. return node;
  482. }
  483. /**
  484. * 添加 项目节
  485. * @param {Object} node - 项目节
  486. * @returns {*}
  487. */
  488. addNode(node) {
  489. if (_.isNil(node.b_code) || node.b_code === '') {
  490. return this.addNodeWithParent(node, null);
  491. } else if (node.b_code.split(this.splitChar).length > 1) {
  492. const parent = this.findParent(node.b_code);
  493. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  494. } else {
  495. return this.addNodeWithParent(node, this.blankParent);
  496. }
  497. }
  498. }
  499. class AnalysisGclExcelTree {
  500. /**
  501. * 构造函数
  502. */
  503. constructor(ctx) {
  504. this.ctx = ctx;
  505. this.colsDef = null;
  506. this.colHeaderMatch = {
  507. b_code: {value: ['编号', '清单编号', '子目号', '子目编号', '清单号'], type: colDefineType.match},
  508. name: {value: ['名称', '清单名称', '子目名称'], type: colDefineType.match},
  509. unit: {value: ['单位'], type: colDefineType.pos},
  510. quantity: {value: ['数量'], type: colDefineType.pos}, // 施工图复核数量
  511. unit_price: {value: ['单价'], type: colDefineType.pos},
  512. };
  513. }
  514. /**
  515. * 读取表头并检查
  516. * @param {Number} row - Excel数据行
  517. */
  518. checkColHeader(row) {
  519. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  520. if (colsDef.b_code) {
  521. this.colsDef = colsDef;
  522. }
  523. }
  524. loadRowData(row) {
  525. const node = {};
  526. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  527. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  528. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  529. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  530. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  531. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  532. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  533. if (node.quantity && node.unit_price) {
  534. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  535. } else {
  536. node.total_price = null;
  537. }
  538. return this.cacheTree.addNode(node);
  539. }
  540. /**
  541. * 将excel清单 平面数据 解析为 树结构数据
  542. * @param {object} sheet - excel清单数据
  543. * @param {Array} parentId - 导入至的节点id
  544. * @returns {ImportBaseTree}
  545. */
  546. analysisData(sheet, parent, maxId, defaultData) {
  547. try {
  548. this.colsDef = null;
  549. this.cacheTree = new ImportGclBaseTree(this.ctx, parent, maxId, defaultData);
  550. this.errorData = [];
  551. this.loadEnd = false;
  552. // 识别表头导入
  553. for (const iRow in sheet.rows) {
  554. const row = sheet.rows[iRow];
  555. if (this.colsDef && !this.loadEnd) {
  556. const result = this.loadRowData(row);
  557. // 读取失败则写入错误数据 todo 返回前端告知用户?
  558. if (!result) {
  559. this.errorData.push({
  560. serialNo: iRow,
  561. data: row,
  562. });
  563. }
  564. } else {
  565. this.checkColHeader(row);
  566. }
  567. }
  568. // 固定列导入
  569. // this.colsDef = {
  570. // b_code: 0,
  571. // name: 1,
  572. // unit: 2,
  573. // quantity: 3,
  574. // unit_price: 4
  575. // };
  576. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  577. // const row = sheet.rows[iRow];
  578. // const result = this.loadRowData(row);
  579. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  580. // if (!result) {
  581. // this.errorData.push({
  582. // serialNo: iRow,
  583. // data: row,
  584. // });
  585. // }
  586. // }
  587. return this.cacheTree;
  588. } catch(err) {
  589. this.ctx.helper.log(err);
  590. }
  591. }
  592. }
  593. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree };