base_future_good_lib.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package future_good
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. type AddPredictEdbDataResponse struct {
  12. Ret int
  13. Msg string
  14. ErrMsg string
  15. ErrCode string
  16. Data data_manage.AddEdbInfoResp
  17. Success bool `description:"true 执行成功,false 执行失败"`
  18. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  19. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  20. }
  21. // AddEdbData 添加指标数据
  22. func AddEdbData(futureGoodEdbCode string) (resp *models.BaseResponse, err error) {
  23. param := make(map[string]interface{})
  24. param["EdbCode"] = futureGoodEdbCode
  25. urlStr := `future_good/add`
  26. resp, err = postRefreshEdbData(param, urlStr)
  27. return
  28. }
  29. // RefreshEdbData 刷新指标数据
  30. func RefreshEdbData(futureGoodEdbInfoId int, futureGoodEdbCode, startDate string) (resp *models.BaseResponse, err error) {
  31. param := make(map[string]interface{})
  32. param["FutureGoodEdbCode"] = futureGoodEdbCode
  33. param["FutureGoodEdbInfoId"] = futureGoodEdbInfoId
  34. param["StartDate"] = startDate
  35. urlStr := `future_good/refresh`
  36. resp, err = postRefreshEdbData(param, urlStr)
  37. return
  38. }
  39. // RefreshEdbRelation 刷新商品期货指标相关的数据
  40. func RefreshEdbRelation(chartInfoId int) (resp *models.BaseResponse, err error) {
  41. param := make(map[string]interface{})
  42. param["ChartInfoId"] = chartInfoId
  43. urlStr := `future_good/relation/refresh`
  44. resp, err = postRefreshEdbData(param, urlStr)
  45. return
  46. }
  47. // postRefreshEdbData 刷新指标数据
  48. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  49. postUrl := utils.EDB_LIB_URL + urlStr
  50. postData, err := json.Marshal(param)
  51. if err != nil {
  52. return
  53. }
  54. result, err := HttpPost(postUrl, string(postData), "application/json")
  55. if err != nil {
  56. return
  57. }
  58. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  59. err = json.Unmarshal(result, &resp)
  60. if err != nil {
  61. return
  62. }
  63. return resp, nil
  64. }
  65. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  66. body := ioutil.NopCloser(strings.NewReader(postData))
  67. client := &http.Client{}
  68. req, err := http.NewRequest("POST", url, body)
  69. if err != nil {
  70. return nil, err
  71. }
  72. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  73. if len(params) > 0 && params[0] != "" {
  74. contentType = params[0]
  75. }
  76. req.Header.Set("Content-Type", contentType)
  77. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  78. resp, err := client.Do(req)
  79. defer resp.Body.Close()
  80. b, err := ioutil.ReadAll(resp.Body)
  81. utils.FileLog.Debug("HttpPost:" + string(b))
  82. return b, err
  83. }