analysis_excel.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. const mainReg = /^(GD*)?G?(\d\d)*\d$/i;
  47. const subReg = /^(GD*)?G?[A-Z]{2}(\d\d)+$/i;
  48. const gdXmjPartReg = /^(GD*)?G?/;
  49. const specCode106 = {
  50. code: ['102', '103', '104'],
  51. reg: /^(GD*)?G?106/i
  52. };
  53. const specCode109 = {
  54. code: ['102', '103', '104', '105', '106', '107', '108'],
  55. reg: /^(GD*)?G?109/i
  56. };
  57. class ImportBaseTree {
  58. /**
  59. * 构造函数
  60. * @param {Array} tempData - 清单模板数据
  61. */
  62. constructor (tempData, ctx) {
  63. this.ctx = ctx;
  64. // 常量
  65. this.splitChar = '-';
  66. // 索引
  67. // 以code为索引
  68. this.codeNodes = {};
  69. this.items = [];
  70. this.roots = [];
  71. this.pos = [];
  72. this.tempData = [];
  73. // 以id为索引
  74. this.nodes = {};
  75. // 缓存
  76. this.finalNode = null;
  77. this.finalPrecision = null;
  78. this.finalXmjNode = null;
  79. this.keyNodeId = 1;
  80. this._loadTemplateTree(tempData);
  81. }
  82. /**
  83. * 加载 清单模板
  84. * @param {Array} data - 模板数据
  85. * @private
  86. */
  87. _loadTemplateTree(data) {
  88. let loadCodeNodes = true;
  89. for (const node of data) {
  90. node.ledger_id = node.template_id;
  91. node.ledger_pid = node.pid;
  92. node.id = this.ctx.app.uuid.v4();
  93. delete node.pid;
  94. if (node.code && loadCodeNodes) {
  95. this.codeNodes[node.code] = node;
  96. }
  97. if (node.code === '3') {
  98. loadCodeNodes = false;
  99. }
  100. this.items.push(node);
  101. if (node.ledger_pid === -1) {
  102. this.roots.push(node);
  103. }
  104. if (node.ledger_id >= this.keyNodeId) {
  105. this.keyNodeId = node.ledger_id + 1;
  106. }
  107. this.tempData.push(node);
  108. this.nodes[node.ledger_id] = node;
  109. }
  110. for (const node of this.items) {
  111. node.tender_id = this.ctx.tender.id;
  112. node.children = this.items.filter(function (i) {
  113. return i.ledger_pid === node.ledger_id;
  114. });
  115. }
  116. }
  117. /**
  118. * 根据 编号、名称 查找模板节点
  119. * @param {Object} node - 要查找的节点
  120. * @returns {*}
  121. */
  122. findTempData(node) {
  123. return this.tempData.find(function (td) {
  124. return td.code === node.code && td.name === node.name;
  125. });
  126. }
  127. /**
  128. * 根据 编号 查找 父项项目节
  129. * @param {String} code - 子项编号
  130. * @returns {*}
  131. */
  132. findXmjParent(code) {
  133. const codePath = code.split(this.splitChar);
  134. if (codePath.length > 1) {
  135. codePath.splice(codePath.length - 1, 1);
  136. return this.codeNodes[codePath.join(this.splitChar)];
  137. }
  138. }
  139. getPosterity(node) {
  140. let posterity = [].concat(node.children);
  141. for (const c of node.children) {
  142. posterity = posterity.concat(this.getPosterity(c));
  143. }
  144. return posterity;
  145. }
  146. findGclParent(code, xmj) {
  147. let parent;
  148. const codePath = code.split(this.splitChar);
  149. if (codePath.length > 1) {
  150. codePath.splice(codePath.length - 1, 1);
  151. const parentCode = codePath.join(this.splitChar);
  152. const posterity = this.getPosterity(xmj);
  153. parent = posterity.find(function (x) {
  154. return x.b_code === parentCode;
  155. })
  156. }
  157. return parent ? parent : xmj;
  158. }
  159. /**
  160. * 添加 树节点 并完善该节点的树结构
  161. * @param {Object} node - 添加节点
  162. * @param {Object} parent - 父项
  163. * @returns {*}
  164. */
  165. addNodeWithParent(node, parent) {
  166. node.id = this.ctx.app.uuid.v4();
  167. node.ledger_id = this.keyNodeId;
  168. this.keyNodeId += 1;
  169. node.ledger_pid = parent ? parent.ledger_id : -1;
  170. node.level = parent ? parent.level + 1 : 1;
  171. node.order = parent ? parent.children.length + 1 : this.roots.length + 1;
  172. node.full_path = parent ? parent.full_path + '-' + node.ledger_id : '' + node.ledger_id;
  173. if (parent) {
  174. parent.children.push(node);
  175. } else {
  176. this.roots.push(node);
  177. }
  178. this.items.push(node);
  179. this.codeNodes[node.code] = node;
  180. this.defineCacheData(node);
  181. return node;
  182. }
  183. /**
  184. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  185. * @param {Object} node - 当前添加的节点
  186. */
  187. defineCacheData(node) {
  188. this.nodes[node.ledger_id] = node;
  189. this.finalNode = node;
  190. this.finalPrecision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  191. if (node.code) {
  192. this.finalXmjNode = node;
  193. }
  194. }
  195. /**
  196. * 添加 项目节
  197. * @param {Object} node - 项目节
  198. * @returns {*}
  199. */
  200. addXmjNode(node) {
  201. node.id = this.ctx.app.uuid.v4();
  202. node.tender_id = this.ctx.tender.id;
  203. node.children = [];
  204. if (node.code.split(this.splitChar).length > 1) {
  205. const temp = this.findTempData(node);
  206. if (temp) {
  207. this.defineCacheData(temp);
  208. return temp;
  209. } else {
  210. const parent = this.findXmjParent(node.code);
  211. return this.addNodeWithParent(node, parent);
  212. }
  213. } else {
  214. const n = this.codeNodes[node.code];
  215. if (!n) {
  216. return this.addNodeWithParent(node, null);
  217. } else {
  218. this.defineCacheData(n);
  219. return n;
  220. }
  221. }
  222. }
  223. /**
  224. * 添加 工程量清单
  225. * @param {Object} node - 工程量清单
  226. */
  227. addGclNode(node) {
  228. node.id = this.ctx.app.uuid.v4();
  229. node.tender_id = this.ctx.tender.id;
  230. node.pos = [];
  231. node.children = [];
  232. if (this.finalXmjNode) {
  233. const parent = node.b_code ? this.findGclParent(node.b_code, this.finalXmjNode) : this.finalXmjNode;
  234. return this.addNodeWithParent(node, parent);
  235. }
  236. }
  237. /**
  238. * 添加 部位明细
  239. * @param {object} pos - 部位明细
  240. * @returns {*}
  241. */
  242. addPos (pos){
  243. if (this.finalNode && this.finalNode.pos) {
  244. pos.id = this.ctx.app.uuid.v4();
  245. pos.lid = this.finalNode.id;
  246. pos.tid = this.ctx.tender.id;
  247. pos.add_stage = 0;
  248. pos.add_times = 0;
  249. pos.in_time = new Date();
  250. pos.porder = this.finalNode.pos.length + 1;
  251. pos.add_user = this.ctx.session.sessionUser.accountId;
  252. this.finalNode.pos.push(pos);
  253. this.pos.push(pos);
  254. pos.sgfh_qty = this.ctx.helper.round(pos.sgfh_qty, this.finalPrecision.value);
  255. pos.quantity = this.ctx.helper.round(pos.quantity, this.finalPrecision.value);
  256. return pos;
  257. }
  258. }
  259. /**
  260. * 第一部分的子节点,顺序重排
  261. */
  262. resortFirstPartChildren () {
  263. const splitChar = this.splitChar;
  264. const firstPart = this.roots[0];
  265. firstPart.children.sort(function (a, b) {
  266. if (a.code === '') {
  267. return 1;
  268. } else if (b.code === '') {
  269. return -1;
  270. }
  271. const codeA = a.code.split(splitChar);
  272. const numA = _.toNumber(codeA[codeA.length -1]);
  273. const codeB = b.code.split(splitChar);
  274. const numB = _.toNumber(codeB[codeB.length -1]);
  275. return numA - numB;
  276. });
  277. for (const [i, c] of firstPart.children.entries()) {
  278. c.order = i + 1;
  279. }
  280. }
  281. calculateLeafWithPos () {
  282. for (const node of this.items) {
  283. if (node.children && node.children.length > 0) {
  284. node.unit_price = null;
  285. node.quantity = null;
  286. node.total_price = null;
  287. }
  288. if (!node.pos || node.pos.length === 0) { continue; }
  289. node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  290. if (node.quantity && node.unit_price) {
  291. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  292. } else {
  293. node.total_price = null;
  294. }
  295. }
  296. }
  297. }
  298. class ImportStd18Tree extends ImportBaseTree {
  299. /**
  300. * 检查是否是父项
  301. * @param parent
  302. * @param code
  303. * @returns {boolean}
  304. * @private
  305. */
  306. _checkParent(parent, code) {
  307. if (!parent.code) return false;
  308. const numberPart = parent.code.replace(gdXmjPartReg, '');
  309. if (!numberPart) return false;
  310. return code.indexOf(numberPart) === 0 ||
  311. code.indexOf('G' + numberPart) === 0 ||
  312. code.indexOf('GD' + numberPart) === 0;
  313. }
  314. /**
  315. * 查找主表项目节父项
  316. * @param code
  317. * @returns {*}
  318. */
  319. findMainXmjParent(code) {
  320. const numberPart = code.replace(gdXmjPartReg, '');
  321. if (numberPart.length <= 1) throw '首层项目节模板中未定义,不支持导入';
  322. let parent = this.cacheMainXmjNode;
  323. while (parent) {
  324. if (this._checkParent(parent, code)) return parent;
  325. parent = this.nodes[parent.ledger_pid];
  326. }
  327. return null;
  328. }
  329. /**
  330. * 查找分表项目节父项
  331. * @param code
  332. * @returns {*}
  333. */
  334. findSubXmjParent(code) {
  335. let parent = this.cacheSubXmjNode;
  336. while (parent && parent.code.match(subReg)) {
  337. if (this._checkParent(parent, code)) return parent;
  338. parent = this.nodes[parent.ledger_pid];
  339. }
  340. return this.cacheMainXmjNode;
  341. }
  342. /**
  343. * 根据 编号 查找 父项项目节
  344. * @param {String} code - 子项编号
  345. * @returns {*}
  346. */
  347. findXmjParent(code) {
  348. if (code.match(mainReg)) {
  349. if (!this.cacheMainXmjNode) throw '主表项目节找不到父项';
  350. return this.findMainXmjParent(code);
  351. } else if (code.match(subReg)) {
  352. if (!this.cacheMainXmjNode) throw '分表项目节找不到所属主表项目节';
  353. return this.findSubXmjParent(code);
  354. }
  355. }
  356. /**
  357. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  358. * @param {Object} node - 当前添加的节点
  359. */
  360. defineCacheData(node) {
  361. super.defineCacheData(node);
  362. if (node.code) {
  363. if (node.code.match(mainReg)) {
  364. this.cacheMainXmjNode = node;
  365. this.cacheSubXmjNode = null;
  366. } else if (node.code.match(subReg)) {
  367. this.cacheSubXmjNode = node;
  368. }
  369. if (node.code.match(specCode106.reg)) {
  370. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg)) {
  371. this.cacheSpecMainXmj2 = node;
  372. } else {
  373. this.cacheSpecMainXmj1 = node;
  374. }
  375. } else if (node.code.match(specCode109.reg)) {
  376. this.cacheSpecMainXmj1 = node;
  377. this.cacheSpecMainXmj2 = null;
  378. }
  379. }
  380. }
  381. /**
  382. * 添加 项目节
  383. * @param {Object} node - 项目节
  384. * @returns {*}
  385. */
  386. addXmjNode(node) {
  387. if (!node.code || (!node.code.match(mainReg) && !node.code.match(subReg))) return null;
  388. node.id = this.ctx.app.uuid.v4();
  389. node.tender_id = this.ctx.tender.id;
  390. node.children = [];
  391. if ((specCode106.code.indexOf(node.code) >= 0)) {
  392. if (this.cacheSpecMainXmj2 && this.cacheSpecMainXmj2.code.match(specCode106.reg))
  393. return this.addNodeWithParent(node, this.cacheSpecMainXmj2);
  394. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode106.reg))
  395. return this.addNodeWithParent(node, this.cacheSpecMainXmj1);
  396. }
  397. if ((specCode109.code.indexOf(node.code) >= 0) &&
  398. (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg))) {
  399. return this.addNodeWithParent(node, this.cacheSpecMainXmj1)
  400. }
  401. const temp = this.findTempData(node);
  402. if (temp) {
  403. this.defineCacheData(temp);
  404. return temp;
  405. } else {
  406. const parent = this.findXmjParent(node.code);
  407. return this.addNodeWithParent(node, parent);
  408. }
  409. }
  410. }
  411. class AnalysisExcelTree {
  412. /**
  413. * 构造函数
  414. */
  415. constructor(ctx) {
  416. this.ctx = ctx;
  417. this.colsDef = null;
  418. this.colHeaderMatch = {
  419. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  420. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  421. pos: {value: ['计量单元'], type: colDefineType.match},
  422. name: {value: ['名称'], type: colDefineType.match},
  423. unit: {value: ['单位'], type: colDefineType.match},
  424. quantity: {value: ['清单数量'], type: colDefineType.match},
  425. dgn_qty1: {value: ['设计数量1'], type: colDefineType.match},
  426. dgn_qty2: {value: ['设计数量2'], type: colDefineType.match},
  427. unit_price: {value: ['单价'], type: colDefineType.match},
  428. drawing_code: {value: ['图号'], type: colDefineType.match},
  429. memo: {value: ['备注'], type: colDefineType.match},
  430. };
  431. }
  432. _isMatch11(tempData) {
  433. return _.find(tempData, x => {
  434. return x.code.indexOf('-') > 0;
  435. })
  436. }
  437. _isMatch18(tempData) {
  438. return _.every(tempData, x => {
  439. return !x.code || !!x.code.match(mainReg);
  440. });
  441. }
  442. _getNewCacheTree(tempData) {
  443. // 模板符合11编办规则,使用11编办树
  444. if (this._isMatch18(tempData)) {
  445. return new ImportStd18Tree(tempData, this.ctx);
  446. // 反之使用11编办(未校验模板是否符合,替换注释部分即可实现)
  447. // } else if (this._isMatch11(tempData)){
  448. } else {
  449. return new ImportBaseTree(tempData, this.ctx);
  450. }
  451. }
  452. /**
  453. * 读取项目节节点
  454. * @param {Array} row - excel行数据
  455. * @returns {*}
  456. * @private
  457. */
  458. _loadXmjNode(row) {
  459. try {
  460. const node = {};
  461. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  462. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  463. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  464. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  465. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  466. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  467. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  468. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  469. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  470. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  471. if (node.quantity && node.unit_price) {
  472. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  473. } else {
  474. node.total_price = null;
  475. }
  476. return this.cacheTree.addXmjNode(node);
  477. } catch (error) {
  478. console.log(error);
  479. if (error.stack) {
  480. this.ctx.logger.error(error);
  481. } else {
  482. this.ctx.getLogger('fail').info(JSON.stringify({
  483. error,
  484. project: this.ctx.session.sessionProject,
  485. user: this.ctx.session.sessionUser,
  486. body: row,
  487. }));
  488. }
  489. return null;
  490. }
  491. }
  492. /**
  493. * 读取工程量清单数据
  494. * @param {Array} row - excel行数据
  495. * @returns {*}
  496. * @private
  497. */
  498. _loadGclNode(row) {
  499. const node = {};
  500. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  501. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  502. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  503. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  504. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  505. node.sgfh_qty = node.quantity;
  506. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  507. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  508. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  509. if (node.quantity && node.unit_price) {
  510. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  511. } else {
  512. node.total_price = null;
  513. }
  514. return this.cacheTree.addGclNode(node);
  515. }
  516. /**
  517. * 读取部位明细数据
  518. * @param {Array} row - excel行数据
  519. * @returns {*}
  520. * @private
  521. */
  522. _loadPos(row) {
  523. const pos = {};
  524. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  525. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  526. pos.sgfh_qty = pos.quantity;
  527. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  528. return this.cacheTree.addPos(pos);
  529. }
  530. /**
  531. * 读取数据行
  532. * @param {Array} row - excel数据行
  533. * @param {Number} index - 行索引号
  534. */
  535. loadRowData(row, index) {
  536. let result;
  537. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  538. if (row[this.colsDef.code]) {
  539. // 第三部分(编号为'3')及之后的数据不读取
  540. if (row[this.colsDef.code] === '3') {
  541. this.loadEnd = true;
  542. return;
  543. }
  544. result = this._loadXmjNode(row)
  545. } else if (row[this.colsDef.pos]) {
  546. result = this._loadPos(row);
  547. } else {
  548. result = this._loadGclNode(row);
  549. }
  550. // 读取失败则写入错误数据 todo 返回前端告知用户?
  551. if (!result) {
  552. this.errorData.push({
  553. serialNo: index,
  554. data: row,
  555. });
  556. }
  557. }
  558. /**
  559. * 读取表头并检查
  560. * @param {Number} row - Excel数据行
  561. */
  562. checkColHeader(row) {
  563. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  564. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  565. this.colsDef = colsDef;
  566. }
  567. }
  568. /**
  569. * 将excel清单 平面数据 解析为 树结构数据
  570. * @param {object} sheet - excel清单数据
  571. * @param {Array} tempData - 新建项目使用的清单模板
  572. * @returns {ImportBaseTree}
  573. */
  574. analysisData(sheet, tempData) {
  575. this.colsDef = null;
  576. this.cacheTree = this._getNewCacheTree(tempData);
  577. this.errorData = [];
  578. this.loadEnd = false;
  579. for (const iRow in sheet.rows) {
  580. const row = sheet.rows[iRow];
  581. if (this.colsDef && !this.loadEnd) {
  582. this.loadRowData(row, iRow);
  583. } else {
  584. this.checkColHeader(row);
  585. }
  586. }
  587. this.cacheTree.resortFirstPartChildren();
  588. this.cacheTree.calculateLeafWithPos();
  589. return this.cacheTree;
  590. }
  591. }
  592. class ImportGclBaseTree {
  593. /**
  594. * 构造函数
  595. * @param {Array} tempData - 清单模板数据
  596. */
  597. constructor (ctx, parent, maxId, defaultData) {
  598. this.ctx = ctx;
  599. this.parent = parent;
  600. this.defaultData = defaultData;
  601. // 常量
  602. this.splitChar = '-';
  603. // 索引
  604. // 以code为索引
  605. this.codeNodes = {};
  606. this.items = [];
  607. // 缓存
  608. this.keyNodeId = maxId ? maxId + 1 : 1;
  609. this.blankParent = null;
  610. }
  611. /**
  612. * 根据 编号 查找 父项项目节
  613. * @param {String} code - 子项编号
  614. * @returns {*}
  615. */
  616. findParent(code) {
  617. const codePath = code.split(this.splitChar);
  618. if (codePath.length > 1) {
  619. codePath.splice(codePath.length - 1, 1);
  620. return this.codeNodes[codePath.join(this.splitChar)];
  621. }
  622. }
  623. /**
  624. * 添加 树节点 并完善该节点的树结构
  625. * @param {Object} node - 添加节点
  626. * @param {Object} parent - 父项
  627. * @returns {*}
  628. */
  629. addNodeWithParent(node, parent) {
  630. parent = parent ? parent : this.parent;
  631. if (!parent.children) parent.children = [];
  632. node.id = this.ctx.app.uuid.v4();
  633. node.tender_id = this.ctx.tender.id;
  634. node.ledger_id = this.keyNodeId;
  635. this.keyNodeId += 1;
  636. node.ledger_pid = parent.ledger_id;
  637. node.level = parent.level + 1;
  638. node.order = parent.children.length + 1;
  639. node.full_path = parent.full_path + '-' + node.ledger_id;
  640. parent.children.push(node);
  641. node.children = [];
  642. if (this.defaultData) _.assignIn(node, this.defaultData);
  643. this.items.push(node);
  644. if (!_.isNil(node.b_code) && node.b_code !== '') {
  645. this.codeNodes[node.b_code] = node;
  646. } else {
  647. this.blankParent = node;
  648. }
  649. return node;
  650. }
  651. /**
  652. * 添加 项目节
  653. * @param {Object} node - 项目节
  654. * @returns {*}
  655. */
  656. addNode(node) {
  657. if (_.isNil(node.b_code) || node.b_code === '') {
  658. return this.addNodeWithParent(node, null);
  659. } else if (node.b_code.split(this.splitChar).length > 1) {
  660. const parent = this.findParent(node.b_code);
  661. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  662. } else {
  663. return this.addNodeWithParent(node, this.blankParent);
  664. }
  665. }
  666. }
  667. class AnalysisGclExcelTree {
  668. /**
  669. * 构造函数
  670. */
  671. constructor(ctx) {
  672. this.ctx = ctx;
  673. this.colsDef = null;
  674. this.colHeaderMatch = {
  675. b_code: {value: ['编号', '清单编号', '子目号', '子目编号', '清单号'], type: colDefineType.match},
  676. name: {value: ['名称', '清单名称', '子目名称'], type: colDefineType.match},
  677. unit: {value: ['单位'], type: colDefineType.pos},
  678. quantity: {value: ['数量'], type: colDefineType.pos}, // 施工图复核数量
  679. unit_price: {value: ['单价'], type: colDefineType.pos},
  680. };
  681. }
  682. /**
  683. * 读取表头并检查
  684. * @param {Number} row - Excel数据行
  685. */
  686. checkColHeader(row) {
  687. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  688. if (colsDef.b_code) {
  689. this.colsDef = colsDef;
  690. }
  691. }
  692. loadRowData(row) {
  693. const node = {};
  694. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  695. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  696. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  697. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  698. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  699. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  700. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  701. if (node.quantity && node.unit_price) {
  702. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  703. } else {
  704. node.total_price = null;
  705. }
  706. return this.cacheTree.addNode(node);
  707. }
  708. /**
  709. * 将excel清单 平面数据 解析为 树结构数据
  710. * @param {object} sheet - excel清单数据
  711. * @param {Array} parentId - 导入至的节点id
  712. * @returns {ImportBaseTree}
  713. */
  714. analysisData(sheet, parent, maxId, defaultData) {
  715. try {
  716. this.colsDef = null;
  717. this.cacheTree = new ImportGclBaseTree(this.ctx, parent, maxId, defaultData);
  718. this.errorData = [];
  719. this.loadEnd = false;
  720. // 识别表头导入
  721. for (const iRow in sheet.rows) {
  722. const row = sheet.rows[iRow];
  723. if (this.colsDef && !this.loadEnd) {
  724. const result = this.loadRowData(row);
  725. // 读取失败则写入错误数据 todo 返回前端告知用户?
  726. if (!result) {
  727. this.errorData.push({
  728. serialNo: iRow,
  729. data: row,
  730. });
  731. }
  732. } else {
  733. this.checkColHeader(row);
  734. }
  735. }
  736. // 固定列导入
  737. // this.colsDef = {
  738. // b_code: 0,
  739. // name: 1,
  740. // unit: 2,
  741. // quantity: 3,
  742. // unit_price: 4
  743. // };
  744. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  745. // const row = sheet.rows[iRow];
  746. // const result = this.loadRowData(row);
  747. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  748. // if (!result) {
  749. // this.errorData.push({
  750. // serialNo: iRow,
  751. // data: row,
  752. // });
  753. // }
  754. // }
  755. return this.cacheTree;
  756. } catch(err) {
  757. this.ctx.helper.log(err);
  758. }
  759. }
  760. }
  761. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree };