template_node.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. /**
  3. * 指标节点业务类
  4. *
  5. * @author Mai
  6. * @date 2018/4/19
  7. * @version
  8. */
  9. const paramCode = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  10. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad', 'ae', 'af',
  11. 'ag', 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au',
  12. 'av', 'aw', 'ax', 'ay', 'az'];
  13. const paramConst = require('../const/template_param');
  14. const nodeConst = require('../const/template_node');
  15. module.exports = app => {
  16. class TemplateNode extends app.BaseService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局context
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. this.tableName = 'template_node';
  26. }
  27. /**
  28. * 加载默认节点参数
  29. * @param {Array} params - 参数数组
  30. * @param {Array} defaultParams - 默认参数数组
  31. * @param {Number} nodeId - 指标节点Id
  32. * @param {Number} tempalteId - 指标模板Id
  33. * @private
  34. */
  35. _loadDefaultParam(params, defaultParams, nodeId, templateId = 1) {
  36. const newParams = JSON.parse(JSON.stringify(defaultParams));
  37. for (const p of newParams) {
  38. p.node_id = nodeId;
  39. p.template_id = templateId;
  40. params.push(p);
  41. }
  42. }
  43. /**
  44. * 过滤指标节点的参数(含节点参数初始化)
  45. * @param {Array} params - 全部参数数组
  46. * @param {Number} nodeId - 指标节点Id
  47. * @private
  48. */
  49. _filterNodeParams(params, nodeId) {
  50. let nodeParams = params.filter(function (p) {
  51. return p.node_id === nodeId;
  52. });
  53. if (nodeParams.length > 0) {
  54. return nodeParams;
  55. } else {
  56. this._loadDefaultParam(params, paramConst.defaultNodeParams, nodeId);
  57. return params.filter(function (p) {
  58. return p.node_id === nodeId;
  59. });
  60. }
  61. }
  62. /**
  63. * 从计算规则中解析出指标参数
  64. * @param {String} rule - 指标规则
  65. * @param {Number} nodeId - 指标节点id
  66. * @param {Array} params - 参数列表
  67. * @param {Number} templateId - 解析至的模板Id
  68. * @returns {*[]}
  69. * @private
  70. */
  71. _parseParam(rule, nodeId, params, templateId = 1) {
  72. if (rule === '') { return ['', '']; }
  73. const self = this;
  74. const ruleParams = this.ctx.helper.splitByOperator(rule);
  75. const codeParams = [];
  76. const parseParams = [];
  77. const nodeParams = this._filterNodeParams(params, nodeId);
  78. const addParam = function (paramName) {
  79. if (paramName === '') { return ''; }
  80. let param = self.ctx.helper.findObj(paramConst.defaultGlobalParams, 'name', paramName);
  81. if (!param) {
  82. param = self.ctx.helper.findObj(nodeParams, 'name', paramName);
  83. }
  84. if (!param) {
  85. const newParam = {
  86. template_id: templateId,
  87. node_id: nodeId,
  88. param_id: nodeParams.length + 1,
  89. code: paramCode[nodeParams.length],
  90. name: paramName,
  91. match_type: paramConst.matchType.non_match,
  92. };
  93. nodeParams.push(newParam);
  94. params.push(newParam);
  95. return newParam.code;
  96. } else {
  97. return param.code;
  98. }
  99. };
  100. if (ruleParams.length > 1) {
  101. for (const i in ruleParams) {
  102. if (!this.ctx.helper.isOperator(ruleParams[i])) {
  103. const paramCode = addParam(ruleParams[i]);
  104. codeParams.push(paramCode);
  105. parseParams.push(paramCode + '(' + ruleParams[i] + ')');
  106. } else {
  107. codeParams.push(ruleParams[i]);
  108. parseParams.push(ruleParams[i]);
  109. }
  110. }
  111. return [codeParams.join(''), parseParams.join('')];
  112. } else {
  113. const paramCode = addParam(rule);
  114. return [paramCode, paramCode + '(' + rule + ')'];
  115. }
  116. }
  117. /**
  118. * 查找父节点(根据编号),忽略大小写
  119. * e.g. z1(z), z1-e(z1), z1-e-a(z1-e)
  120. * @param code
  121. * @param nodes
  122. * @returns {*}
  123. * @private
  124. */
  125. _findParentId(code, nodes) {
  126. if (nodes.length === 0) { return -1; }
  127. const codeList = code.split('-');
  128. if (codeList.length > 1) {
  129. codeList.splice(codeList.length - 1);
  130. const parentCode = codeList.join('-');
  131. for (const node of nodes) {
  132. if (parentCode.toLowerCase() === node.code.toLowerCase()) {
  133. return node.node_id;
  134. }
  135. }
  136. } else {
  137. for (const node of nodes) {
  138. if (code.toLowerCase().search(node.code.toLowerCase()) === 0) {
  139. return node.node_id;
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * 解析一个Excel工作表内的全部 指标节点 和 指标
  146. * @param {Object} excelSheet
  147. * @param {Array} nodes - 解析后的指标节点
  148. * @param {Array} indexes - 解析后的指标
  149. * @param {Number} templateId - 解析至的模板Id
  150. * @private
  151. */
  152. _parseSheetData(excelSheet, nodes, indexes, params, templateId = 1) {
  153. for (const row of excelSheet.data) {
  154. if (!row[0]) { continue; }
  155. if (this.ctx.helper.ValidTemplateNodeCode(row[0])) {
  156. if (!this.ctx.helper.findObj(nodes, 'code', row[0])) {
  157. const node = {
  158. template_id: templateId,
  159. node_id: nodes.length + 1,
  160. node_pid: this._findParentId(row[0], nodes) || -1,
  161. code: row[0],
  162. name: row[1],
  163. };
  164. nodes.push(node);
  165. }
  166. } else if (this.ctx.helper.ValidTemplateIndexCode(row[0])) {
  167. const index = {
  168. code: row[0],
  169. name: row[1],
  170. unit1: row[2],
  171. unit2: row[3],
  172. node_id: nodes.length,
  173. index_id: indexes.length + 1,
  174. rule: row[9] ? row[9] : '',
  175. template_id: templateId,
  176. };
  177. if (row[4] === '√') {
  178. index.index_type = 1;
  179. } else if (row[5] === '√') {
  180. index.index_type = 2;
  181. } else if (row[6] === '√') {
  182. index.index_type = 3;
  183. } else if (row[7] === '√') {
  184. index.index_type = 4;
  185. }
  186. [index.calc_rule, index.parse_rule] = this._parseParam(index.rule, index.node_id, params, templateId);
  187. indexes.push(index);
  188. }
  189. }
  190. }
  191. /**
  192. * 导入Excel数据
  193. *
  194. * @param {Array} excelSheets - Excel文件中的全部工作表
  195. * @returns {Promise<boolean>}
  196. */
  197. async importData(excelSheets, templateId = 1) {
  198. let result = false;
  199. const limit = 30000;
  200. const transaction = await this.db.beginTransaction();
  201. try {
  202. const nodes = [], indexes = [], params = [];
  203. this._loadDefaultParam(params, paramConst.defaultGlobalParams, 0, templateId);
  204. for (const sheet of excelSheets) {
  205. this._parseSheetData(sheet, nodes, indexes, params, templateId);
  206. }
  207. if (nodes.length > 0) {
  208. // 删除旧数据
  209. await transaction.delete(this.tableName, {template_id: templateId});
  210. // 插入指标节点数据
  211. const nodeResult = await transaction.insert(this.tableName, nodes);
  212. if (nodeResult.affectedRows !== nodes.length) {
  213. throw '导入指标节点错误';
  214. }
  215. // 插入指标数据
  216. await this.ctx.service.templateIndex.importData(indexes, transaction, templateId);
  217. // 插入指标参数
  218. await this.ctx.service.templateParam.importData(params, transaction, templateId);
  219. } else {
  220. throw 'Excel文件中无标准的指标数据';
  221. }
  222. await transaction.commit();
  223. result = true;
  224. } catch(err) {
  225. await transaction.rollback();
  226. console.log(err);
  227. throw err;
  228. }
  229. return result;
  230. }
  231. /**
  232. * 保存指标节点绑定信息
  233. *
  234. * @param data
  235. * @returns {Promise<*>}
  236. */
  237. async updateNodeMatch(data, condition) {
  238. try {
  239. if (this.ctx.helper.validMatchCode(data.match_key)) {
  240. data.match_type = nodeConst.matchType.code;
  241. } else {
  242. data.match_type = nodeConst.matchType.name;
  243. }
  244. await this.db.update(this.tableName, data, {where: condition});
  245. } catch (err) {
  246. console.log(err);
  247. }
  248. return await this.getDataByCondition(condition);
  249. }
  250. }
  251. return TemplateNode;
  252. };