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