rpt_tpl.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 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'];
  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 updateRptTpl(tplId, tplName, tplObj) {
  61. let rst = null;
  62. this.transaction = await this.db.beginTransaction();
  63. try {
  64. const data = { id: tplId, rpt_type: 0, rpt_tpl_name: tplName, rpt_content: JSON.stringify(tplObj) };
  65. rst = await this.transaction.update(this.tableName, data);
  66. this.transaction.commit();
  67. } catch (ex) {
  68. console.log(ex);
  69. // 回滚
  70. await this.transaction.rollback();
  71. }
  72. return rst;
  73. }
  74. }
  75. return RptTpl;
  76. };