analysis_excel.js 36 KB

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