tender_param.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. async _calculateNodeIndex(transaction, condition, globalParams, newParam) {
  23. const nodeParams = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
  24. const nodeIndexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
  25. if (newParam) {
  26. for (const np of nodeParams) {
  27. if (np.code === newParam.code) {
  28. np.calc_value = newParam.value;
  29. break;
  30. }
  31. }
  32. }
  33. this.ctx.service.indexCalc.calculate(nodeIndexes, globalParams, nodeParams);
  34. for (const u of this.ctx.service.indexCalc.updateArr) {
  35. await transaction.update(this.ctx.service.tenderIndex.tableName,
  36. { eval_rule: u.eval_rule, value: u.value },
  37. { where: { lib_id: u.lib_id, index_id: u.index_id } },
  38. );
  39. }
  40. }
  41. async _calculateAllIndex(transaction, condition, newParam) {
  42. const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
  43. where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
  44. });
  45. for (const gp of globalParams) {
  46. if (gp.code === newParam.code) {
  47. gp.calc_value = newParam.value;
  48. break;
  49. }
  50. }
  51. const nodes = await this.ctx.tenderNode.getAllDataByCondition({where: condition});
  52. for (const node of nodes) {
  53. condition.node_id = node.node_id;
  54. await this._calculateNodeIndex(transaction, condition, globalParams);
  55. }
  56. }
  57. /**
  58. * 更新参数取值
  59. * @param data
  60. * @returns {Promise<boolean>}
  61. */
  62. async updateCalcValue(data) {
  63. const transaction = await this.db.beginTransaction();
  64. try {
  65. const condition = {
  66. lib_id: parseInt(data.lib_id),
  67. node_id: data.node_id,
  68. code: data.code
  69. };
  70. if (condition.lib_id < 0 || condition.node_id < 0) {
  71. throw '提交数据错误';
  72. }
  73. const updateData = {
  74. calc_value: parseFloat(data.value),
  75. };
  76. await transaction.update(this.tableName, updateData, {where: condition});
  77. if (condition.node_id === paramConst.globalParamNodeId) {
  78. const calcCondition = { lib_id: condition.lib_id };
  79. const calcData = { code: data.code, value: updateData.calc_value };
  80. await this._calculateAllIndex(transaction, condition, calcData);
  81. } else {
  82. const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
  83. where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
  84. });
  85. const calcCondition = { lib_id: condition.lib_id, node_id: data.node_id };
  86. const calcData = { code: data.code, value: updateData.calc_value };
  87. await this._calculateNodeIndex(transaction, calcCondition, globalParams, calcData);
  88. }
  89. await transaction.commit();
  90. return true;
  91. } catch (err) {
  92. await transaction.rollback();
  93. throw err;
  94. }
  95. }
  96. /**
  97. * 重置参数取值
  98. * @param data
  99. * @returns {Promise<boolean>}
  100. */
  101. async resetCalcValue(data) {
  102. try {
  103. const condition = {
  104. lib_id: parseInt(data.lib_id),
  105. node_id: data.node_id,
  106. code: data.code,
  107. };
  108. if (condition.lib_id < 0 || condition.node_id < 0) {
  109. throw '提交数据错误';
  110. }
  111. const param = await this.getDataByCondition(condition);
  112. data.value = param.match_value;
  113. await this.updateCalcValue(data);
  114. return true;
  115. } catch (err) {
  116. throw err;
  117. }
  118. }
  119. };
  120. return TenderParam;
  121. };