report_memory.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const Service = require('egg').Service;
  10. const StageIm = require('../lib/stage_im');
  11. const imType = require('../const/tender').imType;
  12. const audit = require('../const/audit');
  13. module.exports = app => {
  14. class ReportMemory extends Service {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局context
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.db = this.app.mysql;
  24. this.cache = this.app.redis;
  25. this._ = this.app._;
  26. // 需要缓存的数据
  27. this.stageImData = null;
  28. }
  29. async _checkTender(tid) {
  30. const tender = await this.ctx.service.tender.getTender(tid);
  31. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  32. this.ctx.tender = tender;
  33. }
  34. async _checkStage(sid) {
  35. const status = audit.stage.status;
  36. const stage = await this.ctx.service.stage.getDataById(sid);
  37. stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times);
  38. stage.curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  39. const accountId = this.ctx.session.sessionUser.accountId, auditorIds = this._.map(stage.auditors, 'aid'), shareIds = [];
  40. if (accountId === stage.user_id) { // 原报
  41. if (stage.curAuditor) {
  42. stage.readOnly = stage.curAuditor.aid !== accountId;
  43. } else {
  44. stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
  45. }
  46. stage.curTimes = stage.times;
  47. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  48. stage.curOrder = 0;
  49. } else if (stage.status === status.checked) {
  50. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  51. } else {
  52. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  53. }
  54. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  55. if (stage.status === status.uncheck) {
  56. throw '您无权查看该数据';
  57. }
  58. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  59. if (stage.status === status.checked) {
  60. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  61. } else if (stage.status === status.checkNo) {
  62. const audit = await this.service.stageAudit.getDataByCondition({
  63. sid: stage.id, times: stage.times - 1, status: status.checkNo
  64. });
  65. stage.curOrder = audit.order;
  66. } else {
  67. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  68. }
  69. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  70. if (stage.status === status.uncheck) {
  71. throw '您无权查看该数据';
  72. }
  73. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  74. stage.curOrder = stage.status === status.checked ? this._.max(this._.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  75. }
  76. this.ctx.stage = stage;
  77. }
  78. async _generateStageIm(tid, sid, isTz = true) {
  79. await this._checkTender(tid);
  80. await this._checkStage(sid);
  81. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  82. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  83. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  84. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  85. }
  86. const stageIm = new StageIm(this.ctx);
  87. await stageIm.buildImData();
  88. this.stageImData.main = stageIm.ImData;
  89. if (isTz) {
  90. this.stageImData.bills = stageIm.ImBillsData;
  91. }
  92. }
  93. async getStageImTzNoReturn(tid, sid) {
  94. // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用)
  95. // 二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失
  96. if (!this.stageImData) {
  97. this.stageImData = {};
  98. }
  99. try {
  100. await this._generateStageIm(tid, sid);
  101. } catch (err) {
  102. this.stageImData.main = [];
  103. this.stageImData.bills = [];
  104. }
  105. }
  106. getStageImTzDataDirectlyByKey(key) {
  107. let rst = [];
  108. if (key === 'mem_stage_im_tz') {
  109. rst = this.stageImData.main;
  110. } else {
  111. rst = this.stageImData.bills;
  112. }
  113. return rst;
  114. }
  115. async getStageImTzData(tid, sid) {
  116. if (!this.stageImData) {
  117. this.stageImData = {};
  118. try {
  119. await this._generateStageIm(tid, sid);
  120. } catch (err) {
  121. if (err.statck) {
  122. this.ctx.logger.error(err);
  123. }
  124. this.stageImData.main = err.statck ? '数据错误' : err;
  125. this.stageImData.bills = this.stageImData.main;
  126. }
  127. }
  128. return this.stageImData.main;
  129. }
  130. async getStageImTzBillsData(tid, sid) {
  131. if (!this.stageImData) {
  132. this.stageImData = {};
  133. try {
  134. await this._generateStageIm(tid, sid);
  135. } catch (err) {
  136. if (err.statck) {
  137. this.ctx.logger.error(err);
  138. }
  139. this.stageImData.main = err.statck ? '数据错误' : err;
  140. this.stageImData.bills = this.stageImData.main;
  141. }
  142. }
  143. return this.stageImData.bills;
  144. }
  145. async getStageImZlData(tid, sid) {
  146. this.stageImData = {};
  147. try {
  148. await this._generateStageIm(tid, sid, false);
  149. } catch (err) {
  150. if (err.statck) {
  151. this.ctx.logger.error(err);
  152. }
  153. this.stageImData.main = err.statck ? '数据错误' : err;
  154. }
  155. return this.stageImData.main;
  156. }
  157. }
  158. return ReportMemory;
  159. };