base_from_ths.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_Wind_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. if string(body) == "null" {
  51. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  52. return
  53. }
  54. tmpItems := new(EdbDataFromThsInterface)
  55. err = json.Unmarshal(body, &tmpItems)
  56. if err != nil {
  57. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  58. return
  59. }
  60. if tmpItems.Errorcode != 0 {
  61. //session has expired,please re-login after using the system
  62. //如果是同花顺登录session失效了,那么就重新请求获取数据
  63. if tmpItems.Errorcode == -1020 && num == 0 {
  64. return getEdbDataFromThs(edbCode, startDate, endDate, 1)
  65. }
  66. err = errors.New(string(body))
  67. return
  68. }
  69. // 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
  70. tablesList := make([]Tables, 0)
  71. for _, table := range tmpItems.Tables {
  72. tableIdList := make([]string, 0)
  73. tableTimeList := make([]string, 0)
  74. tableValueList := make([]float64, 0)
  75. for _, tableId := range table.ID {
  76. tableIdList = append(tableIdList, tableId)
  77. }
  78. for _, tableTime := range table.Time {
  79. tableTimeList = append(tableTimeList, tableTime)
  80. }
  81. //指标数据
  82. for _, tmpValue := range table.Value {
  83. var tableValue float64
  84. if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
  85. tableValue = reflect.ValueOf(tmpValue).Float()
  86. } else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
  87. tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
  88. if tmpErr != nil {
  89. err = tmpErr
  90. return
  91. }
  92. tableValue, _ = tmpTableValue.Truncate(4).Float64()
  93. } else {
  94. err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
  95. return
  96. }
  97. tableValueList = append(tableValueList, tableValue)
  98. }
  99. tmpTable := Tables{
  100. ID: tableIdList,
  101. Time: tableTimeList,
  102. Value: tableValueList,
  103. }
  104. tablesList = append(tablesList, tmpTable)
  105. }
  106. item = &EdbDataFromThs{
  107. DataVol: tmpItems.DataVol,
  108. Errmsg: tmpItems.Errmsg,
  109. Errorcode: tmpItems.Errorcode,
  110. Perf: tmpItems.Perf,
  111. Tables: tablesList,
  112. }
  113. return item, nil
  114. }