analysis_excel.js 38 KB

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