rpt_archive.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2021/3/30.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const rptArchiveConst = require('../const/rpt_archive');
  7. module.exports = app => {
  8. class RptArchive extends BaseService {
  9. /**
  10. * 构造函数
  11. *
  12. * @param {Object} ctx - egg全局变量
  13. * @return {void}
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'rpt_archive';
  18. this.dataId = 'id';
  19. }
  20. async getArchiveById(id) {
  21. this.initSqlBuilder();
  22. this.sqlBuilder.setAndWhere('id', {
  23. value: id,
  24. operate: '=',
  25. });
  26. this.sqlBuilder.columns = ['id', 'prj_id', 'stage_id', 'content'];
  27. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  28. const list = await this.db.query(sql, sqlParam);
  29. return list;
  30. }
  31. async getArchiveByBzId(prjId, stgId, bzId) {
  32. if (stgId > 0) {
  33. return await this.getPrjStgArchive(prjId, stgId);
  34. }
  35. if (!bzId) return [];
  36. this.initSqlBuilder();
  37. this.sqlBuilder.setAndWhere('prj_id', {
  38. value: prjId,
  39. operate: '=',
  40. });
  41. this.sqlBuilder.setAndWhere('stage_id', {
  42. value: stgId,
  43. operate: '=',
  44. });
  45. this.sqlBuilder.setAndWhere('business_id', {
  46. value: `"${bzId}"`,
  47. operate: '=',
  48. });
  49. // this.sqlBuilder.columns = ['id', 'prj_id', 'stage_id', 'content'];
  50. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  51. const rstList = await this.db.query(sql, sqlParam);
  52. return rstList;
  53. }
  54. async getPrjStgArchiveByBz(prjId, stgId, tenderId) {
  55. this.initSqlBuilder();
  56. this.sqlBuilder.setAndWhere('prj_id', {
  57. value: prjId,
  58. operate: '=',
  59. });
  60. if (stgId === null) {
  61. this.sqlBuilder.setAndWhere('business_type', {
  62. value: this.db.escape('stage'),
  63. operate: '=',
  64. });
  65. } else {
  66. this.sqlBuilder.setAndWhere('stage_id', {
  67. value: stgId,
  68. operate: '=',
  69. });
  70. }
  71. this.sqlBuilder.setAndWhere('tender_id', {
  72. value: tenderId,
  73. operate: '=',
  74. });
  75. // this.sqlBuilder.columns = ['id', 'prj_id', 'stage_id', 'tender_id', 'content'];
  76. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  77. const rstList = await this.db.query(sql, sqlParam);
  78. // 去除content为空或者为[]的
  79. return this._.filter(rstList, function(item) {
  80. return item.content !== null && item.content !== '[]';
  81. });
  82. }
  83. async getPrjStgArchive(prjId, stgId) {
  84. this.initSqlBuilder();
  85. this.sqlBuilder.setAndWhere('prj_id', {
  86. value: prjId,
  87. operate: '=',
  88. });
  89. this.sqlBuilder.setAndWhere('stage_id', {
  90. value: stgId,
  91. operate: '=',
  92. });
  93. this.sqlBuilder.columns = ['id', 'prj_id', 'stage_id', 'content'];
  94. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  95. const rstList = await this.db.query(sql, sqlParam);
  96. return rstList;
  97. }
  98. async createArchive(prj_id, stage_id, tender_id, business_id, archiveArr) {
  99. let rst = null;
  100. this.transaction = await this.db.beginTransaction();
  101. try {
  102. const data = {
  103. prj_id,
  104. stage_id,
  105. business_id,
  106. tender_id,
  107. business_type: rptArchiveConst.getBusinessType(stage_id),
  108. content: JSON.stringify(archiveArr),
  109. };
  110. // console.log(data);
  111. rst = await this.transaction.insert(this.tableName, data);
  112. await this.transaction.commit();
  113. } catch (ex) {
  114. console.log(ex);
  115. // 回滚
  116. await this.transaction.rollback();
  117. }
  118. return rst;
  119. }
  120. async updateArchive(id, prj_id, stage_id, tender_id, business_id, archiveArr) {
  121. let rst = null;
  122. this.transaction = await this.db.beginTransaction();
  123. try {
  124. // console.log(data);
  125. // rst = await this.transaction.update(this.tableName, data);
  126. if (archiveArr.length === 0) {
  127. await this.transaction.delete(this.tableName, { id });
  128. await this.transaction.update(this.ctx.service.stage.tableName, { id: stage_id, rpt_filed: 0 });
  129. } else {
  130. const data = {
  131. id,
  132. prj_id,
  133. stage_id,
  134. business_id,
  135. tender_id,
  136. business_type: rptArchiveConst.getBusinessType(stage_id),
  137. content: JSON.stringify(archiveArr),
  138. };
  139. rst = await this.transaction.update(this.tableName, data);
  140. }
  141. await this.transaction.commit();
  142. } catch (ex) {
  143. console.log(ex);
  144. // 回滚
  145. await this.transaction.rollback();
  146. }
  147. return rst;
  148. }
  149. }
  150. return RptArchive;
  151. };