analysis_excel.js 30 KB

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