|
@@ -0,0 +1,159 @@
|
|
|
+package data_manage
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "hongze/hongze_task/models/data_manage"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+type CalculateItems struct {
|
|
|
+ EdbInfoId int
|
|
|
+ DataMap map[string]float64
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+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
|
|
|
+}
|
|
|
+
|
|
|
+func ReplaceFormula(edbInfoIdArr []*data_manage.EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
|
|
|
+ funMap := GetFormulaMap()
|
|
|
+ for k, v := range funMap {
|
|
|
+ formulaStr = strings.Replace(formulaStr, k, v, -1)
|
|
|
+ }
|
|
|
+
|
|
|
+ replaceCount := 0
|
|
|
+ for dk, dv := range edbInfoIdArr {
|
|
|
+ if dk == 0 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 1 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 2 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 3 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 4 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 5 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 6 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 7 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if dk == 8 {
|
|
|
+ dKey := edbInfoIdBytes[dk]
|
|
|
+ if _, ok := formulaMap[dKey]; ok {
|
|
|
+ if val, valOk := valArr[dv.EdbInfoId]; valOk {
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaStr = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ replaceCount++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for k, v := range funMap {
|
|
|
+ formulaStr = strings.Replace(formulaStr, v, k, -1)
|
|
|
+ }
|
|
|
+ if replaceCount == len(formulaMap) {
|
|
|
+ return formulaStr
|
|
|
+ } else {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func GetFormulaMap() map[string]string {
|
|
|
+ funMap := make(map[string]string)
|
|
|
+ funMap["MAX"] = "[@@]"
|
|
|
+ funMap["MIN"] = "[@!]"
|
|
|
+ funMap["ABS"] = "[@#]"
|
|
|
+ funMap["CEIL"] = "[@$]"
|
|
|
+ funMap["COS"] = "[@%]"
|
|
|
+ funMap["FLOOR"] = "[@^]"
|
|
|
+ funMap["MOD"] = "[@&]"
|
|
|
+ funMap["POW"] = "[@*]"
|
|
|
+ funMap["ROUND"] = "[@(]"
|
|
|
+ return funMap
|
|
|
+}
|