analysis_excel.js 14 KB

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