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) {
  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 = _.sum(_.map(node.pos, 'quantity'));
  187. if (node.quantity && node.unit_price) {
  188. node.total_price = node.quantity * node.unit_price;
  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. if (value) {
  217. return _.isNumber(value) ? value : _.toNumber(value);
  218. } else {
  219. return null;
  220. }
  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 = node.quantity * node.unit_price;
  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 = node.quantity * node.unit_price;
  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);
  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;