template_node.js 11 KB

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