base_from_ths_ds.go 8.7 KB

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