template_node.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) {
  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);
  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);
  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 node = {
  154. template_id: templateId,
  155. node_id: nodes.length + 1,
  156. node_pid: this._findParentId(row[0], nodes) || -1,
  157. code: row[0],
  158. name: row[1],
  159. match_type: row[11] && row[11] !== '' ? nodeConst.matchType.code : nodeConst.matchType.name,
  160. match_key: row[11] && row[11] !== '' ? row[11] : row[1],
  161. };
  162. nodes.push(node);
  163. }
  164. } else if (this.ctx.helper.ValidTemplateIndexCode(row[0])) {
  165. const index = {
  166. code: row[0],
  167. name: row[1],
  168. unit1: row[2],
  169. unit2: row[3],
  170. node_id: nodes.length,
  171. index_id: indexes.length + 1,
  172. rule: row[9] ? row[9] : '',
  173. template_id: templateId,
  174. };
  175. if (row[4] === '√') {
  176. index.index_type = 1;
  177. } else if (row[5] === '√') {
  178. index.index_type = 2;
  179. } else if (row[6] === '√') {
  180. index.index_type = 3;
  181. } else if (row[7] === '√') {
  182. index.index_type = 4;
  183. }
  184. [index.calc_rule, index.parse_rule] = this._parseParam(index.rule, index.node_id, params, templateId);
  185. indexes.push(index);
  186. }
  187. }
  188. }
  189. /**
  190. * 导入Excel数据
  191. *
  192. * @param {Array} excelSheets - Excel文件中的全部工作表
  193. * @returns {Promise<boolean>}
  194. */
  195. async importData(excelSheets, templateId = 1) {
  196. let result = false;
  197. const limit = 30000;
  198. const transaction = await this.db.beginTransaction();
  199. try {
  200. const nodes = [], indexes = [], params = [];
  201. this._loadDefaultParam(params, paramConst.defaultGlobalParams, 0, templateId);
  202. for (const sheet of excelSheets) {
  203. this._parseSheetData(sheet, nodes, indexes, params, templateId);
  204. }
  205. if (nodes.length > 0) {
  206. // 删除旧数据
  207. await transaction.delete(this.tableName, {template_id: templateId});
  208. // 插入指标节点数据
  209. const nodeResult = await transaction.insert(this.tableName, nodes);
  210. if (nodeResult.affectedRows !== nodes.length) {
  211. throw '导入指标节点错误';
  212. }
  213. // 插入指标数据
  214. await this.ctx.service.templateIndex.importData(indexes, transaction, templateId);
  215. // 插入指标参数
  216. await this.ctx.service.templateParam.importData(params, transaction, templateId);
  217. } else {
  218. throw 'Excel文件中无标准的指标数据';
  219. }
  220. await transaction.commit();
  221. result = true;
  222. } catch(err) {
  223. await transaction.rollback();
  224. console.log(err);
  225. throw err;
  226. }
  227. return result;
  228. }
  229. /**
  230. * 保存指标节点绑定信息
  231. *
  232. * @param data
  233. * @returns {Promise<*>}
  234. */
  235. async updateNodeMatch(data, condition) {
  236. try {
  237. if (this.ctx.helper.validMatchCode(data.match_key)) {
  238. data.match_type = nodeConst.matchType.code;
  239. } else {
  240. data.match_type = nodeConst.matchType.name;
  241. }
  242. await this.db.update(this.tableName, data, {where: condition});
  243. } catch (err) {
  244. console.log(err);
  245. }
  246. return await this.getDataByCondition(condition);
  247. }
  248. }
  249. return TemplateNode;
  250. };