gmy пре 5 месеци
родитељ
комит
5333cb5f7a
3 измењених фајлова са 103 додато и 121 уклоњено
  1. 43 48
      models/edb_data_calculate_phase_shift.go
  2. 42 71
      models/predict_edb_data_calculate_phase_shift.go
  3. 18 2
      utils/common.go

+ 43 - 48
models/edb_data_calculate_phase_shift.go

@@ -5,7 +5,6 @@ import (
 	"eta/eta_index_lib/utils"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
-	"github.com/shopspring/decimal"
 	"strconv"
 	"strings"
 	"time"
@@ -276,63 +275,59 @@ func refreshAllCalculatePhaseShift(to orm.TxOrmer, edbInfoId, source, subSource,
 	addSql := ` INSERT INTO edb_data_calculate_phase_shift(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
 	var isAdd bool
 
-	existMap := make(map[string]string)
+	resultMap := make(map[string]float64)
 	dataLen := len(dataList)
 	for i := 0; i < dataLen; i++ {
-		//当期
-		currentItem := dataList[i]
-		existKey := edbCode + currentItem.DataTime
-		if _, ok := existMap[existKey]; !ok {
-			currentDate, _ := time.ParseInLocation(utils.FormatDate, currentItem.DataTime, time.Local)
-			// 计算时间位移天数
-			shiftDay := CalculateIntervalDays(moveFrequency, formulaInt, currentDate)
-			if moveType == 2 {
-				shiftDay = -shiftDay
-			}
-			newDate := currentDate.AddDate(0, 0, shiftDay)
-
-			timestamp := newDate.UnixNano() / 1e6
-			timestampStr := fmt.Sprintf("%d", timestamp)
-			valStr := decimal.NewFromFloat(currentItem.Value).Round(4).String()
-			if existVal, ok := existDataMap[newDate.Format(utils.FormatDate)]; !ok {
-				isAdd = true
-				addSql += GetAddSql(edbInfoIdStr, edbCode, newDate.Format(utils.FormatDate), timestampStr, valStr)
+		// step_1 如果 领先/滞后 之后时间key存在,将该key为目标key,填充
+		currentIndex := dataList[i]
+
+		// 领先
+		if moveType != 2 {
+			periods := i + formulaInt
+			if periods < dataLen {
+				newIndex := dataList[periods]
+				resultMap[newIndex.DataTime] = currentIndex.Value
 			} else {
-				existValDecimal, err := decimal.NewFromString(existVal)
-				existStr := existValDecimal.String()
-				if existStr != valStr {
-					sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
-					sql = fmt.Sprintf(sql, dataTableName)
-					_, err = to.Raw(sql, valStr, edbInfoId, newDate.Format(utils.FormatDate)).Exec()
-					if err != nil {
-						return err
-					}
-				}
+				// 新数据须根据频度补充key
+				currentDate, _ := time.ParseInLocation(utils.FormatDate, currentIndex.DataTime, time.Local)
+
+				shiftDay := CalculateIntervalDays(moveFrequency, formulaInt, currentDate, resultMap, moveType)
+
+				newDate := currentDate.AddDate(0, 0, shiftDay)
+				format := newDate.Format(utils.FormatDate)
+				resultMap[format] = currentIndex.Value
 			}
+		} else {
+			// 滞后
+			periods := i - formulaInt
+			if periods > 0 {
+				newIndex := dataList[periods]
+				resultMap[newIndex.DataTime] = currentIndex.Value
+			} else {
+				// 新数据须根据频度补充key
+				currentDate, _ := time.ParseInLocation(utils.FormatDate, currentIndex.DataTime, time.Local)
+
+				shiftDay := CalculateIntervalDays(moveFrequency, formulaInt, currentDate, resultMap, moveType)
 
-			// 指标已存在,那么就移除该日期
-			delete(removeDateMap, newDate.Format(utils.FormatDate))
+				newDate := currentDate.AddDate(0, 0, -shiftDay)
+				format := newDate.Format(utils.FormatDate)
+				resultMap[format] = currentIndex.Value
+			}
 		}
-		existMap[existKey] = currentItem.DataTime
+	}
+
+	for key, value := range resultMap {
+		currentDate, _ := time.ParseInLocation(utils.FormatDate, key, time.Local)
+		timestamp := currentDate.UnixNano() / 1e6
+		timestampStr := fmt.Sprintf("%d", timestamp)
+		addSql += GetAddSql(edbInfoIdStr, edbCode, key, timestampStr, fmt.Sprintf("%f", value))
+		isAdd = true
 	}
 
 	if isAdd {
 		addSql = strings.TrimRight(addSql, ",")
 		_, err = to.Raw(addSql).Exec()
 		if err != nil {
-			return err
-		}
-	}
-
-	// 移除不存在的日期数据
-	if len(removeDateMap) > 0 {
-		removeDateList := make([]string, 0) //需要移除的日期
-		for k := range removeDateMap {
-			removeDateList = append(removeDateList, k)
-		}
-		err = DelEdbDataByMysql(to, edbInfoId, dataTableName, removeDateList)
-		if err != nil {
-			err = fmt.Errorf("删除年化指标数据失败,Err:" + err.Error())
 			return
 		}
 	}
@@ -340,13 +335,13 @@ func refreshAllCalculatePhaseShift(to orm.TxOrmer, edbInfoId, source, subSource,
 	return
 }
 
-func CalculateIntervalDays(moveFrequency string, formulaInt int, baseDate time.Time) int {
+func CalculateIntervalDays(moveFrequency string, formulaInt int, baseDate time.Time, resultMap map[string]float64, moveType int) int {
 	var shiftDay int
 	switch moveFrequency {
 	case "自然日":
 		shiftDay = formulaInt
 	case "交易日":
-		shiftDay = utils.CalculateTradingDays(baseDate, formulaInt)
+		shiftDay = utils.CalculateTradingDays(baseDate, formulaInt, resultMap, moveType)
 	case "周":
 		shiftDay = formulaInt * 7
 	case "月":

+ 42 - 71
models/predict_edb_data_calculate_phase_shift.go

@@ -5,7 +5,6 @@ import (
 	"eta/eta_index_lib/utils"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
-	"github.com/shopspring/decimal"
 	"strconv"
 	"strings"
 	"time"
@@ -228,73 +227,58 @@ func refreshAllPredictCalculatePhaseShift(to orm.TxOrmer, edbInfoId, source, sub
 		dataMap[v.DataTime] = v
 	}
 
-	fmt.Println("source:", source)
-	//获取指标所有数据
-	existDataList := make([]*EdbData, 0)
-	dataTableName := GetEdbDataTableName(source, subSource)
-	fmt.Println("dataTableName:", dataTableName)
-	sql := `SELECT * FROM %s WHERE edb_info_id=? `
-	sql = fmt.Sprintf(sql, dataTableName)
-	_, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
-	if err != nil {
-		return
-	}
-
-	existDataMap := make(map[string]string)
-	removeDateMap := make(map[string]string)
-	for _, v := range existDataList {
-		existDataMap[v.DataTime] = v.Value
-	}
-	fmt.Println("existDataMap:", existDataMap)
 	addSql := ` INSERT INTO edb_data_predict_calculate_phase_shift(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
 	var isAdd bool
 
-	existMap := make(map[string]string)
+	resultMap := make(map[string]float64)
 	dataLen := len(dataList)
 	for i := 0; i < dataLen; i++ {
-		//当期
-		currentItem := dataList[i]
-		existKey := edbCode + currentItem.DataTime
-		if _, ok := existMap[existKey]; !ok {
-			currentDate, _ := time.ParseInLocation(utils.FormatDate, currentItem.DataTime, time.Local)
-			// 计算时间位移天数
-			shiftDay := CalculateIntervalDays(moveFrequency, formulaInt, currentDate)
-			if moveType == 2 {
-				shiftDay = -shiftDay
-			}
-			latestDateStr = latestDate.AddDate(0, 0, shiftDay).Format(utils.FormatDate)
-			newDate := currentDate.AddDate(0, 0, shiftDay)
+		// step_1 如果 领先/滞后 之后时间key存在,将该key为目标key,填充
+		currentIndex := dataList[i]
 
-			timestamp := newDate.UnixNano() / 1e6
-			timestampStr := fmt.Sprintf("%d", timestamp)
-			valStr := decimal.NewFromFloat(currentItem.Value).Round(4).String()
-			if latestDateStr == newDate.Format(utils.FormatDate) {
-				latestValue = currentItem.Value
-			}
-			if existVal, ok := existDataMap[newDate.Format(utils.FormatDate)]; !ok {
-				isAdd = true
-				addSql += GetAddSql(edbInfoIdStr, edbCode, newDate.Format(utils.FormatDate), timestampStr, valStr)
+		// 领先
+		if moveType != 2 {
+			periods := dataLen - i + formulaInt - 1
+			if periods < dataLen {
+				newIndex := dataList[i]
+				resultMap[newIndex.DataTime] = currentIndex.Value
 			} else {
-				var existValDecimal decimal.Decimal
-				existValDecimal, err = decimal.NewFromString(existVal)
-				if err != nil {
-					return
-				}
-				existStr := existValDecimal.String()
-				if existStr != valStr {
-					sql = ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
-					sql = fmt.Sprintf(sql, dataTableName)
-					_, err = to.Raw(sql, valStr, edbInfoId, newDate.Format(utils.FormatDate)).Exec()
-					if err != nil {
-						return
-					}
-				}
+				// 新数据须根据频度补充key
+				currentDate, _ := time.ParseInLocation(utils.FormatDate, currentIndex.DataTime, time.Local)
+
+				shiftDay := CalculateIntervalDays(moveFrequency, formulaInt, currentDate, resultMap, moveType)
+
+				latestDateStr = latestDate.AddDate(0, 0, shiftDay).Format(utils.FormatDate)
+				newDate := currentDate.AddDate(0, 0, shiftDay)
+				format := newDate.Format(utils.FormatDate)
+				resultMap[format] = currentIndex.Value
 			}
+		} else {
+			// 滞后
+			periods := dataLen - i - formulaInt
+			if periods > 0 {
+				newIndex := dataList[periods]
+				resultMap[newIndex.DataTime] = currentIndex.Value
+			} else {
+				// 新数据须根据频度补充key
+				currentDate, _ := time.ParseInLocation(utils.FormatDate, currentIndex.DataTime, time.Local)
 
-			// 指标已存在,那么就移除该日期
-			delete(removeDateMap, newDate.Format(utils.FormatDate))
+				shiftDay := CalculateIntervalDays(moveFrequency, formulaInt, currentDate, resultMap, moveType)
+
+				latestDateStr = latestDate.AddDate(0, 0, -shiftDay).Format(utils.FormatDate)
+				newDate := currentDate.AddDate(0, 0, -shiftDay)
+				format := newDate.Format(utils.FormatDate)
+				resultMap[format] = currentIndex.Value
+			}
 		}
-		existMap[existKey] = currentItem.DataTime
+	}
+
+	for key, value := range resultMap {
+		currentDate, _ := time.ParseInLocation(utils.FormatDate, key, time.Local)
+		timestamp := currentDate.UnixNano() / 1e6
+		timestampStr := fmt.Sprintf("%d", timestamp)
+		addSql += GetAddSql(edbInfoIdStr, edbCode, key, timestampStr, fmt.Sprintf("%f", value))
+		isAdd = true
 	}
 
 	if isAdd {
@@ -305,18 +289,5 @@ func refreshAllPredictCalculatePhaseShift(to orm.TxOrmer, edbInfoId, source, sub
 		}
 	}
 
-	// 移除不存在的日期数据
-	if len(removeDateMap) > 0 {
-		removeDateList := make([]string, 0) //需要移除的日期
-		for k := range removeDateMap {
-			removeDateList = append(removeDateList, k)
-		}
-		err = DelEdbDataByMysql(to, edbInfoId, dataTableName, removeDateList)
-		if err != nil {
-			err = fmt.Errorf("删除年化指标数据失败,Err:" + err.Error())
-			return
-		}
-	}
-
 	return
 }

+ 18 - 2
utils/common.go

@@ -1491,11 +1491,27 @@ func IsDivideZero(err error) bool {
 }
 
 // CalculateTradingDays 计算天数 跳过周末
-func CalculateTradingDays(baseDate time.Time, tradingDays int) int {
+func CalculateTradingDays(baseDate time.Time, tradingDays int, resultMap map[string]float64, moveType int) int {
 	totalDays := 0
 
 	for tradingDays > 0 {
-		baseDate = baseDate.AddDate(0, 0, 1) // Move to the next day
+		// Move to the next day
+		var moveDays int
+		if moveType != 2 {
+			moveDays = 1
+		} else {
+			moveDays = -1
+		}
+
+		baseDate = baseDate.AddDate(0, 0, moveDays)
+
+		format := baseDate.Format(FormatDate)
+
+		if _, isHoliday := resultMap[format]; isHoliday {
+			totalDays++
+			continue
+		}
+
 		weekday := baseDate.Weekday()
 
 		// Skip weekends (Saturday and Sunday)