tender_param.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const paramConst = require('../const/template_param');
  10. module.exports = app => {
  11. class TenderParam extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局context
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'tender_param';
  21. }
  22. /**
  23. * 计算指标节点下全部指标
  24. * @param transaction - 事务
  25. * @param {Object} condition - 计算范围
  26. * @param {Array} globalParams - 可用全局参数
  27. * @param {Object} newParam - 更新的节点参数
  28. * @returns {Promise<void>}
  29. * @private
  30. */
  31. async _calculateNodeIndex(transaction, condition, globalParams, newParam) {
  32. const nodeParams = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
  33. const nodeIndexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
  34. if (newParam) {
  35. for (const np of nodeParams) {
  36. if (np.code === newParam.code) {
  37. np.calc_value = newParam.value;
  38. break;
  39. }
  40. }
  41. }
  42. this.ctx.service.indexCalc.calculate(nodeIndexes, globalParams, nodeParams);
  43. for (const u of this.ctx.service.indexCalc.updateArr) {
  44. await transaction.update(this.ctx.service.tenderIndex.tableName,
  45. { eval_rule: u.eval_rule, value: u.value },
  46. { where: { lib_id: u.lib_id, index_id: u.index_id } },
  47. );
  48. }
  49. }
  50. /**
  51. * 计算标段内全部指标
  52. * @param transaction - 事务
  53. * @param {Object} condition - 计算范围
  54. * @param {Object} newParam - 更新的全局参数
  55. * @returns {Promise<void>}
  56. * @private
  57. */
  58. async _calculateAllIndex(transaction, condition, newParam) {
  59. const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
  60. where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
  61. });
  62. if (newParam) {
  63. for (const gp of globalParams) {
  64. if (gp.code === newParam.code) {
  65. gp.calc_value = newParam.value;
  66. break;
  67. }
  68. }
  69. }
  70. const nodes = await this.ctx.service.tenderNode.getAllDataByCondition({where: condition});
  71. for (const node of nodes) {
  72. condition.node_id = node.node_id;
  73. await this._calculateNodeIndex(transaction, condition, globalParams);
  74. }
  75. }
  76. /**
  77. * 更新参数取值
  78. * @param data
  79. * @returns {Promise<boolean>}
  80. */
  81. async updateCalcValue(data) {
  82. const transaction = await this.db.beginTransaction();
  83. try {
  84. const condition = {
  85. lib_id: parseInt(data.lib_id),
  86. node_id: data.node_id,
  87. code: data.code
  88. };
  89. if (condition.lib_id < 0 || condition.node_id < 0) {
  90. throw '提交数据错误';
  91. }
  92. data.value = parseFloat(data.value);
  93. const updateData = {
  94. calc_value: (isNaN(data.value) ? null : data.value),
  95. };
  96. await transaction.update(this.tableName, updateData, {where: condition});
  97. if (condition.node_id === paramConst.globalParamNodeId) {
  98. const calcCondition = { lib_id: condition.lib_id };
  99. const calcData = { code: data.code, value: updateData.calc_value };
  100. await this._calculateAllIndex(transaction, condition, calcData);
  101. } else {
  102. const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
  103. where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
  104. });
  105. const calcCondition = { lib_id: condition.lib_id, node_id: data.node_id };
  106. const calcData = { code: data.code, value: updateData.calc_value };
  107. await this._calculateNodeIndex(transaction, calcCondition, globalParams, calcData);
  108. }
  109. await transaction.commit();
  110. return true;
  111. } catch (err) {
  112. await transaction.rollback();
  113. throw err;
  114. }
  115. }
  116. /**
  117. * 重置参数取值
  118. * @param data
  119. * @returns {Promise<boolean>}
  120. */
  121. async resetCalcValue(data) {
  122. try {
  123. const condition = {
  124. lib_id: parseInt(data.lib_id),
  125. node_id: data.node_id,
  126. code: data.code,
  127. };
  128. if (condition.lib_id < 0 || condition.node_id < 0) {
  129. throw '提交数据错误';
  130. }
  131. const param = await this.getDataByCondition(condition);
  132. data.value = param.match_value;
  133. await this.updateCalcValue(data);
  134. return true;
  135. } catch (err) {
  136. throw err;
  137. }
  138. }
  139. };
  140. return TenderParam;
  141. };