瀏覽代碼

修改全局参数,计算优化

MaiXinRong 7 年之前
父節點
當前提交
bc0b04f5c8
共有 3 個文件被更改,包括 80 次插入17 次删除
  1. 12 9
      app/service/index_calc.js
  2. 39 0
      app/service/log.js
  3. 29 8
      app/service/tender_param.js

+ 12 - 9
app/service/index_calc.js

@@ -11,7 +11,7 @@
 const Service = require('egg').Service;
 module.exports = app => {
     class IndexCalc extends Service {
-        _initCalc (indexes, globalParams, nodeParams) {
+        _initCalc (indexes, globalParams, nodeParams, relaParam) {
             this.indexes = indexes;
             this.globalParams = globalParams;
             this.globalParamsIndex = {};
@@ -24,6 +24,7 @@ module.exports = app => {
                 this.nodeParamsIndex[np.code] = np.calc_value;
             }
             this.updateArr = [];
+            this.relaParam = relaParam;
         }
 
         _splitByOperator(expr) {
@@ -72,15 +73,17 @@ module.exports = app => {
             return evalParts.join('');
         }
 
-        calculate(indexes, globalParams, nodeParams) {
-            this._initCalc(indexes, globalParams, nodeParams);
+        calculate(indexes, globalParams, nodeParams, relaParam) {
+            this._initCalc(indexes, globalParams, nodeParams, relaParam);
             for (const index of indexes) {
-                const eval_rule = this._getEvalRule(index.calc_rule);
-                if (eval_rule !== index.eval_rule) {
-                    index.eval_rule = eval_rule;
-                    const value = this.ctx.helper.calcExprStr(index.eval_rule);
-                    index.value = value ? Number(value.toFixed(4)) : null;
-                    this.updateArr.push(index);
+                if (!this.relaParam || index.calc_rule.match(this.relaParam.code)) {
+                    const eval_rule = this._getEvalRule(index.calc_rule);
+                    if (eval_rule !== index.eval_rule) {
+                        index.eval_rule = eval_rule;
+                        const value = this.ctx.helper.calcExprStr(index.eval_rule);
+                        index.value = value ? Number(value.toFixed(4)) : null;
+                        this.updateArr.push(index);
+                    }
                 }
             }
         }

+ 39 - 0
app/service/log.js

@@ -0,0 +1,39 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date 2018/6/8
+ * @version
+ */
+
+module.exports = app => {
+    class Log extends app.BaseService {
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局context
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'log';
+        }
+
+        async addLog(operation, time) {
+            console.log(time);
+            try {
+                await this.db.insert(this.tableName, {
+                    operation: operation,
+                    time: time,
+                });
+            } catch (err) {
+                console.log(err);
+            }
+        }
+
+    };
+
+    return Log;
+};

+ 29 - 8
app/service/tender_param.js

@@ -33,23 +33,28 @@ module.exports = app => {
          * @returns {Promise<void>}
          * @private
          */
-        async _calculateNodeIndex(transaction, condition, globalParams, newParam) {
+        async _calculateNodeIndex(transaction, condition, globalParams, newParam, gRelaParam) {
             const nodeParams = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
             const nodeIndexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
+            let relaParam;
             if (newParam) {
                 for (const np of nodeParams) {
                     if (np.code === newParam.code) {
                         np.calc_value = newParam.value;
+                        relaParam = np;
                         break;
                     }
                 }
+            } else {
+                relaParam = gRelaParam;
             }
-            this.ctx.service.indexCalc.calculate(nodeIndexes, globalParams, nodeParams);
+            this.ctx.service.indexCalc.calculate(nodeIndexes, globalParams, nodeParams, relaParam);
             for (const u of this.ctx.service.indexCalc.updateArr) {
-                await transaction.update(this.ctx.service.tenderIndex.tableName,
-                    { eval_rule: u.eval_rule, value: u.value },
-                    { where: { lib_id: u.lib_id, index_id: u.index_id } },
-                );
+                await transaction.update(this.ctx.service.tenderIndex.tableName, {
+                    id: u.id,
+                    eval_rule: u.eval_rule,
+                    value: u.value,
+                });
             }
         }
 
@@ -65,18 +70,34 @@ module.exports = app => {
             const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
                 where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
             });
+            let relaParam;
             if (newParam) {
                 for (const gp of globalParams) {
                     if (gp.code === newParam.code) {
                         gp.calc_value = newParam.value;
+                        relaParam = gp;
                         break;
                     }
                 }
             }
             const nodes = await this.ctx.service.tenderNode.getAllDataByCondition({where: condition});
+            const indexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
+            const params = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
             for (const node of nodes) {
-                condition.node_id = node.node_id;
-                await this._calculateNodeIndex(transaction, condition, globalParams);
+                node.indexes = indexes.filter(function (a) {
+                    return a.node_id === node.node_id;
+                });
+                node.params = params.filter(function (a) {
+                    return a.node_id === node.node_id;
+                });
+                this.ctx.service.indexCalc.calculate(node.indexes, globalParams, node.params, relaParam);
+                for (const u of this.ctx.service.indexCalc.updateArr) {
+                    await transaction.update(this.ctx.service.tenderIndex.tableName, {
+                        id: u.id,
+                        eval_rule: u.eval_rule,
+                        value: u.value,
+                    });
+                }
             }
         }