analysis_excel.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  312. return this.cacheTree.addPos(pos);
  313. }
  314. /**
  315. * 读取数据行
  316. * @param {Array} row - excel数据行
  317. * @param {Number} index - 行索引号
  318. */
  319. loadRowData(row, index) {
  320. let result;
  321. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  322. if (row[this.colsDef.code]) {
  323. // 第三部分(编号为'3')及之后的数据不读取
  324. if (row[this.colsDef.code] === '3') {
  325. this.loadEnd = true;
  326. return;
  327. }
  328. result = this._loadXmjNode(row)
  329. } else if (row[this.colsDef.pos]) {
  330. result = this._loadPos(row);
  331. } else {
  332. result = this._loadGclNode(row);
  333. }
  334. // 读取失败则写入错误数据 todo 返回前端告知用户?
  335. if (!result) {
  336. this.errorData.push({
  337. serialNo: index,
  338. data: row,
  339. });
  340. }
  341. }
  342. /**
  343. * 读取表头并检查
  344. * @param {Number} row - Excel数据行
  345. */
  346. checkColHeader(row) {
  347. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  348. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  349. this.colsDef = colsDef;
  350. }
  351. }
  352. /**
  353. * 将excel清单 平面数据 解析为 树结构数据
  354. * @param {object} sheet - excel清单数据
  355. * @param {Array} tempData - 新建项目使用的清单模板
  356. * @returns {ImportBaseTree}
  357. */
  358. analysisData(sheet, tempData) {
  359. this.colsDef = null;
  360. this.cacheTree = new ImportBaseTree(tempData, this.ctx);
  361. this.errorData = [];
  362. this.loadEnd = false;
  363. for (const iRow in sheet.rows) {
  364. const row = sheet.rows[iRow];
  365. if (this.colsDef && !this.loadEnd) {
  366. this.loadRowData(row, iRow);
  367. } else {
  368. this.checkColHeader(row);
  369. }
  370. }
  371. this.cacheTree.resortFirstPartChildren();
  372. this.cacheTree.calculateLeafWithPos();
  373. return this.cacheTree;
  374. }
  375. }
  376. class ImportGclBaseTree {
  377. /**
  378. * 构造函数
  379. * @param {Array} tempData - 清单模板数据
  380. */
  381. constructor (ctx, parent, maxId, defaultData) {
  382. this.ctx = ctx;
  383. this.parent = parent;
  384. this.defaultData = defaultData;
  385. // 常量
  386. this.splitChar = '-';
  387. // 索引
  388. // 以code为索引
  389. this.codeNodes = {};
  390. this.items = [];
  391. // 缓存
  392. this.keyNodeId = maxId ? maxId + 1 : 1;
  393. this.blankParent = null;
  394. }
  395. /**
  396. * 根据 编号 查找 父项项目节
  397. * @param {String} code - 子项编号
  398. * @returns {*}
  399. */
  400. findParent(code) {
  401. const codePath = code.split(this.splitChar);
  402. if (codePath.length > 1) {
  403. codePath.splice(codePath.length - 1, 1);
  404. return this.codeNodes[codePath.join(this.splitChar)];
  405. }
  406. }
  407. /**
  408. * 添加 树节点 并完善该节点的树结构
  409. * @param {Object} node - 添加节点
  410. * @param {Object} parent - 父项
  411. * @returns {*}
  412. */
  413. addNodeWithParent(node, parent) {
  414. parent = parent ? parent : this.parent;
  415. if (!parent.children) parent.children = [];
  416. node.id = this.ctx.app.uuid.v4();
  417. node.tender_id = this.ctx.tender.id;
  418. node.ledger_id = this.keyNodeId;
  419. this.keyNodeId += 1;
  420. node.ledger_pid = parent.ledger_id;
  421. node.level = parent.level + 1;
  422. node.order = parent.children.length + 1;
  423. node.full_path = parent.full_path + '-' + node.ledger_id;
  424. parent.children.push(node);
  425. node.children = [];
  426. if (this.defaultData) _.assignIn(node, this.defaultData);
  427. this.items.push(node);
  428. if (!_.isNil(node.b_code) && node.b_code !== '') {
  429. this.codeNodes[node.b_code] = node;
  430. } else {
  431. this.blankParent = node;
  432. }
  433. return node;
  434. }
  435. /**
  436. * 添加 项目节
  437. * @param {Object} node - 项目节
  438. * @returns {*}
  439. */
  440. addNode(node) {
  441. if (_.isNil(node.b_code) || node.b_code === '') {
  442. return this.addNodeWithParent(node, null);
  443. } else if (node.b_code.split(this.splitChar).length > 1) {
  444. const parent = this.findParent(node.b_code);
  445. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  446. } else {
  447. return this.addNodeWithParent(node, this.blankParent);
  448. }
  449. }
  450. }
  451. class AnalysisGclExcelTree {
  452. /**
  453. * 构造函数
  454. */
  455. constructor(ctx) {
  456. this.ctx = ctx;
  457. this.colsDef = null;
  458. this.colHeaderMatch = {
  459. b_code: ['编号', '清单编号', '子目号'],
  460. name: ['名称'],
  461. unit: ['单位'],
  462. quantity: ['清单数量'], // 施工图复核数量
  463. unit_price: ['单价'],
  464. };
  465. }
  466. /**
  467. * 读取表头并检查
  468. * @param {Number} row - Excel数据行
  469. */
  470. checkColHeader(row) {
  471. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  472. if (colsDef.b_code) {
  473. this.colsDef = colsDef;
  474. }
  475. }
  476. loadRowData(row) {
  477. const node = {};
  478. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  479. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  480. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  481. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  482. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  483. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  484. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  485. if (node.quantity && node.unit_price) {
  486. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  487. } else {
  488. node.total_price = null;
  489. }
  490. return this.cacheTree.addNode(node);
  491. }
  492. /**
  493. * 将excel清单 平面数据 解析为 树结构数据
  494. * @param {object} sheet - excel清单数据
  495. * @param {Array} parentId - 导入至的节点id
  496. * @returns {ImportBaseTree}
  497. */
  498. analysisData(sheet, parent, maxId, defaultData) {
  499. try {
  500. this.colsDef = null;
  501. this.cacheTree = new ImportGclBaseTree(this.ctx, parent, maxId, defaultData);
  502. this.errorData = [];
  503. this.loadEnd = false;
  504. for (const iRow in sheet.rows) {
  505. const row = sheet.rows[iRow];
  506. if (this.colsDef && !this.loadEnd) {
  507. const result = this.loadRowData(row);
  508. // 读取失败则写入错误数据 todo 返回前端告知用户?
  509. if (!result) {
  510. this.errorData.push({
  511. serialNo: iRow,
  512. data: row,
  513. });
  514. }
  515. } else {
  516. this.checkColHeader(row);
  517. }
  518. }
  519. return this.cacheTree;
  520. } catch(err) {
  521. this.ctx.helper.log(err);
  522. }
  523. }
  524. }
  525. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree };