소스 검색

预测指标预测日期修改

xyxie 11 달 전
부모
커밋
7e9791435a
2개의 변경된 파일32개의 추가작업 그리고 12개의 파일을 삭제
  1. 31 12
      models/base_predict_from_calculate.go
  2. 1 0
      utils/base_from_calculate.go

+ 31 - 12
models/base_predict_from_calculate.go

@@ -268,7 +268,7 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
 	fmt.Println("startDate:", startDate)
 
 	// 最小的结束日期 , 最晚的数据开始日期
-	var minLatestDate, maxStartDate time.Time
+	var newLatestDate, minLatestDate, maxStartDate time.Time
 	dateList := make([]string, 0) // 第一个指标的日期数据
 
 	realSaveDataMap := make(map[string]map[int]float64)
@@ -276,15 +276,15 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
 
 	for edbInfoIndex, v := range edbInfoIdList {
 		// 单独存储max、min函数里的指标的数据
-		/*tmpLatestDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
+		tmpNewLatestDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
 		if tmpErr != nil {
-			err = errors.New(`最近的日期格式化失败;日期:` + v.EndDate + `;err:` + tmpErr.Error())
+			err = errors.New(`最近的日期格式化失败;日期:` + v.LatestDate + `;err:` + tmpErr.Error())
 			return
 		}
-		if minLatestDate.IsZero() || tmpLatestDate.Before(minLatestDate) {
-			minLatestDate = tmpLatestDate
+		if newLatestDate.IsZero() || tmpNewLatestDate.Before(minLatestDate) {
+			newLatestDate = tmpNewLatestDate
 		}
-		tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
+		/*tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
 		if tmpErr != nil {
 			err = errors.New(`最近的日期格式化失败;日期:` + v.StartDate + `;err:` + tmpErr.Error())
 			return
@@ -343,9 +343,6 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
 	}
 	addSql := ` INSERT INTO edb_data_predict_calculate (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
 	var isAdd bool
-	if !minLatestDate.IsZero() {
-		latestDateStr = minLatestDate.Format(utils.FormatDate)
-	}
 
 	//获取指标所有数据
 	dataList := make([]*EdbData, 0)
@@ -433,10 +430,10 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
 				removeDateList = append(removeDateList, sk)
 				continue
 			}
-			// 最近实际日期的实际数据
-			if latestDateStr == sk {
+			/*// 最近实际日期的实际数据
+			if latestDateStr >= sk {
 				latestValue, _ = decimal.NewFromFloat(calVal).Truncate(4).Float64() //保留4位小数
-			}
+			}*/
 			saveValue := decimal.NewFromFloat(calVal).RoundCeil(4).String() //utils.SubFloatToString(calVal, 4)
 			if existVal, ok := dataMap[sk]; !ok {
 				dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
@@ -493,5 +490,27 @@ func refreshAllPredictCalculate(to orm.TxOrmer, edbInfoIdList []*EdbInfo, edbInf
 			return
 		}
 	}
+
+	//查询预测日期
+	if !newLatestDate.IsZero() {
+		latestDateStr = newLatestDate.Format(utils.FormatDate)
+		sql = fmt.Sprintf(` SELECT * FROM %s WHERE edb_info_id = ? and data_time <= ? ORDER BY data_time desc limit 1`, dataTableName)
+		var latestDate EdbData
+		e := to.Raw(sql, edbInfoId, latestDateStr).QueryRow(&latestDate)
+		if e != nil {
+			if e.Error() == utils.ErrNoRow() {
+				//todo 空值怎么处理
+			} else {
+				err = fmt.Errorf("查询最新日期失败,Err:" + e.Error())
+				return
+			}
+
+		} else {
+			floatValue, _ := strconv.ParseFloat(latestDate.Value, 64)
+			latestDateStr = latestDate.DataTime
+			latestValue = floatValue
+		}
+	}
+
 	return
 }

+ 1 - 0
utils/base_from_calculate.go

@@ -71,6 +71,7 @@ func HandleFormulaJson(formula string, endDate time.Time) (dateSlice []string, f
 		formulaMap[v.Date] = v.Formula
 		dateSlice = append(dateSlice, v.Date)
 	}
+	// 得到的日期序列是从大到小,还是从小到大?
 	sort.Slice(dateSlice, func(i, j int) bool {
 		return dateSlice[i] < dateSlice[j]
 	})