123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package services
- import (
- "encoding/json"
- "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 []interface{} `json:"value"`
- } `json:"tables"`
- }
- func GetEdbDataFromThs(edbCode, startDate, endDate string) (item *EdbDataFromThs, err error) {
- return getEdbDataFromThs(edbCode, startDate, endDate, 0)
- }
- // getEdbDataFromThs 获取同花顺接口数据
- func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDataFromThs, err error) {
- thsUrl := utils.Hz_Wind_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
- thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
- utils.FileLog.Info("thsUrl:" + thsUrl)
- body, err := http.Get(thsUrl)
- utils.FileLog.Info("ths result:" + string(body))
- if err != nil {
- err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
- return
- }
- if string(body) == "null" {
- err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
- return
- }
- tmpItems := new(EdbDataFromThsInterface)
- err = json.Unmarshal(body, &tmpItems)
- if err != nil {
- err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
- return
- }
- if tmpItems.Errorcode != 0 {
- //session has expired,please re-login after using the system
- //如果是同花顺登录session失效了,那么就重新请求获取数据
- if tmpItems.Errorcode == -1020 && num == 0 {
- return getEdbDataFromThs(edbCode, startDate, endDate, 1)
- }
- err = errors.New(string(body))
- return
- }
- // 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
- tablesList := make([]Tables, 0)
- for _, table := range tmpItems.Tables {
- tableIdList := make([]string, 0)
- tableTimeList := make([]string, 0)
- tableValueList := make([]float64, 0)
- for _, tableId := range table.ID {
- tableIdList = append(tableIdList, tableId)
- }
- for _, tableTime := range table.Time {
- tableTimeList = append(tableTimeList, tableTime)
- }
- //指标数据
- for _, tmpValue := range table.Value {
- 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.Truncate(4).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: tmpItems.DataVol,
- Errmsg: tmpItems.Errmsg,
- Errorcode: tmpItems.Errorcode,
- Perf: tmpItems.Perf,
- Tables: tablesList,
- }
- return item, nil
- }
|