analysis_excel.js 14 KB

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