rpt_tree_node_cust.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2020/6/6.
  4. * 用户自定义显示报表模板组织
  5. * @author TonyKang
  6. * @date 2019/05/05
  7. * @version
  8. */
  9. const BaseService = require('../base/base_service');
  10. module.exports = app => {
  11. class RptTreeNodeCust extends BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'rpt_tree_node_cust';
  21. this.dataId = 'id';
  22. }
  23. async getCustFoldersByUserId(userId) {
  24. this.initSqlBuilder();
  25. this.sqlBuilder.setAndWhere('cust_acc_id', {
  26. value: userId,
  27. operate: '=',
  28. });
  29. this.sqlBuilder.columns = ['id', 'cust_acc_id', 'rpt_tpl_items'];
  30. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  31. const list = await this.db.query(sql, sqlParam);
  32. return list;
  33. }
  34. async createCustNode(cust_acc_id, newRptItems) {
  35. let rst = null;
  36. this.transaction = await this.db.beginTransaction();
  37. try {
  38. const data = {
  39. cust_acc_id: cust_acc_id,
  40. rpt_tpl_items: newRptItems,
  41. };
  42. // console.log('createNode:');
  43. // console.log(data);
  44. rst = await this.transaction.insert(this.tableName, data);
  45. await this.transaction.commit();
  46. } catch (ex) {
  47. console.log(ex);
  48. // 回滚
  49. await this.transaction.rollback();
  50. } finally {
  51. return rst;
  52. }
  53. }
  54. async updateCustNode(cust_acc_id, newNodeItems) {
  55. let rst = null;
  56. try {
  57. const custNode = await this.getCustFoldersByUserId(cust_acc_id);
  58. // console.log('query Node:');
  59. // console.log(custNode);
  60. if (custNode.length > 0) {
  61. this.transaction = await this.db.beginTransaction();
  62. custNode[0].rpt_tpl_items = newNodeItems;
  63. rst = await this.transaction.update(this.tableName, custNode[0]);
  64. this.transaction.commit();
  65. } else {
  66. rst = await this.createCustNode(cust_acc_id, newNodeItems);
  67. }
  68. } catch (ex) {
  69. console.log(ex);
  70. this.transaction.rollback();
  71. rst = null;
  72. } finally {
  73. return rst;
  74. }
  75. }
  76. }
  77. return RptTreeNodeCust;
  78. };