report_memory.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. const stageImVersion = '1.0';
  18. module.exports = app => {
  19. class ReportMemory extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局context
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'report_memory';
  29. // 需要缓存的数据
  30. this.stageImData = null;
  31. }
  32. // build-time: 162-384ms, redis-cache: 0-41ms, mysql + IO: 116-146ms
  33. // 一定程度上算是大Value缓存,数据多了以后:
  34. // 1. 达到redis内存阈值时,数据会swap到磁盘,此时将消耗IO时间
  35. // 2. redis单独服务器
  36. // 3. redis集群
  37. async _getReportMemoryCache(name, tid, sid, time, version = '') {
  38. // redis
  39. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
  40. const data = await this.cache.get(cacheKey);
  41. if (data) {
  42. return eval(data);
  43. } else {
  44. return null;
  45. }
  46. // mysql + IO
  47. // const rm = await this.getDataByCondition({
  48. // tid: tid, sid: sid, name: name, time: time
  49. // });
  50. // if (rm && rm.file) {
  51. // const file = path.join(this.ctx.app.config.filePath, 'report', 'cache', rm.file);
  52. // if (fs.existsSync(file)) {
  53. // const data = await fs.readFileSync(file, 'utf8');
  54. // return eval(data);
  55. // } else {
  56. // return null;
  57. // }
  58. // }
  59. }
  60. async _setReportMemoryCache(name, tid, sid, time, data, version = '') {
  61. // redis
  62. const cacheKey = name + '-t' + tid + (sid ? '-s' + sid : '') + (time ? '-' + time : '') + version;
  63. this.cache.set(cacheKey, JSON.stringify(data), 'EX', this.ctx.app.config.cacheTime);
  64. // mysql + IO
  65. // const file = path.join('report', 'cache', 'rm' + (new Date()).getTime() + '.json');
  66. // await this.ctx.helper.saveBufferFile(JSON.stringify(data), path.join(this.ctx.app.config.filePath, file));
  67. // const rm = await this.getDataByCondition({
  68. // tid: tid, sid: sid, name: name, time: time
  69. // });
  70. // if (rm) {
  71. // await this.db.update(this.tableName, {id: rm.id, file: file});
  72. // } else {
  73. // await this.db.insert(this.tableName, {tid: tid, sid: sid, name: name, time: time, file: file});
  74. // }
  75. }
  76. async _generateStageIm(tid, sid, isTz = true) {
  77. if (isTz && this.ctx.stage.im_type !== imType.tz.value) {
  78. throw '您查看的报表跟设置不符,请查看“总量控制”的报表';
  79. } else if (!isTz && this.ctx.stage.im_type === imType.tz.value) {
  80. throw '您查看的报表跟设置不符,请查看“0号台账”的报表';
  81. }
  82. const stageIm = new StageIm(this.ctx);
  83. await stageIm.buildImData();
  84. this.stageImData.main = stageIm.ImData;
  85. if (isTz) {
  86. this.stageImData.bills = stageIm.ImBillsData;
  87. await this._setReportMemoryCache(stageImTz, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
  88. await this._setReportMemoryCache(stageImTzBills, tid, sid, this.ctx.stage.cacheTime, this.stageImData.bills, stageImVersion);
  89. } else {
  90. await this._setReportMemoryCache(stageImZl, tid, sid, this.ctx.stage.cacheTime, this.stageImData.main, stageImVersion);
  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. await this.ctx.service.tender.checkTender(tid);
  117. await this.ctx.service.stage.checkStage(sid);
  118. const cache = await this._getReportMemoryCache('mem_stage_im_tz', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
  119. if (cache) {
  120. // console.log('cache');
  121. return cache;
  122. }
  123. // console.log('build');
  124. if (!this.stageImData) {
  125. this.stageImData = {};
  126. try {
  127. await this._generateStageIm(tid, sid);
  128. } catch (err) {
  129. if (err.statck) {
  130. this.ctx.logger.error(err);
  131. }
  132. this.stageImData.main = err.statck ? '数据错误' : err;
  133. this.stageImData.bills = this.stageImData.main;
  134. }
  135. }
  136. return this.stageImData.main;
  137. }
  138. async getStageImTzBillsData(tid, sid) {
  139. await this.ctx.service.tender.checkTender(tid);
  140. await this.ctx.service.stage.checkStage(sid);
  141. const cache = await this._getReportMemoryCache('mem_stage_im_tz_bills', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
  142. if (cache) return cache;
  143. if (!this.stageImData) {
  144. this.stageImData = {};
  145. try {
  146. await this._generateStageIm(tid, sid);
  147. } catch (err) {
  148. if (err.statck) {
  149. this.ctx.logger.error(err);
  150. }
  151. this.stageImData.main = err.statck ? '数据错误' : err;
  152. this.stageImData.bills = this.stageImData.main;
  153. }
  154. }
  155. return this.stageImData.bills;
  156. }
  157. async getStageImZlData(tid, sid) {
  158. await this.ctx.service.tender.checkTender(tid);
  159. await this.ctx.service.stage.checkStage(sid);
  160. const cache = await this._getReportMemoryCache('mem_stage_im_zl', tid, sid, this.ctx.stage.cacheTime, stageImVersion);
  161. if (cache) return cache;
  162. this.stageImData = {};
  163. try {
  164. await this._generateStageIm(tid, sid, false);
  165. } catch (err) {
  166. if (err.statck) {
  167. this.ctx.logger.error(err);
  168. }
  169. this.stageImData.main = err.statck ? '数据错误' : err;
  170. }
  171. return this.stageImData.main;
  172. }
  173. async getMonthProgress(tid) {
  174. const helper = this.ctx.helper;
  175. await this.ctx.service.tender.checkTender(tid);
  176. const tender = this.ctx.tender;
  177. const stages = await this.ctx.service.stage.getValidStages(tender.id);
  178. const lastStage = stages.length > 0 ? stages[0] : null;
  179. if (lastStage) {
  180. await this.ctx.service.stage.checkStageGatherData(lastStage);
  181. tender.gather_tp = helper.add(lastStage.contract_tp, lastStage.qc_tp);
  182. tender.end_contract_tp = helper.add(lastStage.contract_tp, lastStage.pre_contract_tp);
  183. tender.end_qc_tp = helper.add(lastStage.qc_tp, lastStage.pre_qc_tp);
  184. tender.end_gather_tp = helper.add(tender.end_contract_tp, tender.end_qc_tp);
  185. tender.pre_gather_tp = helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  186. tender.yf_tp = lastStage.yf_tp;
  187. tender.qc_ratio = helper.mul(helper.div(tender.end_qc_tp, tender.info.deal_param.contractPrice, 2), 100);
  188. tender.sum = helper.add(tender.total_price, tender.end_qc_tp);
  189. tender.pre_ratio = helper.mul(helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  190. tender.cur_ratio = helper.mul(helper.div(tender.gather_tp, tender.sum, 2), 100);
  191. tender.other_tp = helper.sub(helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  192. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  193. }
  194. const monthProgress = [];
  195. for (const s of stages) {
  196. if (s.s_time) {
  197. let progress = monthProgress.find(function (x) {
  198. return x.month === s.s_time;
  199. });
  200. if (!progress) {
  201. progress = {month: s.s_time};
  202. monthProgress.push(progress);
  203. }
  204. progress.tp = helper.add(helper.add(progress.tp, s.contract_tp), s.qc_tp);
  205. }
  206. }
  207. monthProgress.sort(function (x, y) {
  208. return Date.parse(x.month) - Date.parse(y.month);
  209. });
  210. let sum = 0;
  211. for (const p of monthProgress) {
  212. p.ratio = helper.mul(helper.div(p.tp, tender.sum, 4), 100);
  213. sum = helper.add(sum, p.tp);
  214. p.end_tp = sum;
  215. p.end_ratio = helper.mul(helper.div(p.end_tp, tender.sum, 4), 100);
  216. }
  217. return monthProgress;
  218. }
  219. }
  220. return ReportMemory;
  221. };