Browse Source

区间计算

xyxie 7 months ago
parent
commit
bf47136387
1 changed files with 358 additions and 154 deletions
  1. 358 154
      models/edb_data_calculate_qjjs.go

+ 358 - 154
models/edb_data_calculate_qjjs.go

@@ -373,6 +373,351 @@ func (obj CalculateRangeAnalysis) refresh(to orm.TxOrmer, edbInfoId, source, sub
 	return
 }
 
+// GetAutoCalculateDateDataList 获取当前时间相关的区间作为计算依据
+func GetAutoCalculateDateDataList(currentDate string, dataList []*EdbInfoSearchData, req RangeAnalysisCalculateFormula) (newDataList []*EdbInfoSearchData, err error) {
+	currentDateTime, _ := time.ParseInLocation(utils.FormatDate, currentDate, time.Local)
+	switch req.DateRangeType {
+	case 0:
+		// 智能划分得到一个开始日期,和结束日期
+		var startDateTime time.Time
+		if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
+			startDate := req.AutoDateConf.StartDate
+			if startDate == "" {
+				startDate = "2020-01-01"
+			}
+			startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+		} else {
+			startConf := req.AutoDateConf.StartDateConf
+			startDate := ""
+			if startConf.BaseDateType == 0 { //
+				startDate = currentDate
+			} else if startConf.BaseDateType == 1 {
+				startDate = time.Now().Format(utils.FormatDate)
+			}
+			if startConf.MoveForward > 0 {
+				startDate = GetEdbDateByMoveForward(startDate, startConf.MoveForward, dataList)
+			}
+			if len(startConf.DateChange) > 0 {
+				startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
+				if err != nil {
+					err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
+					return
+				}
+			}
+			startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+		}
+		var calStartTime, calEndTime time.Time
+		if currentDateTime.Before(startDateTime) {
+			calStartTime = currentDateTime
+			calEndTime = startDateTime
+		} else {
+			calStartTime = startDateTime
+			calEndTime = currentDateTime
+		}
+		// 根据日期,获取数据
+		for _, vv := range dataList {
+			dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
+			if dataTimeT.After(calStartTime) && dataTimeT.Before(calEndTime) ||
+				dataTimeT.Equal(calStartTime) ||
+				dataTimeT.Equal(calEndTime) {
+				newDataList = append(newDataList, vv)
+			}
+		}
+	}
+	return
+}
+
+// HandleDataByCalculateType 根据计算公式处理数据
+func HandleRangeAnalysisDataByCalculateType(originList []*ChartRangeAnalysisDateDataItem, originDataList []*EdbInfoSearchData, req RangeAnalysisCalculateFormula) (newList []*EdbInfoSearchData, err error) {
+	if len(originList) == 0 {
+		return
+	}
+	calculateType := req.CalculateType
+	switch calculateType {
+	case 0: //均值
+		var sum float64
+		if req.DateRangeType == 0 {
+			for _, item := range originList {
+				for _, v := range item.DataList {
+					sum = 0
+					//计算的数据返回需要重新确定
+					calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
+					if e != nil {
+						err = fmt.Errorf("获取区间数据失败:%s", e.Error())
+						return
+					}
+					for _, vv := range calDataList {
+						sum += vv.Value
+					}
+					val := sum / float64(len(calDataList))
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{
+						DataTime:      v.DataTime,
+						Value:         val,
+						DataTimestamp: v.DataTimestamp,
+					})
+				}
+			}
+		} else {
+			for _, item := range originList {
+				sum = 0
+				for k, v := range item.DataList {
+					sum += v.Value
+					val := sum / float64(k+1)
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{
+						DataTime:      v.DataTime,
+						Value:         val,
+						DataTimestamp: v.DataTimestamp,
+					})
+				}
+			}
+		}
+
+	case 1: //累计值
+		var sum float64
+		if req.DateRangeType == 0 {
+			for _, item := range originList {
+				sum = 0
+				for _, v := range item.DataList {
+					sum = 0
+					//计算的数据返回需要重新确定
+					calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
+					if e != nil {
+						err = fmt.Errorf("获取区间数据失败:%s", e.Error())
+						return
+					}
+					for _, vv := range calDataList {
+						sum += vv.Value
+					}
+					val := sum
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{
+						DataTime:      v.DataTime,
+						Value:         val,
+						DataTimestamp: v.DataTimestamp,
+					})
+				}
+			}
+		} else {
+			for _, item := range originList {
+				sum = 0
+				for _, v := range item.DataList {
+					sum += v.Value
+					val := sum
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{
+						DataTime:      v.DataTime,
+						Value:         val,
+						DataTimestamp: v.DataTimestamp,
+					})
+				}
+			}
+		}
+
+	case 2: //涨幅
+		if req.DateRangeType == 0 {
+			for _, item := range originList {
+				for _, v := range item.DataList {
+					var baseVal float64
+					//计算的数据返回需要重新确定
+					calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
+					if e != nil {
+						err = fmt.Errorf("获取区间数据失败:%s", e.Error())
+						return
+					}
+					if len(calDataList) == 0 {
+						continue
+					}
+					baseVal = calDataList[len(calDataList)-1].Value
+					if baseVal == 0 {
+						continue
+					}
+
+					val := (v.Value - baseVal) / baseVal
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{
+						DataTime:      v.DataTime,
+						Value:         val,
+						DataTimestamp: v.DataTimestamp,
+					})
+				}
+			}
+		} else {
+			for _, item := range originList {
+				var baseVal float64
+				for k, v := range item.DataList {
+					if k == 0 {
+						baseVal = v.Value
+						if baseVal == 0 {
+							break
+						}
+					}
+					val := (v.Value - baseVal) / baseVal
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{
+						DataTime:      v.DataTime,
+						Value:         val,
+						DataTimestamp: v.DataTimestamp,
+					})
+				}
+			}
+		}
+	case 3: //复合增长率
+		if req.DateRangeType == 0 {
+			for _, item := range originList {
+				for _, v := range item.DataList {
+					var baseVal float64
+					var baseDate string
+					calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
+					if e != nil {
+						err = fmt.Errorf("获取区间数据失败:%s", e.Error())
+						return
+					}
+					if len(calDataList) == 0 {
+						continue
+					}
+					baseVal = calDataList[len(calDataList)-1].Value
+					baseDate = calDataList[len(calDataList)-1].DataTime
+					if v.DataTime == baseDate {
+						continue
+					}
+					if baseVal == 0 {
+						continue
+					}
+
+					baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
+					if e != nil {
+						err = fmt.Errorf("time.ParseInLocation err: %v", e)
+						return
+					}
+					tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
+					if e != nil {
+						err = fmt.Errorf("time.ParseInLocation err: %v", e)
+						return
+					}
+					// 计算两个日期相差的天数
+					diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
+					val := v.Value / baseVal
+					val = math.Pow(val, 1/diff) - 1
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
+				}
+			}
+		} else {
+			for _, item := range originList {
+				var baseVal float64
+				var baseDate string
+				for k, v := range item.DataList {
+					if k == 0 {
+						baseVal = v.Value
+						baseDate = v.DataTime
+						if baseVal == 0 {
+							break
+						}
+						continue
+					}
+					baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
+					if e != nil {
+						err = fmt.Errorf("time.ParseInLocation err: %v", e)
+						return
+					}
+					tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
+					if e != nil {
+						err = fmt.Errorf("time.ParseInLocation err: %v", e)
+						return
+					}
+					// 计算两个日期相差的天数
+					diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
+					val := v.Value / baseVal
+					val = math.Pow(val, 1/diff) - 1
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
+				}
+			}
+		}
+	case 4: //最大值
+		var maxVal float64
+		if req.DateRangeType == 0 {
+			for _, item := range originList {
+				for _, v := range item.DataList {
+					calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
+					if e != nil {
+						err = fmt.Errorf("获取区间数据失败:%s", e.Error())
+						return
+					}
+					for kk, vv := range calDataList {
+						if kk == 0 {
+							maxVal = vv.Value
+						}
+						if vv.Value > maxVal {
+							maxVal = vv.Value
+						}
+					}
+
+					val := maxVal
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
+				}
+			}
+		} else {
+			for _, item := range originList {
+				for k, v := range item.DataList {
+					if k == 0 {
+						maxVal = v.Value
+					}
+					if v.Value > maxVal {
+						maxVal = v.Value
+					}
+					val := maxVal
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
+				}
+			}
+		}
+	case 5: //最小值
+		var minVal float64
+		if req.DateRangeType == 0 {
+			for _, item := range originList {
+				for _, v := range item.DataList {
+					calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
+					if e != nil {
+						err = fmt.Errorf("获取区间数据失败:%s", e.Error())
+						return
+					}
+					for kk, vv := range calDataList {
+						if kk == 0 {
+							minVal = vv.Value
+						}
+						if vv.Value < minVal {
+							minVal = vv.Value
+						}
+					}
+					val := minVal
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
+				}
+			}
+		} else {
+			for _, item := range originList {
+				for k, v := range item.DataList {
+					if k == 0 {
+						minVal = v.Value
+					}
+					if v.Value < minVal {
+						minVal = v.Value
+					}
+					val := minVal
+					val, _ = decimal.NewFromFloat(val).Round(4).Float64()
+					newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
+				}
+			}
+		}
+	}
+
+	return
+}
+
 // GetRangeAnalysisChartDataByEdbInfo 区间计算
 func GetRangeAnalysisChartDataByEdbInfo(to orm.TxOrmer, fromEdbInfo *EdbInfo, calculateFormula RangeAnalysisCalculateFormula) (newDataList []*EdbInfoSearchData, err error) {
 	// 指标的开始日期和结束日期
@@ -407,32 +752,7 @@ func GetRangeAnalysisChartDataByEdbInfo(to orm.TxOrmer, fromEdbInfo *EdbInfo, ca
 	case 0:
 		// 智能划分得到一个开始日期,和结束日期
 		var startDateTime, endDateTime time.Time
-		if calculateFormula.AutoDateConf.IsAutoStartDate == 0 { //固定设置
-			startDate := calculateFormula.AutoDateConf.StartDate
-			if startDate == "" {
-				startDate = "2020-01-01"
-			}
-			startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
-		} else {
-			startConf := calculateFormula.AutoDateConf.StartDateConf
-			startDate := ""
-			if startConf.BaseDateType == 0 { //
-				startDate = edbEndDate
-			} else if startConf.BaseDateType == 1 {
-				startDate = time.Now().Format(utils.FormatDate)
-			}
-			if startConf.MoveForward > 0 {
-				startDate = GetEdbDateByMoveForward(startConf.MoveForward, dataList)
-			}
-			if len(startConf.DateChange) > 0 {
-				startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
-				if err != nil {
-					err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
-					return
-				}
-			}
-			startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
-		}
+		startDateTime = edbStartDateTime
 
 		if calculateFormula.AutoDateConf.IsAutoEndDate == 0 { //固定设置
 			endDate := calculateFormula.AutoDateConf.EndDate
@@ -446,7 +766,7 @@ func GetRangeAnalysisChartDataByEdbInfo(to orm.TxOrmer, fromEdbInfo *EdbInfo, ca
 			endConf := calculateFormula.AutoDateConf.EndDateConf
 			endDate := edbEndDate
 			if endConf.MoveForward > 0 {
-				endDate = GetEdbDateByMoveForward(endConf.MoveForward, dataList)
+				endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
 			}
 			if len(endConf.DateChange) > 0 {
 				endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
@@ -506,7 +826,7 @@ func GetRangeAnalysisChartDataByEdbInfo(to orm.TxOrmer, fromEdbInfo *EdbInfo, ca
 		}
 	}
 	// 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
-	newDataList, err = HandleRangeAnalysisDataByCalculateType(dateList, calculateFormula.CalculateType)
+	newDataList, err = HandleRangeAnalysisDataByCalculateType(dateList, dataList, calculateFormula)
 	if err != nil {
 		return
 	}
@@ -565,135 +885,19 @@ func GetRangeAnalysisChartDataByEdbInfo(to orm.TxOrmer, fromEdbInfo *EdbInfo, ca
 	return
 }
 
-// HandleRangeAnalysisDataByCalculateType 根据计算公式处理数据
-func HandleRangeAnalysisDataByCalculateType(originList []*ChartRangeAnalysisDateDataItem, calculateType int) (newList []*EdbInfoSearchData, err error) {
-	if len(originList) == 0 {
-		return
-	}
-	switch calculateType {
-	case 0: //均值
-		var sum float64
-		for _, item := range originList {
-			sum = 0
-			for k, v := range item.DataList {
-				sum += v.Value
-				val := sum / float64(k+1)
-				val, _ = decimal.NewFromFloat(val).Round(4).Float64()
-				newList = append(newList, &EdbInfoSearchData{
-					DataTime:      v.DataTime,
-					Value:         val,
-					DataTimestamp: v.DataTimestamp,
-				})
-			}
-		}
-	case 1: //累计值
-		var sum float64
-		for _, item := range originList {
-			sum = 0
-			for _, v := range item.DataList {
-				sum += v.Value
-				val := sum
-				val, _ = decimal.NewFromFloat(val).Round(4).Float64()
-				newList = append(newList, &EdbInfoSearchData{
-					DataTime:      v.DataTime,
-					Value:         val,
-					DataTimestamp: v.DataTimestamp,
-				})
-			}
-		}
-	case 2: //涨幅
-		for _, item := range originList {
-			var baseVal float64
-			for k, v := range item.DataList {
-				if k == 0 {
-					baseVal = v.Value
-					if baseVal == 0 {
-						break
-					}
-				}
-				val := (v.Value - baseVal) / baseVal
-				val, _ = decimal.NewFromFloat(val).Round(4).Float64()
-				newList = append(newList, &EdbInfoSearchData{
-					DataTime:      v.DataTime,
-					Value:         val,
-					DataTimestamp: v.DataTimestamp,
-				})
-			}
-		}
-	case 3: //复合增长率
-		for _, item := range originList {
-			var baseVal float64
-			var baseDate string
-			for k, v := range item.DataList {
-				if k == 0 {
-					baseVal = v.Value
-					baseDate = v.DataTime
-					if baseVal == 0 {
-						break
-					}
-					continue
-				}
-				baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
-				if e != nil {
-					err = fmt.Errorf("time.ParseInLocation err: %v", e)
-					return
-				}
-				tmpT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
-				if e != nil {
-					err = fmt.Errorf("time.ParseInLocation err: %v", e)
-					return
-				}
-				// 计算两个日期相差的天数
-				diff := tmpT.Sub(baseDateT).Hours() / 24
-				val := v.Value / baseVal
-				val = math.Pow(val, diff) - 1
-				val, _ = decimal.NewFromFloat(val).Round(4).Float64()
-				newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
-			}
+func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*EdbInfoSearchData) (date string) {
+	// 根据日期进行排序
+	index := 0
+	for _, v := range edbDataList {
+		if v.DataTime == startDate {
+			index += 1
 		}
-	case 4: //最大值
-		var maxVal float64
-		for _, item := range originList {
-			for k, v := range item.DataList {
-				if k == 0 {
-					maxVal = v.Value
-				}
-				if v.Value > maxVal {
-					maxVal = v.Value
-				}
-				val := maxVal
-				val, _ = decimal.NewFromFloat(val).Round(4).Float64()
-				newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
-			}
+		if index > 0 {
+			index += 1
 		}
-	case 5: //最小值
-		var minVal float64
-		for _, item := range originList {
-			for k, v := range item.DataList {
-				if k == 0 {
-					minVal = v.Value
-				}
-				if v.Value < minVal {
-					minVal = v.Value
-				}
-				val := minVal
-				val, _ = decimal.NewFromFloat(val).Round(4).Float64()
-				newList = append(newList, &EdbInfoSearchData{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
-			}
-		}
-	}
-
-	return
-}
-
-func GetEdbDateByMoveForward(moveForward int, edbDataList []*EdbInfoSearchData) (date string) {
-	moveForward = 0
-	// 根据日期进行排序
-	index := len(edbDataList) - 1 - moveForward
-	for k, v := range edbDataList {
-		if k == index {
+		if index == moveForward {
 			date = v.DataTime
-			return
+			break
 		}
 	}
 	return