report_memory.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const StageIm = require('../lib/stage_im');
  10. const imType = require('../const/tender').imType;
  11. const audit = require('../const/audit');
  12. // const path = require('path');
  13. // const fs = require('fs');
  14. const stageImTz = 'mem_stage_im_tz';
  15. const stageImTzBills = 'mem_stage_im_tz_bills';
  16. const stageImZl = 'mem_stage_im_zl';
  17. module.exports = app => {
  18. class ReportMemory extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局context
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'report_memory';
  28. // 需要缓存的数据
  29. this.stageImData = null;
  30. }
  31. async _checkTender(tid) {
  32. if (this.ctx.tender) return;
  33. const tender = await this.ctx.service.tender.getTender(tid);
  34. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  35. this.ctx.tender = tender;
  36. }
  37. async _checkStage(sid) {
  38. if (!this.ctx.stage) {
  39. const status = audit.stage.status;
  40. const stage = await this.ctx.service.stage.getDataById(sid);
  41. stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times);
  42. stage.curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  43. const accountId = this.ctx.session.sessionUser.accountId, auditorIds = this._.map(stage.auditors, 'aid'), shareIds = [];
  44. if (accountId === stage.user_id) { // 原报
  45. if (stage.curAuditor) {
  46. stage.readOnly = stage.curAuditor.aid !== accountId;
  47. } else {
  48. stage.readOnly = stage.status !== status.uncheck && stage.status !== status.checkNo;
  49. }
  50. stage.curTimes = stage.times;
  51. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  52. stage.curOrder = 0;
  53. } else if (stage.status === status.checked) {
  54. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  55. } else {
  56. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  57. }
  58. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  59. if (stage.status === status.uncheck) {
  60. throw '您无权查看该数据';
  61. }
  62. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  63. if (stage.status === status.checked) {
  64. stage.curOrder = this._.max(this._.map(stage.auditors, 'order'));
  65. } else if (stage.status === status.checkNo) {
  66. const audit = await this.service.stageAudit.getDataByCondition({
  67. sid: stage.id, times: stage.times - 1, status: status.checkNo
  68. });
  69. stage.curOrder = audit.order;
  70. } else {
  71. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  72. }
  73. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  74. if (stage.status === status.uncheck) {
  75. throw '您无权查看该数据';
  76. }
  77. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  78. stage.curOrder = stage.status === status.checked ? this._.max(this._.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  79. }
  80. this.ctx.stage = stage;
  81. let time = this.ctx.stage.readOnly ? this.ctx.stage.cache_time_r : this.ctx.stage.cache_time_l;
  82. if (!time) {
  83. time = this.ctx.stage.in_time ? this.ctx.stage.in_time : new Date();
  84. }
  85. this.ctx.stage.cacheTime = time.getTime();//this.ctx.stage.readOnly ? (this.ctx.stage.cache_time_r).getTime(): (this.ctx.stage.cache_time_l).getTime();
  86. }
  87. }
  88. // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
  89. // 一定程度上算是大Value缓存,数据多了以后:
  90. // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
  91. // 2. redis单独服务器
  92. // 3. redis集群
  93. async _getReportMemoryCache(name, tid, sid, time) {
  94. // redis
  95. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
  96. const data = await this.cache.get(cacheKey);
  97. if (data) {
  98. return eval(data);
  99. } else {
  100. return null;
  101. }
  102. // mysql + IO
  103. // const rm = await this.getDataByCondition({
  104. // tid: tid, sid: sid, name: name, time: time
  105. // });
  106. // if (rm && rm.file) {
  107. // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
  108. // if (fs.existsSync(file)) {
  109. // const data = await fs.readFileSync(file, 'utf8');
  110. // return eval(data);
  111. // } else {
  112. // return null;
  113. // }
  114. // }
  115. }
  116. async _setReportMemoryCache(name, tid, sid, time, data) {
  117. // redis
  118. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '');
  119. this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
  120. // mysql + IO
  121. // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
  122. // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
  123. // const rm = await this.getDataByCondition({
  124. // tid: tid, sid: sid, name: name, time: time
  125. // });
  126. // if (rm) {
  127. // await this.db.update(this.tableName, {id: rm.id, file: file});
  128. // } else {
  129. // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
  130. // }
  131. }
  132. async _generateStageIm(tid, sid, isTz = true) {
  133. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  134. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  135. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  136. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  137. }
  138. const stageIm = new StageIm(this.ctx);
  139. await stageIm.buildImData();
  140. this.stageImData.main = stageIm.ImData;
  141. if (isTz) {
  142. this.stageImData.bills = stageIm.ImBillsData;
  143. await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
  144. await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills);
  145. } else {
  146. await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main);
  147. }
  148. }
  149. async getStageImTzNoReturn(tid, sid) {
  150. // 备注:单独拎出以下几行代码一个是为了提高效率(跟getStageImTzDataDirectlyByKey方法协作使用)
  151. // 二是如果出现并行查询(台账及台账清单)情况下,会出现干扰(已验证过),导致数据丢失
  152. if (!this.stageImData) {
  153. this.stageImData = {};
  154. }
  155. try {
  156. await this._generateStageIm(tid, sid);
  157. } catch (err) {
  158. this.stageImData.main = [];
  159. this.stageImData.bills = [];
  160. }
  161. }
  162. getStageImTzDataDirectlyByKey(key) {
  163. let rst = [];
  164. if (key === 'mem_stage_im_tz') {
  165. rst = this.stageImData.main;
  166. } else {
  167. rst = this.stageImData.bills;
  168. }
  169. return rst;
  170. }
  171. async getStageImTzData(tid, sid) {
  172. await this._checkTender(tid);
  173. await this._checkStage(sid);
  174. const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime);
  175. if (cache) {
  176. // console.log('cache');
  177. return cache;
  178. }
  179. // console.log('build');
  180. if (!this.stageImData) {
  181. this.stageImData = {};
  182. try {
  183. await this._generateStageIm(tid, sid);
  184. } catch (err) {
  185. if (err.statck) {
  186. this.ctx.logger.error(err);
  187. }
  188. this.stageImData.main = err.statck ? '数据错误' : err;
  189. this.stageImData.bills = this.stageImData.main;
  190. }
  191. }
  192. return this.stageImData.main;
  193. }
  194. async getStageImTzBillsData(tid, sid) {
  195. await this._checkTender(tid);
  196. await this._checkStage(sid);
  197. const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime);
  198. if (cache) return cache;
  199. if (!this.stageImData) {
  200. this.stageImData = {};
  201. try {
  202. await this._generateStageIm(tid, sid);
  203. } catch (err) {
  204. if (err.statck) {
  205. this.ctx.logger.error(err);
  206. }
  207. this.stageImData.main = err.statck ? '数据错误' : err;
  208. this.stageImData.bills = this.stageImData.main;
  209. }
  210. }
  211. return this.stageImData.bills;
  212. }
  213. async getStageImZlData(tid, sid) {
  214. await this._checkTender(tid);
  215. await this._checkStage(sid);
  216. const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime);
  217. if (cache) return cache;
  218. this.stageImData = {};
  219. try {
  220. await this._generateStageIm(tid, sid, false);
  221. } catch (err) {
  222. if (err.statck) {
  223. this.ctx.logger.error(err);
  224. }
  225. this.stageImData.main = err.statck ? '数据错误' : err;
  226. }
  227. return this.stageImData.main;
  228. }
  229. }
  230. return ReportMemory;
  231. };