analysis_excel.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 exist;
  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. deal_qty: {value: ['签约数量'], type: colDefineType.match},
  491. deal_tp: {value: ['签约金额'], type: colDefineType.match},
  492. quantity: {value: ['清单数量'], type: colDefineType.match},
  493. dgn_qty1: {value: ['设计数量1'], type: colDefineType.match},
  494. dgn_qty2: {value: ['设计数量2'], type: colDefineType.match},
  495. unit_price: {value: ['单价'], type: colDefineType.match},
  496. total_price: {value: ['金额', '合价'], type: colDefineType.match},
  497. drawing_code: {value: ['图号', '图册号'], type: colDefineType.match},
  498. memo: {value: ['备注'], type: colDefineType.match},
  499. };
  500. this.needCols = needCols || ['code', 'b_code', 'pos'];
  501. }
  502. _getNewCacheTree(tempData) {
  503. // 模板符合11编办规则,使用11编办树
  504. if (aeUtils.isMatch18(tempData)) {
  505. return new ImportStd18Tree(tempData, this.ctx, this.setting);
  506. // 反之使用11编办(未校验模板是否符合,替换注释部分即可实现)
  507. // } else if (aeUtils.isMatch11(tempData)){
  508. } else {
  509. return new ImportBaseTree(tempData, this.ctx, this.setting);
  510. }
  511. }
  512. /**
  513. * 读取项目节节点
  514. * @param {Array} row - excel行数据
  515. * @returns {*}
  516. * @private
  517. */
  518. _loadXmjNode(row) {
  519. try {
  520. const node = {};
  521. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  522. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  523. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  524. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  525. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  526. node.deal_tp = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.deal_tp]), this.decimal.tp);
  527. node.total_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.total_price]), this.decimal.tp);
  528. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  529. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  530. return this.cacheTree.addXmjNode(node);
  531. } catch (error) {
  532. if (error.stack) {
  533. this.ctx.logger.error(error);
  534. } else {
  535. this.ctx.getLogger('fail').info(JSON.stringify({
  536. error,
  537. project: this.ctx.session.sessionProject,
  538. user: this.ctx.session.sessionUser,
  539. body: row,
  540. }));
  541. }
  542. return null;
  543. }
  544. }
  545. /**
  546. * 读取工程量清单数据
  547. * @param {Array} row - excel行数据
  548. * @returns {*}
  549. * @private
  550. */
  551. _loadGclNode(row) {
  552. if (this.filter.filterGcl) return true;
  553. const node = {};
  554. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  555. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  556. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  557. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  558. node.deal_qty = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.deal_qty]), precision.value);
  559. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  560. node.sgfh_qty = node.quantity;
  561. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  562. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  563. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  564. node.deal_tp = node.deal_qty && node.unit_price ? this.ctx.helper.mul(node.deal_qty, node.unit_price, this.decimal.tp) : 0;
  565. if (node.quantity && node.unit_price) {
  566. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  567. } else {
  568. node.total_price = null;
  569. }
  570. if (this.filter.filterZeroGcl && !node.quantity && !node.total_price) return true;
  571. return this.cacheTree.addGclNode(node);
  572. }
  573. /**
  574. * 读取部位明细数据
  575. * @param {Array} row - excel行数据
  576. * @returns {*}
  577. * @private
  578. */
  579. _loadPos(row) {
  580. if (this.filter.filterPos) return true;
  581. const pos = {};
  582. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  583. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  584. pos.sgfh_qty = pos.quantity;
  585. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  586. return this.cacheTree.addPos(pos);
  587. }
  588. /**
  589. * 读取数据行
  590. * @param {Array} row - excel数据行
  591. * @param {Number} index - 行索引号
  592. */
  593. loadRowData(row, index) {
  594. let result;
  595. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  596. if (row[this.colsDef.code]) {
  597. // 第三部分(编号为'3')及之后的数据不读取
  598. if (row[this.colsDef.code] === '3') {
  599. this.loadEnd = true;
  600. return;
  601. }
  602. result = this._loadXmjNode(row)
  603. } else if (row[this.colsDef.pos]) {
  604. result = this._loadPos(row);
  605. } else {
  606. result = this._loadGclNode(row);
  607. }
  608. // 读取失败则写入错误数据 todo 返回前端告知用户?
  609. if (!result) {
  610. this.errorData.push({
  611. serialNo: index,
  612. data: row,
  613. });
  614. }
  615. }
  616. /**
  617. * 读取表头并检查
  618. * @param {Number} row - Excel数据行
  619. */
  620. checkColHeader(row) {
  621. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  622. let check = true;
  623. for (const col of this.needCols) {
  624. if (!colsDef[col]) check = false;
  625. }
  626. if (check) this.colsDef = colsDef;
  627. }
  628. /**
  629. * 将excel清单 平面数据 解析为 树结构数据
  630. * @param {object} sheet - excel清单数据
  631. * @param {Array} tempData - 新建项目使用的清单模板
  632. * @returns {ImportBaseTree}
  633. */
  634. analysisData(sheet, tempData, filter) {
  635. this.filter = filter ? filter : {};
  636. this.colsDef = null;
  637. this.cacheTree = this._getNewCacheTree(tempData);
  638. this.errorData = [];
  639. this.loadEnd = false;
  640. for (const iRow in sheet.rows) {
  641. const row = sheet.rows[iRow];
  642. if (this.colsDef && !this.loadEnd) {
  643. this.loadRowData(row, iRow);
  644. } else {
  645. this.checkColHeader(row);
  646. }
  647. }
  648. return this.cacheTree;
  649. }
  650. }
  651. class ImportGclBaseTree {
  652. /**
  653. * 构造函数
  654. * @param {Array} tempData - 清单模板数据
  655. */
  656. constructor (ctx, setting, parent, maxId, defaultData) {
  657. this.ctx = ctx;
  658. this.setting = setting;
  659. if (ctx.tender) {
  660. this.mid = ctx.tender.id;
  661. this.decimal = ctx.tender.info.decimal;
  662. this.precision = ctx.tender.info.precision;
  663. } else if (ctx.budget) {
  664. this.mid = ctx.budget.id;
  665. this.decimal = { up: 2, tp: 2};
  666. this.precision = {
  667. other: { value: 2 },
  668. };
  669. }
  670. this.parent = parent;
  671. this.defaultData = defaultData;
  672. // 常量
  673. this.splitChar = '-';
  674. // 索引
  675. // 以code为索引
  676. this.codeNodes = {};
  677. this.items = [];
  678. // 缓存
  679. this.keyNodeId = maxId ? maxId + 1 : 1;
  680. this.blankParent = null;
  681. }
  682. /**
  683. * 根据 编号 查找 父项项目节
  684. * @param {String} code - 子项编号
  685. * @returns {*}
  686. */
  687. findParent(code) {
  688. const codePath = code.split(this.splitChar);
  689. if (codePath.length > 1) {
  690. codePath.splice(codePath.length - 1, 1);
  691. return this.codeNodes[codePath.join(this.splitChar)];
  692. }
  693. }
  694. /**
  695. * 添加 树节点 并完善该节点的树结构
  696. * @param {Object} node - 添加节点
  697. * @param {Object} parent - 父项
  698. * @returns {*}
  699. */
  700. addNodeWithParent(node, parent) {
  701. parent = parent ? parent : this.parent;
  702. if (!parent.children) parent.children = [];
  703. node.id = this.ctx.app.uuid.v4();
  704. node[this.setting.mid] = this.mid;
  705. node[this.setting.kid] = this.keyNodeId;
  706. this.keyNodeId += 1;
  707. node[this.setting.pid] = parent[this.setting.kid];
  708. node[this.setting.level] = parent[this.setting.level] + 1;
  709. node[this.setting.order] = parent.children.length + 1;
  710. node[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + node[this.setting.kid];
  711. parent.children.push(node);
  712. node.children = [];
  713. if (this.defaultData) _.assignIn(node, this.defaultData);
  714. this.items.push(node);
  715. if (!_.isNil(node.b_code) && node.b_code !== '') {
  716. this.codeNodes[node.b_code] = node;
  717. } else {
  718. this.blankParent = node;
  719. }
  720. return node;
  721. }
  722. /**
  723. * 添加 项目节
  724. * @param {Object} node - 项目节
  725. * @returns {*}
  726. */
  727. addNode(node) {
  728. if (_.isNil(node.b_code) || node.b_code === '') {
  729. return this.addNodeWithParent(node, null);
  730. } else if (node.b_code.split(this.splitChar).length > 1) {
  731. const parent = this.findParent(node.b_code);
  732. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  733. } else {
  734. return this.addNodeWithParent(node, this.blankParent);
  735. }
  736. }
  737. }
  738. class AnalysisGclExcelTree {
  739. /**
  740. * 构造函数
  741. */
  742. constructor(ctx, setting) {
  743. this.ctx = ctx;
  744. this.setting = setting;
  745. if (ctx.tender) {
  746. this.mid = ctx.tender.id;
  747. this.decimal = ctx.tender.info.decimal;
  748. this.precision = ctx.tender.info.precision;
  749. } else if (ctx.budget) {
  750. this.mid = ctx.budget.id;
  751. this.decimal = { up: 2, tp: 2};
  752. this.precision = {
  753. other: { value: 2 },
  754. };
  755. }
  756. this.colsDef = null;
  757. this.colHeaderMatch = {
  758. b_code: {value: ['编号', '清单编号', '子目号', '子目编号', '清单号'], type: colDefineType.match},
  759. name: {value: ['名称', '清单名称', '子目名称'], type: colDefineType.match},
  760. unit: {value: ['单位'], type: colDefineType.pos},
  761. quantity: {value: ['数量'], type: colDefineType.pos}, // 施工图复核数量
  762. unit_price: {value: ['单价'], type: colDefineType.pos},
  763. };
  764. }
  765. /**
  766. * 读取表头并检查
  767. * @param {Number} row - Excel数据行
  768. */
  769. checkColHeader(row) {
  770. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  771. if (colsDef.b_code) {
  772. this.colsDef = colsDef;
  773. }
  774. }
  775. loadRowData(row) {
  776. const node = {};
  777. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  778. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  779. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  780. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  781. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  782. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  783. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  784. if (node.quantity && node.unit_price) {
  785. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  786. } else {
  787. node.total_price = null;
  788. }
  789. return this.cacheTree.addNode(node);
  790. }
  791. /**
  792. * 将excel清单 平面数据 解析为 树结构数据
  793. * @param {object} sheet - excel清单数据
  794. * @param {Array} parentId - 导入至的节点id
  795. * @returns {ImportBaseTree}
  796. */
  797. analysisData(sheet, parent, maxId, defaultData) {
  798. try {
  799. this.colsDef = null;
  800. this.cacheTree = new ImportGclBaseTree(this.ctx, this.setting, parent, maxId, defaultData);
  801. this.errorData = [];
  802. this.loadEnd = false;
  803. // 识别表头导入
  804. for (const iRow in sheet.rows) {
  805. const row = sheet.rows[iRow];
  806. if (this.colsDef && !this.loadEnd) {
  807. const result = this.loadRowData(row);
  808. // 读取失败则写入错误数据
  809. if (!result) {
  810. this.errorData.push({ serialNo: iRow, data: row });
  811. }
  812. } else {
  813. this.checkColHeader(row);
  814. }
  815. }
  816. // 固定列导入
  817. // this.colsDef = {
  818. // b_code: 0,
  819. // name: 1,
  820. // unit: 2,
  821. // quantity: 3,
  822. // unit_price: 4
  823. // };
  824. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  825. // const row = sheet.rows[iRow];
  826. // const result = this.loadRowData(row);
  827. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  828. // if (!result) {
  829. // this.errorData.push({
  830. // serialNo: iRow,
  831. // data: row,
  832. // });
  833. // }
  834. // }
  835. return this.cacheTree;
  836. } catch(err) {
  837. this.ctx.helper.log(err);
  838. }
  839. }
  840. }
  841. class AnalysisStageExcelTree extends AnalysisExcelTree {
  842. /**
  843. * 构造函数
  844. */
  845. constructor(ctx, setting) {
  846. super(ctx, setting);
  847. this.mid = ctx.tender.id;
  848. this.decimal = ctx.tender.info.decimal;
  849. this.precision = ctx.tender.info.precision;
  850. this.colsDef = null;
  851. this.colHeaderMatch = {
  852. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  853. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  854. pos: {value: ['计量单元'], type: colDefineType.match},
  855. name: {value: ['名称'], type: colDefineType.match},
  856. unit: {value: ['单位'], type: colDefineType.match},
  857. unit_price: {value: ['单价'], type: colDefineType.match},
  858. contract_qty: {value: ['本期合同计量|数量'], type: colDefineType.match},
  859. contract_tp: {value: ['本期合同计量|金额'], type: colDefineType.match},
  860. deal_dgn_qty1: {value: ['合同|项目节数量1'], type: colDefineType.match},
  861. deal_dgn_qty2: {value: ['合同|项目节数量2'], type: colDefineType.match},
  862. c_dgn_qty1: {value: ['变更|项目节数量1'], type: colDefineType.match},
  863. c_dgn_qty2: {value: ['变更|项目节数量2'], type: colDefineType.match},
  864. postil: {value: ['本期批注'], type: colDefineType.match}
  865. };
  866. this.needCols = ['code', 'b_code', 'pos', 'name', 'unit', 'unit_price', 'contract_qty', 'contract_tp'];
  867. }
  868. mergeHeaderRow(iRow, row, subRow, merge) {
  869. const result = [];
  870. for (let iCol = 0, iLen = row.length; iCol < iLen; iCol++) {
  871. const colMerge = merge.find(x => { return x.s.c === iCol && x.s.r === iRow});
  872. if (colMerge && colMerge.s.c !== colMerge.e.c) {
  873. let iSubCol = iCol;
  874. while (iSubCol <= colMerge.e.c) {
  875. result.push(row[iCol] + '|' + subRow[iSubCol]);
  876. iSubCol++;
  877. }
  878. iCol = colMerge.e.c;
  879. } else {
  880. result.push(row[iCol])
  881. }
  882. }
  883. return result;
  884. }
  885. /**
  886. * 读取项目节节点
  887. * @param {Array} row - excel行数据
  888. * @returns {*}
  889. * @private
  890. */
  891. _loadXmjNode(row) {
  892. try {
  893. const node = {};
  894. node.code = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.code]));
  895. node.name = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.name]));
  896. node.unit = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.unit]));
  897. const xmj = this.cacheTree.addXmjNode(node);
  898. if (xmj) {
  899. xmj.deal_dgn_qty1 = aeUtils.toNumber(row[this.colsDef.deal_dgn_qty1]);
  900. xmj.deal_dgn_qty2 = aeUtils.toNumber(row[this.colsDef.deal_dgn_qty2]);
  901. xmj.c_dgn_qty1 = aeUtils.toNumber(row[this.colsDef.c_dgn_qty1]);
  902. xmj.c_dgn_qty2 = aeUtils.toNumber(row[this.colsDef.c_dgn_qty2]);
  903. xmj.postil = this.ctx.helper.replaceReturn(row[this.colsDef.postil]);
  904. this.ctx.helper.checkDgnQtyPrecision(xmj);
  905. return xmj;
  906. } else {
  907. return null;
  908. }
  909. } catch (error) {
  910. if (error.stack) {
  911. this.ctx.logger.error(error);
  912. } else {
  913. this.ctx.getLogger('fail').info(JSON.stringify({
  914. error,
  915. project: this.ctx.session.sessionProject,
  916. user: this.ctx.session.sessionUser,
  917. body: row,
  918. }));
  919. }
  920. return null;
  921. }
  922. }
  923. /**
  924. * 读取工程量清单数据
  925. * @param {Array} row - excel行数据
  926. * @returns {*}
  927. * @private
  928. */
  929. _loadGclNode(row) {
  930. if (this.filter.filterGcl) return true;
  931. const node = {};
  932. node.b_code = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.b_code]));
  933. node.name = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.name]));
  934. node.unit = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.unit]));
  935. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  936. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  937. node.contract_qty = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.contract_qty]), precision.value);
  938. if (node.contract_qty && node.unit_price) {
  939. node.contract_tp = this.ctx.helper.mul(node.contract_qty, node.unit_price, this.decimal.tp);
  940. } else {
  941. node.contract_tp = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.contract_tp]), this.decimal.tp);
  942. }
  943. node.postil = this.ctx.helper.replaceReturn(row[this.colsDef.postil]);
  944. if (this.filter.filterZeroGcl && !node.contract_qty && !node.contract_tp) return true;
  945. return this.cacheTree.addGclNode(node);
  946. }
  947. /**
  948. * 读取部位明细数据
  949. * @param {Array} row - excel行数据
  950. * @returns {*}
  951. * @private
  952. */
  953. _loadPos(row) {
  954. if (this.filter.filterPos) return true;
  955. let pos = {};
  956. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  957. pos.quantity = aeUtils.toNumber(row[this.colsDef.contract_qty]);
  958. pos = this.cacheTree.addPos(pos, true);
  959. pos.contract_qty = pos.quantity;
  960. pos.postil = this.ctx.helper.replaceReturn(row[this.colsDef.postil]);
  961. return pos;
  962. }
  963. /**
  964. * 将excel清单 平面数据 解析为 树结构数据
  965. * @param {object} sheet - excel清单数据
  966. * @param {Array} tempData - 新建项目使用的清单模板
  967. * @returns {ImportBaseTree}
  968. */
  969. analysisData(sheet, tempData, filter) {
  970. this.filter = filter ? filter : {};
  971. this.colsDef = null;
  972. this.cacheTree = this._getNewCacheTree(tempData);
  973. this.errorData = [];
  974. this.loadEnd = false;
  975. this.loadBegin = sheet.rows.length;
  976. for (const [iRow, row] of sheet.rows.entries()) {
  977. if (this.colsDef && !this.loadEnd) {
  978. if (iRow < this.loadBegin) continue;
  979. this.loadRowData(row, iRow);
  980. } else {
  981. if (iRow === sheet.rows.length - 1) continue;
  982. const mergeRow = this.mergeHeaderRow(iRow, row, sheet.rows[iRow + 1], sheet.merge);
  983. this.checkColHeader(mergeRow);
  984. if (this.colsDef) this.loadBegin = iRow + 2;
  985. }
  986. }
  987. return this.cacheTree;
  988. }
  989. }
  990. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree, AnalysisStageExcelTree };