analysis_excel.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 (code === 'LJ0703') console.log(parent.code, code);
  308. const codeNumberPart = code.replace(gdXmjPartReg, '');
  309. if (!parent.code) return false;
  310. const numberPart = parent.code.replace(gdXmjPartReg, '');
  311. if (code === 'LJ0703') console.log(numberPart, codeNumberPart);
  312. if (!numberPart || !codeNumberPart || numberPart.length >= codeNumberPart.length) return false;
  313. return code.indexOf(numberPart) === 0 ||
  314. code.indexOf('G' + numberPart) === 0 ||
  315. code.indexOf('GD' + numberPart) === 0;
  316. }
  317. /**
  318. * 查找主表项目节父项
  319. * @param code
  320. * @returns {*}
  321. */
  322. findMainXmjParent(code) {
  323. const numberPart = code.replace(gdXmjPartReg, '');
  324. if (numberPart.length <= 1) throw '首层项目节模板中未定义,不支持导入';
  325. let parent = this.cacheMainXmjNode;
  326. while (parent) {
  327. if (this._checkParent(parent, code)) return parent;
  328. parent = this.nodes[parent.ledger_pid];
  329. }
  330. return null;
  331. }
  332. /**
  333. * 查找分表项目节父项
  334. * @param code
  335. * @returns {*}
  336. */
  337. findSubXmjParent(code) {
  338. let parent = this.cacheSubXmjNode;
  339. while (parent && parent.is_sub && parent.code.match(subReg)) {
  340. if (this._checkParent(parent, code)) return parent;
  341. parent = this.nodes[parent.ledger_pid];
  342. }
  343. return this.cacheMainXmjNode;
  344. }
  345. /**
  346. * 根据 编号 查找 父项项目节
  347. * @param {String} code - 子项编号
  348. * @returns {*}
  349. */
  350. findXmjParent(code) {
  351. if (code.match(mainReg)) {
  352. if (!this.cacheMainXmjNode) throw '主表项目节找不到父项';
  353. return this.findMainXmjParent(code);
  354. } else if (code.match(subReg)) {
  355. if (!this.cacheMainXmjNode) throw '分表项目节找不到所属主表项目节';
  356. return this.findSubXmjParent(code);
  357. }
  358. }
  359. /**
  360. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  361. * @param {Object} node - 当前添加的节点
  362. */
  363. defineCacheData(node) {
  364. super.defineCacheData(node);
  365. if (node.code) {
  366. if (node.code.match(mainReg)) {
  367. node.is_main = true;
  368. this.cacheMainXmjNode = node;
  369. this.cacheSubXmjNode = null;
  370. } else if (node.code.match(subReg)) {
  371. node.is_sub = true;
  372. this.cacheSubXmjNode = node;
  373. }
  374. if (node.code.match(specCode106.reg)) {
  375. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg)) {
  376. this.cacheSpecMainXmj2 = node;
  377. } else {
  378. this.cacheSpecMainXmj1 = node;
  379. }
  380. } else if (node.code.match(specCode109.reg)) {
  381. this.cacheSpecMainXmj1 = node;
  382. this.cacheSpecMainXmj2 = null;
  383. }
  384. }
  385. }
  386. /**
  387. * 添加 项目节
  388. * @param {Object} node - 项目节
  389. * @returns {*}
  390. */
  391. addXmjNode(node) {
  392. if (!node.code || (!node.code.match(mainReg) && !node.code.match(subReg))) return null;
  393. node.id = this.ctx.app.uuid.v4();
  394. node.tender_id = this.ctx.tender.id;
  395. node.children = [];
  396. if ((specCode106.code.indexOf(node.code) >= 0)) {
  397. if (this.cacheSpecMainXmj2 && this.cacheSpecMainXmj2.code.match(specCode106.reg))
  398. return this.addNodeWithParent(node, this.cacheSpecMainXmj2);
  399. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode106.reg))
  400. return this.addNodeWithParent(node, this.cacheSpecMainXmj1);
  401. }
  402. if ((specCode109.code.indexOf(node.code) >= 0) &&
  403. (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg))) {
  404. return this.addNodeWithParent(node, this.cacheSpecMainXmj1)
  405. }
  406. const temp = this.findTempData(node);
  407. if (temp) {
  408. this.defineCacheData(temp);
  409. return temp;
  410. } else {
  411. const parent = this.findXmjParent(node.code);
  412. return this.addNodeWithParent(node, parent);
  413. }
  414. }
  415. }
  416. class AnalysisExcelTree {
  417. /**
  418. * 构造函数
  419. */
  420. constructor(ctx) {
  421. this.ctx = ctx;
  422. this.colsDef = null;
  423. this.colHeaderMatch = {
  424. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  425. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  426. pos: {value: ['计量单元'], type: colDefineType.match},
  427. name: {value: ['名称'], type: colDefineType.match},
  428. unit: {value: ['单位'], type: colDefineType.match},
  429. quantity: {value: ['清单数量'], type: colDefineType.match},
  430. dgn_qty1: {value: ['设计数量1'], type: colDefineType.match},
  431. dgn_qty2: {value: ['设计数量2'], type: colDefineType.match},
  432. unit_price: {value: ['单价'], type: colDefineType.match},
  433. drawing_code: {value: ['图号'], type: colDefineType.match},
  434. memo: {value: ['备注'], type: colDefineType.match},
  435. };
  436. }
  437. _isMatch11(tempData) {
  438. return _.find(tempData, x => {
  439. return x.code.indexOf('-') > 0;
  440. })
  441. }
  442. _isMatch18(tempData) {
  443. return _.every(tempData, x => {
  444. return !x.code || !!x.code.match(mainReg);
  445. });
  446. }
  447. _getNewCacheTree(tempData) {
  448. // 模板符合11编办规则,使用11编办树
  449. if (this._isMatch18(tempData)) {
  450. return new ImportStd18Tree(tempData, this.ctx);
  451. // 反之使用11编办(未校验模板是否符合,替换注释部分即可实现)
  452. // } else if (this._isMatch11(tempData)){
  453. } else {
  454. return new ImportBaseTree(tempData, this.ctx);
  455. }
  456. }
  457. /**
  458. * 读取项目节节点
  459. * @param {Array} row - excel行数据
  460. * @returns {*}
  461. * @private
  462. */
  463. _loadXmjNode(row) {
  464. try {
  465. const node = {};
  466. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  467. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  468. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  469. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  470. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  471. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  472. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  473. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  474. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  475. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  476. if (node.quantity && node.unit_price) {
  477. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  478. } else {
  479. node.total_price = null;
  480. }
  481. return this.cacheTree.addXmjNode(node);
  482. } catch (error) {
  483. console.log(error);
  484. if (error.stack) {
  485. this.ctx.logger.error(error);
  486. } else {
  487. this.ctx.getLogger('fail').info(JSON.stringify({
  488. error,
  489. project: this.ctx.session.sessionProject,
  490. user: this.ctx.session.sessionUser,
  491. body: row,
  492. }));
  493. }
  494. return null;
  495. }
  496. }
  497. /**
  498. * 读取工程量清单数据
  499. * @param {Array} row - excel行数据
  500. * @returns {*}
  501. * @private
  502. */
  503. _loadGclNode(row) {
  504. const node = {};
  505. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  506. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  507. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  508. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  509. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  510. node.sgfh_qty = node.quantity;
  511. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  512. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  513. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  514. if (node.quantity && node.unit_price) {
  515. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  516. } else {
  517. node.total_price = null;
  518. }
  519. if (this.filter && !node.quantity && !node.total_price) {
  520. return this.filter;
  521. }
  522. return this.cacheTree.addGclNode(node);
  523. }
  524. /**
  525. * 读取部位明细数据
  526. * @param {Array} row - excel行数据
  527. * @returns {*}
  528. * @private
  529. */
  530. _loadPos(row) {
  531. const pos = {};
  532. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  533. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  534. pos.sgfh_qty = pos.quantity;
  535. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  536. return this.cacheTree.addPos(pos);
  537. }
  538. /**
  539. * 读取数据行
  540. * @param {Array} row - excel数据行
  541. * @param {Number} index - 行索引号
  542. */
  543. loadRowData(row, index) {
  544. let result;
  545. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  546. if (row[this.colsDef.code]) {
  547. // 第三部分(编号为'3')及之后的数据不读取
  548. if (row[this.colsDef.code] === '3') {
  549. this.loadEnd = true;
  550. return;
  551. }
  552. result = this._loadXmjNode(row)
  553. } else if (row[this.colsDef.pos]) {
  554. result = this._loadPos(row);
  555. } else {
  556. result = this._loadGclNode(row);
  557. }
  558. // 读取失败则写入错误数据 todo 返回前端告知用户?
  559. if (!result) {
  560. this.errorData.push({
  561. serialNo: index,
  562. data: row,
  563. });
  564. }
  565. }
  566. /**
  567. * 读取表头并检查
  568. * @param {Number} row - Excel数据行
  569. */
  570. checkColHeader(row) {
  571. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  572. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  573. this.colsDef = colsDef;
  574. }
  575. }
  576. /**
  577. * 将excel清单 平面数据 解析为 树结构数据
  578. * @param {object} sheet - excel清单数据
  579. * @param {Array} tempData - 新建项目使用的清单模板
  580. * @returns {ImportBaseTree}
  581. */
  582. analysisData(sheet, tempData, filter) {
  583. this.filter = filter;
  584. this.colsDef = null;
  585. this.cacheTree = this._getNewCacheTree(tempData);
  586. this.errorData = [];
  587. this.loadEnd = false;
  588. for (const iRow in sheet.rows) {
  589. const row = sheet.rows[iRow];
  590. if (this.colsDef && !this.loadEnd) {
  591. this.loadRowData(row, iRow);
  592. } else {
  593. this.checkColHeader(row);
  594. }
  595. }
  596. this.cacheTree.resortFirstPartChildren();
  597. this.cacheTree.calculateLeafWithPos();
  598. return this.cacheTree;
  599. }
  600. }
  601. class ImportGclBaseTree {
  602. /**
  603. * 构造函数
  604. * @param {Array} tempData - 清单模板数据
  605. */
  606. constructor (ctx, parent, maxId, defaultData) {
  607. this.ctx = ctx;
  608. this.parent = parent;
  609. this.defaultData = defaultData;
  610. // 常量
  611. this.splitChar = '-';
  612. // 索引
  613. // 以code为索引
  614. this.codeNodes = {};
  615. this.items = [];
  616. // 缓存
  617. this.keyNodeId = maxId ? maxId + 1 : 1;
  618. this.blankParent = null;
  619. }
  620. /**
  621. * 根据 编号 查找 父项项目节
  622. * @param {String} code - 子项编号
  623. * @returns {*}
  624. */
  625. findParent(code) {
  626. const codePath = code.split(this.splitChar);
  627. if (codePath.length > 1) {
  628. codePath.splice(codePath.length - 1, 1);
  629. return this.codeNodes[codePath.join(this.splitChar)];
  630. }
  631. }
  632. /**
  633. * 添加 树节点 并完善该节点的树结构
  634. * @param {Object} node - 添加节点
  635. * @param {Object} parent - 父项
  636. * @returns {*}
  637. */
  638. addNodeWithParent(node, parent) {
  639. parent = parent ? parent : this.parent;
  640. if (!parent.children) parent.children = [];
  641. node.id = this.ctx.app.uuid.v4();
  642. node.tender_id = this.ctx.tender.id;
  643. node.ledger_id = this.keyNodeId;
  644. this.keyNodeId += 1;
  645. node.ledger_pid = parent.ledger_id;
  646. node.level = parent.level + 1;
  647. node.order = parent.children.length + 1;
  648. node.full_path = parent.full_path + '-' + node.ledger_id;
  649. parent.children.push(node);
  650. node.children = [];
  651. if (this.defaultData) _.assignIn(node, this.defaultData);
  652. this.items.push(node);
  653. if (!_.isNil(node.b_code) && node.b_code !== '') {
  654. this.codeNodes[node.b_code] = node;
  655. } else {
  656. this.blankParent = node;
  657. }
  658. return node;
  659. }
  660. /**
  661. * 添加 项目节
  662. * @param {Object} node - 项目节
  663. * @returns {*}
  664. */
  665. addNode(node) {
  666. if (_.isNil(node.b_code) || node.b_code === '') {
  667. return this.addNodeWithParent(node, null);
  668. } else if (node.b_code.split(this.splitChar).length > 1) {
  669. const parent = this.findParent(node.b_code);
  670. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  671. } else {
  672. return this.addNodeWithParent(node, this.blankParent);
  673. }
  674. }
  675. }
  676. class AnalysisGclExcelTree {
  677. /**
  678. * 构造函数
  679. */
  680. constructor(ctx) {
  681. this.ctx = ctx;
  682. this.colsDef = null;
  683. this.colHeaderMatch = {
  684. b_code: {value: ['编号', '清单编号', '子目号', '子目编号', '清单号'], type: colDefineType.match},
  685. name: {value: ['名称', '清单名称', '子目名称'], type: colDefineType.match},
  686. unit: {value: ['单位'], type: colDefineType.pos},
  687. quantity: {value: ['数量'], type: colDefineType.pos}, // 施工图复核数量
  688. unit_price: {value: ['单价'], type: colDefineType.pos},
  689. };
  690. }
  691. /**
  692. * 读取表头并检查
  693. * @param {Number} row - Excel数据行
  694. */
  695. checkColHeader(row) {
  696. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  697. if (colsDef.b_code) {
  698. this.colsDef = colsDef;
  699. }
  700. }
  701. loadRowData(row) {
  702. const node = {};
  703. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  704. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  705. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  706. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  707. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  708. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  709. node.unit_price = aeUtils.toNumber(row[this.colsDef.unit_price]);
  710. if (node.quantity && node.unit_price) {
  711. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.ctx.tender.info.decimal.tp);
  712. } else {
  713. node.total_price = null;
  714. }
  715. return this.cacheTree.addNode(node);
  716. }
  717. /**
  718. * 将excel清单 平面数据 解析为 树结构数据
  719. * @param {object} sheet - excel清单数据
  720. * @param {Array} parentId - 导入至的节点id
  721. * @returns {ImportBaseTree}
  722. */
  723. analysisData(sheet, parent, maxId, defaultData) {
  724. try {
  725. this.colsDef = null;
  726. this.cacheTree = new ImportGclBaseTree(this.ctx, parent, maxId, defaultData);
  727. this.errorData = [];
  728. this.loadEnd = false;
  729. // 识别表头导入
  730. for (const iRow in sheet.rows) {
  731. const row = sheet.rows[iRow];
  732. if (this.colsDef && !this.loadEnd) {
  733. const result = this.loadRowData(row);
  734. // 读取失败则写入错误数据 todo 返回前端告知用户?
  735. if (!result) {
  736. this.errorData.push({
  737. serialNo: iRow,
  738. data: row,
  739. });
  740. }
  741. } else {
  742. this.checkColHeader(row);
  743. }
  744. }
  745. // 固定列导入
  746. // this.colsDef = {
  747. // b_code: 0,
  748. // name: 1,
  749. // unit: 2,
  750. // quantity: 3,
  751. // unit_price: 4
  752. // };
  753. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  754. // const row = sheet.rows[iRow];
  755. // const result = this.loadRowData(row);
  756. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  757. // if (!result) {
  758. // this.errorData.push({
  759. // serialNo: iRow,
  760. // data: row,
  761. // });
  762. // }
  763. // }
  764. return this.cacheTree;
  765. } catch(err) {
  766. this.ctx.helper.log(err);
  767. }
  768. }
  769. }
  770. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree };