base_from_ths.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 string) (item models.EdbDataFromThs, err error) {
  26. //if utils.BusinessCode == utils.HZ_TRIAL_BUSSINESS_CODE { // 体验版也走客户端
  27. // return getEdbDataFromThsApp(edbCode, startDate, endDate, 0)
  28. //}
  29. if utils.RunMode == `release` { // 生产环境走官方http请求,测试环境走终端
  30. return getEdbDataFromThsHttp(edbCode, startDate, endDate)
  31. } else {
  32. return getEdbDataFromThsApp(edbCode, startDate, endDate, 0)
  33. }
  34. }
  35. // getEdbDataFromThs 获取同花顺接口数据
  36. func getEdbDataFromThsApp(edbCode, startDate, endDate string, num int) (item models.EdbDataFromThs, err error) {
  37. if utils.Hz_Ths_Data_Url == `` {
  38. err = errors.New("同花顺接口未配置")
  39. return
  40. }
  41. thsUrl := utils.Hz_Ths_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
  42. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  43. utils.FileLog.Info("thsUrl:" + thsUrl)
  44. body, err := http.Get(thsUrl)
  45. utils.FileLog.Info("ths result:" + string(body))
  46. if err != nil {
  47. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  48. return
  49. }
  50. if string(body) == "null" {
  51. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  52. return
  53. }
  54. tmpItems := new(EdbDataFromThsInterface)
  55. err = json.Unmarshal(body, &tmpItems)
  56. if err != nil {
  57. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  58. return
  59. }
  60. if tmpItems.Errorcode != 0 {
  61. //session has expired,please re-login after using the system
  62. //如果是同花顺登录session失效了,那么就重新请求获取数据
  63. if tmpItems.Errorcode == -1020 && num == 0 {
  64. return getEdbDataFromThsApp(edbCode, startDate, endDate, 1)
  65. }
  66. err = errors.New(string(body))
  67. return
  68. }
  69. // 因为table里面的value有的时候返回的是string,有的是float64,所以需要用interface来反射取值
  70. tablesList := make([]models.Tables, 0)
  71. for _, table := range tmpItems.Tables {
  72. tableIdList := make([]string, 0)
  73. tableTimeList := make([]string, 0)
  74. tableValueList := make([]float64, 0)
  75. for _, tableId := range table.ID {
  76. tableIdList = append(tableIdList, tableId)
  77. }
  78. for _, tableTime := range table.Time {
  79. tableTimeList = append(tableTimeList, tableTime)
  80. }
  81. //指标数据
  82. for _, tmpValue := range table.Value {
  83. var tableValue float64
  84. if reflect.TypeOf(tmpValue).Kind() == reflect.Float64 {
  85. tableValue = reflect.ValueOf(tmpValue).Float()
  86. } else if reflect.TypeOf(tmpValue).Kind() == reflect.String {
  87. tmpTableValue, tmpErr := decimal.NewFromString(reflect.ValueOf(tmpValue).String())
  88. if tmpErr != nil {
  89. err = tmpErr
  90. return
  91. }
  92. tableValue, _ = tmpTableValue.Truncate(4).Float64()
  93. } else {
  94. err = errors.New("错误的数据类型" + reflect.TypeOf(tmpValue).String())
  95. return
  96. }
  97. tableValueList = append(tableValueList, tableValue)
  98. }
  99. tmpTable := models.Tables{
  100. ID: tableIdList,
  101. Time: tableTimeList,
  102. Value: tableValueList,
  103. }
  104. tablesList = append(tablesList, tmpTable)
  105. }
  106. item = models.EdbDataFromThs{
  107. DataVol: tmpItems.DataVol,
  108. Errmsg: tmpItems.Errmsg,
  109. Errorcode: tmpItems.Errorcode,
  110. Perf: tmpItems.Perf,
  111. Tables: tablesList,
  112. }
  113. return item, nil
  114. }
  115. // FutureGoodDataFromThsInterface 同花顺商品数据类型转为interface
  116. type FutureGoodDataFromThsInterface struct {
  117. Errmsg string `json:"errmsg"`
  118. Errorcode int64 `json:"errorcode"`
  119. DataVol int64 `json:"dataVol"`
  120. Perf interface{} `json:"perf"`
  121. Tables []struct {
  122. ThsCode string `json:"thscode"`
  123. Time []string `json:"time"`
  124. Table struct {
  125. LastClose []float64 `json:"lastclose"`
  126. Open []float64 `json:"open"`
  127. High []float64 `json:"high"`
  128. Low []float64 `json:"low"`
  129. Close []float64 `json:"close"`
  130. AvgPrice []float64 `json:"avgprice"`
  131. Change []float64 `json:"change"`
  132. ChangePer []float64 `json:"changeper"`
  133. Volume []float64 `json:"volume"`
  134. Amount []float64 `json:"amount"`
  135. Hsl []float64 `json:"hsl"`
  136. LastSettlement []float64 `json:"lastsettlement"`
  137. Settlement []float64 `json:"settlement"`
  138. ZdSettlement []float64 `json:"zdsettlement"`
  139. ZdfSettlement []float64 `json:"zdfsettlement"`
  140. Ccl []float64 `json:"ccl"`
  141. Ccbd []float64 `json:"ccbd"`
  142. Zf []float64 `json:"zf"`
  143. Zjlx []float64 `json:"zjlx"`
  144. Zjcd []float64 `json:"zjcd"`
  145. } `json:"table"`
  146. } `json:"tables"`
  147. }
  148. func GetFutureGoodDataFromThs(edbCode, startDate, endDate string) (item future_good.FutureGoodDataFromThs, err error) {
  149. if utils.RunMode == `release` { // 生产环境走官方http请求,测试环境走终端
  150. return getFutureGoodDataFromThsHttp(edbCode, startDate, endDate)
  151. } else {
  152. return getFutureGoodDataFromThsApp(edbCode, startDate, endDate, 0)
  153. }
  154. }
  155. // getFutureGoodDataFromThsApp 通过终端获取wind的商品数据
  156. func getFutureGoodDataFromThsApp(edbCode, startDate, endDate string, num int) (item future_good.FutureGoodDataFromThs, err error) {
  157. if utils.Hz_Ths_Data_Url == `` {
  158. err = errors.New("同花顺接口未配置")
  159. return
  160. }
  161. thsUrl := utils.Hz_Ths_Data_Url + `edbInfo/ths/future_good?EdbCode=%s&StartDate=%s&EndDate=%s`
  162. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  163. utils.FileLog.Info("thsUrl:" + thsUrl)
  164. body, err := http.Get(thsUrl)
  165. utils.FileLog.Info("ths result:" + string(body))
  166. if err != nil {
  167. err = errors.New(" Err:" + err.Error() + ";result:" + string(body))
  168. return
  169. }
  170. if string(body) == "null" {
  171. err = errors.New("同花顺数据获取异常:" + err.Error() + ";result:" + string(body))
  172. return
  173. }
  174. tmpItems := new(FutureGoodDataFromThsInterface)
  175. err = json.Unmarshal(body, &tmpItems)
  176. if err != nil {
  177. err = errors.New("GetEdbDataFromThs json.Unmarshal Err:" + err.Error())
  178. return
  179. }
  180. if tmpItems.Errorcode != 0 {
  181. //session has expired,please re-login after using the system
  182. //如果是同花顺登录session失效了,那么就重新请求获取数据
  183. if tmpItems.Errorcode == -1020 && num == 0 {
  184. return getFutureGoodDataFromThsApp(edbCode, startDate, endDate, 1)
  185. }
  186. err = errors.New(string(body))
  187. return
  188. }
  189. if len(tmpItems.Tables) <= 0 {
  190. return
  191. }
  192. table := tmpItems.Tables[0]
  193. item = future_good.FutureGoodDataFromThs{
  194. DataVol: tmpItems.DataVol,
  195. Errmsg: tmpItems.Errmsg,
  196. Errorcode: tmpItems.Errorcode,
  197. Perf: tmpItems.Perf,
  198. Tables: future_good.FutureGoodDataTables{
  199. Time: table.Time,
  200. Open: table.Table.Open,
  201. High: table.Table.High,
  202. Low: table.Table.Low,
  203. Close: table.Table.Close,
  204. Volume: table.Table.Volume,
  205. Amount: table.Table.Amount,
  206. Ccl: table.Table.Ccl,
  207. Settlement: table.Table.Settlement,
  208. },
  209. }
  210. return
  211. }