base_from_ths.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. }
  115. // FutureGoodDataFromThsInterface 同花顺商品数据类型转为interface
  116. type FutureGoodDataFromThsInterface struct {
  117. Errmsg string `json:"errmsg"`
  118. Errorcode int64 `json:"errorcode"`
  119. DataVol int64 `json:"dataVol"`
  120. Perf interface{} `json:"perf"`
  121. Tables []struct {
  122. ThsCode string `json:"thscode"`
  123. Time []string `json:"time"`
  124. Table struct {
  125. LastClose []float64 `json:"lastclose"`
  126. Open []float64 `json:"open"`
  127. High []float64 `json:"high"`
  128. Low []float64 `json:"low"`
  129. Close []float64 `json:"close"`
  130. AvgPrice []float64 `json:"avgprice"`
  131. Change []float64 `json:"change"`
  132. ChangePer []float64 `json:"changeper"`
  133. Volume []float64 `json:"volume"`
  134. Amount []float64 `json:"amount"`
  135. Hsl []float64 `json:"hsl"`
  136. LastSettlement []float64 `json:"lastsettlement"`
  137. Settlement []float64 `json:"settlement"`
  138. ZdSettlement []float64 `json:"zdsettlement"`
  139. ZdfSettlement []float64 `json:"zdfsettlement"`
  140. Ccl []float64 `json:"ccl"`
  141. Ccbd []float64 `json:"ccbd"`
  142. Zf []float64 `json:"zf"`
  143. Zjlx []float64 `json:"zjlx"`
  144. Zjcd []float64 `json:"zjcd"`
  145. } `json:"table"`
  146. } `json:"tables"`
  147. }
  148. // FutureGoodDataFromThs 同花顺期货数据
  149. type FutureGoodDataFromThs struct {
  150. DataVol int64 `json:"dataVol"`
  151. Errmsg string `json:"errmsg"`
  152. Errorcode int64 `json:"errorcode"`
  153. Perf interface{} `json:"perf"`
  154. Tables FutureGoodDataTables `json:"tables"`
  155. }
  156. // FutureGoodDataTables 同花顺表格数据
  157. type FutureGoodDataTables struct {
  158. //TradeCode []string `json:"id"`
  159. Time []string `json:"time"`
  160. Open []float64 `json:"open"`
  161. High []float64 `json:"high"`
  162. Low []float64 `json:"low"`
  163. Close []float64 `json:"close"`
  164. Volume []float64 `json:"volume"`
  165. Amount []float64 `json:"amount"`
  166. Ccl []float64 `json:"ccl"`
  167. Settlement []float64 `json:"settlement"`
  168. }
  169. // GetFutureGoodDataFromThs 通过url获取wind的商品数据
  170. func GetFutureGoodDataFromThs(edbCode, startDate, endDate string, num int) (item *FutureGoodDataFromThs, err error) {
  171. thsUrl := utils.Hz_Wind_Data_Url + `edbInfo/ths/future_good?EdbCode=%s&StartDate=%s&EndDate=%s`
  172. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  173. utils.FileLog.Info("thsUrl:" + thsUrl)
  174. body, err := http.Get(thsUrl)
  175. utils.FileLog.Info("ths result:" + string(body))
  176. if err != nil {
  177. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  178. return
  179. }
  180. if string(body) == "null" {
  181. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  182. return
  183. }
  184. tmpItems := new(FutureGoodDataFromThsInterface)
  185. err = json.Unmarshal(body, &tmpItems)
  186. if err != nil {
  187. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  188. return
  189. }
  190. if tmpItems.Errorcode != 0 {
  191. //session has expired,please re-login after using the system
  192. //如果是同花顺登录session失效了,那么就重新请求获取数据
  193. if tmpItems.Errorcode == -1020 && num == 0 {
  194. return GetFutureGoodDataFromThs(edbCode, startDate, endDate, 1)
  195. }
  196. err = errors.New(string(body))
  197. return
  198. }
  199. if len(tmpItems.Tables) <= 0 {
  200. return
  201. }
  202. table := tmpItems.Tables[0]
  203. item = &FutureGoodDataFromThs{
  204. DataVol: tmpItems.DataVol,
  205. Errmsg: tmpItems.Errmsg,
  206. Errorcode: tmpItems.Errorcode,
  207. Perf: tmpItems.Perf,
  208. Tables: FutureGoodDataTables{
  209. Time: table.Time,
  210. Open: table.Table.Open,
  211. High: table.Table.High,
  212. Low: table.Table.Low,
  213. Close: table.Table.Close,
  214. Volume: table.Table.Volume,
  215. Amount: table.Table.Amount,
  216. Ccl: table.Table.Ccl,
  217. Settlement: table.Table.Settlement,
  218. },
  219. }
  220. return
  221. }