tender_param.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. const paramConst = app.paramConst;
  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, gRelaParam) {
  32. // let time1 = new Date();
  33. const nodeParams = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
  34. // await this.ctx.service.log.addLog('node' + this.serialNo + ':select tenderParam', new Date() - time1);
  35. //
  36. // time1 = new Date();
  37. const nodeIndexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
  38. // await this.ctx.service.log.addLog('node' + this.serialNo + ':select tenderIndex', new Date() - time1);
  39. //
  40. // time1 = new Date();
  41. let relaParam;
  42. if (newParam) {
  43. for (const np of nodeParams) {
  44. if (np.code === newParam.code) {
  45. np.calc_value = newParam.value;
  46. relaParam = np;
  47. break;
  48. }
  49. }
  50. } else {
  51. relaParam = gRelaParam;
  52. }
  53. this.ctx.service.indexCalc.calculate(nodeIndexes, globalParams, nodeParams, relaParam);
  54. for (const u of this.ctx.service.indexCalc.updateArr) {
  55. await transaction.update(this.ctx.service.tenderIndex.tableName, {
  56. id: u.id,
  57. eval_rule: u.eval_rule,
  58. value: u.value,
  59. });
  60. }
  61. // await this.ctx.service.log.addLog('node' + this.serialNo + ':update tenderIndex', new Date() - time1);
  62. }
  63. /**
  64. * 计算标段内全部指标
  65. * @param transaction - 事务
  66. * @param {Object} condition - 计算范围
  67. * @param {Object} newParam - 更新的全局参数
  68. * @returns {Promise<void>}
  69. * @private
  70. */
  71. async _calculateAllIndex(transaction, condition, newParam) {
  72. const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
  73. where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
  74. });
  75. let relaParam;
  76. if (newParam) {
  77. for (const gp of globalParams) {
  78. if (gp.code === newParam.code) {
  79. gp.calc_value = newParam.value;
  80. relaParam = gp;
  81. break;
  82. }
  83. }
  84. }
  85. const nodes = await this.ctx.service.tenderNode.getAllDataByCondition({where: condition});
  86. const indexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
  87. const params = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
  88. // this.serialNo = 1;
  89. for (const node of nodes) {
  90. // condition.node_id = node.node_id;
  91. // await this._calculateNodeIndex(transaction, condition, globalParams);
  92. // this.serialNo += 1;
  93. node.indexes = indexes.filter(function (a) {
  94. return a.node_id === node.node_id;
  95. });
  96. node.params = params.filter(function (a) {
  97. return a.node_id === node.node_id;
  98. });
  99. this.ctx.service.indexCalc.calculate(node.indexes, globalParams, node.params, relaParam);
  100. for (const u of this.ctx.service.indexCalc.updateArr) {
  101. await transaction.update(this.ctx.service.tenderIndex.tableName, {
  102. id: u.id,
  103. eval_rule: u.eval_rule,
  104. value: u.value,
  105. });
  106. }
  107. }
  108. }
  109. /**
  110. * 更新参数取值
  111. * @param data
  112. * @returns {Promise<boolean>}
  113. */
  114. async updateCalcValue(data) {
  115. const transaction = await this.db.beginTransaction();
  116. try {
  117. const condition = {
  118. lib_id: parseInt(data.lib_id),
  119. node_id: data.node_id,
  120. code: data.code
  121. };
  122. if (condition.lib_id < 0 || condition.node_id < 0) {
  123. throw '提交数据错误';
  124. }
  125. data.value = parseFloat(data.value);
  126. const updateData = {
  127. calc_value: (isNaN(data.value) ? null : data.value),
  128. };
  129. await transaction.update(this.tableName, updateData, {where: condition});
  130. if (condition.node_id === paramConst.globalParamNodeId) {
  131. const calcCondition = { lib_id: condition.lib_id };
  132. const calcData = { code: data.code, value: updateData.calc_value };
  133. await this._calculateAllIndex(transaction, calcCondition, calcData);
  134. } else {
  135. const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
  136. where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
  137. });
  138. const calcCondition = { lib_id: condition.lib_id, node_id: data.node_id };
  139. const calcData = { code: data.code, value: updateData.calc_value };
  140. await this._calculateNodeIndex(transaction, calcCondition, globalParams, calcData);
  141. }
  142. await transaction.commit();
  143. return true;
  144. } catch (err) {
  145. await transaction.rollback();
  146. throw err;
  147. }
  148. }
  149. /**
  150. * 重置参数取值
  151. * @param data
  152. * @returns {Promise<boolean>}
  153. */
  154. async resetCalcValue(data) {
  155. try {
  156. const condition = {
  157. lib_id: parseInt(data.lib_id),
  158. node_id: data.node_id,
  159. code: data.code,
  160. };
  161. if (condition.lib_id < 0 || condition.node_id < 0) {
  162. throw '提交数据错误';
  163. }
  164. const param = await this.getDataByCondition(condition);
  165. data.value = param.match_value;
  166. await this.updateCalcValue(data);
  167. return true;
  168. } catch (err) {
  169. throw err;
  170. }
  171. }
  172. };
  173. return TenderParam;
  174. };