|
@@ -89,140 +89,6 @@ func EditPredictCalculate(edbInfo *EdbInfo, calculateMappingList []*EdbInfoCalcu
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// AddPredictCalculateData 新增预测计算(运算)指标的数据
|
|
|
-func AddPredictCalculateDataBak(edbInfoIdList []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (latestDateStr string, latestValue float64, err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- defer func() {
|
|
|
- if err != nil {
|
|
|
- utils.FileLog.Info(fmt.Sprintf("Calculate Err:%s" + err.Error()))
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- var minLatestDate time.Time
|
|
|
- oldSaveDataMap := make(map[string]map[int]float64)
|
|
|
- saveDataMap := make(map[string]map[int]float64)
|
|
|
- dateList := make([]string, 0)
|
|
|
-
|
|
|
- for edbInfoIndex, v := range edbInfoIdList {
|
|
|
- tmpLatestDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
|
|
|
- if tmpErr != nil {
|
|
|
- err = errors.New(`最近的日期格式化失败;日期:` + v.LatestDate + `;err:` + tmpErr.Error())
|
|
|
- return
|
|
|
- }
|
|
|
- // 如果不存在最小的真实数据日期,那么将当前指标的真实数据日期给赋值;
|
|
|
- // 如果存在最小的真实数据日期,同时当前指标的真实数据日期 早于 最小的真实数据日期, 那么将当前指标的真实数据日期给赋值;
|
|
|
- if minLatestDate.IsZero() || tmpLatestDate.Before(minLatestDate) {
|
|
|
- minLatestDate = tmpLatestDate
|
|
|
- }
|
|
|
-
|
|
|
- dataList, tmpErr := GetPredictEdbDataListAll(v, 1)
|
|
|
- if tmpErr != nil {
|
|
|
- err = tmpErr
|
|
|
- return
|
|
|
- }
|
|
|
- dataMap := make(map[string]float64)
|
|
|
- for _, dv := range dataList {
|
|
|
- // 现有实际数据
|
|
|
- if val, ok := oldSaveDataMap[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
|
|
|
- oldSaveDataMap[dv.DataTime] = temp
|
|
|
- }
|
|
|
-
|
|
|
- // 待处理的数据
|
|
|
- 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
|
|
|
- }
|
|
|
-
|
|
|
- // 以第一个指标的日期作为基准日期
|
|
|
- if edbInfoIndex == 0 {
|
|
|
- dateList = append(dateList, dv.DataTime)
|
|
|
- }
|
|
|
- }
|
|
|
- item := new(PredictCalculateItems)
|
|
|
- item.EdbInfoId = v.EdbInfoId
|
|
|
- item.DataMap = dataMap
|
|
|
- }
|
|
|
-
|
|
|
- //数据处理,将日期内不全的数据做补全
|
|
|
- handleDateSaveDataMap(dateList, oldSaveDataMap, saveDataMap, edbInfoIdList)
|
|
|
-
|
|
|
- formulaMap := utils.CheckFormula(formulaStr)
|
|
|
- addSql := ` INSERT INTO edb_data_predict_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
|
|
|
-
|
|
|
- if !minLatestDate.IsZero() {
|
|
|
- latestDateStr = minLatestDate.Format(utils.FormatDate)
|
|
|
- }
|
|
|
- for sk, sv := range saveDataMap {
|
|
|
- formulaStr = strings.ToUpper(formulaStr)
|
|
|
- formulaFormStr := ReplaceFormula(edbInfoIdList, sv, formulaMap, formulaStr, edbInfoIdBytes)
|
|
|
- if formulaStr == "" {
|
|
|
- return
|
|
|
- }
|
|
|
- if formulaFormStr != "" {
|
|
|
- expression := formula.NewExpression(formulaFormStr)
|
|
|
- calResult, tmpErr := expression.Evaluate()
|
|
|
- if tmpErr != nil {
|
|
|
- err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
- fmt.Println(err)
|
|
|
- return
|
|
|
- }
|
|
|
- calVal, tmpErr := calResult.Float64()
|
|
|
- if tmpErr != nil {
|
|
|
- err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
- fmt.Println(err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- //需要存入的数据
|
|
|
- {
|
|
|
- if latestDateStr == sk {
|
|
|
- nanCheck := fmt.Sprintf("%0.f", calVal)
|
|
|
- // 分母为0.0的报错
|
|
|
- if nanCheck == "NaN" || nanCheck == "+Inf" || nanCheck == "-Inf" {
|
|
|
- err = errors.New("计算失败 分母为0:Err: formulaStr:" + formulaFormStr)
|
|
|
- fmt.Println(err)
|
|
|
- return
|
|
|
- }
|
|
|
- latestValue, _ = decimal.NewFromFloat(calVal).Truncate(4).Float64() //保留4位小数
|
|
|
- }
|
|
|
- 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("AddPredictCalculate Err:" + err.Error())
|
|
|
- //errMsg = " tx.Exec Err :" + err.Error()
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
func AddPredictCalculateData(edbInfoIdList []*EdbInfo, edbInfo *EdbInfo, edbCode, formulaStr string, edbInfoIdBytes []string) (latestDateStr string, latestValue float64, err error) {
|
|
|
o := orm.NewOrm()
|
|
|
to, err := o.Begin()
|