role_rpt_rel.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2019/9/26.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class RoleRptRel extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'role_rpt_rel';
  17. this.dataId = 'id';
  18. }
  19. async getRoleRptRelById(id) {
  20. this.initSqlBuilder();
  21. this.sqlBuilder.setAndWhere('id', {
  22. value: id,
  23. operate: '=',
  24. });
  25. this.sqlBuilder.columns = ['id', 'tender_id', 'rpt_id', 'rel_content'];
  26. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  27. const list = await this.db.query(sql, sqlParam);
  28. return list;
  29. }
  30. async getRoleRptRelByTenderId(tenderId) {
  31. this.initSqlBuilder();
  32. this.sqlBuilder.setAndWhere('tender_id', {
  33. value: tenderId,
  34. operate: '=',
  35. });
  36. this.sqlBuilder.columns = ['id', 'tender_id', 'rpt_id', 'rel_content'];
  37. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  38. const list = await this.db.query(sql, sqlParam);
  39. return list;
  40. }
  41. async getRoleRptRelByDetailIds(tenderId, rptId, sid) {
  42. this.initSqlBuilder();
  43. this.sqlBuilder.setAndWhere('tender_id', {
  44. value: tenderId,
  45. operate: '=',
  46. });
  47. if (rptId instanceof Array) {
  48. this.sqlBuilder.setAndWhere('rpt_id', {
  49. value: rptId,
  50. operate: 'in',
  51. });
  52. } else {
  53. this.sqlBuilder.setAndWhere('rpt_id', {
  54. value: rptId,
  55. operate: '=',
  56. });
  57. }
  58. this.sqlBuilder.setAndWhere('sid', {
  59. value: sid,
  60. operate: '=',
  61. });
  62. this.sqlBuilder.columns = ['id', 'tender_id', 'rpt_id', 'rel_content'];
  63. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  64. const list = await this.db.query(sql, sqlParam);
  65. return list;
  66. }
  67. async createRoleRelationship(tender_id, rpt_id, sid, relArr) {
  68. let rst = null;
  69. this.transaction = await this.db.beginTransaction();
  70. try {
  71. const data = {
  72. tender_id: tender_id,
  73. rpt_id: rpt_id,
  74. sid: sid,
  75. rel_content: JSON.stringify(relArr),
  76. };
  77. rst = await this.transaction.insert(this.tableName, data);
  78. await this.transaction.commit();
  79. } catch (ex) {
  80. console.log(ex);
  81. // 回滚
  82. await this.transaction.rollback();
  83. }
  84. return rst;
  85. }
  86. async updateRoleRelationship(id, tender_id, rpt_id, sid, relArr) {
  87. let rst = null;
  88. if (id < 0) {
  89. rst = this.createRoleRelationship(tender_id, rpt_id, sid, relArr);
  90. } else {
  91. this.transaction = await this.db.beginTransaction();
  92. try {
  93. const data = { id: id, tender_id: tender_id, rpt_id: rpt_id, sid: sid, rel_content: JSON.stringify(relArr) };
  94. rst = await this.transaction.update(this.tableName, data);
  95. this.transaction.commit();
  96. } catch (ex) {
  97. console.log(ex);
  98. // 回滚
  99. await this.transaction.rollback();
  100. }
  101. }
  102. return rst;
  103. }
  104. }
  105. return RoleRptRel;
  106. };