123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package services
- import (
- "github.com/yidane/formula"
- "hongze/hongze_edb_lib/models"
- "strings"
- )
- // CheckFormula 检测计算公式是否异常
- func CheckFormula(formula string) map[string]string {
- mathFormula := []string{"MAX", "MIN", "ABS", "ACOS", "ASIN", "CEIL", "MOD", "POW", "ROUND", "SIGN", "SIN", "TAN", "LOG10", "LOG2", "LOG"}
- str := strings.ToUpper(formula)
- for _, v := range mathFormula {
- str = strings.Replace(str, v, "", -1)
- }
- str = strings.Replace(str, "(", "", -1)
- str = strings.Replace(str, ")", "", -1)
- byteMap := make(map[string]string)
- for i := 0; i < len(str); i++ {
- byteInt := str[i]
- if byteInt >= 65 && byteInt <= 90 {
- byteStr := string(byteInt)
- if _, ok := byteMap[byteStr]; !ok {
- byteMap[byteStr] = byteStr
- }
- }
- }
- 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
- }
|