analysis_excel.js 38 KB

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