analysis_excel.js 28 KB

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