analysis_excel.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const _ = require('lodash');
  10. class ImportBaseTree {
  11. /**
  12. * 构造函数
  13. * @param {Array} tempData - 清单模板数据
  14. */
  15. constructor (tempData, ctx) {
  16. this.ctx = ctx;
  17. // 常量
  18. this.splitChar = '-';
  19. // 索引
  20. // 以code为索引
  21. this.codeNodes = {};
  22. this.items = [];
  23. this.roots = [];
  24. this.pos = [];
  25. this.tempData = [];
  26. // 缓存
  27. this.finalNode = null;
  28. this.finalPrecision = null;
  29. this.finalXmjNode = null;
  30. this.keyNodeId = 1;
  31. this._loadTemplateTree(tempData);
  32. }
  33. /**
  34. * 加载 清单模板
  35. * @param {Array} data - 模板数据
  36. * @private
  37. */
  38. _loadTemplateTree(data) {
  39. for (const node of data) {
  40. node.ledger_id = node.template_id;
  41. node.ledger_pid = node.pid;
  42. node.id = this.ctx.app.uuid.v4();
  43. delete node.pid;
  44. if (node.code) {
  45. this.codeNodes[node.code] = node;
  46. }
  47. this.items.push(node);
  48. if (node.ledger_pid === -1) {
  49. this.roots.push(node);
  50. }
  51. if (node.ledger_id >= this.keyNodeId) {
  52. this.keyNodeId = node.ledger_id + 1;
  53. }
  54. this.tempData.push(node);
  55. }
  56. for (const node of this.items) {
  57. node.tender_id = this.ctx.tender.id;
  58. node.children = this.items.filter(function (i) {
  59. return i.ledger_pid === node.ledger_id;
  60. });
  61. }
  62. }
  63. /**
  64. * 根据 编号、名称 查找模板节点
  65. * @param {Object} node - 要查找的节点
  66. * @returns {*}
  67. */
  68. findTempData(node) {
  69. return this.tempData.find(function (td) {
  70. return td.code === node.code && td.name === node.name;
  71. });
  72. }
  73. /**
  74. * 根据 编号 查找 父项项目节
  75. * @param {String} code - 子项编号
  76. * @returns {*}
  77. */
  78. findXmjParent(code) {
  79. const codePath = code.split(this.splitChar);
  80. if (codePath.length > 1) {
  81. codePath.splice(codePath.length - 1, 1);
  82. return this.codeNodes[codePath.join(this.splitChar)];
  83. }
  84. }
  85. /**
  86. * 添加 树节点 并完善该节点的树结构
  87. * @param {Object} node - 添加节点
  88. * @param {Object} parent - 父项
  89. * @returns {*}
  90. */
  91. addNodeWithParent(node, parent) {
  92. node.id = this.ctx.app.uuid.v4();
  93. node.ledger_id = this.keyNodeId;
  94. this.keyNodeId += 1;
  95. node.ledger_pid = parent ? parent.ledger_id : -1;
  96. node.level = parent ? parent.level + 1 : 1;
  97. node.order = parent ? parent.children.length + 1 : this.roots.length + 1;
  98. node.full_path = parent ? parent.full_path + '.' + node.ledger_id : '' + node.ledger_id;
  99. if (parent) {
  100. parent.children.push(node);
  101. } else {
  102. this.roots.push(node);
  103. }
  104. this.items.push(node);
  105. this.codeNodes[node.code] = node;
  106. this.defineCacheData(node);
  107. return node;
  108. }
  109. /**
  110. * 定义缓存节点(添加工程量清单、部位明细需使用缓存定位)
  111. * @param {Object} node - 当前添加的节点
  112. */
  113. defineCacheData(node) {
  114. this.finalNode = node;
  115. this.finalPrecision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  116. if (node.code) {
  117. this.finalXmjNode = node;
  118. }
  119. }
  120. /**
  121. * 添加 项目节
  122. * @param {Object} node - 项目节
  123. * @returns {*}
  124. */
  125. addXmjNode(node) {
  126. node.id = this.ctx.app.uuid.v4();
  127. node.tender_id = this.ctx.tender.id;
  128. node.children = [];
  129. if (node.code.split(this.splitChar).length > 1) {
  130. const temp = this.findTempData(node);
  131. if (temp) {
  132. this.defineCacheData(temp);
  133. return temp;
  134. } else {
  135. const parent = this.findXmjParent(node.code);
  136. return this.addNodeWithParent(node, parent);
  137. }
  138. } else {
  139. const n = this.codeNodes[node.code];
  140. if (!n) {
  141. return this.addNodeWithParent(node, null);
  142. } else {
  143. this.defineCacheData(n);
  144. return n;
  145. }
  146. }
  147. }
  148. /**
  149. * 添加 工程量清单
  150. * @param {Object} node - 工程量清单
  151. */
  152. addGclNode(node) {
  153. node.id = this.ctx.app.uuid.v4();
  154. node.tender_id = this.ctx.tender.id;
  155. node.pos = [];
  156. if (this.finalXmjNode) {
  157. return this.addNodeWithParent(node, this.finalXmjNode);
  158. }
  159. }
  160. /**
  161. * 添加 部位明细
  162. * @param {object} pos - 部位明细
  163. * @returns {*}
  164. */
  165. addPos (pos){
  166. if (this.finalNode && this.finalNode.pos) {
  167. pos.id = this.ctx.app.uuid.v4();
  168. pos.lid = this.finalNode.id;
  169. pos.tid = this.ctx.tender.id;
  170. pos.add_stage = 0;
  171. pos.add_times = 0;
  172. pos.in_time = new Date();
  173. pos.porder = this.finalNode.pos.length + 1;
  174. pos.add_user = this.ctx.session.sessionUser.accountId;
  175. this.finalNode.pos.push(pos);
  176. this.pos.push(pos);
  177. pos.sgfh_qty = this.ctx.helper.round(pos.sgfh_qty, this.finalPrecision.value);
  178. pos.quantity = pos.sgfh_qty;
  179. return pos;
  180. }
  181. }
  182. /**
  183. * 第一部分的子节点,顺序重排
  184. */
  185. resortFirstPartChildren () {
  186. const splitChar = this.splitChar;
  187. const firstPart = this.roots[0];
  188. firstPart.children.sort(function (a, b) {
  189. if (a.code === '') {
  190. return 1;
  191. } else if (b.code === '') {
  192. return -1;
  193. }
  194. const codeA = a.code.split(splitChar);
  195. const numA = _.toNumber(codeA[codeA.length -1]);
  196. const codeB = b.code.split(splitChar);
  197. const numB = _.toNumber(codeB[codeB.length -1]);
  198. return numA - numB;
  199. });
  200. }
  201. calculateLeafWithPos () {
  202. for (const node of this.items) {
  203. if (node.children && node.children.length > 0) { continue; }
  204. if (!node.pos || node.pos.length === 0) { continue; }
  205. // node.sgfh_qty = this.ctx.helper.sum(_.map(node.pos, 'sgfh_qty'));
  206. // if (node.sgfh_qty && node.unit_price) {
  207. // node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price),
  208. // this.ctx.tender.info.decimal.tp);
  209. // } else {
  210. // node.sgfh_tp = null;
  211. // }
  212. // node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
  213. // if (node.quantity && node.unit_price) {
  214. // node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price),
  215. // this.ctx.tender.info.decimal.tp);
  216. // } else {
  217. // node.total_price = null;
  218. // }
  219. node.sgfh_qty = this.ctx.helper.sum(_.map(node.pos, 'sgfh_qty'));
  220. if (node.sgfh_qty && node.unit_price) {
  221. node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  222. } else {
  223. node.sgfh_tp = null;
  224. }
  225. node.quantity = node.sgfh_qty;
  226. node.total_price = node.sgfh_tp;
  227. }
  228. }
  229. }
  230. class AnalysisExcelTree {
  231. /**
  232. * 构造函数
  233. */
  234. constructor(ctx) {
  235. this.ctx = ctx;
  236. this.colsDef = null;
  237. this.colHeaderMatch = {
  238. code: ['项目节编号', '预算项目节'],
  239. b_code: ['清单子目号', '清单编号', '子目号'],
  240. pos: ['部位明细'],
  241. name: ['名称'],
  242. unit: ['单位'],
  243. sgfh_qty: ['清单数量'], // 施工图复核数量
  244. dgn_qty1: ['设计数量1'],
  245. dgn_qty2: ['设计数量2'],
  246. unit_price: ['单价'],
  247. drawing_code: ['图号'],
  248. memo: ['备注'],
  249. };
  250. }
  251. toNumber (value) {
  252. let num = _.toNumber(value);
  253. return _.isNaN(num) ? null : num;
  254. }
  255. /**
  256. * 读取项目节节点
  257. * @param {Array} row - excel行数据
  258. * @returns {*}
  259. * @private
  260. */
  261. _loadXmjNode(row) {
  262. const node = {};
  263. node.code = this.ctx.helper.replaceReturn(row[this.colsDef.code]);
  264. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  265. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  266. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  267. node.sgfh_qty = this.ctx.helper.round(this.toNumber(row[this.colsDef.sgfh_qty]), precision.value);
  268. node.dgn_qty1 = this.toNumber(row[this.colsDef.dgn_qty1]);
  269. node.dgn_qty2 = this.toNumber(row[this.colsDef.dgn_qty2]);
  270. node.unit_price = this.toNumber(row[this.colsDef.unit_price]);
  271. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  272. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  273. // if (node.sgfh_qty && node.unit_price) {
  274. // node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price), this.ctx.tender.info.decimal.tp);
  275. // } else {
  276. // node.sgfh_tp = null;
  277. // }
  278. // node.quantity = node.sgfh_qty;
  279. // if (node.quantity && node.unit_price) {
  280. // node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price), this.ctx.tender.info.decimal.tp);
  281. // } else {
  282. // node.total_price = null;
  283. // }
  284. if (node.sgfh_qty && node.unit_price) {
  285. node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  286. } else {
  287. node.sgfh_tp = null;
  288. }
  289. node.quantity = node.sgfh_qty;
  290. node.total_price = node.sgfh_tp;
  291. return this.cacheTree.addXmjNode(node);
  292. }
  293. /**
  294. * 读取工程量清单数据
  295. * @param {Array} row - excel行数据
  296. * @returns {*}
  297. * @private
  298. */
  299. _loadGclNode(row) {
  300. const node = {};
  301. node.b_code = this.ctx.helper.replaceReturn(row[this.colsDef.b_code]);
  302. node.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  303. node.unit = this.ctx.helper.replaceReturn(row[this.colsDef.unit]);
  304. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, node.unit);
  305. node.sgfh_qty = this.ctx.helper.round(this.toNumber(row[this.colsDef.sgfh_qty]), precision.value);
  306. node.unit_price = this.toNumber(row[this.colsDef.unit_price]);
  307. node.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  308. node.memo = this.ctx.helper.replaceReturn(row[this.colsDef.memo]);
  309. // if (node.sgfh_qty && node.unit_price) {
  310. // node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price), this.ctx.tender.info.decimal.tp);
  311. // } else {
  312. // node.sgfh_tp = null;
  313. // }
  314. // node.quantity = node.sgfh_qty;
  315. // if (node.quantity && node.unit_price) {
  316. // node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price), this.ctx.tender.info.decimal.tp);
  317. // } else {
  318. // node.total_price = null;
  319. // }
  320. if (node.sgfh_qty && node.unit_price) {
  321. node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
  322. } else {
  323. node.sgfh_tp = null;
  324. }
  325. node.quantity = node.sgfh_qty;
  326. node.total_price = node.sgfh_tp;
  327. return this.cacheTree.addGclNode(node);
  328. }
  329. /**
  330. * 读取部位明细数据
  331. * @param {Array} row - excel行数据
  332. * @returns {*}
  333. * @private
  334. */
  335. _loadPos(row) {
  336. const pos = {};
  337. pos.name = this.ctx.helper.replaceReturn(row[this.colsDef.name]);
  338. pos.sgfh_qty = this.toNumber(row[this.colsDef.sgfh_qty]);
  339. pos.drawing_code = this.ctx.helper.replaceReturn(row[this.colsDef.drawing_code]);
  340. return this.cacheTree.addPos(pos);
  341. }
  342. /**
  343. * 读取数据行
  344. * @param {Array} row - excel数据行
  345. * @param {Number} index - 行索引号
  346. */
  347. loadRowData(row, index) {
  348. let result;
  349. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  350. if (row[this.colsDef.code]) {
  351. // 第三部分(编号为'3')及之后的数据不读取
  352. if (row[this.colsDef.code] === '3') {
  353. this.loadEnd = true;
  354. return;
  355. }
  356. result = this._loadXmjNode(row)
  357. } else if (row[this.colsDef.pos]) {
  358. result = this._loadPos(row);
  359. } else {
  360. result = this._loadGclNode(row);
  361. }
  362. // 读取失败则写入错误数据 todo 返回前端告知用户?
  363. if (!result) {
  364. this.errorData.push({
  365. serialNo: index,
  366. data: row,
  367. });
  368. }
  369. }
  370. /**
  371. * 读取表头并检查
  372. * @param {Number} row - Excel数据行
  373. */
  374. checkColHeader(row) {
  375. const colsDef = {};
  376. for (const iCol in row) {
  377. const text = row[iCol];
  378. for (const head in this.colHeaderMatch) {
  379. const match = this.colHeaderMatch[head];
  380. if (match.indexOf(text) >= 0) {
  381. colsDef[head] = iCol;
  382. }
  383. }
  384. }
  385. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  386. this.colsDef = colsDef;
  387. }
  388. }
  389. /**
  390. * 将excel清单 平面数据 解析为 树结构数据
  391. * @param {object} sheet - excel清单数据
  392. * @param {Array} tempData - 新建项目使用的清单模板
  393. * @returns {ImportBaseTree}
  394. */
  395. analysisData(sheet, tempData) {
  396. this.colsDef = null;
  397. this.cacheTree = new ImportBaseTree(tempData, this.ctx);
  398. this.errorData = [];
  399. this.loadEnd = false;
  400. for (const iRow in sheet.rows) {
  401. const row = sheet.rows[iRow];
  402. if (this.colsDef && !this.loadEnd) {
  403. this.loadRowData(row, iRow);
  404. } else {
  405. this.checkColHeader(row);
  406. }
  407. }
  408. this.cacheTree.resortFirstPartChildren();
  409. this.cacheTree.calculateLeafWithPos();
  410. return this.cacheTree;
  411. }
  412. }
  413. module.exports = AnalysisExcelTree;