analysis_excel.js 14 KB

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