analysis_excel.js 11 KB

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