analysis_excel.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. class AnalysisExcelTree {
  165. /**
  166. * 构造函数
  167. */
  168. constructor() {
  169. this.colsDef = null;
  170. this.colHeaderMatch = {
  171. code: ['项目节编号', '预算项目节'],
  172. b_code: ['清单子目号', '清单编号', '子目号'],
  173. pos: ['部位明细'],
  174. name: ['名称'],
  175. unit: ['单位'],
  176. quantity: ['清单数量'], // 施工图复核数量
  177. dgn_qty1: ['设计数量1'],
  178. dgn_qty2: ['设计数量2'],
  179. unit_price: ['单价'],
  180. drawing_code: ['图号'],
  181. memo: ['备注'],
  182. };
  183. }
  184. /**
  185. * 读取项目节节点
  186. * @param {Array} row - excel行数据
  187. * @returns {*}
  188. * @private
  189. */
  190. _loadXmjNode(row) {
  191. const node = {};
  192. node.code = row[this.colsDef.code];
  193. node.name = row[this.colsDef.name];
  194. node.unit = row[this.colsDef.unit];
  195. node.quantity = row[this.colsDef.quantity];
  196. node.dgn_qty1 = row[this.colsDef.dgn_qty1];
  197. node.dgn_qty2 = row[this.colsDef.dgn_qty2];
  198. node.unit_price = row[this.colsDef.unit_price];
  199. node.drawing_code = row[this.colsDef.drawing_code];
  200. node.memo = row[this.colsDef.memo];
  201. if (node.quantity && node.unit_price) {
  202. node.total_price = node.quantity * node.unit_price;
  203. } else {
  204. node.total_price = null;
  205. }
  206. return this.cacheTree.addXmjNode(node);
  207. }
  208. /**
  209. * 读取工程量清单数据
  210. * @param {Array} row - excel行数据
  211. * @returns {*}
  212. * @private
  213. */
  214. _loadGclNode(row) {
  215. const node = {};
  216. node.b_code = row[this.colsDef.b_code];
  217. node.name = row[this.colsDef.name];
  218. node.unit = row[this.colsDef.unit];
  219. node.quantity = row[this.colsDef.quantity];
  220. node.unit_price = row[this.colsDef.unit_price];
  221. node.drawing_code = row[this.colsDef.drawing_code];
  222. node.memo = row[this.colsDef.memo];
  223. if (node.quantity && node.unit_price) {
  224. node.total_price = node.quantity * node.unit_price;
  225. } else {
  226. node.total_price = null;
  227. }
  228. return this.cacheTree.addGclNode(node);
  229. }
  230. /**
  231. * 读取部位明细数据
  232. * @param {Array} row - excel行数据
  233. * @returns {*}
  234. * @private
  235. */
  236. _loadPos(row) {
  237. const pos = {};
  238. pos.name = row[this.colsDef.name];
  239. pos.quantity = row[this.colsDef.quantity];
  240. pos.drawing_code = row[this.colsDef.drawing_code];
  241. return this.cacheTree.addPos(pos);
  242. }
  243. /**
  244. * 读取数据行
  245. * @param {Array} row - excel数据行
  246. * @param {Number} index - 行索引号
  247. */
  248. loadRowData(row, index) {
  249. let result;
  250. // 含code识别为项目节,含posCode识别为部位明细,其他识别为工程量清单
  251. if (row[this.colsDef.code]) {
  252. // 第三部分(编号为'3')及之后的数据不读取
  253. if (row[this.colsDef.code] === '3') {
  254. this.loadEnd = true;
  255. return;
  256. }
  257. result = this._loadXmjNode(row)
  258. } else if (row[this.colsDef.pos]) {
  259. result = this._loadPos(row);
  260. } else {
  261. result = this._loadGclNode(row);
  262. }
  263. // 读取失败则写入错误数据 todo 返回前端告知用户?
  264. if (!result) {
  265. this.errorData.push({
  266. serialNo: index,
  267. data: row,
  268. });
  269. }
  270. }
  271. /**
  272. * 读取表头并检查
  273. * @param {Number} row - Excel数据行
  274. */
  275. checkColHeader(row) {
  276. const colsDef = {};
  277. for (const iCol in row) {
  278. const text = row[iCol];
  279. for (const head in this.colHeaderMatch) {
  280. const match = this.colHeaderMatch[head];
  281. if (match.indexOf(text) >= 0) {
  282. colsDef[head] = iCol;
  283. }
  284. }
  285. }
  286. if (colsDef.code && colsDef.b_code && colsDef.pos) {
  287. this.colsDef = colsDef;
  288. }
  289. }
  290. /**
  291. * 将excel清单 平面数据 解析为 树结构数据
  292. * @param {object} sheet - excel清单数据
  293. * @param {Array} tempData - 新建项目使用的清单模板
  294. * @returns {ImportBaseTree}
  295. */
  296. analysisData(sheet, tempData) {
  297. this.colsDef = null;
  298. this.cacheTree = new ImportBaseTree(tempData);
  299. this.errorData = [];
  300. this.loadEnd = false;
  301. for (const iRow in sheet.rows) {
  302. const row = sheet.rows[iRow];
  303. if (this.colsDef && !this.loadEnd) {
  304. this.loadRowData(row, iRow);
  305. } else {
  306. this.checkColHeader(row);
  307. }
  308. }
  309. return this.cacheTree;
  310. }
  311. }
  312. module.exports = AnalysisExcelTree;