rpt_tree_node.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. /**
  3. * 报表模板组织架构模型
  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 RptTreeNode 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';
  21. this.dataId = 'id';
  22. }
  23. async getNodesByType(typeArr) {
  24. this.initSqlBuilder();
  25. this.sqlBuilder.setAndWhere('rpt_type', {
  26. value: typeArr,
  27. operate: 'in',
  28. });
  29. this.sqlBuilder.columns = ['id', 'name', 'rpt_type', 'pid', 'items', 'last_update_time'];
  30. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  31. const list = await this.db.query(sql, sqlParam);
  32. return list;
  33. }
  34. async getNodesByProjectId(prjIdArr) {
  35. this.initSqlBuilder();
  36. this.sqlBuilder.setAndWhere('pid', {
  37. value: prjIdArr,
  38. operate: 'in',
  39. });
  40. this.sqlBuilder.columns = ['id', 'name', 'rpt_type', 'pid', 'items', 'last_update_time'];
  41. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  42. const list = await this.db.query(sql, sqlParam);
  43. return list;
  44. }
  45. async getNodeById(id) {
  46. this.initSqlBuilder();
  47. this.sqlBuilder.setAndWhere('id', {
  48. value: id,
  49. operate: '=',
  50. });
  51. this.sqlBuilder.columns = ['id', 'name', 'rpt_type', 'pid', 'items', 'last_update_time'];
  52. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  53. const list = await this.db.query(sql, sqlParam);
  54. return list;
  55. }
  56. async createNode(prjId, name) {
  57. let rst = null;
  58. this.transaction = await this.db.beginTransaction();
  59. try {
  60. const data = {
  61. // id: 0,
  62. rpt_type: 0,
  63. name: name,
  64. pid: prjId,
  65. items: '[]',
  66. last_update_time: (new Date()).getTime().toString(),
  67. };
  68. console.log('createNode:');
  69. console.log(data);
  70. rst = await this.transaction.insert(this.tableName, data);
  71. await this.transaction.commit();
  72. } catch (ex) {
  73. console.log(ex);
  74. // 回滚
  75. await this.transaction.rollback();
  76. }
  77. return rst;
  78. }
  79. async updateTopNode(topNode) {
  80. try {
  81. this.transaction = await this.db.beginTransaction();
  82. await this.transaction.update(this.tableName, topNode);
  83. // const operate = await this.db.update(this.tableName, topNode);
  84. // console.log(operate.affectedRows);
  85. // return operate.affectedRows === 1;
  86. this.transaction.commit();
  87. return true;
  88. } catch (ex) {
  89. console.log(ex);
  90. this.transaction.rollback();
  91. return false;
  92. }
  93. }
  94. }
  95. return RptTreeNode;
  96. };