analysis_excel.js 21 KB

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