Browse Source

fix:同花顺指标 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值

Roc 2 years ago
parent
commit
00b9ee6473
1 changed files with 67 additions and 4 deletions
  1. 67 4
      services/base_from_ths.go

+ 67 - 4
services/base_from_ths.go

@@ -5,18 +5,36 @@ import (
 	"errors"
 	"fmt"
 	"github.com/rdlucklib/rdluck_tools/http"
+	"github.com/shopspring/decimal"
 	"hongze/hongze_edb_lib/utils"
+	"reflect"
 )
 
 type EdbDataFromThs struct {
+	DataVol   int64       `json:"dataVol"`
+	Errmsg    string      `json:"errmsg"`
+	Errorcode int64       `json:"errorcode"`
+	Perf      interface{} `json:"perf"`
+	Tables    []Tables    `json:"tables"`
+}
+
+// Tables 表格数据
+type Tables struct {
+	ID    []string  `json:"id"`
+	Time  []string  `json:"time"`
+	Value []float64 `json:"value"`
+}
+
+// EdbDataFromThsInterface 数据类型转为interface
+type EdbDataFromThsInterface struct {
 	DataVol   int64       `json:"dataVol"`
 	Errmsg    string      `json:"errmsg"`
 	Errorcode int64       `json:"errorcode"`
 	Perf      interface{} `json:"perf"`
 	Tables    []struct {
-		ID    []string  `json:"id"`
-		Time  []string  `json:"time"`
-		Value []float64 `json:"value"`
+		ID    []string      `json:"id"`
+		Time  []string      `json:"time"`
+		Value []interface{} `json:"value"`
 	} `json:"tables"`
 }
 
@@ -35,7 +53,8 @@ func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDa
 		err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
 		return
 	}
-	item = new(EdbDataFromThs)
+	items := new(EdbDataFromThsInterface)
+
 	err = json.Unmarshal(body, &item)
 	if err != nil {
 		err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
@@ -52,5 +71,49 @@ func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDa
 		err = errors.New(string(body))
 		return
 	}
+
+	// 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
+	tablesList := make([]Tables, 0)
+	for _, table := range items.Tables {
+		tableIdList := make([]string, 0)
+		tableTimeList := make([]string, 0)
+		tableValueList := make([]float64, 0)
+		for _, tableId := range table.ID {
+			tableTimeList = append(tableTimeList, tableId)
+		}
+		for _, tableTime := range table.Time {
+			tableIdList = append(tableIdList, tableTime)
+		}
+		for _, tmpValue := range table.Time {
+			var tableValue float64
+			if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
+				tableValue = reflect.ValueOf(tmpValue).Float()
+			} else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
+				tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
+				if tmpErr != nil {
+					err = tmpErr
+					return
+				}
+				tableValue, _ = tmpTableValue.Float64()
+			} else {
+				err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
+				return
+			}
+			tableValueList = append(tableValueList, tableValue)
+		}
+		tmpTable := Tables{
+			ID:    tableIdList,
+			Time:  tableTimeList,
+			Value: tableValueList,
+		}
+		tablesList = append(tablesList, tmpTable)
+	}
+	item = &EdbDataFromThs{
+		DataVol:   items.DataVol,
+		Errmsg:    items.Errmsg,
+		Errorcode: items.Errorcode,
+		Perf:      items.Perf,
+		Tables:    tablesList,
+	}
 	return item, nil
 }