Browse Source

fix:商品价格曲线,新增海外交易所

Roc 1 year ago
parent
commit
35ff05f34c
2 changed files with 351 additions and 70 deletions
  1. 11 7
      models/response/chart_info/chart_info.go
  2. 340 63
      services/chart/future_good/chart_info.go

+ 11 - 7
models/response/chart_info/chart_info.go

@@ -4,6 +4,7 @@ import (
 	responseModel "hongze/hongze_yb/models/response"
 	"hongze/hongze_yb/models/tables/chart_edb_mapping"
 	"hongze/hongze_yb/models/tables/chart_info"
+	"time"
 )
 
 type ChartInfoDetailResp struct {
@@ -25,13 +26,16 @@ type XData struct {
 
 // YData 柱方图的y轴数据
 type YData struct {
-	Date           string    `description:"数据日期"`
-	Color          string    `description:"数据颜色"`
-	Name           string    `description:"别名"`
-	NameEn         string    `description:"英文别名"`
-	Value          []float64 `description:"每个指标的值"`
-	NoDataEdbList  []int     `description:"没有数据的指标列表"`
-	XEdbInfoIdList []int     `description:"对应X轴的指标id列表"`
+	Date           string          `description:"数据日期"`
+	ConfigDate     time.Time       `description:"配置的日期" json:"-"`
+	Color          string          `description:"数据颜色"`
+	Name           string          `description:"别名"`
+	NameEn         string          `description:"英文别名"`
+	Value          []float64       `description:"每个指标的值"`
+	NoDataEdbList  []int           `description:"没有数据的指标列表"`
+	XEdbInfoIdList []int           `description:"对应X轴的指标id列表"`
+	EdbValMap      map[int]float64 `description:"指标与值的对应" json:"-"`
+	M              []int           `description:"对应开始日期的间隔值" json:"-"`
 }
 
 type CorrelationInfo struct {

+ 340 - 63
services/chart/future_good/chart_info.go

@@ -13,15 +13,15 @@ import (
 	"hongze/hongze_yb/services/alarm_msg"
 	"hongze/hongze_yb/services/chart"
 	"hongze/hongze_yb/utils"
+	"sort"
 	"strconv"
 	"time"
 )
 
 // GetChartEdbData 获取图表的指标数据
-func GetChartEdbData(chartInfoId int, startDate, endDate string, edbInfoMapping, futureGoodEdbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList, barChartInfoDateList []request.BarChartInfoDateReq) (barConfigEdbInfoIdList []request.BarChartInfoEdbItemReq, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, xEdbIdValue []int, xDataList []chart_info.XData, yDataList []chart_info.YData, sourceArr []string, err error) {
+func GetChartEdbData(chartInfoId int, startDate, endDate string, edbInfoMapping, futureGoodEdbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList, barChartInfoDateList []request.BarChartInfoDateReq) (barConfigEdbInfoIdList []request.BarChartInfoEdbItemReq, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, xEdbIdValue []int, xDataList []chart_info.XData, yDataList []chart_info.YData, err error) {
 	edbList = make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
 
-	sourceArr = make([]string, 0)
 	if futureGoodEdbInfoMapping == nil {
 		err = errors.New("商品指标未选取")
 		return
@@ -87,12 +87,6 @@ func GetChartEdbData(chartInfoId int, startDate, endDate string, edbInfoMapping,
 			NameEn: "Spot Price",
 			Source: edbInfoMapping.Source,
 		})
-
-		if !utils.InArray(edbInfoMapping.Source, utils.SystemSourceList) { //来源于系统的指标,都展示为弘则研究
-			if !utils.InArray(edbInfoMapping.SourceName, sourceArr) {
-				sourceArr = append(sourceArr, edbInfoMapping.SourceName)
-			}
-		}
 	}
 
 	// 获取主力合约和最新日期
@@ -130,41 +124,10 @@ func GetChartEdbData(chartInfoId int, startDate, endDate string, edbInfoMapping,
 		return
 	}
 
-	maxM := 36
-	futureGoodEdbInfoList := make([]*future_good_edb_info.FutureGoodEdbInfo, 0)
 	latestDateTime, _ := time.ParseInLocation(utils.FormatDate, latestDate, time.Local)
-
-	for _, v := range tmpFutureGoodEdbInfoList {
-		if v.RegionType == `国内` {
-			futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
-			continue
-		}
-		//海外的连续日期,目前
-		if v.FutureGoodEdbType == 2 {
-			if int(v.Month) <= maxM {
-				addMonth := int(latestDateTime.Month()) + int(v.Month)
-				v.Year = latestDateTime.Year() + addMonth/12
-				realMonth := addMonth % 12
-				if realMonth == 0 {
-					realMonth = 12
-				}
-				v.Month = uint32(realMonth)
-				futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
-			}
-			continue
-		}
-
-		// 小于等于当前年,那么就肯定是ok的
-		if v.Year <= latestDateTime.Year() {
-			futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
-			continue
-		}
-		// 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
-		if (v.Year-latestDateTime.Year())*12+(int(v.Month)-int(latestDateTime.Month())) <= maxM {
-			futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
-			continue
-		}
-
+	_, futureGoodEdbInfoList, err := getFutureGoodEdbInfoList(latestDateTime, tmpFutureGoodEdbInfoList, barChartInfoDateList)
+	if err != nil {
+		return
 	}
 
 	futureGoodMappingList := make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
@@ -252,27 +215,113 @@ func GetChartEdbData(chartInfoId int, startDate, endDate string, edbInfoMapping,
 			NameEn: "Spot Price",
 		},
 	}
-	if regionType == `国内` {
-		for i := range futureGoodEdbInfoList {
-			xDataList = append(xDataList, chart_info.XData{
-				Name:   fmt.Sprint("M+", i),
-				NameEn: fmt.Sprint("M+ ", i),
-			})
+	//yDataList =make([]chart_info.YData,0)
+	//chart_info.YData{
+	//	Date:           "",
+	//	Color:          "",
+	//	Name:           "",
+	//	NameEn:         "",
+	//	Value:          nil,
+	//	NoDataEdbList:  nil,
+	//	XEdbInfoIdList: nil,
+	//}
+	futureGoodEdbInfoIndexMap := make(map[int]int)
+	for index, v := range futureGoodEdbInfoList {
+		futureGoodEdbInfoIndexMap[int(v.FutureGoodEdbInfoID)] = index
+	}
+	nMap := make(map[int]int)
+	maxIndex := 0 // 最大x轴的下标
+	for k, tmpYData := range yDataList {
+		yDataMap := tmpYData.EdbValMap
+		edbInfoIdList := make([]int, 0)
+		noDataEdbInfoIdList := make([]int, 0)
+		valueList := make([]float64, 0)
+		noDataEdbIdMap := make(map[int]int)
+
+		// 所有的N值
+		for _, n := range tmpYData.M {
+			nMap[n] = n
 		}
-	} else {
-		for _, v := range futureGoodEdbInfoList {
-			divMonth := (v.Year-latestDateTime.Year())*12 + (int(v.Month) - int(latestDateTime.Month()))
-			xDataList = append(xDataList, chart_info.XData{
-				Name:   fmt.Sprint("M+", divMonth),
-				NameEn: fmt.Sprint("M+ ", divMonth),
-			})
+
+		// 基础指标
+		{
+			baseEdbInfId := edbList[0].EdbInfoId
+			edbInfoIdList = append(edbInfoIdList, baseEdbInfId)
+			tmpVal, ok := yDataMap[baseEdbInfId]
+			valueList = append(valueList, tmpVal)
+			if !ok || tmpVal == 0 {
+				noDataEdbInfoIdList = append(noDataEdbInfoIdList, baseEdbInfId)
+				noDataEdbIdMap[baseEdbInfId] = baseEdbInfId
+			}
 		}
+
+		for _, futureGoodEdbInfo := range futureGoodEdbInfoList {
+			tmpEdbInfId := int(futureGoodEdbInfo.FutureGoodEdbInfoID)
+			edbInfoIdList = append(edbInfoIdList, tmpEdbInfId)
+			tmpVal, ok := yDataMap[tmpEdbInfId]
+			valueList = append(valueList, tmpVal)
+			if !ok || tmpVal == 0 {
+				noDataEdbInfoIdList = append(noDataEdbInfoIdList, tmpEdbInfId)
+				noDataEdbIdMap[tmpEdbInfId] = tmpEdbInfId
+			}
+		}
+		//tmpYData.Value = valueList
+		tmpYData.NoDataEdbList = noDataEdbInfoIdList
+		yDataList[k] = tmpYData
+
+		lenEdbId := len(edbInfoIdList)
+		tmpMaxIndex := lenEdbId - 1 // 当前数据的最大x轴的下标
+
+		for i := lenEdbId - 1; i >= 0; i-- {
+			// 如果没有在无数据的指标列表中找到,那么就找到了最大x轴的下标
+			if _, ok := noDataEdbIdMap[edbInfoIdList[i]]; !ok {
+				// 如果最大x轴的下标 小于 当前下标,那么就重新赋值
+				if maxIndex < i-1 {
+					maxIndex = i - 1
+				}
+				break
+			}
+
+			tmpMaxIndex = i - 1
+		}
+
+		// 如果最大x轴的下标 小于 当前下标,那么就重新赋值
+		if maxIndex < tmpMaxIndex {
+			maxIndex = tmpMaxIndex
+		}
+	}
+
+	//xEdbIdValue = xEdbIdValue[0:maxIndex]
+
+	// 找出所有的N值,并进行正序排列
+	nList := make([]int, 0)
+	for _, n := range nMap {
+		nList = append(nList, n)
 	}
+	sort.Slice(nList, func(i, j int) bool {
+		return nList[i] < nList[j]
+	})
+
+	for k, v := range yDataList {
+		if len(v.XEdbInfoIdList) >= maxIndex+1 {
+			yDataList[k].XEdbInfoIdList = v.XEdbInfoIdList[0 : maxIndex+1]
+		}
+		if len(v.Value) >= maxIndex+1 {
+			yDataList[k].Value = v.Value[0 : maxIndex+1]
+		}
+	}
+
+	tmpXDataList, newYDataList, err := handleResultData(regionType, yDataList, futureGoodEdbInfoList, maxIndex)
+	if err != nil {
+		return
+	}
+	xDataList = append(xDataList, tmpXDataList...)
+	yDataList = newYDataList
 
 	return
 }
 
-// BarChartData 商品价格曲线的数据处理
+// BarChartData 获取数据
 func BarChartData(edbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList, futureGoodMappingList []*future_good_edb_info.FutureGoodEdbInfo, edbDataListMap map[int][]*edbDataModel.EdbDataList, barChartInfoDateList []request.BarChartInfoDateReq, regionType, latestDate string) (edbIdList []int, yDataList []chart_info.YData, err error) {
 	// 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
 	edbDataMap := make(map[int]map[string]float64)
@@ -296,6 +345,7 @@ func BarChartData(edbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList,
 	yDataList = make([]chart_info.YData, 0) //y轴的数据列表
 
 	for _, barChartInfoDate := range barChartInfoDateList {
+		yDataMap := make(map[int]float64)
 		var maxDate time.Time
 
 		var findDateTime time.Time
@@ -333,6 +383,7 @@ func BarChartData(edbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList,
 			return
 		}
 		findDataList = append(findDataList, findDataValue)
+		yDataMap[edbInfoMapping.EdbInfoId] = findDataValue
 		if isFind {
 			maxDate = realDateTime
 		} else {
@@ -342,30 +393,37 @@ func BarChartData(edbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList,
 		currMonth := findDateTime.Month() // 当前月份
 		currYear := findDateTime.Year()   // 当前年份
 		xEdbInfoIdList = append(xEdbInfoIdList, edbInfoMapping.EdbInfoId)
-
+		mList := make([]int, 0) // 间隔月份
 		indexList := make([]int, 0)
 		if regionType == `国内` {
 			for i := currMonth; i < 12; i++ {
 				indexList = append(indexList, int(i))
+				mList = append(mList, int(i-currMonth))
 			}
 			for i := 1; i < int(currMonth); i++ {
 				indexList = append(indexList, i)
+				mList = append(mList, 12+i-int(currMonth))
 			}
 		} else {
 			for i, v := range futureGoodMappingList {
-				if v.Year > currYear || (v.Year == currYear && int(v.Month) >= int(currMonth)) {
+				if v.Year > currYear || (v.Year == currYear && int(v.Month) > int(currMonth)) {
 					indexList = append(indexList, i)
+					mList = append(mList, (v.Year-currYear)*12+int(v.Month)-int(currMonth))
 				}
 			}
 		}
 
 		for _, i := range indexList {
 			futureGoodMapping := futureGoodMappingList[i] // 当前的期货指标
-			tmpRealDateTime, tmpFindDataValue, tmpIsFind, tmpErr := GetNeedDateData(findDateTime, edbDataListMap[int(futureGoodMapping.FutureGoodEdbInfoID)], edbDataMap[int(futureGoodMapping.FutureGoodEdbInfoID)])
-			if tmpErr != nil {
-				err = tmpErr
-				return
-			}
+			//tmpRealDateTime, tmpFindDataValue, tmpIsFind, tmpErr := GetNeedDateData(findDateTime, edbDataListMap[futureGoodMapping.FutureGoodEdbInfoId], edbDataMap[futureGoodMapping.FutureGoodEdbInfoId])
+			//if tmpErr != nil {
+			//	err = tmpErr
+			//	return
+			//}
+			tmpRealDateTime := findDateTime
+			tmpFindDataValue, tmpIsFind := edbDataMap[int(futureGoodMapping.FutureGoodEdbInfoID)][findDateTime.Format(utils.FormatDate)]
+			yDataMap[int(futureGoodMapping.FutureGoodEdbInfoID)] = tmpFindDataValue
+
 			findDataList = append(findDataList, tmpFindDataValue)
 			if tmpIsFind {
 				if maxDate.IsZero() || maxDate.Before(tmpRealDateTime) {
@@ -429,12 +487,15 @@ func BarChartData(edbInfoMapping *chartEdbMappingModel.ChartEdbInfoMappingList,
 
 		yDataList = append(yDataList, chart_info.YData{
 			Date:           yDate,
+			ConfigDate:     findDateTime,
 			Value:          findDataList,
 			NoDataEdbList:  noDataIdList,
 			XEdbInfoIdList: xEdbInfoIdList,
 			Color:          barChartInfoDate.Color,
 			Name:           yName,
 			NameEn:         yNameEn,
+			EdbValMap:      yDataMap,
+			M:              mList,
 		})
 	}
 
@@ -517,3 +578,219 @@ func FutureGoodChartInfoRefresh(chartInfoId int) (err error) {
 
 	return
 }
+
+// handleResultData 处理成最终的结果数据
+func handleResultData(regionType string, yDataList []chart_info.YData, futureGoodEdbInfoList []*future_good_edb_info.FutureGoodEdbInfo, maxIndex int) (xDataList []chart_info.XData, newYDataList []chart_info.YData, err error) {
+	xDataList = make([]chart_info.XData, 0)
+	newYDataList = yDataList
+
+	if regionType == `国内` {
+		for i := range futureGoodEdbInfoList {
+			if i > maxIndex {
+				break
+			}
+			xDataList = append(xDataList, chart_info.XData{
+				Name:   fmt.Sprint("M+", i),
+				NameEn: fmt.Sprint("M+", i),
+			})
+		}
+		return
+	}
+
+	futureGoodEdbInfoMap := make(map[int]*future_good_edb_info.FutureGoodEdbInfo)
+	for _, v := range futureGoodEdbInfoList {
+		futureGoodEdbInfoMap[int(v.FutureGoodEdbInfoID)] = v
+	}
+
+	nMap := make(map[int]int)
+
+	for _, v := range yDataList {
+		findDateTime := v.ConfigDate
+		currMonth := findDateTime.Month() // 当前月份
+		currYear := findDateTime.Year()   // 当前年份
+		//v.XEdbInfoIdList
+		for edbInfoIndex, edbInfoId := range v.XEdbInfoIdList {
+			// 第一个不处理
+			if edbInfoIndex == 0 {
+				continue
+			}
+
+			futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
+			if !ok {
+				err = errors.New("找不到指标")
+				return
+			}
+			n := (futureGoodEdbInfo.Year-currYear)*12 + int(futureGoodEdbInfo.Month) - int(currMonth)
+			nMap[n] = n
+		}
+
+	}
+
+	// 找出所有的N值,并进行正序排列
+	nList := make([]int, 0)
+	for _, n := range nMap {
+		nList = append(nList, n)
+	}
+	sort.Slice(nList, func(i, j int) bool {
+		return nList[i] < nList[j]
+	})
+
+	//prevMonth := 1
+
+	for _, n := range nList {
+		xDataList = append(xDataList, chart_info.XData{
+			Name:   fmt.Sprint("M+", n),
+			NameEn: fmt.Sprint("M+", n),
+		})
+	}
+
+	for yIndex, yData := range yDataList {
+		newYDataList[yIndex].XEdbInfoIdList = []int{}
+		newYDataList[yIndex].Value = []float64{}
+		tmpNList := nList
+
+		//当前的主力合约
+		newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, yData.XEdbInfoIdList[0])
+		newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[0])
+
+		xEdbInfoIdList := yData.XEdbInfoIdList[1:]
+		currDataTime := yData.ConfigDate
+		valIndex := 1
+		needNum := 0
+		for _, n := range tmpNList {
+			if len(xEdbInfoIdList) > 0 {
+				edbInfoId := xEdbInfoIdList[0]
+				futureGoodEdbInfo, ok := futureGoodEdbInfoMap[edbInfoId]
+				if !ok {
+					err = errors.New("找不到指标")
+					return
+				}
+				// 当前距离最早的日期相差的N数
+				divMonth := (futureGoodEdbInfo.Year-currDataTime.Year())*12 + (int(futureGoodEdbInfo.Month) - int(currDataTime.Month()))
+				if divMonth == n {
+					if needNum > 0 {
+						currVal := yData.Value[valIndex]
+						preVal := yData.Value[valIndex-1]
+						hcValDeci := decimal.NewFromFloat(currVal).Sub(decimal.NewFromFloat(preVal)).Div(decimal.NewFromInt(int64(needNum + 1)))
+
+						for tmpNum := 0; tmpNum < needNum; tmpNum++ {
+							newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, 0)
+
+							// 赋值平均值
+							tmpVal, _ := decimal.NewFromFloat(preVal).Add(hcValDeci.Mul(decimal.NewFromInt(int64(tmpNum + 1)))).RoundCeil(4).Float64()
+							newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, tmpVal)
+						}
+					}
+
+					newYDataList[yIndex].XEdbInfoIdList = append(newYDataList[yIndex].XEdbInfoIdList, edbInfoId)
+					newYDataList[yIndex].Value = append(newYDataList[yIndex].Value, yData.Value[valIndex])
+					valIndex++
+					needNum = 0
+					if len(xEdbInfoIdList) > 0 {
+						xEdbInfoIdList = xEdbInfoIdList[1:]
+					}
+				} else {
+					needNum++
+				}
+			}
+		}
+	}
+
+	maxI := 100000
+	for _, yData := range newYDataList {
+		lenEdb := len(yData.XEdbInfoIdList)
+		for i := lenEdb - 1; i >= 0; i-- {
+			if yData.XEdbInfoIdList[i] == 0 || utils.InArrayByInt(yData.NoDataEdbList, yData.XEdbInfoIdList[i]) {
+				maxI = i
+				if maxI > i {
+					maxI = i
+				}
+			} else {
+				break
+			}
+		}
+	}
+
+	//xDataList = xDataList[0:maxI]
+	//for yIndex, yData := range newYDataList {
+	//	if len(yData.XEdbInfoIdList) > maxI {
+	//		newYDataList[yIndex].XEdbInfoIdList = yData.XEdbInfoIdList[0:maxI]
+	//	}
+	//	if len(yData.Value) > maxI {
+	//		newYDataList[yIndex].Value = yData.Value[0:maxI]
+	//	}
+	//}
+	return
+}
+
+// getFutureGoodEdbInfoList 获取适用的指标列表
+func getFutureGoodEdbInfoList(latestDateTime time.Time, tmpFutureGoodEdbInfoList []*future_good_edb_info.FutureGoodEdbInfo, barChartInfoDateList []request.BarChartInfoDateReq) (earliestDateTime time.Time, futureGoodEdbInfoList []*future_good_edb_info.FutureGoodEdbInfo, err error) {
+	maxM := 32 //最大32期合约
+	futureGoodEdbInfoList = make([]*future_good_edb_info.FutureGoodEdbInfo, 0)
+	earliestDateTime = latestDateTime // 数据的最早日期,目的是为了找出最早的合约
+	for _, barChartInfoDate := range barChartInfoDateList {
+		var findDateTime time.Time
+		switch barChartInfoDate.Type {
+		case 1: //最新值
+			findDateTime = latestDateTime
+		case 2: //近期几天
+			findDateTime = latestDateTime.AddDate(0, 0, -barChartInfoDate.Value)
+		case 3: // 固定日期
+			//寻找固定日期的数据
+			tmpFindDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
+			if tmpErr != nil {
+				err = tmpErr
+				return
+			}
+			findDateTime = tmpFindDateTime
+		default:
+			err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
+			return
+		}
+		if findDateTime.IsZero() {
+			err = errors.New("错误的日期")
+			return
+		}
+		if findDateTime.Before(earliestDateTime) {
+			earliestDateTime = findDateTime
+		}
+	}
+
+	for _, v := range tmpFutureGoodEdbInfoList {
+		if v.RegionType == `国内` {
+			futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
+			continue
+		}
+		//海外的连续日期,目前
+		if v.FutureGoodEdbType == 2 {
+			if int(v.Month) <= maxM {
+				addMonth := int(earliestDateTime.Month()) + int(v.Month)
+				v.Year = earliestDateTime.Year() + addMonth/12
+				realMonth := addMonth % 12
+				if realMonth == 0 {
+					realMonth = 12
+				}
+				v.Month = uint32(realMonth)
+				futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
+			}
+			continue
+		}
+
+		if v.Year < earliestDateTime.Year() {
+			continue
+		}
+		// 小于等于当前年,那么就肯定是ok的
+		if v.Year <= earliestDateTime.Year() {
+			futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
+			continue
+		}
+		// 如果(当前年-最新日期的年份) * 12个月 + (当前月-最新日期的月份) 小于总月份
+		if (v.Year-earliestDateTime.Year())*12+(int(v.Month)-int(earliestDateTime.Month())) <= maxM {
+			futureGoodEdbInfoList = append(futureGoodEdbInfoList, v)
+			continue
+		}
+
+	}
+
+	return
+}