analysis_excel.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 mainReg = /^(GD*)?G?(\d\d)*\d$/i;
  15. const subReg = /^(GD*)?G?[A-Z]{2}(\d\d)+$/i;
  16. const gdXmjPartReg = /^(GD*)?G?/;
  17. const specCode106 = {
  18. code: ['102', '103', '104'],
  19. reg: /^(GD*)?G?106/i
  20. };
  21. const specCode109 = {
  22. code: ['102', '103', '104', '105', '106', '107', '108'],
  23. reg: /^(GD*)?G?109/i
  24. };
  25. const aeUtils = {
  26. toNumber: function (value) {
  27. let num = _.toNumber(value);
  28. return _.isNaN(num) ? null : num;
  29. },
  30. checkColHeader: function (row, colHeaderMatch) {
  31. const colsDef = {};
  32. for (const iCol in row) {
  33. const text = row[iCol];
  34. if (text === null) continue;
  35. for (const header in colHeaderMatch) {
  36. const match = colHeaderMatch[header];
  37. switch (match.type) {
  38. case colDefineType.match:
  39. if (match.value.indexOf(text) >= 0) {
  40. colsDef[header] = iCol;
  41. }
  42. break;
  43. case colDefineType.pos:
  44. for (const v of match.value) {
  45. if (text.indexOf(v) >= 0) {
  46. colsDef[header] = iCol;
  47. break;
  48. }
  49. }
  50. break;
  51. }
  52. }
  53. }
  54. return colsDef;
  55. },
  56. isMatch11(tempData) {
  57. return _.find(tempData, x => {
  58. return x.code.indexOf('-') > 0;
  59. })
  60. },
  61. isMatch18(tempData) {
  62. return _.every(tempData, x => {
  63. return !x.code || !!x.code.match(mainReg);
  64. });
  65. }
  66. };
  67. class ImportBaseTree {
  68. /**
  69. * 构造函数
  70. * @param {Array} tempData - 清单模板数据
  71. */
  72. constructor (tempData, ctx, setting) {
  73. this.ctx = ctx;
  74. if (ctx.tender) {
  75. this.mid = ctx.tender.id;
  76. this.decimal = ctx.tender.info.decimal;
  77. this.precision = ctx.tender.info.precision;
  78. } else if (ctx.budget) {
  79. this.mid = ctx.budget.id;
  80. this.decimal = { up: 2, tp: 2};
  81. this.precision = {
  82. other: { value: 2 },
  83. };
  84. }
  85. this.setting = setting;
  86. // 常量
  87. this.splitChar = '-';
  88. // 索引
  89. // 以code为索引
  90. this.codeNodes = {};
  91. this.items = [];
  92. this.roots = [];
  93. this.pos = [];
  94. this.tempData = [];
  95. // 以id为索引
  96. this.nodes = {};
  97. // 缓存
  98. this.finalNode = null;
  99. this.finalPrecision = null;
  100. this.finalXmjNode = null;
  101. this.keyNodeId = 1;
  102. this._loadTemplateTree(tempData);
  103. }
  104. /**
  105. * 加载 清单模板
  106. * @param {Array} data - 模板数据
  107. * @private
  108. */
  109. _loadTemplateTree(data) {
  110. const self = this;
  111. let loadCodeNodes = true;
  112. for (const node of data) {
  113. node[this.setting.kid] = node.template_id;
  114. node[this.setting.pid] = node.pid;
  115. node.id = this.ctx.app.uuid.v4();
  116. delete node.pid;
  117. if (node.code && loadCodeNodes) {
  118. this.codeNodes[node.code] = node;
  119. }
  120. if (node.code === '3') {
  121. loadCodeNodes = false;
  122. }
  123. this.items.push(node);
  124. if (node[this.setting.pid] === -1) {
  125. this.roots.push(node);
  126. }
  127. if (node[this.setting.kid] >= this.keyNodeId) {
  128. this.keyNodeId = node[this.setting.kid] + 1;
  129. }
  130. this.tempData.push(node);
  131. this.nodes[node[this.setting.kid]] = node;
  132. }
  133. for (const node of this.items) {
  134. node[this.setting.mid] = this.mid;
  135. node.children = this.items.filter(function (i) {
  136. return i[self.setting.pid] === node[self.setting.kid];
  137. });
  138. }
  139. }
  140. /**
  141. * 根据 编号、名称 查找模板节点
  142. * @param {Object} node - 要查找的节点
  143. * @returns {*}
  144. */
  145. findTempData(node) {
  146. return this.tempData.find(function (td) {
  147. return td.code === node.code && td.name === node.name;
  148. });
  149. }
  150. /**
  151. * 根据 编号 查找 父项项目节
  152. * @param {String} code - 子项编号
  153. * @returns {*}
  154. */
  155. findXmjParent(code) {
  156. const codePath = code.split(this.splitChar);
  157. if (codePath.length > 1) {
  158. codePath.splice(codePath.length - 1, 1);
  159. return this.codeNodes[codePath.join(this.splitChar)];
  160. }
  161. }
  162. getPosterity(node) {
  163. let posterity = [].concat(node.children);
  164. for (const c of node.children) {
  165. posterity = posterity.concat(this.getPosterity(c));
  166. }
  167. return posterity;
  168. }
  169. findGclParent(code, xmj) {
  170. let parent;
  171. const codePath = code.split(this.splitChar);
  172. if (codePath.length > 1) {
  173. codePath.splice(codePath.length - 1, 1);
  174. const parentCode = codePath.join(this.splitChar);
  175. const posterity = this.getPosterity(xmj);
  176. parent = posterity.find(function (x) {
  177. return x.b_code === parentCode;
  178. })
  179. }
  180. return parent ? parent : xmj;
  181. }
  182. /**
  183. * 添加 树节点 并完善该节点的树结构
  184. * @param {Object} node - 添加节点
  185. * @param {Object} parent - 父项
  186. * @returns {*}
  187. */
  188. addNodeWithParent(node, parent) {
  189. node.id = this.ctx.app.uuid.v4();
  190. node[this.setting.kid] = this.keyNodeId;
  191. this.keyNodeId += 1;
  192. node[this.setting.pid] = parent ? parent[this.setting.kid] : -1;
  193. node[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  194. node[this.setting.order] = parent ? parent.children.length + 1 : this.roots.length + 1;
  195. node[this.setting.fullPath] = parent ? parent[this.setting.fullPath] + '-' + node[this.setting.kid] : '' + node[this.setting.kid];
  196. if (parent) {
  197. parent.children.push(node);
  198. } else {
  199. this.roots.push(node);
  200. }
  201. this.items.push(node);
  202. this.codeNodes[node.code] = node;
  203. this.defineCacheData(node);
  204. return node;
  205. }
  206. /**
  207. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  208. * @param {Object} node - 当前添加的节点
  209. */
  210. defineCacheData(node) {
  211. this.nodes[node[this.setting.kid]] = node;
  212. this.finalNode = node;
  213. this.finalPrecision = this.ctx.helper.findPrecision(this.precision, node.unit);
  214. if (node.code) {
  215. this.finalXmjNode = node;
  216. }
  217. }
  218. _assignRelaField(temp, node) {
  219. if (!temp.quantity) temp.quantity = node.quantity;
  220. if (!temp.dgn_qty1) temp.dgn_qty1 = node.dgn_qty1;
  221. if (!temp.dgn_qty2) temp.dgn_qty2 = node.dgn_qty2;
  222. if (!temp.unit_price) temp.unit_price = node.unit_price;
  223. if (!temp.drawing_code) temp.drawing_code = node.drawing_code;
  224. if (!temp.memo) temp.memo = node.memo;
  225. if (!temp.total_price) temp.total_price = node.total_price;
  226. }
  227. /**
  228. * 添加 项目节
  229. * @param {Object} node - 项目节
  230. * @returns {*}
  231. */
  232. addXmjNode(node) {
  233. node.id = this.ctx.app.uuid.v4();
  234. node[this.setting.mid] = this.mid;
  235. node.children = [];
  236. if (node.code.split(this.splitChar).length > 1) {
  237. const temp = this.findTempData(node);
  238. if (temp) {
  239. this.defineCacheData(temp);
  240. this._assignRelaField(temp, node);
  241. return temp;
  242. } else {
  243. const parent = this.findXmjParent(node.code);
  244. if (parent) {
  245. return this.addNodeWithParent(node, parent);
  246. } else {
  247. const newNode = this.addNodeWithParent(node, this.codeNodes['1']);
  248. newNode.error = 1;
  249. return null;
  250. }
  251. }
  252. } else {
  253. const n = this.codeNodes[node.code];
  254. if (!n) {
  255. return this.addNodeWithParent(node, null);
  256. } else {
  257. this.defineCacheData(n);
  258. this._assignRelaField(n, node);
  259. return n;
  260. }
  261. }
  262. }
  263. /**
  264. * 添加 工程量清单
  265. * @param {Object} node - 工程量清单
  266. */
  267. addGclNode(node) {
  268. node.id = this.ctx.app.uuid.v4();
  269. node[this.setting.mid] = this.mid;
  270. node.pos = [];
  271. node.children = [];
  272. if (this.finalXmjNode) {
  273. const parent = node.b_code ? this.findGclParent(node.b_code, this.finalXmjNode) : this.finalXmjNode;
  274. return this.addNodeWithParent(node, parent);
  275. }
  276. }
  277. /**
  278. * 添加 部位明细
  279. * @param {object} pos - 部位明细
  280. * @returns {*}
  281. */
  282. addPos (pos, strict = false){
  283. if (this.finalNode && this.finalNode.pos) {
  284. if (strict) {
  285. const exist = this.finalNode.pos.find(x => { return x.name === pos.name; });
  286. if (exist) return;
  287. }
  288. pos.id = this.ctx.app.uuid.v4();
  289. pos.lid = this.finalNode.id;
  290. pos.tid = this.ctx.tender.id;
  291. pos.add_stage = 0;
  292. pos.add_stage_order = 0;
  293. pos.add_times = 0;
  294. pos.in_time = new Date();
  295. pos.porder = this.finalNode.pos.length + 1;
  296. pos.add_user = this.ctx.session.sessionUser.accountId;
  297. this.finalNode.pos.push(pos);
  298. this.pos.push(pos);
  299. pos.sgfh_qty = this.ctx.helper.round(pos.sgfh_qty, this.finalPrecision.value);
  300. pos.quantity = this.ctx.helper.round(pos.quantity, this.finalPrecision.value);
  301. return pos;
  302. }
  303. }
  304. /**
  305. * 第一部分的子节点,顺序重排
  306. */
  307. resortFirstPartChildren () {
  308. const splitChar = this.splitChar;
  309. const firstPart = this.roots[0];
  310. firstPart.children.sort(function (a, b) {
  311. if (!a.code) {
  312. return 1;
  313. } else if (!b.code) {
  314. return -1;
  315. }
  316. if (a.error === b.error) {
  317. const codeA = a.code.split(splitChar);
  318. const numA = _.toNumber(codeA[codeA.length -1]);
  319. const codeB = b.code.split(splitChar);
  320. const numB = _.toNumber(codeB[codeB.length -1]);
  321. return numA - numB;
  322. } else if (a.error) {
  323. return 1
  324. } else if (b.error) {
  325. return -1;
  326. }
  327. });
  328. for (const [i, c] of firstPart.children.entries()) {
  329. c[this.setting.order] = i + 1;
  330. }
  331. }
  332. calculateLeafWithPos () {
  333. for (const node of this.items) {
  334. if (node.children && node.children.length > 0) {
  335. node.unit_price = null;
  336. node.quantity = null;
  337. node.total_price = null;
  338. }
  339. if (!node.pos || node.pos.length === 0) { continue; }
  340. node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  341. if (node.quantity && node.unit_price) {
  342. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  343. } else {
  344. node.total_price = null;
  345. }
  346. }
  347. }
  348. }
  349. class ImportStd18Tree extends ImportBaseTree {
  350. /**
  351. * 检查是否是父项
  352. * @param parent
  353. * @param code
  354. * @returns {boolean}
  355. * @private
  356. */
  357. _checkParent(parent, code) {
  358. const codeNumberPart = code.replace(gdXmjPartReg, '');
  359. if (!parent.code) return false;
  360. const numberPart = parent.code.replace(gdXmjPartReg, '');
  361. if (!numberPart || !codeNumberPart || numberPart.length >= codeNumberPart.length) return false;
  362. return code.indexOf(numberPart) === 0 ||
  363. code.indexOf('G' + numberPart) === 0 ||
  364. code.indexOf('GD' + numberPart) === 0;
  365. }
  366. /**
  367. * 查找主表项目节父项
  368. * @param code
  369. * @returns {*}
  370. */
  371. findMainXmjParent(code) {
  372. const numberPart = code.replace(gdXmjPartReg, '');
  373. if (numberPart.length <= 1) throw '首层项目节模板中未定义,不支持导入';
  374. let parent = this.cacheMainXmjNode;
  375. while (parent) {
  376. if (this._checkParent(parent, code)) return parent;
  377. parent = this.nodes[parent[this.setting.pid]];
  378. }
  379. return null;
  380. }
  381. /**
  382. * 查找分表项目节父项
  383. * @param code
  384. * @returns {*}
  385. */
  386. findSubXmjParent(code) {
  387. let parent = this.cacheSubXmjNode;
  388. while (parent && parent.is_sub && parent.code.match(subReg)) {
  389. if (this._checkParent(parent, code)) return parent;
  390. parent = this.nodes[parent[this.setting.pid]];
  391. }
  392. return this.cacheMainXmjNode;
  393. }
  394. /**
  395. * 根据 编号 查找 父项项目节
  396. * @param {String} code - 子项编号
  397. * @returns {*}
  398. */
  399. findXmjParent(code) {
  400. if (code.match(mainReg)) {
  401. if (!this.cacheMainXmjNode) throw '主表项目节找不到父项';
  402. return this.findMainXmjParent(code);
  403. } else if (code.match(subReg)) {
  404. if (!this.cacheMainXmjNode) throw '分表项目节找不到所属主表项目节';
  405. return this.findSubXmjParent(code);
  406. }
  407. }
  408. /**
  409. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  410. * @param {Object} node - 当前添加的节点
  411. */
  412. defineCacheData(node) {
  413. super.defineCacheData(node);
  414. if (node.code) {
  415. if (node.code.match(mainReg)) {
  416. node.is_main = true;
  417. this.cacheMainXmjNode = node;
  418. this.cacheSubXmjNode = null;
  419. } else if (node.code.match(subReg)) {
  420. node.is_sub = true;
  421. this.cacheSubXmjNode = node;
  422. }
  423. if (node.code.match(specCode106.reg)) {
  424. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg)) {
  425. this.cacheSpecMainXmj2 = node;
  426. } else {
  427. this.cacheSpecMainXmj1 = node;
  428. }
  429. } else if (node.code.match(specCode109.reg)) {
  430. this.cacheSpecMainXmj1 = node;
  431. this.cacheSpecMainXmj2 = null;
  432. }
  433. }
  434. }
  435. /**
  436. * 添加 项目节
  437. * @param {Object} node - 项目节
  438. * @returns {*}
  439. */
  440. addXmjNode(node) {
  441. if (!node.code || (!node.code.match(mainReg) && !node.code.match(subReg))) return null;
  442. node.id = this.ctx.app.uuid.v4();
  443. node.children = [];
  444. if ((specCode106.code.indexOf(node.code) >= 0)) {
  445. if (this.cacheSpecMainXmj2 && this.cacheSpecMainXmj2.code.match(specCode106.reg))
  446. return this.addNodeWithParent(node, this.cacheSpecMainXmj2);
  447. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode106.reg))
  448. return this.addNodeWithParent(node, this.cacheSpecMainXmj1);
  449. }
  450. if ((specCode109.code.indexOf(node.code) >= 0) &&
  451. (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg))) {
  452. return this.addNodeWithParent(node, this.cacheSpecMainXmj1)
  453. }
  454. const temp = this.findTempData(node);
  455. if (temp) {
  456. this.defineCacheData(temp);
  457. this._assignRelaField(temp, node);
  458. return temp;
  459. } else {
  460. const parent = this.findXmjParent(node.code);
  461. return this.addNodeWithParent(node, parent);
  462. }
  463. }
  464. }
  465. class AnalysisExcelTree {
  466. /**
  467. * 构造函数
  468. */
  469. constructor(ctx, setting, needCols) {
  470. this.ctx = ctx;
  471. this.setting = setting;
  472. if (ctx.tender) {
  473. this.mid = ctx.tender.id;
  474. this.decimal = ctx.tender.info.decimal;
  475. this.precision = ctx.tender.info.precision;
  476. } else if (ctx.budget) {
  477. this.mid = ctx.budget.id;
  478. this.decimal = ctx.budget.decimal;
  479. this.precision = {
  480. other: { value: ctx.budget.decimal.qty },
  481. };
  482. }
  483. this.colsDef = null;
  484. this.colHeaderMatch = {
  485. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  486. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  487. pos: {value: ['计量单元'], type: colDefineType.match},
  488. name: {value: ['名称'], type: colDefineType.match},
  489. unit: {value: ['单位'], type: colDefineType.match},
  490. quantity: {value: ['清单数量'], type: colDefineType.match},
  491. dgn_qty1: {value: ['设计数量1'], type: colDefineType.match},
  492. dgn_qty2: {value: ['设计数量2'], type: colDefineType.match},
  493. unit_price: {value: ['单价'], type: colDefineType.match},
  494. total_price: {value: ['金额', '合价'], type: colDefineType.match},
  495. drawing_code: {value: ['图号', '图册号'], type: colDefineType.match},
  496. memo: {value: ['备注'], type: colDefineType.match},
  497. };
  498. this.needCols = needCols || ['code', 'b_code', 'pos'];
  499. }
  500. _getNewCacheTree(tempData) {
  501. // 模板符合11编办规则,使用11编办树
  502. if (aeUtils.isMatch18(tempData)) {
  503. return new ImportStd18Tree(tempData, this.ctx, this.setting);
  504. // 反之使用11编办(未校验模板是否符合,替换注释部分即可实现)
  505. // } else if (aeUtils.isMatch11(tempData)){
  506. } else {
  507. return new ImportBaseTree(tempData, this.ctx, this.setting);
  508. }
  509. }
  510. /**
  511. * 读取项目节节点
  512. * @param {Array} row - excel行数据
  513. * @returns {*}
  514. * @private
  515. */
  516. _loadXmjNode(row) {
  517. try {
  518. const node = {};
  519. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  520. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  521. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  522. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  523. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  524. node.total_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.total_price]), this.decimal.tp);
  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. return this.cacheTree.addXmjNode(node);
  528. } catch (error) {
  529. if (error.stack) {
  530. this.ctx.logger.error(error);
  531. } else {
  532. this.ctx.getLogger('fail').info(JSON.stringify({
  533. error,
  534. project: this.ctx.session.sessionProject,
  535. user: this.ctx.session.sessionUser,
  536. body: row,
  537. }));
  538. }
  539. return null;
  540. }
  541. }
  542. /**
  543. * 读取工程量清单数据
  544. * @param {Array} row - excel行数据
  545. * @returns {*}
  546. * @private
  547. */
  548. _loadGclNode(row) {
  549. if (this.filter.filterGcl) return true;
  550. const node = {};
  551. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  552. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  553. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  554. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  555. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  556. node.sgfh_qty = node.quantity;
  557. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  558. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  559. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  560. if (node.quantity && node.unit_price) {
  561. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  562. } else {
  563. node.total_price = null;
  564. }
  565. if (this.filter.filterZeroGcl && !node.quantity && !node.total_price) return true;
  566. return this.cacheTree.addGclNode(node);
  567. }
  568. /**
  569. * 读取部位明细数据
  570. * @param {Array} row - excel行数据
  571. * @returns {*}
  572. * @private
  573. */
  574. _loadPos(row) {
  575. if (this.filter.filterPos) return true;
  576. const pos = {};
  577. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  578. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  579. pos.sgfh_qty = pos.quantity;
  580. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  581. return this.cacheTree.addPos(pos);
  582. }
  583. /**
  584. * 读取数据行
  585. * @param {Array} row - excel数据行
  586. * @param {Number} index - 行索引号
  587. */
  588. loadRowData(row, index) {
  589. let result;
  590. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  591. if (row[this.colsDef.code]) {
  592. // 第三部分(编号为'3')及之后的数据不读取
  593. if (row[this.colsDef.code] === '3') {
  594. this.loadEnd = true;
  595. return;
  596. }
  597. result = this._loadXmjNode(row)
  598. } else if (row[this.colsDef.pos]) {
  599. result = this._loadPos(row);
  600. } else {
  601. result = this._loadGclNode(row);
  602. }
  603. // 读取失败则写入错误数据 todo 返回前端告知用户?
  604. if (!result) {
  605. this.errorData.push({
  606. serialNo: index,
  607. data: row,
  608. });
  609. }
  610. }
  611. /**
  612. * 读取表头并检查
  613. * @param {Number} row - Excel数据行
  614. */
  615. checkColHeader(row) {
  616. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  617. let check = true;
  618. for (const col of this.needCols) {
  619. if (!colsDef[col]) check = false;
  620. }
  621. if (check) this.colsDef = colsDef;
  622. }
  623. /**
  624. * 将excel清单 平面数据 解析为 树结构数据
  625. * @param {object} sheet - excel清单数据
  626. * @param {Array} tempData - 新建项目使用的清单模板
  627. * @returns {ImportBaseTree}
  628. */
  629. analysisData(sheet, tempData, filter) {
  630. this.filter = filter ? filter : {};
  631. this.colsDef = null;
  632. this.cacheTree = this._getNewCacheTree(tempData);
  633. this.errorData = [];
  634. this.loadEnd = false;
  635. for (const iRow in sheet.rows) {
  636. const row = sheet.rows[iRow];
  637. if (this.colsDef && !this.loadEnd) {
  638. this.loadRowData(row, iRow);
  639. } else {
  640. this.checkColHeader(row);
  641. }
  642. }
  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. class AnalysisStageExcelTree extends AnalysisExcelTree {
  837. /**
  838. * 构造函数
  839. */
  840. constructor(ctx, setting) {
  841. super(ctx, setting);
  842. this.mid = ctx.tender.id;
  843. this.decimal = ctx.tender.info.decimal;
  844. this.precision = ctx.tender.info.precision;
  845. this.colsDef = null;
  846. this.colHeaderMatch = {
  847. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  848. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  849. pos: {value: ['计量单元'], type: colDefineType.match},
  850. name: {value: ['名称'], type: colDefineType.match},
  851. unit: {value: ['单位'], type: colDefineType.match},
  852. unit_price: {value: ['单价'], type: colDefineType.match},
  853. contract_qty: {value: ['本期合同计量|数量'], type: colDefineType.match},
  854. contract_tp: {value: ['本期合同计量|金额'], type: colDefineType.match},
  855. };
  856. this.needCols = ['code', 'b_code', 'pos', 'name', 'unit', 'unit_price', 'contract_qty', 'contract_tp'];
  857. }
  858. mergeHeaderRow(iRow, row, subRow, merge) {
  859. const result = [];
  860. for (let iCol = 0, iLen = row.length; iCol < iLen; iCol++) {
  861. const colMerge = merge.find(x => { return x.s.c === iCol && x.s.r === iRow});
  862. if (colMerge && colMerge.s.c !== colMerge.e.c) {
  863. let iSubCol = iCol;
  864. while (iSubCol <= colMerge.e.c) {
  865. result.push(row[iCol] + '|' + subRow[iSubCol]);
  866. iSubCol++;
  867. }
  868. iCol = colMerge.e.c;
  869. } else {
  870. result.push(row[iCol])
  871. }
  872. }
  873. return result;
  874. }
  875. /**
  876. * 读取项目节节点
  877. * @param {Array} row - excel行数据
  878. * @returns {*}
  879. * @private
  880. */
  881. _loadXmjNode(row) {
  882. try {
  883. const node = {};
  884. node.code = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.code]));
  885. node.name = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.name]));
  886. node.unit = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.unit]));
  887. return this.cacheTree.addXmjNode(node);
  888. } catch (error) {
  889. if (error.stack) {
  890. this.ctx.logger.error(error);
  891. } else {
  892. this.ctx.getLogger('fail').info(JSON.stringify({
  893. error,
  894. project: this.ctx.session.sessionProject,
  895. user: this.ctx.session.sessionUser,
  896. body: row,
  897. }));
  898. }
  899. return null;
  900. }
  901. }
  902. /**
  903. * 读取工程量清单数据
  904. * @param {Array} row - excel行数据
  905. * @returns {*}
  906. * @private
  907. */
  908. _loadGclNode(row) {
  909. if (this.filter.filterGcl) return true;
  910. const node = {};
  911. node.b_code = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.b_code]));
  912. node.name = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.name]));
  913. node.unit = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.unit]));
  914. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  915. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  916. node.contract_qty = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.contract_qty]), precision.value);
  917. if (node.quantity && node.unit_price) {
  918. node.contract_tp = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  919. } else {
  920. node.contract_tp = null;
  921. }
  922. if (this.filter.filterZeroGcl && !node.quantity && !node.total_price) return true;
  923. return this.cacheTree.addGclNode(node);
  924. }
  925. /**
  926. * 读取部位明细数据
  927. * @param {Array} row - excel行数据
  928. * @returns {*}
  929. * @private
  930. */
  931. _loadPos(row) {
  932. if (this.filter.filterPos) return true;
  933. const pos = {};
  934. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  935. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  936. pos.contrac_qty = pos.contract_qty;
  937. return this.cacheTree.addPos(pos, true);
  938. }
  939. /**
  940. * 将excel清单 平面数据 解析为 树结构数据
  941. * @param {object} sheet - excel清单数据
  942. * @param {Array} tempData - 新建项目使用的清单模板
  943. * @returns {ImportBaseTree}
  944. */
  945. analysisData(sheet, tempData, filter) {
  946. this.filter = filter ? filter : {};
  947. this.colsDef = null;
  948. this.cacheTree = this._getNewCacheTree(tempData);
  949. this.errorData = [];
  950. this.loadEnd = false;
  951. this.loadBegin = sheet.rows.length;
  952. for (const [iRow, row] of sheet.rows.entries()) {
  953. if (this.colsDef && !this.loadEnd) {
  954. if (iRow < this.loadBegin) continue;
  955. this.loadRowData(row, iRow);
  956. } else {
  957. if (iRow === sheet.rows.length - 1) continue;
  958. const mergeRow = this.mergeHeaderRow(iRow, row, sheet.rows[iRow + 1], sheet.merge);
  959. this.checkColHeader(mergeRow);
  960. if (this.colsDef) this.loadBegin = iRow + 2;
  961. }
  962. }
  963. return this.cacheTree;
  964. }
  965. }
  966. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree, AnalysisStageExcelTree };