tender_param.js 7.5 KB

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