Procházet zdrojové kódy

指标参数值更新接口,更新指标参数同时,计算指标

MaiXinRong před 7 roky
rodič
revize
591629d530

+ 2 - 0
app/const/template_param.js

@@ -41,6 +41,7 @@ matchNumStr[matchNum.dgn_quantity1] = '设计数量1';
 matchNumStr[matchNum.dgn_quantity2] = '设计数量2';
 matchNumStr[matchNum.quantity] = '清单数量';
 
+const globalParamNodeId = 0;
 // 默认全局参数
 const defaultGlobalParams = [
     {
@@ -133,6 +134,7 @@ module.exports = {
     validProperties,
     matchNum,
     matchNumStr,
+    globalParamNodeId,
     defaultGlobalParams,
     defaultNodeParams
 }

+ 9 - 6
app/service/index_calc.js

@@ -23,6 +23,8 @@ module.exports = app => {
             for (const np of this.nodeParams) {
                 this.nodeParamsIndex[np.code] = np.calc_value;
             }
+            console.log(this.nodeParams);
+            console.log(this.nodeParamsIndex);
             this.updateArr = [];
         }
 
@@ -38,14 +40,14 @@ module.exports = app => {
             let param = '', isParamBefore = false;
             while(exprStack.length > 0){
                 const cur = exprStack.shift();
-                if(this.ctx.helper.isOperator(cur)){
+                if (this.ctx.helper.isOperator(cur)) {
                     if (isParamBefore) {
                         result.push(param);
                         param = '';
                     }
                     result.push(cur);
                     isParamBefore = false;
-                }else{
+                } else {
                     param = param + cur;
                     isParamBefore = true;
                     if (exprStack.length === 0) {
@@ -66,7 +68,7 @@ module.exports = app => {
                 } else if (this.globalParamsIndex[p]) {
                     evalParts.push(this.globalParamsIndex[p]);
                 } else if (this.nodeParamsIndex[p]) {
-                    evalParts.push(this.globalParamsIndex[p]);
+                    evalParts.push(this.nodeParamsIndex[p]);
                 }
             }
             return evalParts.join('');
@@ -75,9 +77,10 @@ module.exports = app => {
         calculate(indexes, globalParams, nodeParams) {
             this._initCalc(indexes, globalParams, nodeParams);
             for (const index of indexes) {
-                index.eval_rule = this._getEvalRule(index.calc_rule);
-                const value = this.ctx.helper.calcExprStr(index.eval_rule);
-                if (value !== index.value) {
+                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);
                 }

+ 4 - 4
app/service/match.js

@@ -153,9 +153,9 @@ module.exports = app => {
          * @param {Number} nodeId
          * @private
          */
-        _syncNodeParam(nodeId, nodeBills) {
+        _syncNodeParam(sourceId, nodeId, nodeBills) {
             const nodeParams = this.templateParams.filter(function (p) {
-                return p.node_id === nodeId;
+                return p.node_id === sourceId;
             });
             for (const np of nodeParams) {
                 const newParam = {
@@ -224,7 +224,7 @@ module.exports = app => {
                     bills_id: mb.n_id,
                 }
                 this.nodes.push(newNode);
-                this._syncNodeParam(newNode.source_id, mb);
+                this._syncNodeParam(newNode.source_id, newNode.node_id, mb);
                 this._syncNodeIndex(newNode);
                 mb.match_node = newNode.node_id;
             }
@@ -240,7 +240,7 @@ module.exports = app => {
             // 获取指标模板全部数据
             await this._getTemplateData(1);
             // 同步全局指标参数
-            this._syncNodeParam(0, this.bills[0]);
+            this._syncNodeParam(paramConst.globalParamNodeId, paramConst.globalParamNodeId, this.bills[0]);
             // 遍历模板中所有指标节点,匹配清单
             for (const node of this.templateNodes) {
                 this._matchNode(node);

+ 1 - 1
app/service/template_index.js

@@ -48,7 +48,7 @@ module.exports = app => {
             const result = await this.db.update(this.tableName, data);
             return result;
         }
-    };
+    }
 
     return TemplateIndex;
 };

+ 51 - 1
app/service/tender_param.js

@@ -8,6 +8,7 @@
  * @version
  */
 
+const paramConst = require('../const/template_param');
 module.exports = app => {
     class TenderParam extends app.BaseService {
         /**
@@ -21,6 +22,44 @@ module.exports = app => {
             this.tableName = 'tender_param';
         }
 
+
+        async _calculateNodeIndex(transaction, condition, globalParams, newParam) {
+            const nodeParams = await this.ctx.service.tenderParam.getAllDataByCondition({where: condition});
+            const nodeIndexes = await this.ctx.service.tenderIndex.getAllDataByCondition({where: condition});
+            if (newParam) {
+                for (const np of nodeParams) {
+                    if (np.code === newParam.code) {
+                        np.calc_value = newParam.value;
+                        break;
+                    }
+                }
+            }
+            this.ctx.service.indexCalc.calculate(nodeIndexes, globalParams, nodeParams);
+            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 } },
+                );
+            }
+        }
+
+        async _calculateAllIndex(transaction, condition, newParam) {
+            const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
+                where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
+            });
+            for (const gp of globalParams) {
+                if (gp.code === newParam.code) {
+                    gp.calc_value = newParam.value;
+                    break;
+                }
+            }
+            const nodes = await this.ctx.tenderNode.getAllDataByCondition({where: condition});
+            for (const node of nodes) {
+                condition.node_id = node.node_id;
+                await this._calculateNodeIndex(transaction, condition, globalParams);
+            }
+        }
+
         /**
          * 更新参数取值
          * @param data
@@ -41,7 +80,18 @@ module.exports = app => {
                     calc_value: parseFloat(data.value),
                 };
                 await transaction.update(this.tableName, updateData, {where: condition});
-                // to do 计算
+                if (condition.node_id === paramConst.globalParamNodeId) {
+                    const calcCondition = { lib_id: condition.lib_id };
+                    const calcData = { code: data.code, value: updateData.calc_value };
+                    await this._calculateAllIndex(transaction, condition, calcData);
+                } else {
+                    const globalParams = await this.ctx.service.tenderParam.getAllDataByCondition({
+                        where: {lib_id: condition.lib_id, node_id: paramConst.globalParamNodeId}
+                    });
+                    const calcCondition = { lib_id: condition.lib_id, node_id: data.node_id };
+                    const calcData = { code: data.code, value: updateData.calc_value };
+                    await this._calculateNodeIndex(transaction, calcCondition, globalParams, calcData);
+                }
                 await transaction.commit();
                 return true;
             } catch (err) {