rpt_tree_node_cust.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 removeCustFolder(id) {
  24. this.transaction = await this.db.beginTransaction();
  25. try {
  26. const rst = await this.transaction.delete(this.tableName, { id });
  27. await this.transaction.commit();
  28. return rst;
  29. } catch (ex) {
  30. console.log(ex);
  31. // 回滚
  32. await this.transaction.rollback();
  33. }
  34. }
  35. async getCustFoldersByUserId(userId) {
  36. this.initSqlBuilder();
  37. this.sqlBuilder.setAndWhere('cust_acc_id', {
  38. value: userId,
  39. operate: '=',
  40. });
  41. this.sqlBuilder.columns = ['id', 'cust_acc_id', 'rpt_tpl_items'];
  42. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  43. const list = await this.db.query(sql, sqlParam);
  44. return list;
  45. }
  46. async getCustFolderByTenderId(tenderId) {
  47. this.initSqlBuilder();
  48. this.sqlBuilder.setAndWhere('tender_id', {
  49. value: tenderId,
  50. operate: '=',
  51. });
  52. this.sqlBuilder.columns = ['id', 'cust_acc_id', 'tender_id', 'rpt_tpl_items'];
  53. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  54. const list = await this.db.query(sql, sqlParam);
  55. return list;
  56. }
  57. async createCustNode(cust_acc_id, newRptItems) {
  58. let rst = null;
  59. this.transaction = await this.db.beginTransaction();
  60. try {
  61. const data = {
  62. cust_acc_id,
  63. tender_id: -1,
  64. rpt_tpl_items: newRptItems,
  65. };
  66. // console.log('createNode:');
  67. // console.log(data);
  68. rst = await this.transaction.insert(this.tableName, data);
  69. await this.transaction.commit();
  70. } catch (ex) {
  71. console.log(ex);
  72. // 回滚
  73. await this.transaction.rollback();
  74. } finally {
  75. return rst;
  76. }
  77. }
  78. async createCustNodeByTenderId(cust_acc_id, tender_id, newRptItems) {
  79. let rst = null;
  80. this.transaction = await this.db.beginTransaction();
  81. try {
  82. const data = {
  83. cust_acc_id: -1,
  84. tender_id,
  85. rpt_tpl_items: newRptItems,
  86. };
  87. // console.log('createNode:');
  88. // console.log(data);
  89. rst = await this.transaction.insert(this.tableName, data);
  90. await this.transaction.commit();
  91. } catch (ex) {
  92. console.log(ex);
  93. // 回滚
  94. await this.transaction.rollback();
  95. } finally {
  96. return rst;
  97. }
  98. }
  99. async updateCustNodeByTenderId(cust_acc_id, tender_id, newNodeItems) {
  100. let rst = null;
  101. try {
  102. const custNode = await this.getCustFolderByTenderId(tender_id);
  103. // console.log('query Node:');
  104. // console.log(custNode);
  105. if (custNode.length > 0) {
  106. this.transaction = await this.db.beginTransaction();
  107. custNode[0].rpt_tpl_items = newNodeItems;
  108. custNode[0].cust_acc_id = -1;
  109. rst = await this.transaction.update(this.tableName, custNode[0]);
  110. await this.transaction.commit();
  111. } else {
  112. rst = await this.createCustNodeByTenderId(cust_acc_id, tender_id, newNodeItems);
  113. }
  114. } catch (ex) {
  115. console.log(ex);
  116. await this.transaction.rollback();
  117. rst = null;
  118. } finally {
  119. return rst;
  120. }
  121. }
  122. async updateCustNode(cust_acc_id, newNodeItems, custSelectKeys) {
  123. let rst = null;
  124. try {
  125. const custNode = await this.getCustFoldersByUserId(cust_acc_id);
  126. if (custNode.length > 0) {
  127. this.transaction = await this.db.beginTransaction();
  128. const orgNodeItems = JSON.parse(custNode[0].rpt_tpl_items);
  129. orgNodeItems[custSelectKeys[0]] = newNodeItems.common;
  130. orgNodeItems[custSelectKeys[1]] = newNodeItems.customize;
  131. // custNode[0].rpt_tpl_items = newNodeItems;
  132. custNode[0].rpt_tpl_items = JSON.stringify(orgNodeItems);
  133. rst = await this.transaction.update(this.tableName, custNode[0]);
  134. await this.transaction.commit();
  135. } else {
  136. const nodeItems = {};
  137. nodeItems[custSelectKeys[0]] = newNodeItems.common;
  138. nodeItems[custSelectKeys[1]] = newNodeItems.customize;
  139. // rst = await this.createCustNode(cust_acc_id, newNodeItems);
  140. rst = await this.createCustNode(cust_acc_id, JSON.stringify(nodeItems));
  141. }
  142. } catch (ex) {
  143. console.log(ex);
  144. await this.transaction.rollback();
  145. rst = null;
  146. } finally {
  147. return rst;
  148. }
  149. }
  150. }
  151. return RptTreeNodeCust;
  152. };