base_from_ths.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "github.com/shopspring/decimal"
  8. "hongze/hongze_edb_lib/utils"
  9. "reflect"
  10. )
  11. type EdbDataFromThs struct {
  12. DataVol int64 `json:"dataVol"`
  13. Errmsg string `json:"errmsg"`
  14. Errorcode int64 `json:"errorcode"`
  15. Perf interface{} `json:"perf"`
  16. Tables []Tables `json:"tables"`
  17. }
  18. // Tables 表格数据
  19. type Tables struct {
  20. ID []string `json:"id"`
  21. Time []string `json:"time"`
  22. Value []float64 `json:"value"`
  23. }
  24. // EdbDataFromThsInterface 数据类型转为interface
  25. type EdbDataFromThsInterface struct {
  26. DataVol int64 `json:"dataVol"`
  27. Errmsg string `json:"errmsg"`
  28. Errorcode int64 `json:"errorcode"`
  29. Perf interface{} `json:"perf"`
  30. Tables []struct {
  31. ID []string `json:"id"`
  32. Time []string `json:"time"`
  33. Value []interface{} `json:"value"`
  34. } `json:"tables"`
  35. }
  36. func GetEdbDataFromThs(edbCode, startDate, endDate string) (item *EdbDataFromThs, err error) {
  37. return getEdbDataFromThs(edbCode, startDate, endDate, 0)
  38. }
  39. // getEdbDataFromThs 获取同花顺接口数据
  40. func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *EdbDataFromThs, err error) {
  41. thsUrl := utils.Hz_Ths_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  42. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  43. utils.FileLog.Info("thsUrl:" + thsUrl)
  44. body, err := http.Get(thsUrl)
  45. utils.FileLog.Info("ths result:" + string(body))
  46. if err != nil {
  47. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  48. return
  49. }
  50. tmpItems := new(EdbDataFromThsInterface)
  51. err = json.Unmarshal(body, &tmpItems)
  52. if err != nil {
  53. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  54. return
  55. }
  56. if tmpItems.Errorcode != 0 {
  57. //session has expired,please re-login after using the system
  58. //如果是同花顺登录session失效了,那么就重新请求获取数据
  59. if tmpItems.Errorcode == -1020 && num == 0 {
  60. return getEdbDataFromThs(edbCode, startDate, endDate, 1)
  61. }
  62. err = errors.New(string(body))
  63. return
  64. }
  65. // 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
  66. tablesList := make([]Tables, 0)
  67. for _, table := range tmpItems.Tables {
  68. tableIdList := make([]string, 0)
  69. tableTimeList := make([]string, 0)
  70. tableValueList := make([]float64, 0)
  71. for _, tableId := range table.ID {
  72. tableIdList = append(tableIdList, tableId)
  73. }
  74. for _, tableTime := range table.Time {
  75. tableTimeList = append(tableTimeList, tableTime)
  76. }
  77. //指标数据
  78. for _, tmpValue := range table.Value {
  79. var tableValue float64
  80. if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
  81. tableValue = reflect.ValueOf(tmpValue).Float()
  82. } else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
  83. tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
  84. if tmpErr != nil {
  85. err = tmpErr
  86. return
  87. }
  88. tableValue, _ = tmpTableValue.Float64()
  89. } else {
  90. err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
  91. return
  92. }
  93. tableValueList = append(tableValueList, tableValue)
  94. }
  95. tmpTable := Tables{
  96. ID: tableIdList,
  97. Time: tableTimeList,
  98. Value: tableValueList,
  99. }
  100. tablesList = append(tablesList, tmpTable)
  101. }
  102. item = &EdbDataFromThs{
  103. DataVol: tmpItems.DataVol,
  104. Errmsg: tmpItems.Errmsg,
  105. Errorcode: tmpItems.Errorcode,
  106. Perf: tmpItems.Perf,
  107. Tables: tablesList,
  108. }
  109. return item, nil
  110. }