xyxie 1 жил өмнө
parent
commit
3dac82604e

+ 24 - 21
models/base_from_calculate.go

@@ -7,7 +7,6 @@ import (
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/shopspring/decimal"
 	"github.com/yidane/formula"
-	"regexp"
 	"strconv"
 	"strings"
 	"time"
@@ -515,13 +514,13 @@ func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, sou
 func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr, valArrMax map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string, maxDealFlag bool) string {
 	// todo 先处理max和min函数的特殊情况
 	//if strings.Contains(formulaStr, "MAX") || strings.Contains(formulaStr, "MIN") {
-	if maxDealFlag {
-		formulaStr = GetMaxMinEdbInfo(formulaStr)
-	}
 	funMap := GetFormulaMap()
 	for k, v := range funMap {
 		formulaStr = strings.Replace(formulaStr, k, v, -1)
 	}
+	if maxDealFlag {
+		formulaStr = GetMaxMinEdbInfo(formulaStr)
+	}
 	replaceCount := 0
 	for dk, dv := range edbInfoIdArr {
 		var isReplace bool
@@ -576,7 +575,7 @@ func GetFormulaMap() map[string]string {
 	funMap["FLOOR"] = "[@^]"
 	funMap["MOD"] = "[@&]"
 	funMap["POW"] = "[@*]"
-	funMap["ROUND"] = "[@(]"
+	funMap["ROUND"] = "[@`]"
 	funMap["LN"] = "[@-]"
 	funMap["EXP"] = "[@+]"
 	return funMap
@@ -676,7 +675,7 @@ func HandleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.T
 		if dateTime.Before(maxStartDate) {
 			continue
 		}
-		// 如果当前日期晚于数据的最小结束日期,那么不处理,直接返回了
+		// todo 规则是否生效如果当前日期晚于数据的最小结束日期,那么不处理,直接返回了
 		if dateTime.After(minLatestDate) {
 			return
 		}
@@ -851,21 +850,25 @@ func handleDateDataMapZero(saveDataMap map[string]map[int]float64, date string,
 }
 
 func GetMaxMinEdbInfo(formula string) string {
-	//formula := "A+min(A,B,max(A,C))"
-	// todo 无法处理max里嵌套max或者min的情况
-	// 使用正则表达式匹配MAX和MIN函数及其参数
-	regex := regexp.MustCompile(`(?i)(MAX|MIN)\((.*?)\)`)
-	matches := regex.FindAllStringSubmatch(formula, -1)
-	// 遍历匹配结果,输出MAX和MIN函数及其参数
-	for _, match := range matches {
-		if len(match) == 3 {
-			parameter := strings.ToLower(match[0]) // 参数
-			formula = strings.ReplaceAll(formula, match[0], parameter)
-			fmt.Printf("formula: %s\n", formula)
-		}
-	}
-	formula = strings.ReplaceAll(formula, "max", "MAX")
-	formula = strings.ReplaceAll(formula, "min", "MIN")
+	flag := 0
+	for k, v := range formula {
+		if v >= 65 && v <= 122 {
+			// 从左边找到第一个左括号的位置,往前截取三个字符,判断是不是max或者min,如果是的话,把该入参改成小写
+			for i := k; i >= 4; i-- {
+				if formula[i:i+1] == "(" && flag >= 1 {
+					t := formula[i-4 : i]
+					if t == "[@@]" || t == "[@!]" {
+						formula = formula[:k] + strings.ToLower(formula[k:k+1]) + formula[k+1:]
+					}
+					break
+				}
+			}
+		} else if formula[k:k+1] == "(" {
+			flag += 1
+		} else if formula[k:k+1] == ")" {
+			flag -= 1
+		}
+	}
 	return formula
 }
 

+ 1 - 0
models/base_predict_from_calculate.go

@@ -397,6 +397,7 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
 		formulaFormStr := ReplaceFormula(edbInfoIdList, sv, svMax, formulaMap, formulaStr, edbInfoIdBytes, maxDealFlag)
 		if formulaFormStr != "" {
 			//utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
+			fmt.Println(fmt.Sprintf("date %s:formulaFormStr:%s", sk, formulaFormStr))
 			expression := formula.NewExpression(formulaFormStr)
 			calResult, tmpErr := expression.Evaluate()
 			if tmpErr != nil {