helper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. /**
  3. * 指标模板控制器
  4. *
  5. * @author Mai
  6. * @data 2018/4/19
  7. * @version
  8. */
  9. const fs = require('fs');
  10. const streamToArray = require('stream-to-array');
  11. module.exports = {
  12. /**
  13. * 转换数据
  14. *
  15. * @param {Array} list - 数据库查找的数据
  16. * @param {Boolean} tree - 是否展示为树形结构
  17. * @param {String} id - id属性名
  18. * @param {String} pid - pid属性名
  19. * @return {Object} - 返回转换后的数据
  20. */
  21. convertData(list, tree = true, id, pid) {
  22. const rootData = [];
  23. const childData = {};
  24. let listData = [];
  25. for (const tmp of list) {
  26. if (tmp[pid] === 0 || tmp[pid] === -1) {
  27. rootData.push(tmp);
  28. continue;
  29. }
  30. if (childData[tmp[pid]] === undefined) {
  31. childData[tmp[pid]] = [];
  32. }
  33. childData[tmp[pid]].push(tmp);
  34. }
  35. // 递归组织数据
  36. if (tree) {
  37. this._recursionTreeData(rootData, childData, id);
  38. } else {
  39. this._recursionData(rootData, childData, listData, id);
  40. }
  41. return tree ? rootData : listData;
  42. },
  43. /**
  44. * 递归组织数组结构数据
  45. *
  46. * @param {Array} parent - 父节点
  47. * @param {Object} childData - 子元素(以pid为key的对象)
  48. * @param {Array} outputData - 处理后的数据
  49. * @param {String} id - id属性名
  50. * @return {void}
  51. */
  52. _recursionData(parent, childData, outputData = [], id) {
  53. for (const index in parent) {
  54. // 父节点先进数组
  55. outputData.push(parent[index]);
  56. // 判断是否存在子项
  57. if (childData[parent[index][id]] !== undefined) {
  58. // 子项作为父节点递归查找子项中是否还有子项
  59. const tmpParent = childData[parent[index][id]];
  60. // 已用的子项删除
  61. delete childData[parent[index][id]];
  62. // 递归元素
  63. this._recursionData(tmpParent, childData, outputData);
  64. }
  65. }
  66. },
  67. /**
  68. * 递归组织树结构数据
  69. *
  70. * @param {Array} parent - 父节点
  71. * @param {Object} childData - 子元素(以pid为key的对象)
  72. * @param {String} id - id属性名
  73. * @return {void}
  74. */
  75. _recursionTreeData(parent, childData = {}, id) {
  76. if (Object.keys(childData).length <= 0) {
  77. return;
  78. }
  79. for (const index in parent) {
  80. // 判断是否存在子项
  81. if (childData[parent[index][id]] !== undefined) {
  82. // 子项作为父节点递归查找子项中是否还有子项
  83. const tmpParent = childData[parent[index][id]];
  84. // 已用的子项删除
  85. delete childData[parent[index][id]];
  86. this._recursionTreeData(tmpParent, childData, id);
  87. // 递归完赋值回原数据
  88. parent[index].children = tmpParent;
  89. }
  90. }
  91. },
  92. /**
  93. * 将文件流的数据保存至本地文件
  94. * @param stream
  95. * @param fileName
  96. * @returns {Promise<void>}
  97. */
  98. async saveStreamFile(stream, fileName) {
  99. // 读取字节流
  100. const parts = await streamToArray(stream);
  101. // 转化为buffer
  102. const buffer = Buffer.concat(parts);
  103. // 写入文件
  104. await fs.writeFileSync(fileName, buffer);
  105. },
  106. /**
  107. * 检查code是否是指标模板数据
  108. * @param {String} code
  109. * @returns {boolean}
  110. */
  111. ValidTemplateCode(code) {
  112. const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
  113. const reg2 = /([a-z0-9]+$)/i;
  114. return reg1.test(code) && reg2.test(code);
  115. },
  116. /**
  117. * 检查code 是否是 指标节点编号
  118. * @param {String} code
  119. * @returns {boolean}
  120. */
  121. ValidTemplateNodeCode(code) {
  122. const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
  123. const reg2 = /([a-z0-9]+$)/i;
  124. const reg3 = /([\-][0-9]+$)/i;
  125. return reg1.test(code) && reg2.test(code) && (!reg3.test(code));
  126. },
  127. /**
  128. * 检查code 是否是 指标节点编号
  129. * @param {String} code
  130. * @returns {boolean}
  131. */
  132. ValidTemplateIndexCode(code) {
  133. const reg1 = /(^[a-z]+)([a-z0-9\-]*)/i;
  134. const reg2 = /([0-9]+$)/i;
  135. return reg1.test(code) && reg2.test(code);
  136. },
  137. findObj(arr, field, value) {
  138. if (arr.length === 0) { return undefined; }
  139. for (const a of arr) {
  140. if (a[field] && a[field] === value) {
  141. return a;
  142. }
  143. }
  144. return undefined;
  145. }
  146. }