123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- package data
- import (
- "eta/eta_task/models/data_manage"
- "fmt"
- "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++
- }
- }
- }
- if dk == 9 {
- 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 == 10 {
- 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 == 11 {
- 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 == 12 {
- 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 == 13 {
- 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 == 14 {
- 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 == 15 {
- 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 == 16 {
- 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 == 17 {
- 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 == 18 {
- 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 == 19 {
- 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
- }
- // 修复计算指标与基础指标的关联关系
- func FixEdbInfoCalculateMapping() {
- fmt.Println("start")
- data_manage.FixEdbInfoCalculateMapping()
- fmt.Println("end")
- }
|