base_from_ths_ds.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/http"
  9. "strings"
  10. )
  11. func GetEdbDataFromThsDs(stockCode, edbCode, startDate, endDate, edbTerminalCode, extraPars string) (item models.EdbDataFromThs, err error) {
  12. terminal, err := GetTerminal(utils.DATA_SOURCE_THS, edbTerminalCode)
  13. if err != nil {
  14. err = fmt.Errorf("获取同花顺接口配置出错 Err: %s", err)
  15. return
  16. }
  17. if terminal.ServerUrl == "" {
  18. err = fmt.Errorf("同花顺接口未配置")
  19. return
  20. }
  21. if edbTerminalCode == "" {
  22. // 设置指标与终端关系的缓存
  23. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + stockCode
  24. _ = utils.Rc.Put(terminalCodeCacheKey, terminal.TerminalCode, utils.GetTodayLastSecond())
  25. }
  26. // 如果没有配置,获取配置的方式是api,那么就走官方接口
  27. if utils.ThsDataMethod == "" || utils.ThsDataMethod == "api" {
  28. var token string
  29. token, err = GetAccessToken(false, terminal.Value)
  30. if err != nil {
  31. return
  32. }
  33. // TEST
  34. //token = "23f339e97fac48d8b99024228fafb6f0128cfbb7.signs_NTc2NjQ4MTA5"
  35. return getEdbDataFromThsDsHttp(stockCode, edbCode, startDate, endDate, terminal.Value, token, extraPars)
  36. }
  37. return getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate, 0, terminal.ServerUrl, extraPars)
  38. }
  39. type EdbDataFromThsSdInterface struct {
  40. Errorcode int `json:"errorcode"`
  41. Errmsg string `json:"errmsg"`
  42. Tables []Table `json:"tables"`
  43. Datatype []Type `json:"datatype"`
  44. InputParams interface{} `json:"inputParams"`
  45. DataVol int `json:"dataVol"`
  46. Perf int `json:"perf"`
  47. }
  48. type Table struct {
  49. Thscode string `json:"thscode"`
  50. Time []string `json:"time"`
  51. Table map[string]interface{} `json:"table"`
  52. }
  53. type StockData struct {
  54. THSOpenPriceStock []float64 `json:"ths_open_price_stock"`
  55. THSHighPriceStock []float64 `json:"ths_high_price_stock"`
  56. THSLowStock []float64 `json:"ths_low_stock"`
  57. THSClosePriceStock []float64 `json:"ths_close_price_stock"`
  58. THSChgRatioStock []float64 `json:"ths_chg_ratio_stock"`
  59. THSChgStock []float64 `json:"ths_chg_stock"`
  60. THSVolStock []float64 `json:"ths_vol_stock"`
  61. THSPreCloseStock []float64 `json:"ths_pre_close_stock"`
  62. THSSwingStock []float64 `json:"ths_swing_stock"`
  63. THSTurnoverRatioStock []float64 `json:"ths_turnover_ratio_stock"`
  64. THSAmtStock []float64 `json:"ths_amt_stock"`
  65. }
  66. type Type struct {
  67. Itemid string `json:"itemid"`
  68. Type string `json:"type"`
  69. }
  70. //
  71. //type Params struct {
  72. // Jsonrpc bool `json:"jsonrpc"`
  73. // Params []Param `json:"params"`
  74. //}
  75. //
  76. //type Param struct {
  77. // Function string `json:"function"`
  78. // ID string `json:"id"`
  79. // Params []Param2 `json:"params"`
  80. //}
  81. //
  82. //type Param2 struct {
  83. // Name string `json:"name"`
  84. // System string `json:"system"`
  85. // Value string `json:"value"`
  86. //}
  87. // getEdbDataFromThsDs 获取同花顺接口数据
  88. func getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate string, num int, serverUrl, extraPars string) (item models.EdbDataFromThs, err error) {
  89. if serverUrl == `` {
  90. err = errors.New("同花顺接口未配置")
  91. return
  92. }
  93. thsUrl := serverUrl + `edbInfo/ths/ds?StockCode=%s&EdbCode=%s&StartDate=%s&EndDate=%s&ExtraPars=%s`
  94. thsUrl = fmt.Sprintf(thsUrl, stockCode, edbCode, startDate, endDate, extraPars)
  95. utils.FileLog.Info("thsUrl:" + thsUrl)
  96. body, err := http.Get(thsUrl)
  97. utils.FileLog.Info("ths result:" + string(body))
  98. if err != nil {
  99. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  100. return
  101. }
  102. if string(body) == "null" {
  103. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  104. return
  105. }
  106. println(string(body))
  107. tablesList := make([]models.Tables, 0)
  108. var errCode int64
  109. if strings.Contains(edbCode, ",") {
  110. var jsonArray []string
  111. if err = json.Unmarshal(body, &jsonArray); err != nil {
  112. fmt.Println("json.Unmarshal Err:", err)
  113. return
  114. }
  115. // 解码数组内的每个 JSON 字符串
  116. //var responses []TerminalResponse
  117. for _, data := range jsonArray {
  118. data = strings.Replace(data, "NaN", "null", -1)
  119. tableTimeList := make([]string, 0)
  120. tableValueList := make([]float64, 0)
  121. var response TerminalResponse
  122. if err = json.Unmarshal([]byte(data), &response); err != nil {
  123. fmt.Println("json.Unmarshal Err:", err)
  124. return
  125. }
  126. errCode = int64(response.ErrorCode)
  127. if response.ErrorCode != 0 {
  128. //session has expired,please re-login after using the system
  129. //如果是同花顺登录session失效了,那么就重新请求获取数据
  130. if response.ErrorCode == -1020 && num == 0 {
  131. return getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate, 1, serverUrl, extraPars)
  132. }
  133. err = errors.New(string(body))
  134. return
  135. }
  136. for _, stockData := range response.Data {
  137. time := stockData["time"].(string)
  138. //thsCode := stockData["thscode"].(string)
  139. //tableTimeList = append(tableTimeList, time)
  140. for k, v := range stockData {
  141. if k != "time" && k != "thscode" {
  142. if v != nil {
  143. tableTimeList = append(tableTimeList, time)
  144. tableValueList = append(tableValueList, v.(float64))
  145. }
  146. }
  147. }
  148. }
  149. tmpTable := models.Tables{
  150. ID: []string{},
  151. Time: tableTimeList,
  152. Value: tableValueList,
  153. }
  154. tablesList = append(tablesList, tmpTable)
  155. }
  156. } else {
  157. var data string
  158. if err = json.Unmarshal(body, &data); err != nil {
  159. fmt.Println("json.Unmarshal Err:", err)
  160. return
  161. }
  162. // 解码数组内的每个 JSON 字符串
  163. //var responses []TerminalResponse
  164. data = strings.Replace(data, "NaN", "null", -1)
  165. tableTimeList := make([]string, 0)
  166. tableValueList := make([]float64, 0)
  167. var response TerminalResponse
  168. if err = json.Unmarshal([]byte(data), &response); err != nil {
  169. fmt.Println("json.Unmarshal Err:", err)
  170. return
  171. }
  172. errCode = int64(response.ErrorCode)
  173. if response.ErrorCode != 0 {
  174. //session has expired,please re-login after using the system
  175. //如果是同花顺登录session失效了,那么就重新请求获取数据
  176. if response.ErrorCode == -1020 && num == 0 {
  177. return getEdbDataFromThsDsApp(stockCode, edbCode, startDate, endDate, 1, serverUrl, extraPars)
  178. }
  179. err = errors.New(string(body))
  180. return
  181. }
  182. for _, stockData := range response.Data {
  183. time := stockData["time"].(string)
  184. //thsCode := stockData["thscode"].(string)
  185. for k, v := range stockData {
  186. if k != "time" && k != "thscode" {
  187. if v != nil {
  188. tableTimeList = append(tableTimeList, time)
  189. tableValueList = append(tableValueList, v.(float64))
  190. }
  191. }
  192. }
  193. }
  194. tmpTable := models.Tables{
  195. ID: []string{},
  196. Time: tableTimeList,
  197. Value: tableValueList,
  198. }
  199. tablesList = append(tablesList, tmpTable)
  200. }
  201. item = models.EdbDataFromThs{
  202. DataVol: 0,
  203. Errmsg: "",
  204. Errorcode: errCode,
  205. Perf: "",
  206. Tables: tablesList,
  207. }
  208. //tmpItems := new([]TerminalResponse)
  209. //err = json.Unmarshal(body, &tmpItems)
  210. //if err != nil {
  211. // err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  212. // return
  213. //}
  214. //// 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
  215. //tablesList := make([]models.Tables, 0)
  216. //for _, table := range tmpItems.Tables {
  217. // tableIdList := make([]string, 0)
  218. // tableTimeList := make([]string, 0)
  219. // tableValueList := make([]float64, 0)
  220. //
  221. // for _, tableId := range table.ID {
  222. // tableIdList = append(tableIdList, tableId)
  223. // }
  224. // for _, tableTime := range table.Time {
  225. // tableTimeList = append(tableTimeList, tableTime)
  226. // }
  227. //
  228. // //指标数据
  229. // for _, tmpValue := range table.Value {
  230. // var tableValue float64
  231. // if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
  232. // tableValue = reflect.ValueOf(tmpValue).Float()
  233. // } else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
  234. // tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
  235. // if tmpErr != nil {
  236. // err = tmpErr
  237. // return
  238. // }
  239. // tableValue, _ = tmpTableValue.Truncate(4).Float64()
  240. // } else {
  241. // err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
  242. // return
  243. // }
  244. // tableValueList = append(tableValueList, tableValue)
  245. // }
  246. // tmpTable := models.Tables{
  247. // ID: tableIdList,
  248. // Time: tableTimeList,
  249. // Value: tableValueList,
  250. // }
  251. // tablesList = append(tablesList, tmpTable)
  252. //}
  253. //item = models.EdbDataFromThs{
  254. // DataVol: tmpItems.DataVol,
  255. // Errmsg: tmpItems.Errmsg,
  256. // Errorcode: tmpItems.Errorcode,
  257. // Perf: tmpItems.Perf,
  258. // Tables: tablesList,
  259. //}
  260. return item, nil
  261. }