'use strict'; /** * Created by Tony on 2019/5/13. */ const BaseService = require('../base/base_service'); module.exports = app => { class RptTpl extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'rpt_tpl'; this.dataId = 'id'; } async getTplById(id) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('id', { value: id, operate: '=', }); this.sqlBuilder.columns = ['id', 'rpt_type', 'rpt_tpl_name', 'rpt_content', 'source_type']; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const list = await this.db.query(sql, sqlParam); return list; } async getAllTplByIds(rpt_ids) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('id', { value: rpt_ids, operate: 'in', }); this.sqlBuilder.columns = ['id', 'rpt_type', 'rpt_tpl_name', 'rpt_content', 'source_type']; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const list = await this.db.query(sql, sqlParam); return list; } async copyAndCreateTemplate(refTpl) { let rst = null; this.transaction = await this.db.beginTransaction(); try { const data = { id: 0, rpt_type: refTpl.rpt_type, rpt_tpl_name: refTpl.rpt_tpl_name, rpt_content: refTpl.rpt_content }; rst = await this.transaction.insert(this.tableName, data); await this.transaction.commit(); } catch (ex) { console.log(ex); // 回滚 await this.transaction.rollback(); } return rst; } async insertRptTpl(refTpl) { let rst = null; this.transaction = await this.db.beginTransaction(); try { const data = { id: refTpl.id, rpt_type: refTpl.rpt_type, rpt_tpl_name: refTpl.rpt_tpl_name, rpt_content: refTpl.rpt_content }; rst = await this.transaction.insert(this.tableName, data); await this.transaction.commit(); } catch (ex) { console.log(ex); // 回滚 await this.transaction.rollback(); } return rst; } async updateRptTpl(tplId, tplName, tplObj) { let rst = null; this.transaction = await this.db.beginTransaction(); try { const data = { id: tplId, rpt_type: 0, rpt_tpl_name: tplName, rpt_content: JSON.stringify(tplObj) }; rst = await this.transaction.update(this.tableName, data); await this.transaction.commit(); } catch (ex) { console.log(ex); // 回滚 await this.transaction.rollback(); } return rst; } } return RptTpl; };