template_param.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/4/23
  7. * @version
  8. */
  9. const paramConst = require('../const/template_param');
  10. module.exports = app => {
  11. class TemplateParam extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局context
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'template_param';
  21. }
  22. /**
  23. * 导入指标参数
  24. *
  25. * @param {Array|Object} datas
  26. * @param transaction - 事务
  27. * @returns {Promise<void>}
  28. */
  29. async importData(data, transaction) {
  30. const datas = data instanceof Array ? data : [data];
  31. await transaction.delete(this.tableName, {template_id: 1});
  32. const insertResult = await transaction.insert(this.tableName, datas);
  33. if (insertResult.affectedRows !== datas.length) {
  34. throw '导入指标参数错误';
  35. }
  36. }
  37. /**
  38. * 保存指标节点匹配规则
  39. *
  40. * @param {Object} data
  41. * @returns {Promise<*>}
  42. */
  43. async updateNodeMatch(data, condition) {
  44. try {
  45. if (data.match_key && !this.ctx.helper.validMatchCode(data.match_key)) {
  46. throw '用于绑定的分项编号有误'
  47. }
  48. data.match_type = paramConst.matchType.code;
  49. await this.db.update(this.tableName, data, {where: condition});
  50. } catch (err) {
  51. console.log(err);
  52. }
  53. return await this.getDataByCondition(condition);
  54. }
  55. };
  56. return TemplateParam;
  57. };