rpt_tpl.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2019/5/13.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class RptTpl extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'rpt_tpl';
  17. this.dataId = 'id';
  18. }
  19. async getTplById(id) {
  20. this.initSqlBuilder();
  21. this.sqlBuilder.setAndWhere('id', {
  22. value: id,
  23. operate: '=',
  24. });
  25. this.sqlBuilder.columns = ['id', 'rpt_type', 'rpt_tpl_name', 'rpt_content', 'source_type'];
  26. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  27. const list = await this.db.query(sql, sqlParam);
  28. return list;
  29. }
  30. async getAllTplByIds(rpt_ids) {
  31. this.initSqlBuilder();
  32. this.sqlBuilder.setAndWhere('id', {
  33. value: rpt_ids,
  34. operate: 'in',
  35. });
  36. this.sqlBuilder.columns = ['id', 'rpt_type', 'rpt_tpl_name', 'rpt_content', 'source_type'];
  37. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  38. const list = await this.db.query(sql, sqlParam);
  39. return list;
  40. }
  41. async copyAndCreateTemplate(refTpl) {
  42. let rst = null;
  43. this.transaction = await this.db.beginTransaction();
  44. try {
  45. const data = {
  46. id: 0,
  47. rpt_type: refTpl.rpt_type,
  48. rpt_tpl_name: refTpl.rpt_tpl_name,
  49. rpt_content: refTpl.rpt_content
  50. };
  51. rst = await this.transaction.insert(this.tableName, data);
  52. await this.transaction.commit();
  53. } catch (ex) {
  54. console.log(ex);
  55. // 回滚
  56. await this.transaction.rollback();
  57. }
  58. return rst;
  59. }
  60. async insertRptTpl(refTpl) {
  61. let rst = null;
  62. this.transaction = await this.db.beginTransaction();
  63. try {
  64. const data = {
  65. id: refTpl.id,
  66. rpt_type: refTpl.rpt_type,
  67. rpt_tpl_name: refTpl.rpt_tpl_name,
  68. rpt_content: refTpl.rpt_content
  69. };
  70. rst = await this.transaction.insert(this.tableName, data);
  71. await this.transaction.commit();
  72. } catch (ex) {
  73. console.log(ex);
  74. // 回滚
  75. await this.transaction.rollback();
  76. }
  77. return rst;
  78. }
  79. async updateRptTpl(tplId, tplName, tplObj) {
  80. let rst = null;
  81. this.transaction = await this.db.beginTransaction();
  82. try {
  83. const data = { id: tplId, rpt_type: 0, rpt_tpl_name: tplName, rpt_content: JSON.stringify(tplObj) };
  84. rst = await this.transaction.update(this.tableName, data);
  85. await this.transaction.commit();
  86. } catch (ex) {
  87. console.log(ex);
  88. // 回滚
  89. await this.transaction.rollback();
  90. }
  91. return rst;
  92. }
  93. }
  94. return RptTpl;
  95. };