template_node.js 11 KB

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