base_from_ths.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/models"
  9. "hongze/hongze_edb_lib/models/future_good"
  10. "hongze/hongze_edb_lib/utils"
  11. "reflect"
  12. )
  13. // EdbDataFromThsInterface 数据类型转为interface
  14. type EdbDataFromThsInterface struct {
  15. DataVol int64 `json:"dataVol"`
  16. Errmsg string `json:"errmsg"`
  17. Errorcode int64 `json:"errorcode"`
  18. Perf interface{} `json:"perf"`
  19. Tables []struct {
  20. ID []string `json:"id"`
  21. Time []string `json:"time"`
  22. Value []interface{} `json:"value"`
  23. } `json:"tables"`
  24. }
  25. func GetEdbDataFromThs(edbCode, startDate, endDate string) (item *models.EdbDataFromThs, err error) {
  26. return getEdbDataFromThs(edbCode, startDate, endDate, 0)
  27. }
  28. // getEdbDataFromThs 获取同花顺接口数据
  29. func getEdbDataFromThs(edbCode, startDate, endDate string, num int) (item *models.EdbDataFromThs, err error) {
  30. thsUrl := utils.Hz_Wind_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  31. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  32. utils.FileLog.Info("thsUrl:" + thsUrl)
  33. body, err := http.Get(thsUrl)
  34. utils.FileLog.Info("ths result:" + string(body))
  35. if err != nil {
  36. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  37. return
  38. }
  39. if string(body) == "null" {
  40. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  41. return
  42. }
  43. tmpItems := new(EdbDataFromThsInterface)
  44. err = json.Unmarshal(body, &tmpItems)
  45. if err != nil {
  46. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  47. return
  48. }
  49. if tmpItems.Errorcode != 0 {
  50. //session has expired,please re-login after using the system
  51. //如果是同花顺登录session失效了,那么就重新请求获取数据
  52. if tmpItems.Errorcode == -1020 && num == 0 {
  53. return getEdbDataFromThs(edbCode, startDate, endDate, 1)
  54. }
  55. err = errors.New(string(body))
  56. return
  57. }
  58. // 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
  59. tablesList := make([]models.Tables, 0)
  60. for _, table := range tmpItems.Tables {
  61. tableIdList := make([]string, 0)
  62. tableTimeList := make([]string, 0)
  63. tableValueList := make([]float64, 0)
  64. for _, tableId := range table.ID {
  65. tableIdList = append(tableIdList, tableId)
  66. }
  67. for _, tableTime := range table.Time {
  68. tableTimeList = append(tableTimeList, tableTime)
  69. }
  70. //指标数据
  71. for _, tmpValue := range table.Value {
  72. var tableValue float64
  73. if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
  74. tableValue = reflect.ValueOf(tmpValue).Float()
  75. } else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
  76. tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
  77. if tmpErr != nil {
  78. err = tmpErr
  79. return
  80. }
  81. tableValue, _ = tmpTableValue.Truncate(4).Float64()
  82. } else {
  83. err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
  84. return
  85. }
  86. tableValueList = append(tableValueList, tableValue)
  87. }
  88. tmpTable := models.Tables{
  89. ID: tableIdList,
  90. Time: tableTimeList,
  91. Value: tableValueList,
  92. }
  93. tablesList = append(tablesList, tmpTable)
  94. }
  95. item = &models.EdbDataFromThs{
  96. DataVol: tmpItems.DataVol,
  97. Errmsg: tmpItems.Errmsg,
  98. Errorcode: tmpItems.Errorcode,
  99. Perf: tmpItems.Perf,
  100. Tables: tablesList,
  101. }
  102. return item, nil
  103. }
  104. // FutureGoodDataFromThsInterface 同花顺商品数据类型转为interface
  105. type FutureGoodDataFromThsInterface struct {
  106. Errmsg string `json:"errmsg"`
  107. Errorcode int64 `json:"errorcode"`
  108. DataVol int64 `json:"dataVol"`
  109. Perf interface{} `json:"perf"`
  110. Tables []struct {
  111. ThsCode string `json:"thscode"`
  112. Time []string `json:"time"`
  113. Table struct {
  114. LastClose []float64 `json:"lastclose"`
  115. Open []float64 `json:"open"`
  116. High []float64 `json:"high"`
  117. Low []float64 `json:"low"`
  118. Close []float64 `json:"close"`
  119. AvgPrice []float64 `json:"avgprice"`
  120. Change []float64 `json:"change"`
  121. ChangePer []float64 `json:"changeper"`
  122. Volume []float64 `json:"volume"`
  123. Amount []float64 `json:"amount"`
  124. Hsl []float64 `json:"hsl"`
  125. LastSettlement []float64 `json:"lastsettlement"`
  126. Settlement []float64 `json:"settlement"`
  127. ZdSettlement []float64 `json:"zdsettlement"`
  128. ZdfSettlement []float64 `json:"zdfsettlement"`
  129. Ccl []float64 `json:"ccl"`
  130. Ccbd []float64 `json:"ccbd"`
  131. Zf []float64 `json:"zf"`
  132. Zjlx []float64 `json:"zjlx"`
  133. Zjcd []float64 `json:"zjcd"`
  134. } `json:"table"`
  135. } `json:"tables"`
  136. }
  137. // GetFutureGoodDataFromThs 通过url获取wind的商品数据
  138. func GetFutureGoodDataFromThs(edbCode, startDate, endDate string, num int) (item *future_good.FutureGoodDataFromThs, err error) {
  139. thsUrl := utils.Hz_Wind_Data_Url + `edbInfo/ths/future_good?EdbCode=%s&StartDate=%s&EndDate=%s`
  140. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  141. utils.FileLog.Info("thsUrl:" + thsUrl)
  142. body, err := http.Get(thsUrl)
  143. utils.FileLog.Info("ths result:" + string(body))
  144. if err != nil {
  145. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  146. return
  147. }
  148. if string(body) == "null" {
  149. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  150. return
  151. }
  152. tmpItems := new(FutureGoodDataFromThsInterface)
  153. err = json.Unmarshal(body, &tmpItems)
  154. if err != nil {
  155. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  156. return
  157. }
  158. if tmpItems.Errorcode != 0 {
  159. //session has expired,please re-login after using the system
  160. //如果是同花顺登录session失效了,那么就重新请求获取数据
  161. if tmpItems.Errorcode == -1020 && num == 0 {
  162. return GetFutureGoodDataFromThs(edbCode, startDate, endDate, 1)
  163. }
  164. err = errors.New(string(body))
  165. return
  166. }
  167. if len(tmpItems.Tables) <= 0 {
  168. return
  169. }
  170. table := tmpItems.Tables[0]
  171. item = &future_good.FutureGoodDataFromThs{
  172. DataVol: tmpItems.DataVol,
  173. Errmsg: tmpItems.Errmsg,
  174. Errorcode: tmpItems.Errorcode,
  175. Perf: tmpItems.Perf,
  176. Tables: future_good.FutureGoodDataTables{
  177. Time: table.Time,
  178. Open: table.Table.Open,
  179. High: table.Table.High,
  180. Low: table.Table.Low,
  181. Close: table.Table.Close,
  182. Volume: table.Table.Volume,
  183. Amount: table.Table.Amount,
  184. Ccl: table.Table.Ccl,
  185. Settlement: table.Table.Settlement,
  186. },
  187. }
  188. return
  189. }