analysis_excel.js 11 KB

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