|
@@ -7,6 +7,7 @@ import (
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"github.com/yidane/formula"
|
|
|
+ "regexp"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -22,6 +23,8 @@ type EdbInfoCalculateSaveReq struct {
|
|
|
Unit string `description:"单位"`
|
|
|
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
|
|
|
}
|
|
|
|
|
@@ -172,6 +175,8 @@ func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbIn
|
|
|
LatestDate: "",
|
|
|
LatestValue: 0,
|
|
|
ChartImage: "",
|
|
|
+ EmptyType: req.EmptyType,
|
|
|
+ MaxEmptyType: req.MaxEmptyType,
|
|
|
}
|
|
|
lastId, err := to.Insert(edbInfo)
|
|
|
if err != nil {
|
|
@@ -194,13 +199,13 @@ func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbIn
|
|
|
}
|
|
|
|
|
|
//计算数据
|
|
|
- err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes)
|
|
|
+ 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 {
|
|
@@ -223,7 +228,9 @@ func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap
|
|
|
edbInfo.ClassifyId = req.ClassifyId
|
|
|
edbInfo.CalculateFormula = req.CalculateFormula
|
|
|
edbInfo.ModifyTime = time.Now()
|
|
|
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
|
|
|
+ edbInfo.EmptyType = req.EmptyType
|
|
|
+ edbInfo.MaxEmptyType = req.MaxEmptyType
|
|
|
+ _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EmptyType", "MaxEmptyType")
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -274,12 +281,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
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//关联关系表
|
|
@@ -293,7 +303,7 @@ func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap
|
|
|
}
|
|
|
|
|
|
//计算数据
|
|
|
- err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes)
|
|
|
+ err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType)
|
|
|
|
|
|
}
|
|
|
|
|
@@ -337,7 +347,7 @@ func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
|
|
|
}
|
|
|
|
|
|
// RefreshAllCalculate 刷新全部数据
|
|
|
-func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (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 {
|
|
@@ -354,13 +364,13 @@ func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode
|
|
|
fmt.Println(startDate, endDate)
|
|
|
|
|
|
//计算数据
|
|
|
- err = refreshAllCalculate(to, edbInfoIdArr, edbInfoId, source, edbCode, formulaStr, startDate, endDate, edbInfoIdBytes)
|
|
|
+ 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) (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)
|
|
|
|
|
@@ -431,9 +441,12 @@ func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, sou
|
|
|
}
|
|
|
|
|
|
//数据处理,将日期内不全的数据做补全
|
|
|
- handleDateSaveDataMap(dateList, maxStartDate, minLatestDate, realSaveDataMap, saveDataMap, edbInfoIdArr)
|
|
|
+ handleDateSaveDataMap(dateList, maxStartDate, minLatestDate, realSaveDataMap, saveDataMap, edbInfoIdArr, emptyType)
|
|
|
|
|
|
- formulaMap := utils.CheckFormula(formulaStr)
|
|
|
+ formulaDateSlice, formulaDateMap, err := utils.HandleFormulaJson(formulaStr, maxStartDate.Format(utils.FormatDate))
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
var isAdd bool
|
|
|
|
|
@@ -455,10 +468,44 @@ 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 {
|
|
|
+ if len(sv) != len(edbInfoIdArr) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
//fmt.Println(sk, sv)
|
|
|
+ // 根据时间范围,选择对应的公式
|
|
|
+ formulaMap := make(map[string]string)
|
|
|
+ formulaStr = ""
|
|
|
+ for _, fv := range formulaDateSlice {
|
|
|
+ if sk >= fv {
|
|
|
+ if f, ok := formulaDateMap[fv]; ok {
|
|
|
+ formulaStr = f
|
|
|
+ formulaMap = utils.CheckFormula(formulaStr)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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)
|
|
@@ -551,7 +598,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)
|
|
@@ -559,7 +611,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++
|
|
|
}
|
|
@@ -567,6 +619,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 {
|
|
@@ -574,14 +629,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) {
|
|
|
+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 { //公式中存在
|
|
|
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
|
|
@@ -599,6 +663,8 @@ func GetFormulaMap() map[string]string {
|
|
|
funMap["MOD"] = "[@&]"
|
|
|
funMap["POW"] = "[@*]"
|
|
|
funMap["ROUND"] = "[@(]"
|
|
|
+ funMap["LN"] = "[@-]"
|
|
|
+ funMap["EXP"] = "[@+]"
|
|
|
return funMap
|
|
|
}
|
|
|
|
|
@@ -647,10 +713,10 @@ type EdbInfoCalculateBatchEditReq struct {
|
|
|
FromTag string `description:"指标对应标签"`
|
|
|
MoveValue int `description:"移动的值"`
|
|
|
}
|
|
|
- MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
|
|
|
- MoveFrequency string `description:"移动频度:天/周/月/季/年"`
|
|
|
- Calendar string `description:"公历/农历" orm:"default(公历)"`
|
|
|
- Data interface{} `description:"数据"`
|
|
|
+ MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
|
|
|
+ MoveFrequency string `description:"移动频度:天/周/月/季/年"`
|
|
|
+ Calendar string `description:"公历/农历" orm:"default(公历)"`
|
|
|
+ Data interface{} `description:"数据"`
|
|
|
}
|
|
|
|
|
|
// DeleteCalculateData 删除计算数据
|
|
@@ -661,101 +727,17 @@ func DeleteCalculateData(edbInfoId int) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// Calculate 重新计算
|
|
|
-func Calculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
|
|
|
- defer func() {
|
|
|
- if err != nil {
|
|
|
- utils.FileLog.Info("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,status,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.Parse(utils.FormatDate, sk)
|
|
|
- timestamp := dataTime.UnixNano() / 1e6
|
|
|
- timeStr := fmt.Sprintf("%d", timestamp)
|
|
|
- addSql += "("
|
|
|
- addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
|
|
|
- "," + "'" + nowStr + "'" + "," + "1"
|
|
|
- addSql += "," + "'" + timeStr + "'"
|
|
|
- addSql += "),"
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- } else {
|
|
|
- fmt.Println("formulaFormStr is empty")
|
|
|
- }
|
|
|
- }
|
|
|
- if isAdd {
|
|
|
- addSql = strings.TrimRight(addSql, ",")
|
|
|
- o := orm.NewOrm()
|
|
|
- _, err = o.Raw(addSql).Exec()
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- fmt.Println("AddEdbDataCalculate Err:" + err.Error())
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
// CheckFormula2 校验公式是否正常(比如说除法的分母不能为0之类的,实际上就是用预设的字段数据做一次计算)
|
|
|
func CheckFormula2(edbInfoArr []*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 := 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
|
|
|
}
|
|
@@ -769,7 +751,22 @@ func CheckFormula2(edbInfoArr []*EdbInfo, formulaMap map[string]string, formulaS
|
|
|
}
|
|
|
|
|
|
// 处理整个数据
|
|
|
-func handleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.Time, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*EdbInfo) {
|
|
|
+func handleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.Time, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*EdbInfo, emptyType int) {
|
|
|
+ var startDate, endDate string
|
|
|
+ var startDateT, endDateT time.Time
|
|
|
+ if emptyType == 2 || emptyType == 3 {
|
|
|
+ for k, _ := range realSaveDataMap {
|
|
|
+ if k > endDate {
|
|
|
+ endDate = k
|
|
|
+ }
|
|
|
+ if k < startDate || startDate == "" {
|
|
|
+ startDate = k
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ startDateT, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
|
|
|
+ endDateT, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
|
|
|
+ }
|
|
|
for _, date := range dateList {
|
|
|
dateTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
|
|
|
// 如果当前日期早于数据的最大开始日期,那么不处理,进入下一个循环
|
|
@@ -786,6 +783,7 @@ func handleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.T
|
|
|
tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
|
|
|
// 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
|
|
|
if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
|
|
|
+
|
|
|
//day := 0
|
|
|
//switch edbInfo.Frequency {
|
|
|
//case "周度":
|
|
@@ -800,14 +798,24 @@ func handleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.T
|
|
|
// day = 365
|
|
|
//}
|
|
|
// 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
|
|
|
- day := 35
|
|
|
- handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, day)
|
|
|
+
|
|
|
+ switch emptyType {
|
|
|
+ case 0:
|
|
|
+ handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, 35)
|
|
|
+ case 2:
|
|
|
+ handleDateDataMapBefore(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
|
|
|
+ case 3:
|
|
|
+ handleDateDataMapAfter(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
|
|
|
+ case 4:
|
|
|
+ handleDateDataMapZero(saveDataMap, date, tmpEdbInfoId)
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// handleDataMap 处理单个日期的数据
|
|
|
+// handleDateDataMap 前后35天 处理单个日期的数据
|
|
|
func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
|
|
|
currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
|
|
|
|
|
@@ -846,3 +854,127 @@ func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// handleDateDataMapBefore 前值填充:空值优先以最近的前值填充,没有前值时,用后值填充
|
|
|
+func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
|
|
|
+ currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
|
|
|
+
|
|
|
+ // 后一天
|
|
|
+ nextDateDay := currDate
|
|
|
+
|
|
|
+ // 前一天
|
|
|
+ preDateDay := currDate
|
|
|
+
|
|
|
+ for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
|
|
|
+ // 上个日期的数据
|
|
|
+ {
|
|
|
+ preDateDay = currDate.AddDate(0, 0, -i)
|
|
|
+ preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
+ if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
|
|
|
+ if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
|
|
|
+ fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
|
|
|
+ saveDataMap[date][edbInfoId] = val
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
|
|
|
+ // 下个日期的数据
|
|
|
+ {
|
|
|
+ nextDateDay = currDate.AddDate(0, 0, i)
|
|
|
+ nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
+ if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
|
|
|
+ if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
|
|
|
+ fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
|
|
|
+ saveDataMap[date][edbInfoId] = val
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// handleDateDataMapAfter 后值填充:空值优先以最近的后值填充,没有后值时,用前值填充
|
|
|
+func handleDateDataMapAfter(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
|
|
|
+ currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
|
|
|
+
|
|
|
+ // 后一天
|
|
|
+ nextDateDay := currDate
|
|
|
+
|
|
|
+ // 前一天
|
|
|
+ preDateDay := currDate
|
|
|
+
|
|
|
+ for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
|
|
|
+ // 下个日期的数据
|
|
|
+ {
|
|
|
+ nextDateDay = currDate.AddDate(0, 0, i)
|
|
|
+ nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
+ if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
|
|
|
+ if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
|
|
|
+ fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
|
|
|
+ saveDataMap[date][edbInfoId] = val
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
|
|
|
+ // 上个日期的数据
|
|
|
+ {
|
|
|
+ preDateDay = currDate.AddDate(0, 0, -i)
|
|
|
+ preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
+ if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
|
|
|
+ if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
|
|
|
+ fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
|
|
|
+ saveDataMap[date][edbInfoId] = val
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// handleDateDataMapZero 等于0
|
|
|
+func handleDateDataMapZero(saveDataMap map[string]map[int]float64, date string, edbInfoId int) {
|
|
|
+ saveDataMap[date][edbInfoId] = 0
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+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))"
|
|
|
+ // 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")
|
|
|
+ return formula
|
|
|
+}
|