rpt_tpl.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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'];
  26. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  27. const list = await this.db.query(sql, sqlParam);
  28. return list;
  29. }
  30. async copyAndCreateTemplate(refTpl) {
  31. let rst = null;
  32. this.transaction = await this.db.beginTransaction();
  33. try {
  34. const data = {
  35. id: 0,
  36. rpt_type: refTpl.rpt_type,
  37. rpt_tpl_name: refTpl.rpt_tpl_name,
  38. rpt_content: refTpl.rpt_content
  39. };
  40. rst = await this.transaction.insert(this.tableName, data);
  41. await this.transaction.commit();
  42. } catch (ex) {
  43. console.log(ex);
  44. // 回滚
  45. await this.transaction.rollback();
  46. }
  47. return rst;
  48. }
  49. async updateRptTpl(tplId, tplName, tplObj) {
  50. let rst = null;
  51. this.transaction = await this.db.beginTransaction();
  52. try {
  53. const data = { id: tplId, rpt_type: 0, rpt_tpl_name: tplName, rpt_content: JSON.stringify(tplObj) };
  54. rst = await this.transaction.update(this.tableName, data);
  55. this.transaction.commit();
  56. } catch (ex) {
  57. console.log(ex);
  58. // 回滚
  59. await this.transaction.rollback();
  60. }
  61. return rst;
  62. }
  63. }
  64. return RptTpl;
  65. };