base_from_ths_ds.go 9.0 KB

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