base_from_ths.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/models/future_good"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/http"
  10. "github.com/shopspring/decimal"
  11. "reflect"
  12. )
  13. // EdbDataFromThsInterface 数据类型转为interface
  14. type EdbDataFromThsInterface struct {
  15. DataVol int64 `json:"dataVol"`
  16. Errmsg string `json:"errmsg"`
  17. Errorcode int64 `json:"errorcode"`
  18. Perf interface{} `json:"perf"`
  19. Tables []struct {
  20. ID []string `json:"id"`
  21. Time []string `json:"time"`
  22. Value []interface{} `json:"value"`
  23. } `json:"tables"`
  24. }
  25. func GetEdbDataFromThs(edbCode, startDate, endDate, edbTerminalCode string) (item models.EdbDataFromThs, err error) {
  26. terminal, err := GetTerminal(utils.DATA_SOURCE_THS, edbTerminalCode)
  27. if err != nil {
  28. err = fmt.Errorf("获取同花顺接口配置出错 Err: %s", err)
  29. return
  30. }
  31. if edbTerminalCode == "" {
  32. // 设置指标与终端关系的缓存
  33. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  34. _ = utils.Rc.Put(terminalCodeCacheKey, terminal.TerminalCode, utils.GetTodayLastSecond())
  35. }
  36. // 如果没有配置,获取配置的方式是api,那么就走官方接口
  37. if utils.ThsDataMethod == "" || utils.ThsDataMethod == "api" {
  38. if terminal.Value == "" {
  39. err = fmt.Errorf("同花顺接口未配置")
  40. return
  41. }
  42. var token string
  43. token, err = GetAccessToken(false, terminal.Value)
  44. if err != nil {
  45. return
  46. }
  47. return getEdbDataFromThsHttp(edbCode, startDate, endDate, terminal.Value, token)
  48. }
  49. return getEdbDataFromThsApp(edbCode, startDate, endDate, 0, terminal.ServerUrl)
  50. }
  51. // getEdbDataFromThs 获取同花顺接口数据
  52. func getEdbDataFromThsApp(edbCode, startDate, endDate string, num int, serverUrl string) (item models.EdbDataFromThs, err error) {
  53. if serverUrl == `` {
  54. err = errors.New("同花顺接口未配置")
  55. return
  56. }
  57. thsUrl := serverUrl + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  58. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  59. utils.FileLog.Info("thsUrl:" + thsUrl)
  60. body, err := http.Get(thsUrl)
  61. utils.FileLog.Info("ths result:" + string(body))
  62. if err != nil {
  63. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  64. return
  65. }
  66. if string(body) == "null" {
  67. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  68. return
  69. }
  70. tmpItems := new(EdbDataFromThsInterface)
  71. err = json.Unmarshal(body, &tmpItems)
  72. if err != nil {
  73. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  74. return
  75. }
  76. if tmpItems.Errorcode != 0 {
  77. //session has expired,please re-login after using the system
  78. //如果是同花顺登录session失效了,那么就重新请求获取数据
  79. if tmpItems.Errorcode == -1020 && num == 0 {
  80. return getEdbDataFromThsApp(edbCode, startDate, endDate, 1, serverUrl)
  81. }
  82. err = errors.New(string(body))
  83. return
  84. }
  85. // 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
  86. tablesList := make([]models.Tables, 0)
  87. for _, table := range tmpItems.Tables {
  88. tableIdList := make([]string, 0)
  89. tableTimeList := make([]string, 0)
  90. tableValueList := make([]float64, 0)
  91. for _, tableId := range table.ID {
  92. tableIdList = append(tableIdList, tableId)
  93. }
  94. for _, tableTime := range table.Time {
  95. tableTimeList = append(tableTimeList, tableTime)
  96. }
  97. //指标数据
  98. for _, tmpValue := range table.Value {
  99. var tableValue float64
  100. if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
  101. tableValue = reflect.ValueOf(tmpValue).Float()
  102. } else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
  103. tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
  104. if tmpErr != nil {
  105. err = tmpErr
  106. return
  107. }
  108. tableValue, _ = tmpTableValue.Truncate(4).Float64()
  109. } else {
  110. err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
  111. return
  112. }
  113. tableValueList = append(tableValueList, tableValue)
  114. }
  115. tmpTable := models.Tables{
  116. ID: tableIdList,
  117. Time: tableTimeList,
  118. Value: tableValueList,
  119. }
  120. tablesList = append(tablesList, tmpTable)
  121. }
  122. item = models.EdbDataFromThs{
  123. DataVol: tmpItems.DataVol,
  124. Errmsg: tmpItems.Errmsg,
  125. Errorcode: tmpItems.Errorcode,
  126. Perf: tmpItems.Perf,
  127. Tables: tablesList,
  128. }
  129. return item, nil
  130. }
  131. // FutureGoodDataFromThsInterface 同花顺商品数据类型转为interface
  132. type FutureGoodDataFromThsInterface struct {
  133. Errmsg string `json:"errmsg"`
  134. Errorcode int64 `json:"errorcode"`
  135. DataVol int64 `json:"dataVol"`
  136. Perf interface{} `json:"perf"`
  137. Tables []struct {
  138. ThsCode string `json:"thscode"`
  139. Time []string `json:"time"`
  140. Table struct {
  141. LastClose []float64 `json:"lastclose"`
  142. Open []float64 `json:"open"`
  143. High []float64 `json:"high"`
  144. Low []float64 `json:"low"`
  145. Close []float64 `json:"close"`
  146. AvgPrice []float64 `json:"avgprice"`
  147. Change []float64 `json:"change"`
  148. ChangePer []float64 `json:"changeper"`
  149. Volume []float64 `json:"volume"`
  150. Amount []float64 `json:"amount"`
  151. Hsl []float64 `json:"hsl"`
  152. LastSettlement []float64 `json:"lastsettlement"`
  153. Settlement []float64 `json:"settlement"`
  154. ZdSettlement []float64 `json:"zdsettlement"`
  155. ZdfSettlement []float64 `json:"zdfsettlement"`
  156. Ccl []float64 `json:"ccl"`
  157. Ccbd []float64 `json:"ccbd"`
  158. Zf []float64 `json:"zf"`
  159. Zjlx []float64 `json:"zjlx"`
  160. Zjcd []float64 `json:"zjcd"`
  161. } `json:"table"`
  162. } `json:"tables"`
  163. }
  164. func GetFutureGoodDataFromThs(edbCode, startDate, endDate, edbTerminalCode string) (item future_good.FutureGoodDataFromThs, err error) {
  165. terminal, err := GetFirstTerminal(utils.DATA_SOURCE_THS, edbTerminalCode)
  166. if err != nil {
  167. err = fmt.Errorf("获取同花顺接口配置出错 Err: %s", err)
  168. return
  169. }
  170. if terminal.ServerUrl == "" {
  171. err = fmt.Errorf("同花顺接口未配置")
  172. return
  173. }
  174. if edbTerminalCode == "" {
  175. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_GOOD_URL + edbCode
  176. _ = utils.Rc.Put(terminalCodeCacheKey, terminal.TerminalCode, utils.GetTodayLastSecond())
  177. }
  178. if utils.ThsDataMethod == "" || utils.ThsDataMethod == "api" { // 生产环境走官方http请求,测试环境走终端
  179. var token string
  180. token, err = GetAccessToken(false, terminal.Value)
  181. if err != nil {
  182. return
  183. }
  184. return getFutureGoodDataFromThsHttp(edbCode, startDate, endDate, terminal.Value, token)
  185. } else {
  186. return getFutureGoodDataFromThsApp(edbCode, startDate, endDate, 0, terminal.ServerUrl)
  187. }
  188. }
  189. // getFutureGoodDataFromThsApp 通过终端获取wind的商品数据
  190. func getFutureGoodDataFromThsApp(edbCode, startDate, endDate string, num int, serverUrl string) (item future_good.FutureGoodDataFromThs, err error) {
  191. /*if utils.Hz_Ths_Data_Url == `` {
  192. err = errors.New("同花顺接口未配置")
  193. return
  194. }*/
  195. if serverUrl == "" {
  196. err = errors.New("同花顺接口未配置")
  197. return
  198. }
  199. thsUrl := serverUrl + `edbInfo/ths/future_good?EdbCode=%s&StartDate=%s&EndDate=%s`
  200. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  201. utils.FileLog.Info("thsUrl:" + thsUrl)
  202. body, err := http.Get(thsUrl)
  203. utils.FileLog.Info("ths result:" + string(body))
  204. if err != nil {
  205. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  206. return
  207. }
  208. if string(body) == "null" {
  209. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  210. return
  211. }
  212. tmpItems := new(FutureGoodDataFromThsInterface)
  213. err = json.Unmarshal(body, &tmpItems)
  214. if err != nil {
  215. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  216. return
  217. }
  218. if tmpItems.Errorcode != 0 {
  219. //session has expired,please re-login after using the system
  220. //如果是同花顺登录session失效了,那么就重新请求获取数据
  221. if tmpItems.Errorcode == -1020 && num == 0 {
  222. return getFutureGoodDataFromThsApp(edbCode, startDate, endDate, 1, serverUrl)
  223. }
  224. err = errors.New(string(body))
  225. return
  226. }
  227. if len(tmpItems.Tables) <= 0 {
  228. return
  229. }
  230. table := tmpItems.Tables[0]
  231. item = future_good.FutureGoodDataFromThs{
  232. DataVol: tmpItems.DataVol,
  233. Errmsg: tmpItems.Errmsg,
  234. Errorcode: tmpItems.Errorcode,
  235. Perf: tmpItems.Perf,
  236. Tables: future_good.FutureGoodDataTables{
  237. Time: table.Time,
  238. Open: table.Table.Open,
  239. High: table.Table.High,
  240. Low: table.Table.Low,
  241. Close: table.Table.Close,
  242. Volume: table.Table.Volume,
  243. Amount: table.Table.Amount,
  244. Ccl: table.Table.Ccl,
  245. Settlement: table.Table.Settlement,
  246. },
  247. }
  248. return
  249. }
  250. type StockDatas struct {
  251. Time string `json:"time"`
  252. ThsCode string `json:"thscode"`
  253. //OpenPriceStock *float64 `json:"ths_open_price_stock"`
  254. //HighPriceStock *float64 `json:"ths_high_price_stock"`
  255. //LowStock *float64 `json:"ths_low_stock,omitempty"`
  256. Value *float64
  257. }
  258. type TerminalResponse struct {
  259. ErrorCode int `json:"errorcode"`
  260. ErrMsg string `json:"errmsg"`
  261. Data []map[string]interface{} `json:"data"`
  262. }