Browse Source

fix:修复移动平均同比预测bug

Roc 2 years ago
parent
commit
5f2a83e2df
1 changed files with 11 additions and 4 deletions
  1. 11 4
      services/data/predict_edb_info_rule.go

+ 11 - 4
services/data/predict_edb_info_rule.go

@@ -963,6 +963,11 @@ func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, ye
 
 	//获取后面的预测数据
 	dayList := getPredictEdbDayList(startDate, endDate, frequency)
+	if len(dayList) <= 0 {
+		return
+	}
+	// 需要减去的年份
+	subYear := year - dayList[0].Year()
 	for k, currentDate := range dayList {
 		tmpLenAllDataList := len(allDataList)
 		tmpIndex := tmpLenAllDataList - 1 //上1期数据的下标
@@ -972,7 +977,7 @@ func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, ye
 		// 数据集合中的最后一个数据
 		tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
 		averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
-		for tmpK := 2; tmpK <= nValue; tmpK++ {
+		for tmpK := 1; tmpK < nValue; tmpK++ {
 			tmpIndex2 := tmpIndex - tmpK //上N期的值
 			tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
 			tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
@@ -990,7 +995,7 @@ func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, ye
 			tmpHistoryValNum := 0
 			{
 				//前几年当日的日期
-				tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, 0)
+				tmpHistoryCurrentDate := currentDate.AddDate(subYear, 0, 0)
 				for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
 					tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
 					if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
@@ -1015,7 +1020,7 @@ func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, ye
 					return
 				}
 				//前几年上一期的日期
-				tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, 0)
+				tmpHistoryLastDate := lastDay.AddDate(subYear, 0, 0)
 				for i := 0; i <= 35; i++ { // 前后35天找数据,找到最近的值,先向后面找,再往前面找
 					tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
 					if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
@@ -1046,8 +1051,10 @@ func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, ye
 			continue
 		}
 
+		// 计算最近N期平均值
+		tmpHistoryAverageVal := tmpHistoryDecimalVal.Div(decimalN)
 		// 计算最近N期同比值
-		tbVal := tmpAverageVal.Div(tmpHistoryDecimalVal)
+		tbVal := tmpAverageVal.Div(tmpHistoryAverageVal)
 
 		// 预测值结果 = 同比年份同期值(tmpHistoryCurrentVal的值)* 同比值(tbVal的值)
 		val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).RoundCeil(4).Float64()