analysis_excel.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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){
  283. if (this.finalNode && this.finalNode.pos) {
  284. pos.id = this.ctx.app.uuid.v4();
  285. pos.lid = this.finalNode.id;
  286. pos.tid = this.ctx.tender.id;
  287. pos.add_stage = 0;
  288. pos.add_stage_order = 0;
  289. pos.add_times = 0;
  290. pos.in_time = new Date();
  291. pos.porder = this.finalNode.pos.length + 1;
  292. pos.add_user = this.ctx.session.sessionUser.accountId;
  293. this.finalNode.pos.push(pos);
  294. this.pos.push(pos);
  295. pos.sgfh_qty = this.ctx.helper.round(pos.sgfh_qty, this.finalPrecision.value);
  296. pos.quantity = this.ctx.helper.round(pos.quantity, this.finalPrecision.value);
  297. return pos;
  298. }
  299. }
  300. /**
  301. * 第一部分的子节点,顺序重排
  302. */
  303. resortFirstPartChildren () {
  304. const splitChar = this.splitChar;
  305. const firstPart = this.roots[0];
  306. firstPart.children.sort(function (a, b) {
  307. if (!a.code) {
  308. return 1;
  309. } else if (!b.code) {
  310. return -1;
  311. }
  312. if (a.error === b.error) {
  313. const codeA = a.code.split(splitChar);
  314. const numA = _.toNumber(codeA[codeA.length -1]);
  315. const codeB = b.code.split(splitChar);
  316. const numB = _.toNumber(codeB[codeB.length -1]);
  317. return numA - numB;
  318. } else if (a.error) {
  319. return 1
  320. } else if (b.error) {
  321. return -1;
  322. }
  323. });
  324. for (const [i, c] of firstPart.children.entries()) {
  325. c[this.setting.order] = i + 1;
  326. }
  327. }
  328. calculateLeafWithPos () {
  329. for (const node of this.items) {
  330. if (node.children && node.children.length > 0) {
  331. node.unit_price = null;
  332. node.quantity = null;
  333. node.total_price = null;
  334. }
  335. if (!node.pos || node.pos.length === 0) { continue; }
  336. node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  337. if (node.quantity && node.unit_price) {
  338. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  339. } else {
  340. node.total_price = null;
  341. }
  342. }
  343. }
  344. }
  345. class ImportStd18Tree extends ImportBaseTree {
  346. /**
  347. * 检查是否是父项
  348. * @param parent
  349. * @param code
  350. * @returns {boolean}
  351. * @private
  352. */
  353. _checkParent(parent, code) {
  354. const codeNumberPart = code.replace(gdXmjPartReg, '');
  355. if (!parent.code) return false;
  356. const numberPart = parent.code.replace(gdXmjPartReg, '');
  357. if (!numberPart || !codeNumberPart || numberPart.length >= codeNumberPart.length) return false;
  358. return code.indexOf(numberPart) === 0 ||
  359. code.indexOf('G' + numberPart) === 0 ||
  360. code.indexOf('GD' + numberPart) === 0;
  361. }
  362. /**
  363. * 查找主表项目节父项
  364. * @param code
  365. * @returns {*}
  366. */
  367. findMainXmjParent(code) {
  368. const numberPart = code.replace(gdXmjPartReg, '');
  369. if (numberPart.length <= 1) throw '首层项目节模板中未定义,不支持导入';
  370. let parent = this.cacheMainXmjNode;
  371. while (parent) {
  372. if (this._checkParent(parent, code)) return parent;
  373. parent = this.nodes[parent[this.setting.pid]];
  374. }
  375. return null;
  376. }
  377. /**
  378. * 查找分表项目节父项
  379. * @param code
  380. * @returns {*}
  381. */
  382. findSubXmjParent(code) {
  383. let parent = this.cacheSubXmjNode;
  384. while (parent && parent.is_sub && parent.code.match(subReg)) {
  385. if (this._checkParent(parent, code)) return parent;
  386. parent = this.nodes[parent[this.setting.pid]];
  387. }
  388. return this.cacheMainXmjNode;
  389. }
  390. /**
  391. * 根据 编号 查找 父项项目节
  392. * @param {String} code - 子项编号
  393. * @returns {*}
  394. */
  395. findXmjParent(code) {
  396. if (code.match(mainReg)) {
  397. if (!this.cacheMainXmjNode) throw '主表项目节找不到父项';
  398. return this.findMainXmjParent(code);
  399. } else if (code.match(subReg)) {
  400. if (!this.cacheMainXmjNode) throw '分表项目节找不到所属主表项目节';
  401. return this.findSubXmjParent(code);
  402. }
  403. }
  404. /**
  405. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  406. * @param {Object} node - 当前添加的节点
  407. */
  408. defineCacheData(node) {
  409. super.defineCacheData(node);
  410. if (node.code) {
  411. if (node.code.match(mainReg)) {
  412. node.is_main = true;
  413. this.cacheMainXmjNode = node;
  414. this.cacheSubXmjNode = null;
  415. } else if (node.code.match(subReg)) {
  416. node.is_sub = true;
  417. this.cacheSubXmjNode = node;
  418. }
  419. if (node.code.match(specCode106.reg)) {
  420. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg)) {
  421. this.cacheSpecMainXmj2 = node;
  422. } else {
  423. this.cacheSpecMainXmj1 = node;
  424. }
  425. } else if (node.code.match(specCode109.reg)) {
  426. this.cacheSpecMainXmj1 = node;
  427. this.cacheSpecMainXmj2 = null;
  428. }
  429. }
  430. }
  431. /**
  432. * 添加 项目节
  433. * @param {Object} node - 项目节
  434. * @returns {*}
  435. */
  436. addXmjNode(node) {
  437. if (!node.code || (!node.code.match(mainReg) && !node.code.match(subReg))) return null;
  438. node.id = this.ctx.app.uuid.v4();
  439. node.children = [];
  440. if ((specCode106.code.indexOf(node.code) >= 0)) {
  441. if (this.cacheSpecMainXmj2 && this.cacheSpecMainXmj2.code.match(specCode106.reg))
  442. return this.addNodeWithParent(node, this.cacheSpecMainXmj2);
  443. if (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode106.reg))
  444. return this.addNodeWithParent(node, this.cacheSpecMainXmj1);
  445. }
  446. if ((specCode109.code.indexOf(node.code) >= 0) &&
  447. (this.cacheSpecMainXmj1 && this.cacheSpecMainXmj1.code.match(specCode109.reg))) {
  448. return this.addNodeWithParent(node, this.cacheSpecMainXmj1)
  449. }
  450. const temp = this.findTempData(node);
  451. if (temp) {
  452. this.defineCacheData(temp);
  453. this._assignRelaField(temp, node);
  454. return temp;
  455. } else {
  456. const parent = this.findXmjParent(node.code);
  457. return this.addNodeWithParent(node, parent);
  458. }
  459. }
  460. }
  461. class AnalysisExcelTree {
  462. /**
  463. * 构造函数
  464. */
  465. constructor(ctx, setting, needCols) {
  466. this.ctx = ctx;
  467. this.setting = setting;
  468. if (ctx.tender) {
  469. this.mid = ctx.tender.id;
  470. this.decimal = ctx.tender.info.decimal;
  471. this.precision = ctx.tender.info.precision;
  472. } else if (ctx.budget) {
  473. this.mid = ctx.budget.id;
  474. this.decimal = ctx.budget.decimal;
  475. this.precision = {
  476. other: { value: ctx.budget.decimal.qty },
  477. };
  478. }
  479. this.colsDef = null;
  480. this.colHeaderMatch = {
  481. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  482. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  483. pos: {value: ['计量单元'], type: colDefineType.match},
  484. name: {value: ['名称'], type: colDefineType.match},
  485. unit: {value: ['单位'], type: colDefineType.match},
  486. quantity: {value: ['清单数量'], type: colDefineType.match},
  487. dgn_qty1: {value: ['设计数量1'], type: colDefineType.match},
  488. dgn_qty2: {value: ['设计数量2'], type: colDefineType.match},
  489. unit_price: {value: ['单价'], type: colDefineType.match},
  490. total_price: {value: ['金额', '合价'], type: colDefineType.match},
  491. drawing_code: {value: ['图号', '图册号'], type: colDefineType.match},
  492. memo: {value: ['备注'], type: colDefineType.match},
  493. };
  494. this.needCols = needCols || ['code', 'b_code', 'pos'];
  495. }
  496. _getNewCacheTree(tempData) {
  497. // 模板符合11编办规则,使用11编办树
  498. if (aeUtils.isMatch18(tempData)) {
  499. return new ImportStd18Tree(tempData, this.ctx, this.setting);
  500. // 反之使用11编办(未校验模板是否符合,替换注释部分即可实现)
  501. // } else if (aeUtils.isMatch11(tempData)){
  502. } else {
  503. return new ImportBaseTree(tempData, this.ctx, this.setting);
  504. }
  505. }
  506. /**
  507. * 读取项目节节点
  508. * @param {Array} row - excel行数据
  509. * @returns {*}
  510. * @private
  511. */
  512. _loadXmjNode(row) {
  513. try {
  514. const node = {};
  515. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  516. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  517. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  518. node.dgn_qty1 = aeUtils.toNumber(row[this.colsDef.dgn_qty1]);
  519. node.dgn_qty2 = aeUtils.toNumber(row[this.colsDef.dgn_qty2]);
  520. node.total_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.total_price]), this.decimal.tp);
  521. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  522. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  523. return this.cacheTree.addXmjNode(node);
  524. } catch (error) {
  525. if (error.stack) {
  526. this.ctx.logger.error(error);
  527. } else {
  528. this.ctx.getLogger('fail').info(JSON.stringify({
  529. error,
  530. project: this.ctx.session.sessionProject,
  531. user: this.ctx.session.sessionUser,
  532. body: row,
  533. }));
  534. }
  535. return null;
  536. }
  537. }
  538. /**
  539. * 读取工程量清单数据
  540. * @param {Array} row - excel行数据
  541. * @returns {*}
  542. * @private
  543. */
  544. _loadGclNode(row) {
  545. if (this.filter.filterGcl) return true;
  546. const node = {};
  547. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  548. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  549. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  550. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  551. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  552. node.sgfh_qty = node.quantity;
  553. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  554. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  555. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  556. if (node.quantity && node.unit_price) {
  557. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  558. } else {
  559. node.total_price = null;
  560. }
  561. if (this.filter.filterZeroGcl && !node.quantity && !node.total_price) return true;
  562. return this.cacheTree.addGclNode(node);
  563. }
  564. /**
  565. * 读取部位明细数据
  566. * @param {Array} row - excel行数据
  567. * @returns {*}
  568. * @private
  569. */
  570. _loadPos(row) {
  571. if (this.filter.filterPos) return true;
  572. const pos = {};
  573. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  574. pos.quantity = aeUtils.toNumber(row[this.colsDef.quantity]);
  575. pos.sgfh_qty = pos.quantity;
  576. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  577. return this.cacheTree.addPos(pos);
  578. }
  579. /**
  580. * 读取数据行
  581. * @param {Array} row - excel数据行
  582. * @param {Number} index - 行索引号
  583. */
  584. loadRowData(row, index) {
  585. let result;
  586. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  587. if (row[this.colsDef.code]) {
  588. // 第三部分(编号为'3')及之后的数据不读取
  589. if (row[this.colsDef.code] === '3') {
  590. this.loadEnd = true;
  591. return;
  592. }
  593. result = this._loadXmjNode(row)
  594. } else if (row[this.colsDef.pos]) {
  595. result = this._loadPos(row);
  596. } else {
  597. result = this._loadGclNode(row);
  598. }
  599. // 读取失败则写入错误数据 todo 返回前端告知用户?
  600. if (!result) {
  601. this.errorData.push({
  602. serialNo: index,
  603. data: row,
  604. });
  605. }
  606. }
  607. /**
  608. * 读取表头并检查
  609. * @param {Number} row - Excel数据行
  610. */
  611. checkColHeader(row) {
  612. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  613. let check = true;
  614. for (const col of this.needCols) {
  615. if (!colsDef[col]) check = false;
  616. }
  617. if (check) this.colsDef = colsDef;
  618. }
  619. /**
  620. * 将excel清单 平面数据 解析为 树结构数据
  621. * @param {object} sheet - excel清单数据
  622. * @param {Array} tempData - 新建项目使用的清单模板
  623. * @returns {ImportBaseTree}
  624. */
  625. analysisData(sheet, tempData, filter) {
  626. this.filter = filter ? filter : {};
  627. this.colsDef = null;
  628. this.cacheTree = this._getNewCacheTree(tempData);
  629. this.errorData = [];
  630. this.loadEnd = false;
  631. for (const iRow in sheet.rows) {
  632. const row = sheet.rows[iRow];
  633. if (this.colsDef && !this.loadEnd) {
  634. this.loadRowData(row, iRow);
  635. } else {
  636. this.checkColHeader(row);
  637. }
  638. }
  639. return this.cacheTree;
  640. }
  641. }
  642. class ImportGclBaseTree {
  643. /**
  644. * 构造函数
  645. * @param {Array} tempData - 清单模板数据
  646. */
  647. constructor (ctx, setting, parent, maxId, defaultData) {
  648. this.ctx = ctx;
  649. this.setting = setting;
  650. if (ctx.tender) {
  651. this.mid = ctx.tender.id;
  652. this.decimal = ctx.tender.info.decimal;
  653. this.precision = ctx.tender.info.precision;
  654. } else if (ctx.budget) {
  655. this.mid = ctx.budget.id;
  656. this.decimal = { up: 2, tp: 2};
  657. this.precision = {
  658. other: { value: 2 },
  659. };
  660. }
  661. this.parent = parent;
  662. this.defaultData = defaultData;
  663. // 常量
  664. this.splitChar = '-';
  665. // 索引
  666. // 以code为索引
  667. this.codeNodes = {};
  668. this.items = [];
  669. // 缓存
  670. this.keyNodeId = maxId ? maxId + 1 : 1;
  671. this.blankParent = null;
  672. }
  673. /**
  674. * 根据 编号 查找 父项项目节
  675. * @param {String} code - 子项编号
  676. * @returns {*}
  677. */
  678. findParent(code) {
  679. const codePath = code.split(this.splitChar);
  680. if (codePath.length > 1) {
  681. codePath.splice(codePath.length - 1, 1);
  682. return this.codeNodes[codePath.join(this.splitChar)];
  683. }
  684. }
  685. /**
  686. * 添加 树节点 并完善该节点的树结构
  687. * @param {Object} node - 添加节点
  688. * @param {Object} parent - 父项
  689. * @returns {*}
  690. */
  691. addNodeWithParent(node, parent) {
  692. parent = parent ? parent : this.parent;
  693. if (!parent.children) parent.children = [];
  694. node.id = this.ctx.app.uuid.v4();
  695. node[this.setting.mid] = this.mid;
  696. node[this.setting.kid] = this.keyNodeId;
  697. this.keyNodeId += 1;
  698. node[this.setting.pid] = parent[this.setting.kid];
  699. node[this.setting.level] = parent[this.setting.level] + 1;
  700. node[this.setting.order] = parent.children.length + 1;
  701. node[this.setting.fullPath] = parent[this.setting.fullPath] + '-' + node[this.setting.kid];
  702. parent.children.push(node);
  703. node.children = [];
  704. if (this.defaultData) _.assignIn(node, this.defaultData);
  705. this.items.push(node);
  706. if (!_.isNil(node.b_code) && node.b_code !== '') {
  707. this.codeNodes[node.b_code] = node;
  708. } else {
  709. this.blankParent = node;
  710. }
  711. return node;
  712. }
  713. /**
  714. * 添加 项目节
  715. * @param {Object} node - 项目节
  716. * @returns {*}
  717. */
  718. addNode(node) {
  719. if (_.isNil(node.b_code) || node.b_code === '') {
  720. return this.addNodeWithParent(node, null);
  721. } else if (node.b_code.split(this.splitChar).length > 1) {
  722. const parent = this.findParent(node.b_code);
  723. return this.addNodeWithParent(node, parent ? parent : this.blankParent);
  724. } else {
  725. return this.addNodeWithParent(node, this.blankParent);
  726. }
  727. }
  728. }
  729. class AnalysisGclExcelTree {
  730. /**
  731. * 构造函数
  732. */
  733. constructor(ctx, setting) {
  734. this.ctx = ctx;
  735. this.setting = setting;
  736. if (ctx.tender) {
  737. this.mid = ctx.tender.id;
  738. this.decimal = ctx.tender.info.decimal;
  739. this.precision = ctx.tender.info.precision;
  740. } else if (ctx.budget) {
  741. this.mid = ctx.budget.id;
  742. this.decimal = { up: 2, tp: 2};
  743. this.precision = {
  744. other: { value: 2 },
  745. };
  746. }
  747. this.colsDef = null;
  748. this.colHeaderMatch = {
  749. b_code: {value: ['编号', '清单编号', '子目号', '子目编号', '清单号'], type: colDefineType.match},
  750. name: {value: ['名称', '清单名称', '子目名称'], type: colDefineType.match},
  751. unit: {value: ['单位'], type: colDefineType.pos},
  752. quantity: {value: ['数量'], type: colDefineType.pos}, // 施工图复核数量
  753. unit_price: {value: ['单价'], type: colDefineType.pos},
  754. };
  755. }
  756. /**
  757. * 读取表头并检查
  758. * @param {Number} row - Excel数据行
  759. */
  760. checkColHeader(row) {
  761. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  762. if (colsDef.b_code) {
  763. this.colsDef = colsDef;
  764. }
  765. }
  766. loadRowData(row) {
  767. const node = {};
  768. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  769. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  770. if ((_.isNil(node.b_code) || node.b_code === '') && (_.isNil(node.name) || node.name === '')) return node;
  771. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  772. const precision = this.ctx.helper.findPrecision(this.precision, node.unit);
  773. node.quantity = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.quantity]), precision.value);
  774. node.unit_price = this.ctx.helper.round(aeUtils.toNumber(row[this.colsDef.unit_price]), this.decimal.up);
  775. if (node.quantity && node.unit_price) {
  776. node.total_price = this.ctx.helper.mul(node.quantity, node.unit_price, this.decimal.tp);
  777. } else {
  778. node.total_price = null;
  779. }
  780. return this.cacheTree.addNode(node);
  781. }
  782. /**
  783. * 将excel清单 平面数据 解析为 树结构数据
  784. * @param {object} sheet - excel清单数据
  785. * @param {Array} parentId - 导入至的节点id
  786. * @returns {ImportBaseTree}
  787. */
  788. analysisData(sheet, parent, maxId, defaultData) {
  789. try {
  790. this.colsDef = null;
  791. this.cacheTree = new ImportGclBaseTree(this.ctx, this.setting, parent, maxId, defaultData);
  792. this.errorData = [];
  793. this.loadEnd = false;
  794. // 识别表头导入
  795. for (const iRow in sheet.rows) {
  796. const row = sheet.rows[iRow];
  797. if (this.colsDef && !this.loadEnd) {
  798. const result = this.loadRowData(row);
  799. // 读取失败则写入错误数据
  800. if (!result) {
  801. this.errorData.push({ serialNo: iRow, data: row });
  802. }
  803. } else {
  804. this.checkColHeader(row);
  805. }
  806. }
  807. // 固定列导入
  808. // this.colsDef = {
  809. // b_code: 0,
  810. // name: 1,
  811. // unit: 2,
  812. // quantity: 3,
  813. // unit_price: 4
  814. // };
  815. // for (let iRow = 1, iLen = sheet.rows.length; iRow < iLen; iRow++) {
  816. // const row = sheet.rows[iRow];
  817. // const result = this.loadRowData(row);
  818. // // 读取失败则写入错误数据 todo 返回前端告知用户?
  819. // if (!result) {
  820. // this.errorData.push({
  821. // serialNo: iRow,
  822. // data: row,
  823. // });
  824. // }
  825. // }
  826. return this.cacheTree;
  827. } catch(err) {
  828. this.ctx.helper.log(err);
  829. }
  830. }
  831. }
  832. class ImportStageBaseTree {
  833. }
  834. class AnalysisStageTree {
  835. /**
  836. * 构造函数
  837. */
  838. constructor(ctx, setting) {
  839. this.ctx = ctx;
  840. this.setting = setting;
  841. this.mid = ctx.tender.id;
  842. this.decimal = ctx.tender.info.decimal;
  843. this.precision = ctx.tender.info.precision;
  844. this.colsDef = null;
  845. this.colHeaderMatch = {
  846. code: {value: ['项目节编号', '预算项目节'], type: colDefineType.match},
  847. b_code: {value: ['清单子目号', '清单编号', '子目号'], type: colDefineType.match},
  848. pos: {value: ['计量单元'], type: colDefineType.match},
  849. name: {value: ['名称'], type: colDefineType.match},
  850. unit: {value: ['单位'], type: colDefineType.match},
  851. contract_qty: {value: ['本期合同计量|数量'], type: colDefineType.match},
  852. contract_tp: {value: ['本期合同计量|金额'], type: colDefineType.match},
  853. };
  854. this.needCols = ['code', 'b_code', 'pos', 'name', 'unit', 'contract_qty', 'contract_tp'];
  855. }
  856. /**
  857. * 读取表头并检查
  858. * @param {Number} row - Excel数据行
  859. */
  860. checkColHeader(row) {
  861. const colsDef = aeUtils.checkColHeader(row, this.colHeaderMatch);
  862. let check = true;
  863. for (const col of this.needCols) {
  864. if (!colsDef[col]) check = false;
  865. }
  866. if (check) this.colsDef = colsDef;
  867. }
  868. mergeHeaderRow(iRow, row, subRow, merge) {
  869. const result = [];
  870. for (let iCol = 0, iLen = row.length; iCol < iLen; iCol++) {
  871. const colMerge = merge.find(x => { return x.s.c === iCol && x.s.r === iRow});
  872. if (colMerge && colMerge.s.c !== colMerge.e.c) {
  873. let iSubCol = iCol;
  874. while (iSubCol <= colMerge.e.c) {
  875. result.push(row[iCol] + '|' + subRow[iSubCol]);
  876. iSubCol++;
  877. }
  878. iCol = colMerge.e.c;
  879. } else {
  880. result.push(row[iCol])
  881. }
  882. }
  883. return result;
  884. }
  885. loadRowData(row, iRow) {
  886. }
  887. /**
  888. * 将excel清单 平面数据 解析为 树结构数据
  889. * @param {object} sheet - excel清单数据
  890. * @param {Array} tempData - 新建项目使用的清单模板
  891. * @returns {ImportBaseTree}
  892. */
  893. analysisData(ledger, pos, sheet) {
  894. this.filter = filter ? filter : {};
  895. this.colsDef = null;
  896. this.cacheTree = this._getNewLedger();
  897. this.errorData = [];
  898. this.loadEnd = false;
  899. this.loadBegin = sheet.rows.length;
  900. for (const iRow in sheet.rows) {
  901. if (this.colsDef && !this.loadEnd) {
  902. if (iRow < this.loadBegin) continue;
  903. this.loadRowData(sheet.rows[iRow], iRow);
  904. } else {
  905. const mergeRow = this.mergeHeaderRow(iRow, sheet.rows[iRow], sheet.rows[iRow + 1], sheet.merge);
  906. this.checkColHeader(mergeRow);
  907. if (this.colsDef) this.loadBegin = iRow + 2;
  908. }
  909. }
  910. return this.cacheTree;
  911. }
  912. }
  913. module.exports = { AnalysisExcelTree, AnalysisGclExcelTree, AnalysisStageTree };