template_node.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. /**
  3. * 指标节点业务类
  4. *
  5. * @author Mai
  6. * @date 2018/4/19
  7. * @version
  8. */
  9. module.exports = app => {
  10. class TemplateNode extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局context
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'template_node';
  20. }
  21. /**
  22. * 查找父节点(根据编号),忽略大小写
  23. * e.g. z1(z), z1-e(z1), z1-e-a(z1-e)
  24. * @param code
  25. * @param nodes
  26. * @returns {*}
  27. * @private
  28. */
  29. _findParentId(code, nodes) {
  30. if (nodes.length === 0) { return -1; }
  31. const codeList = code.split('-');
  32. if (codeList.length > 1) {
  33. codeList.splice(codeList.length - 1);
  34. const parentCode = codeList.join('-');
  35. for (const node of nodes) {
  36. if (parentCode.toLowerCase() === node.code.toLowerCase()) {
  37. return node.node_id;
  38. }
  39. }
  40. } else {
  41. for (const node of nodes) {
  42. if (code.toLowerCase().search(node.code.toLowerCase()) === 0) {
  43. return node.node_id;
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * 解析一个Excel工作表内的全部 指标节点 和 指标
  50. * @param {Object} excelSheet
  51. * @param {Array} nodes - 解析后的指标节点
  52. * @param {Array} indexes - 解析后的指标
  53. * @private
  54. */
  55. _parseSheetData(excelSheet, nodes, indexes) {
  56. for (const row of excelSheet.data) {
  57. if (!row[0]) { continue; }
  58. if (this.ctx.helper.ValidTemplateNodeCode(row[0])) {
  59. if (!this.ctx.helper.findObj(nodes, 'code', row[0])) {
  60. const node = {
  61. template_id: 1,
  62. node_id: nodes.length + 1,
  63. node_pid: this._findParentId(row[0], nodes) || -1,
  64. code: row[0],
  65. name: row[1],
  66. };
  67. nodes.push(node);
  68. }
  69. } else if (this.ctx.helper.ValidTemplateIndexCode(row[0])) {
  70. const index = {
  71. code: row[0],
  72. name: row[1],
  73. unit1: row[2],
  74. unit2: row[3],
  75. node_id: nodes.length,
  76. index_id: indexes.length + 1,
  77. rule: row[9]
  78. };
  79. if (row[4] === '√') {
  80. index.index_type = 1;
  81. } else if (row[5] === '√') {
  82. index.index_type = 2;
  83. } else if (row[6] === '√') {
  84. index.index_type = 3;
  85. } else if (row[7] === '√') {
  86. index.index_type = 4;
  87. }
  88. indexes.push(index);
  89. }
  90. }
  91. }
  92. /**
  93. * 导入Excel数据
  94. *
  95. * @param {Array} excelSheets - Excel文件中的全部工作表
  96. * @returns {Promise<boolean>}
  97. */
  98. async importData(excelSheets) {
  99. let result = false;
  100. const limit = 30000;
  101. const transaction = await this.db.beginTransaction();
  102. try {
  103. const nodes = [], indexes = [];
  104. for (const sheet of excelSheets) {
  105. this._parseSheetData(sheet, nodes, indexes);
  106. }
  107. if (nodes.length > 0) {
  108. await transaction.delete(this.tableName, {template_id: 1});
  109. const insertResult = await transaction.insert(this.tableName, nodes);
  110. if (insertResult.affectedRows !== nodes.length) {
  111. throw '导入指标节点错误';
  112. }
  113. await this.ctx.service.templateIndex.importData(indexes, transaction);
  114. } else {
  115. throw 'Excel文件中无标准的指标数据';
  116. }
  117. await transaction.commit();
  118. result = true;
  119. } catch(err) {
  120. await transaction.rollback();
  121. throw err;
  122. }
  123. return result;
  124. }
  125. }
  126. return TemplateNode;
  127. };