|
@@ -24,6 +24,7 @@ type EdbInfoCalculateSaveReq struct {
|
|
|
ClassifyId int `description:"分类id"`
|
|
|
CalculateFormula string `description:"计算公式"`
|
|
|
EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
|
|
|
+ MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
|
|
|
EdbInfoIdArr []EdbInfoFromTag
|
|
|
}
|
|
|
|
|
@@ -39,92 +40,6 @@ type CalculateItems struct {
|
|
|
DataMap map[string]float64
|
|
|
}
|
|
|
|
|
|
-// AddCalculate 新增计算(运算)指标
|
|
|
-func AddCalculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- defer func() {
|
|
|
- if err != nil {
|
|
|
- utils.FileLog.Info(fmt.Sprintf("Calculate Err:%s" + err.Error()))
|
|
|
- }
|
|
|
- }()
|
|
|
- saveDataMap := make(map[string]map[int]float64)
|
|
|
- for _, v := range edbInfoIdArr {
|
|
|
- var condition string
|
|
|
- var pars []interface{}
|
|
|
- condition += " AND edb_info_id=? "
|
|
|
- pars = append(pars, v.EdbInfoId)
|
|
|
- dataList, err := GetEdbDataListAll(condition, pars, v.Source, 1)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- dataMap := make(map[string]float64)
|
|
|
- for _, dv := range dataList {
|
|
|
- if val, ok := saveDataMap[dv.DataTime]; ok {
|
|
|
- if _, ok := val[v.EdbInfoId]; !ok {
|
|
|
- val[v.EdbInfoId] = dv.Value
|
|
|
- }
|
|
|
- } else {
|
|
|
- temp := make(map[int]float64)
|
|
|
- temp[v.EdbInfoId] = dv.Value
|
|
|
- saveDataMap[dv.DataTime] = temp
|
|
|
- }
|
|
|
- }
|
|
|
- item := new(CalculateItems)
|
|
|
- item.EdbInfoId = v.EdbInfoId
|
|
|
- item.DataMap = dataMap
|
|
|
- }
|
|
|
- formulaMap := utils.CheckFormula(formulaStr)
|
|
|
- addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
- nowStr := time.Now().Format(utils.FormatDateTime)
|
|
|
- var isAdd bool
|
|
|
- for sk, sv := range saveDataMap {
|
|
|
- formulaStr = strings.ToUpper(formulaStr)
|
|
|
- formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
|
|
|
- if formulaStr == "" {
|
|
|
- return
|
|
|
- }
|
|
|
- if formulaFormStr != "" {
|
|
|
- expression := formula.NewExpression(formulaFormStr)
|
|
|
- calResult, err := expression.Evaluate()
|
|
|
- if err != nil {
|
|
|
- err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
- fmt.Println(err)
|
|
|
- return err
|
|
|
- }
|
|
|
- calVal, err := calResult.Float64()
|
|
|
- if err != nil {
|
|
|
- err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
- fmt.Println(err)
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- //需要存入的数据
|
|
|
- {
|
|
|
- dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
|
|
|
- timestamp := dataTime.UnixNano() / 1e6
|
|
|
- timeStr := fmt.Sprintf("%d", timestamp)
|
|
|
- addSql += "("
|
|
|
- addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
|
|
|
- "," + "'" + nowStr + "'" + "," + "'" + timeStr + "'"
|
|
|
- addSql += "),"
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- } else {
|
|
|
- fmt.Println("formulaFormStr is empty")
|
|
|
- }
|
|
|
- }
|
|
|
- if isAdd {
|
|
|
- addSql = strings.TrimRight(addSql, ",")
|
|
|
- _, err = o.Raw(addSql).Exec()
|
|
|
- if err != nil {
|
|
|
- fmt.Println("AddEdbDataCalculate Err:" + err.Error())
|
|
|
- //errMsg = " tx.Exec Err :" + err.Error()
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
// AddCalculateInfo 新增计算(运算)指标
|
|
|
func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbInfoCalculateMapping, edbInfoList []*EdbInfo, edbCode, uniqueCode string, edbInfoIdBytes []string) (edbInfo *EdbInfo, err error) {
|
|
|
|
|
@@ -175,6 +90,7 @@ func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbIn
|
|
|
LatestValue: 0,
|
|
|
ChartImage: "",
|
|
|
EmptyType: req.EmptyType,
|
|
|
+ MaxEmptyType: req.MaxEmptyType,
|
|
|
}
|
|
|
lastId, err := to.Insert(edbInfo)
|
|
|
if err != nil {
|
|
@@ -197,13 +113,13 @@ func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbIn
|
|
|
}
|
|
|
|
|
|
//计算数据
|
|
|
- err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes, edbInfo.EmptyType)
|
|
|
+ err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType)
|
|
|
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// EditCalculateInfo 编辑计算(运算)指标
|
|
|
-func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap map[string]string, edbInfoIdBytes []string, needCalculate bool) (err error, errMsg string) {
|
|
|
+func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaSlice []string, edbInfoIdBytes []string, needCalculate bool) (err error, errMsg string) {
|
|
|
o := orm.NewOrm()
|
|
|
to, err := o.Begin()
|
|
|
if err != nil {
|
|
@@ -227,7 +143,8 @@ func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap
|
|
|
edbInfo.CalculateFormula = req.CalculateFormula
|
|
|
edbInfo.ModifyTime = time.Now()
|
|
|
edbInfo.EmptyType = req.EmptyType
|
|
|
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EmptyType")
|
|
|
+ edbInfo.MaxEmptyType = req.MaxEmptyType
|
|
|
+ _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EmptyType", "MaxEmptyType")
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -278,12 +195,15 @@ func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //预先计算,判断公式是否正常
|
|
|
- ok, _ := CheckFormula2(edbInfoList, formulaMap, req.CalculateFormula, edbInfoIdBytes)
|
|
|
- if !ok {
|
|
|
- errMsg = "生成计算指标失败,请使用正确的计算公式"
|
|
|
- err = errors.New(errMsg)
|
|
|
- return
|
|
|
+ for _, v := range formulaSlice {
|
|
|
+ formulaMap := utils.CheckFormula(v)
|
|
|
+ //预先计算,判断公式是否正常
|
|
|
+ ok, _ := CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
|
|
|
+ if !ok {
|
|
|
+ errMsg = "生成计算指标失败,请使用正确的计算公式"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//关联关系表
|
|
@@ -297,7 +217,7 @@ func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap
|
|
|
}
|
|
|
|
|
|
//计算数据
|
|
|
- err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes, edbInfo.EmptyType)
|
|
|
+ err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType)
|
|
|
|
|
|
}
|
|
|
|
|
@@ -341,7 +261,7 @@ func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
|
|
|
}
|
|
|
|
|
|
// RefreshAllCalculate 刷新全部数据
|
|
|
-func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string, emptyType int) (err error) {
|
|
|
+func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int) (err error) {
|
|
|
o := orm.NewOrm()
|
|
|
to, err := o.Begin()
|
|
|
if err != nil {
|
|
@@ -358,13 +278,13 @@ func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode
|
|
|
fmt.Println(startDate, endDate)
|
|
|
|
|
|
//计算数据
|
|
|
- err = refreshAllCalculate(to, edbInfoIdArr, edbInfoId, source, edbCode, formulaStr, startDate, endDate, edbInfoIdBytes, emptyType)
|
|
|
+ err = refreshAllCalculate(to, edbInfoIdArr, edbInfoId, source, edbCode, formulaStr, startDate, endDate, edbInfoIdBytes, emptyType, maxEmptyType)
|
|
|
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// refreshAllCalculate 刷新全部数据
|
|
|
-func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string, emptyType int) (err error) {
|
|
|
+func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string, emptyType, maxEmptyType int) (err error) {
|
|
|
realSaveDataMap := make(map[string]map[int]float64)
|
|
|
saveDataMap := make(map[string]map[int]float64)
|
|
|
|
|
@@ -437,7 +357,7 @@ func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, sou
|
|
|
//数据处理,将日期内不全的数据做补全
|
|
|
handleDateSaveDataMap(dateList, maxStartDate, minLatestDate, realSaveDataMap, saveDataMap, edbInfoIdArr, emptyType)
|
|
|
|
|
|
- formulaDateSlice, formulaDateMap, err := utils.CheckFormulaJson(formulaStr, maxStartDate.Format(utils.FormatDate))
|
|
|
+ formulaDateSlice, formulaDateMap, err := utils.HandleFormulaJson(formulaStr, maxStartDate.Format(utils.FormatDate))
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -462,6 +382,12 @@ func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, sou
|
|
|
}
|
|
|
edbInfoIdStr := strconv.Itoa(edbInfoId)
|
|
|
existDataMap := make(map[string]string)
|
|
|
+
|
|
|
+ // 判断是否特殊处理max和min函数
|
|
|
+ maxDealFlag := false
|
|
|
+ if emptyType == 4 && maxEmptyType == 2 {
|
|
|
+ maxDealFlag = true
|
|
|
+ }
|
|
|
for sk, sv := range saveDataMap {
|
|
|
// 当空值处理类型选择了不计算时,只要有一个指标在某个日期没有值(即空值),则计算指标在该日期没有值
|
|
|
if emptyType == 1 {
|
|
@@ -485,8 +411,15 @@ func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, sou
|
|
|
if formulaStr == "" {
|
|
|
continue
|
|
|
}
|
|
|
+ svMax := make(map[int]float64)
|
|
|
+ if maxDealFlag {
|
|
|
+ // 特殊处理max和min函数,如果原本的值为空,则选择空值参与运算
|
|
|
+ if svMaxData, ok := realSaveDataMap[sk]; ok {
|
|
|
+ svMax = svMaxData
|
|
|
+ }
|
|
|
+ }
|
|
|
formulaStr = strings.ToUpper(formulaStr)
|
|
|
- formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
|
|
|
+ formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, svMax, formulaMap, formulaStr, edbInfoIdBytes, maxDealFlag)
|
|
|
//计算公式异常,那么就移除该指标
|
|
|
if formulaFormStr == `` {
|
|
|
//removeDateList = append(removeDateList, sk)
|
|
@@ -579,7 +512,12 @@ func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, sou
|
|
|
}
|
|
|
|
|
|
// ReplaceFormula 替换计算方式
|
|
|
-func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
|
|
|
+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)
|
|
@@ -587,7 +525,7 @@ func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap
|
|
|
replaceCount := 0
|
|
|
for dk, dv := range edbInfoIdArr {
|
|
|
var isReplace bool
|
|
|
- formulaStr, isReplace = GetFormulaReplace(dk, dv.EdbInfoId, formulaStr, edbInfoIdBytes, formulaMap, valArr)
|
|
|
+ formulaStr, isReplace = GetFormulaReplace(dk, dv.EdbInfoId, formulaStr, edbInfoIdBytes, formulaMap, valArr, valArrMax, maxDealFlag)
|
|
|
if isReplace {
|
|
|
replaceCount++
|
|
|
}
|
|
@@ -595,6 +533,9 @@ func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap
|
|
|
for k, v := range funMap {
|
|
|
formulaStr = strings.Replace(formulaStr, v, k, -1)
|
|
|
}
|
|
|
+ if strings.Contains(formulaStr, "MAX()") || strings.Contains(formulaStr, "MIN()") {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
if replaceCount == len(formulaMap) {
|
|
|
return formulaStr
|
|
|
} else {
|
|
@@ -602,18 +543,23 @@ func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func GetFormulaReplace(index, edbInfoId int, formulaStr string, edbInfoIdBytes []string, formulaMap map[string]string, valArr map[int]float64) (formulaResult string, isReplace bool) {
|
|
|
- // todo 获取原本的值
|
|
|
- valArrMax := make(map[int]float64)
|
|
|
-
|
|
|
+func GetFormulaReplace(index, edbInfoId int, formulaStr string, edbInfoIdBytes []string, formulaMap map[string]string, valArr, valArrMax map[int]float64, maxDealFlag bool) (formulaResult string, isReplace bool) {
|
|
|
formulaResult = formulaStr
|
|
|
dKey := edbInfoIdBytes[index]
|
|
|
if _, ok := formulaMap[dKey]; ok { //公式中存在
|
|
|
- // todo 判断公式中存在max、min,如果存在,则值从valArrMax中获取,其余的值从valArr中获取
|
|
|
if val, valOk := valArr[edbInfoId]; valOk { //值存在
|
|
|
dvStr := fmt.Sprintf("%v", val)
|
|
|
- formulaResult = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ formulaResult = strings.Replace(formulaResult, dKey, dvStr, -1)
|
|
|
isReplace = true
|
|
|
+ if maxDealFlag {
|
|
|
+ if valM, valMOk := valArrMax[edbInfoId]; valMOk { //值存在
|
|
|
+ dvMStr := fmt.Sprintf("%v", valM)
|
|
|
+ formulaResult = strings.Replace(formulaResult, strings.ToLower(dKey), dvMStr, -1)
|
|
|
+ } else {
|
|
|
+ formulaResult = strings.Replace(formulaResult, strings.ToLower(dKey)+",", "", -1)
|
|
|
+ formulaResult = strings.Replace(formulaResult, ","+strings.ToLower(dKey), "", -1)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return
|
|
@@ -690,8 +636,11 @@ func CheckFormula2(edbInfoArr []*EdbInfo, formulaMap map[string]string, formulaS
|
|
|
for _, v := range edbInfoArr {
|
|
|
valArr[v.EdbInfoId] = 100
|
|
|
}
|
|
|
+
|
|
|
formulaStr = strings.ToUpper(formulaStr)
|
|
|
- formulaFormStr := ReplaceFormula(edbInfoArr, valArr, formulaMap, formulaStr, edbInfoIdBytes)
|
|
|
+ // 预设里的max和min无需特殊处理
|
|
|
+ valArrMax := make(map[int]float64)
|
|
|
+ formulaFormStr := ReplaceFormula(edbInfoArr, valArr, valArrMax, formulaMap, formulaStr, edbInfoIdBytes, false)
|
|
|
if formulaFormStr == "" {
|
|
|
return
|
|
|
}
|
|
@@ -899,22 +848,36 @@ func handleDateDataMapZero(saveDataMap map[string]map[int]float64, date string,
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func GetMaxMinEdbInfo(formula string, formulaMap map[string]string) (edbInfoIdBytes []string) {
|
|
|
+func GetFormulaMaxMinReplace(index, edbInfoId int, formulaStr string, edbInfoIdBytes []string, formulaMap map[string]string, valArr map[int]float64) (formulaResult string, isReplace bool) {
|
|
|
+ formulaStr = GetMaxMinEdbInfo(formulaStr)
|
|
|
+ formulaResult = formulaStr
|
|
|
+ dKey := edbInfoIdBytes[index]
|
|
|
+ if _, ok := formulaMap[dKey]; ok { //公式中存在
|
|
|
+ // todo 判断公式中存在max、min,如果存在,则值从valArrMax中获取,其余的值从valArr中获取
|
|
|
+ if val, valOk := valArr[edbInfoId]; valOk { //值存在
|
|
|
+ dvStr := fmt.Sprintf("%v", val)
|
|
|
+ formulaResult = strings.Replace(formulaStr, dKey, dvStr, -1)
|
|
|
+ isReplace = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func GetMaxMinEdbInfo(formula string) string {
|
|
|
//formula := "A+min(A,B,max(A,C))"
|
|
|
- // 定义正则表达式匹配max和min函数及其参数
|
|
|
- regex := regexp.MustCompile(`MIN\((.*?)\)|MAX\((.*?)\)`)
|
|
|
+ // todo 无法处理max里嵌套max或者min的情况
|
|
|
+ // 使用正则表达式匹配MAX和MIN函数及其参数
|
|
|
+ regex := regexp.MustCompile(`(?i)(MAX|MIN)\((.*?)\)`)
|
|
|
matches := regex.FindAllStringSubmatch(formula, -1)
|
|
|
-
|
|
|
- // 遍历匹配结果,输出max和min函数及其参数
|
|
|
+ // 遍历匹配结果,输出MAX和MIN函数及其参数
|
|
|
for _, match := range matches {
|
|
|
if len(match) == 3 {
|
|
|
- function := match[1] // min或max函数
|
|
|
- parameter := match[2]
|
|
|
- // todo 将max和min函数里的入参替换成小写
|
|
|
- //lowercaseParameter := parameter // 参数保持不变
|
|
|
- //lowercaseFormula = strings.ReplaceAll(lowercaseFormula, match[0], lowercaseFunction+lowercaseParameter) // 替换公式中的MAX/MIN函数及其参数为小写形式
|
|
|
- fmt.Printf("Function: %s, Parameter: %s\n", function, parameter)
|
|
|
+ parameter := strings.ToLower(match[0]) // 参数
|
|
|
+ formula = strings.ReplaceAll(formula, match[0], parameter)
|
|
|
+ fmt.Printf("formula: %s\n", formula)
|
|
|
}
|
|
|
}
|
|
|
- return
|
|
|
+ formula = strings.ReplaceAll(formula, "max", "MAX")
|
|
|
+ formula = strings.ReplaceAll(formula, "min", "MIN")
|
|
|
+ return formula
|
|
|
}
|