analysis_excel.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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.features) temp.features = node.features;
  241. if (!temp.total_price) temp.total_price = node.total_price;
  242. }
  243. /**
  244. * 添加 项目节
  245. * @param {Object} node - 项目节
  246. * @returns {*}
  247. */
  248. addXmjNode(node) {
  249. node.id = this.ctx.app.uuid.v4();
  250. node[this.setting.mid] = this.mid;
  251. node.children = [];
  252. if (node.code.split(this.splitChar).length > 1) {
  253. const temp = this.findTempData(node);
  254. if (temp) {
  255. this.defineCacheData(temp);
  256. this._assignRelaField(temp, node);
  257. return temp;
  258. } else {
  259. const parent = this.findXmjParent(node.code);
  260. if (parent) {
  261. return this.addNodeWithParent(node, parent);
  262. } else {
  263. const newNode = this.addNodeWithParent(node, this.codeNodes['1']);
  264. newNode.error = 1;
  265. return null;
  266. }
  267. }
  268. } else {
  269. const n = this.codeNodes[node.code];
  270. if (!n) {
  271. return this.addNodeWithParent(node, null);
  272. } else {
  273. this.defineCacheData(n);
  274. this._assignRelaField(n, node);
  275. return n;
  276. }
  277. }
  278. }
  279. /**
  280. * 添加 工程量清单
  281. * @param {Object} node - 工程量清单
  282. */
  283. addGclNode(node) {
  284. node.id = this.ctx.app.uuid.v4();
  285. node[this.setting.mid] = this.mid;
  286. node.pos = [];
  287. node.children = [];
  288. if (this.finalXmjNode) {
  289. const parent = node.b_code ? this.findGclParent(node.b_code, this.finalXmjNode) : this.finalXmjNode;
  290. return this.addNodeWithParent(node, parent);
  291. }
  292. }
  293. /**
  294. * 添加 部位明细
  295. * @param {object} pos - 部位明细
  296. * @returns {*}
  297. */
  298. addPos (pos, strict = false){
  299. if (this.finalNode && this.finalNode.pos) {
  300. if (strict) {
  301. const exist = this.finalNode.pos.find(x => { return x.name === pos.name; });
  302. if (exist) return exist;
  303. }
  304. pos.id = this.ctx.app.uuid.v4();
  305. pos.lid = this.finalNode.id;
  306. pos.tid = this.ctx.tender.id;
  307. pos.add_stage = 0;
  308. pos.add_stage_order = 0;
  309. pos.add_times = 0;
  310. pos.in_time = new Date();
  311. pos.porder = this.finalNode.pos.length + 1;
  312. pos.add_user = this.ctx.session.sessionUser.accountId;
  313. this.finalNode.pos.push(pos);
  314. this.pos.push(pos);
  315. pos.sgfh_qty = this.ctx.helper.round(pos.sgfh_qty, this.finalPrecision.value);
  316. pos.quantity = this.ctx.helper.round(pos.quantity, this.finalPrecision.value);
  317. return pos;
  318. }
  319. }
  320. /**
  321. * 第一部分的子节点,顺序重排
  322. */
  323. resortFirstPartChildren () {
  324. const splitChar = this.splitChar;
  325. const firstPart = this.roots[0];
  326. firstPart.children.sort(function (a, b) {
  327. if (!a.code) {
  328. return 1;
  329. } else if (!b.code) {
  330. return -1;
  331. }
  332. if (a.error === b.error) {
  333. const codeA = a.code.split(splitChar);
  334. const numA = _.toNumber(codeA[codeA.length -1]);
  335. const codeB = b.code.split(splitChar);
  336. const numB = _.toNumber(codeB[codeB.length -1]);
  337. return numA - numB;
  338. } else if (a.error) {
  339. return 1
  340. } else if (b.error) {
  341. return -1;
  342. }
  343. });
  344. for (const [i, c] of firstPart.children.entries()) {
  345. c[this.setting.order] = i + 1;
  346. }
  347. }
  348. calculateLeafWithPos () {
  349. for (const node of this.items) {
  350. if (node.children && node.children.length > 0) {
  351. node.unit_price = null;
  352. node.quantity = null;
  353. node.total_price = null;
  354. }
  355. if (!node.pos || node.pos.length === 0) { continue; }
  356. node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  357. if (node.quantity && node.unit_price) {
  358. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  359. } else {
  360. node.total_price = null;
  361. }
  362. }
  363. }
  364. }
  365. class ImportStd18Tree extends ImportBaseTree {
  366. /**
  367. * 检查是否是父项
  368. * @param parent
  369. * @param code
  370. * @returns {boolean}
  371. * @private
  372. */
  373. _checkParent(parent, code) {
  374. const codeNumberPart = code.replace(gdXmjPartReg, '');
  375. if (!parent.code) return false;
  376. const numberPart = parent.code.replace(gdXmjPartReg, '');
  377. if (!numberPart || !codeNumberPart || numberPart.length >= codeNumberPart.length) return false;
  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. features: {value: ['项目特征'], type: colDefineType.match},
  519. };
  520. this.needCols = needCols || ['code', 'b_code', 'pos'];
  521. }
  522. _getNewCacheTree(tempData) {
  523. // 模板符合11编办规则,使用11编办树
  524. if (aeUtils.isMatch18(tempData)) {
  525. return new ImportStd18Tree(tempData, this.ctx, this.setting);
  526. // 反之使用11编办(未校验模板是否符合,替换注释部分即可实现)
  527. // } else if (aeUtils.isMatch11(tempData)){
  528. } else {
  529. return new ImportBaseTree(tempData, this.ctx, this.setting);
  530. }
  531. }
  532. /**
  533. * 读取项目节节点
  534. * @param {Array} row - excel行数据
  535. * @returns {*}
  536. * @private
  537. */
  538. _loadXmjNode(row) {
  539. try {
  540. const node = {};
  541. node.code = _.trimEnd(this.ctx.helper.replaceReturn(row[this.colsDef.code]));
  542. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  543. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  544. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  545. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  546. node.deal_tp = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.deal_tp]), this.decimal.tp);
  547. node.total_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.total_price]), this.decimal.tp);
  548. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  549. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  550. node.features = row[this.colsDef.features];
  551. this.ctx.helper.checkDgnQtyPrecision(node);
  552. return this.cacheTree.addXmjNode(node);
  553. } catch (error) {
  554. if (error.stack) {
  555. this.ctx.logger.error(error);
  556. } else {
  557. this.ctx.getLogger('fail').info(JSON.stringify({
  558. error,
  559. project: this.ctx.session.sessionProject,
  560. user: this.ctx.session.sessionUser,
  561. body: row,
  562. }));
  563. }
  564. return null;
  565. }
  566. }
  567. /**
  568. * 读取工程量清单数据
  569. * @param {Array} row - excel行数据
  570. * @returns {*}
  571. * @private
  572. */
  573. _loadGclNode(row) {
  574. if (this.filter.filterGcl) return true;
  575. const node = {};
  576. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  577. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  578. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  579. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  580. node.deal_qty = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.deal_qty]), precision.value);
  581. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  582. node.sgfh_qty = node.quantity;
  583. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  584. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  585. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  586. node.features = row[this.colsDef.features];
  587. node.deal_tp = node.deal_qty && node.unit_price ? this.ctx.helper.mul(node.deal_qty, node.unit_price, this.decimal.tp) : 0;
  588. if (node.quantity && node.unit_price) {
  589. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  590. } else {
  591. node.total_price = null;
  592. }
  593. if (this.filter.filterZeroGcl && !node.quantity && !node.total_price) return true;
  594. return this.cacheTree.addGclNode(node);
  595. }
  596. /**
  597. * 读取部位明细数据
  598. * @param {Array} row - excel行数据
  599. * @returns {*}
  600. * @private
  601. */
  602. _loadPos(row) {
  603. if (this.filter.filterPos) return true;
  604. const pos = {};
  605. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  606. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  607. pos.sgfh_qty = pos.quantity;
  608. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  609. return this.cacheTree.addPos(pos);
  610. }
  611. /**
  612. * 读取数据行
  613. * @param {Array} row - excel数据行
  614. * @param {Number} index - 行索引号
  615. */
  616. loadRowData(row, index) {
  617. let result;
  618. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  619. if (row[this.colsDef.code]) {
  620. // 第三部分(编号为'3')及之后的数据不读取
  621. if (row[this.colsDef.code] === '3') {
  622. this.loadEnd = true;
  623. return;
  624. }
  625. result = this._loadXmjNode(row)
  626. } else if (row[this.colsDef.pos]) {
  627. result = this._loadPos(row);
  628. } else {
  629. result = this._loadGclNode(row);
  630. }
  631. // 读取失败则写入错误数据 todo 返回前端告知用户?
  632. if (!result) {
  633. this.errorData.push({
  634. serialNo: index,
  635. data: row,
  636. });
  637. }
  638. }
  639. /**
  640. * 读取表头并检查
  641. * @param {Number} row - Excel数据行
  642. */
  643. checkColHeader(row) {
  644. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  645. let check = true;
  646. for (const col of this.needCols) {
  647. if (!colsDef[col]) check = false;
  648. }
  649. if (check) this.colsDef = colsDef;
  650. }
  651. /**
  652. * 将excel清单 平面数据 解析为 树结构数据
  653. * @param {object} sheet - excel清单数据
  654. * @param {Array} tempData - 新建项目使用的清单模板
  655. * @returns {ImportBaseTree}
  656. */
  657. analysisData(sheet, tempData, filter) {
  658. this.filter = filter ? filter : {};
  659. this.colsDef = null;
  660. this.cacheTree = this._getNewCacheTree(tempData);
  661. this.errorData = [];
  662. this.loadEnd = false;
  663. for (const iRow in sheet.rows) {
  664. const row = sheet.rows[iRow];
  665. if (this.colsDef && !this.loadEnd) {
  666. this.loadRowData(row, iRow);
  667. } else {
  668. this.checkColHeader(row);
  669. }
  670. }
  671. return this.cacheTree;
  672. }
  673. }
  674. class ImportGclBaseTree {
  675. /**
  676. * 构造函数
  677. * @param {Array} tempData - 清单模板数据
  678. */
  679. constructor (ctx, setting, parent, maxId, defaultData) {
  680. this.ctx = ctx;
  681. this.setting = setting;
  682. if (ctx.tender) {
  683. this.mid = ctx.tender.id;
  684. this.decimal = ctx.tender.info.decimal;
  685. this.precision = ctx.tender.info.precision;
  686. } else if (ctx.budget) {
  687. this.mid = ctx.budget.id;
  688. this.decimal = { up: 2, tp: 2};
  689. this.precision = {
  690. other: { value: 2 },
  691. };
  692. }
  693. this.parent = parent;
  694. this.defaultData = defaultData;
  695. // 常量
  696. this.splitChar = '-';
  697. // 索引
  698. // 以code为索引
  699. this.codeNodes = {};
  700. this.items = [];
  701. // 缓存
  702. this.keyNodeId = maxId ? maxId + 1 : 1;
  703. this.blankParent = null;
  704. }
  705. /**
  706. * 根据 编号 查找 父项项目节
  707. * @param {String} code - 子项编号
  708. * @returns {*}
  709. */
  710. findParent(code) {
  711. const codePath = code.split(this.splitChar);
  712. if (codePath.length > 1) {
  713. codePath.splice(codePath.length - 1, 1);
  714. return this.codeNodes[codePath.join(this.splitChar)];
  715. }
  716. }
  717. /**
  718. * 添加 树节点 并完善该节点的树结构
  719. * @param {Object} node - 添加节点
  720. * @param {Object} parent - 父项
  721. * @returns {*}
  722. */
  723. addNodeWithParent(node, parent) {
  724. parent = parent ? parent : this.parent;
  725. if (!parent.children) parent.children = [];
  726. node.id = this.ctx.app.uuid.v4();
  727. node[this.setting.mid] = this.mid;
  728. node[this.setting.kid] = this.keyNodeId;
  729. this.keyNodeId += 1;
  730. node[this.setting.pid] = parent[this.setting.kid];
  731. node[this.setting.level] = parent[this.setting.level] + 1;
  732. node[this.setting.order] = parent.children.length + 1;
  733. node[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + node[this.setting.kid];
  734. parent.children.push(node);
  735. node.children = [];
  736. if (this.defaultData) _.assignIn(node, this.defaultData);
  737. this.items.push(node);
  738. if (!_.isNil(node.b_code) && node.b_code !== '') {
  739. this.codeNodes[node.b_code] = node;
  740. } else {
  741. this.blankParent = node;
  742. }
  743. return node;
  744. }
  745. /**
  746. * 添加 项目节
  747. * @param {Object} node - 项目节
  748. * @returns {*}
  749. */
  750. addNode(node) {
  751. if (_.isNil(node.b_code) || node.b_code === '') {
  752. return this.addNodeWithParent(node, null);
  753. } else if (node.b_code.split(this.splitChar).length > 1) {
  754. const parent = this.findParent(node.b_code);
  755. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  756. } else {
  757. return this.addNodeWithParent(node, this.blankParent);
  758. }
  759. }
  760. }
  761. class AnalysisGclExcelTree {
  762. /**
  763. * 构造函数
  764. */
  765. constructor(ctx, setting) {
  766. this.ctx = ctx;
  767. this.setting = setting;
  768. if (ctx.tender) {
  769. this.mid = ctx.tender.id;
  770. this.decimal = ctx.tender.info.decimal;
  771. this.precision = ctx.tender.info.precision;
  772. } else if (ctx.budget) {
  773. this.mid = ctx.budget.id;
  774. this.decimal = { up: 2, tp: 2};
  775. this.precision = {
  776. other: { value: 2 },
  777. };
  778. }
  779. this.colsDef = null;
  780. this.colHeaderMatch = {
  781. b_code: {value: ['编号', '清单编号', '子目号', '子目编号', '清单号'], type: colDefineType.match},
  782. name: {value: ['名称', '清单名称', '子目名称'], type: colDefineType.match},
  783. unit: {value: ['单位'], type: colDefineType.pos},
  784. quantity: {value: ['数量'], type: colDefineType.pos}, // 施工图复核数量
  785. unit_price: {value: ['单价'], type: colDefineType.pos},
  786. };
  787. }
  788. /**
  789. * 读取表头并检查
  790. * @param {Number} row - Excel数据行
  791. */
  792. checkColHeader(row) {
  793. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  794. if (colsDef.b_code) {
  795. this.colsDef = colsDef;
  796. }
  797. }
  798. loadRowData(row) {
  799. const node = {};
  800. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  801. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  802. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  803. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  804. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  805. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  806. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  807. if (node.quantity && node.unit_price) {
  808. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  809. } else {
  810. node.total_price = null;
  811. }
  812. return this.cacheTree.addNode(node);
  813. }
  814. /**
  815. * 将excel清单 平面数据 解析为 树结构数据
  816. * @param {object} sheet - excel清单数据
  817. * @param {Array} parentId - 导入至的节点id
  818. * @returns {ImportBaseTree}
  819. */
  820. analysisData(sheet, parent, maxId, defaultData) {
  821. try {
  822. this.colsDef = null;
  823. this.cacheTree = new ImportGclBaseTree(this.ctx, this.setting, parent, maxId, defaultData);
  824. this.errorData = [];
  825. this.loadEnd = false;
  826. // 识别表头导入
  827. for (const iRow in sheet.rows) {
  828. const row = sheet.rows[iRow];
  829. if (this.colsDef && !this.loadEnd) {
  830. const result = this.loadRowData(row);
  831. // 读取失败则写入错误数据
  832. if (!result) {
  833. this.errorData.push({ serialNo: iRow, data: row });
  834. }
  835. } else {
  836. this.checkColHeader(row);
  837. }
  838. }
  839. // 固定列导入
  840. // this.colsDef = {
  841. // b_code: 0,
  842. // name: 1,
  843. // unit: 2,
  844. // quantity: 3,
  845. // unit_price: 4
  846. // };
  847. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  848. // const row = sheet.rows[iRow];
  849. // const result = this.loadRowData(row);
  850. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  851. // if (!result) {
  852. // this.errorData.push({
  853. // serialNo: iRow,
  854. // data: row,
  855. // });
  856. // }
  857. // }
  858. return this.cacheTree;
  859. } catch(err) {
  860. this.ctx.helper.log(err);
  861. }
  862. }
  863. }
  864. class AnalysisStageExcelTree extends AnalysisExcelTree {
  865. /**
  866. * 构造函数
  867. */
  868. constructor(ctx, setting) {
  869. super(ctx, setting);
  870. this.mid = ctx.tender.id;
  871. this.decimal = ctx.tender.info.decimal;
  872. this.precision = ctx.tender.info.precision;
  873. this.colsDef = null;
  874. this.colHeaderMatch = {
  875. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  876. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  877. pos: {value: ['计量单元'], type: colDefineType.match},
  878. name: {value: ['名称'], type: colDefineType.match},
  879. unit: {value: ['单位'], type: colDefineType.match},
  880. unit_price: {value: ['单价'], type: colDefineType.match},
  881. contract_qty: {value: ['本期合同计量|数量'], type: colDefineType.match},
  882. contract_tp: {value: ['本期合同计量|金额'], type: colDefineType.match},
  883. deal_dgn_qty1: {value: ['合同|项目节数量1'], type: colDefineType.match},
  884. deal_dgn_qty2: {value: ['合同|项目节数量2'], type: colDefineType.match},
  885. c_dgn_qty1: {value: ['变更|项目节数量1'], type: colDefineType.match},
  886. c_dgn_qty2: {value: ['变更|项目节数量2'], type: colDefineType.match},
  887. postil: {value: ['本期批注'], type: colDefineType.match}
  888. };
  889. this.needCols = ['code', 'b_code', 'pos', 'name', 'unit', 'unit_price', 'contract_qty', 'contract_tp'];
  890. }
  891. mergeHeaderRow(iRow, row, subRow, merge) {
  892. const result = [];
  893. for (let iCol = 0, iLen = row.length; iCol < iLen; iCol++) {
  894. const colMerge = merge.find(x => { return x.s.c === iCol && x.s.r === iRow});
  895. if (colMerge && colMerge.s.c !== colMerge.e.c) {
  896. let iSubCol = iCol;
  897. while (iSubCol <= colMerge.e.c) {
  898. result.push(row[iCol] + '|' + subRow[iSubCol]);
  899. iSubCol++;
  900. }
  901. iCol = colMerge.e.c;
  902. } else {
  903. result.push(row[iCol])
  904. }
  905. }
  906. return result;
  907. }
  908. /**
  909. * 读取项目节节点
  910. * @param {Array} row - excel行数据
  911. * @returns {*}
  912. * @private
  913. */
  914. _loadXmjNode(row) {
  915. try {
  916. const node = {};
  917. node.code = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.code]));
  918. node.name = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.name]));
  919. node.unit = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.unit]));
  920. const xmj = this.cacheTree.addXmjNode(node);
  921. if (xmj) {
  922. xmj.deal_dgn_qty1 = aeUtils.toNumber(row[this.colsDef.deal_dgn_qty1]);
  923. xmj.deal_dgn_qty2 = aeUtils.toNumber(row[this.colsDef.deal_dgn_qty2]);
  924. xmj.c_dgn_qty1 = aeUtils.toNumber(row[this.colsDef.c_dgn_qty1]);
  925. xmj.c_dgn_qty2 = aeUtils.toNumber(row[this.colsDef.c_dgn_qty2]);
  926. xmj.postil = this.ctx.helper.replaceReturn(row[this.colsDef.postil]);
  927. this.ctx.helper.checkDgnQtyPrecision(xmj);
  928. return xmj;
  929. } else {
  930. return null;
  931. }
  932. } catch (error) {
  933. if (error.stack) {
  934. this.ctx.logger.error(error);
  935. } else {
  936. this.ctx.getLogger('fail').info(JSON.stringify({
  937. error,
  938. project: this.ctx.session.sessionProject,
  939. user: this.ctx.session.sessionUser,
  940. body: row,
  941. }));
  942. }
  943. return null;
  944. }
  945. }
  946. /**
  947. * 读取工程量清单数据
  948. * @param {Array} row - excel行数据
  949. * @returns {*}
  950. * @private
  951. */
  952. _loadGclNode(row) {
  953. if (this.filter.filterGcl) return true;
  954. const node = {};
  955. node.b_code = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.b_code]));
  956. node.name = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.name]));
  957. node.unit = this.ctx.helper.replaceReturn(this.ctx.helper._.trimEnd(row[this.colsDef.unit]));
  958. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  959. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  960. node.contract_qty = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.contract_qty]), precision.value);
  961. if (node.contract_qty && node.unit_price) {
  962. node.contract_tp = this.ctx.helper.mul(node.contract_qty, node.unit_price, this.decimal.tp);
  963. } else {
  964. node.contract_tp = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.contract_tp]), this.decimal.tp);
  965. }
  966. node.postil = this.ctx.helper.replaceReturn(row[this.colsDef.postil]);
  967. if (this.filter.filterZeroGcl && !node.contract_qty && !node.contract_tp) return true;
  968. return this.cacheTree.addGclNode(node);
  969. }
  970. /**
  971. * 读取部位明细数据
  972. * @param {Array} row - excel行数据
  973. * @returns {*}
  974. * @private
  975. */
  976. _loadPos(row) {
  977. if (this.filter.filterPos) return true;
  978. let pos = {};
  979. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  980. pos.quantity = aeUtils.toNumber(row[this.colsDef.contract_qty]);
  981. pos = this.cacheTree.addPos(pos, true);
  982. pos.contract_qty = pos.quantity;
  983. pos.postil = this.ctx.helper.replaceReturn(row[this.colsDef.postil]);
  984. return pos;
  985. }
  986. /**
  987. * 将excel清单 平面数据 解析为 树结构数据
  988. * @param {object} sheet - excel清单数据
  989. * @param {Array} tempData - 新建项目使用的清单模板
  990. * @returns {ImportBaseTree}
  991. */
  992. analysisData(sheet, tempData, filter) {
  993. this.filter = filter ? filter : {};
  994. this.colsDef = null;
  995. this.cacheTree = this._getNewCacheTree(tempData);
  996. this.errorData = [];
  997. this.loadEnd = false;
  998. this.loadBegin = sheet.rows.length;
  999. for (const [iRow, row] of sheet.rows.entries()) {
  1000. if (this.colsDef && !this.loadEnd) {
  1001. if (iRow < this.loadBegin) continue;
  1002. this.loadRowData(row, iRow);
  1003. } else {
  1004. if (iRow === sheet.rows.length - 1) continue;
  1005. const mergeRow = this.mergeHeaderRow(iRow, row, sheet.rows[iRow + 1], sheet.merge);
  1006. this.checkColHeader(mergeRow);
  1007. if (this.colsDef) this.loadBegin = iRow + 2;
  1008. }
  1009. }
  1010. return this.cacheTree;
  1011. }
  1012. }
  1013. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree, AnalysisStageExcelTree };