Преглед изворни кода

Merge branch 'chart/10.6' into debug

Roc пре 2 година
родитељ
комит
6d916fae0c
2 измењених фајлова са 37 додато и 1 уклоњено
  1. 14 0
      controllers/base_from_predict_calculate.go
  2. 23 1
      services/base_from_calculate.go

+ 14 - 0
controllers/base_from_predict_calculate.go

@@ -173,6 +173,13 @@ func addPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSav
 		}
 	}
 
+	//预先计算,判断公式是否正常
+	ok, _ := services.CheckFormula2(edbInfoList, formulaMap, calculateFormula, edbInfoIdBytes)
+	if !ok {
+		br.Msg = "生成计算指标失败,请使用正确的计算公式"
+		return
+	}
+
 	// 指标入库
 	randStr := utils.GetRandDigit(4)
 	edbCode := `C2` + time.Now().Format("060102") + randStr
@@ -401,6 +408,13 @@ func editPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSa
 		}
 	}
 
+	//预先计算,判断公式是否正常
+	ok, _ := services.CheckFormula2(edbInfoList, formulaMap, calculateFormula, edbInfoIdBytes)
+	if !ok {
+		br.Msg = "生成计算指标失败,请使用正确的计算公式"
+		return
+	}
+
 	//处理同名指标
 	{
 		edbNameList, err := models.GetEdbInfoByName(req.EdbName)

+ 23 - 1
services/base_from_calculate.go

@@ -1,6 +1,8 @@
 package services
 
 import (
+	"github.com/yidane/formula"
+	"hongze/hongze_edb_lib/models"
 	"strings"
 )
 
@@ -26,4 +28,24 @@ func CheckFormula(formula string) map[string]string {
 		}
 	}
 	return byteMap
-}
+}
+
+// CheckFormula2 校验公式是否正常(比如说除法的分母不能为0之类的,实际上就是用预设的字段数据做一次计算)
+func CheckFormula2(edbInfoArr []*models.EdbInfo, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) (ok bool, err error) {
+	valArr := make(map[int]float64)
+	for _, v := range edbInfoArr {
+		valArr[v.EdbInfoId] = 100
+	}
+	formulaStr = strings.ToUpper(formulaStr)
+	formulaFormStr := models.ReplaceFormula(edbInfoArr, valArr, formulaMap, formulaStr, edbInfoIdBytes)
+	if formulaFormStr == "" {
+		return
+	}
+	expression := formula.NewExpression(formulaFormStr)
+	_, err = expression.Evaluate()
+	if err != nil {
+	} else {
+		ok = true
+	}
+	return
+}