template_param.js 1.7 KB

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