analysis_excel.js 12 KB

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