analysis_excel.js 28 KB

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