base_from_ths_ds.go 8.4 KB

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