analysis_excel.js 30 KB

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