analysis_excel.js 28 KB

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