rpt_tree_node_cust.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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,
  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, custSelectKeys) {
  55. let rst = null;
  56. try {
  57. const custNode = await this.getCustFoldersByUserId(cust_acc_id);
  58. if (custNode.length > 0) {
  59. this.transaction = await this.db.beginTransaction();
  60. const orgNodeItems = JSON.parse(custNode[0].rpt_tpl_items);
  61. orgNodeItems[custSelectKeys[0]] = newNodeItems.common;
  62. orgNodeItems[custSelectKeys[1]] = newNodeItems.customize;
  63. // custNode[0].rpt_tpl_items = newNodeItems;
  64. custNode[0].rpt_tpl_items = JSON.stringify(orgNodeItems);
  65. rst = await this.transaction.update(this.tableName, custNode[0]);
  66. this.transaction.commit();
  67. } else {
  68. const nodeItems = {};
  69. nodeItems[custSelectKeys[0]] = newNodeItems.common;
  70. nodeItems[custSelectKeys[1]] = newNodeItems.customize;
  71. // rst = await this.createCustNode(cust_acc_id, newNodeItems);
  72. rst = await this.createCustNode(cust_acc_id, JSON.stringify(nodeItems));
  73. }
  74. } catch (ex) {
  75. console.log(ex);
  76. this.transaction.rollback();
  77. rst = null;
  78. } finally {
  79. return rst;
  80. }
  81. }
  82. }
  83. return RptTreeNodeCust;
  84. };