analysis_excel.js 21 KB

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