analysis_excel.js 38 KB

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