base_future_good_lib.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package future_good
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_task/models"
  5. "hongze/hongze_task/utils"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. )
  10. // AddEdbData 添加指标数据
  11. func AddEdbData(futureGoodEdbCode string) (resp *models.BaseResponse, err error) {
  12. param := make(map[string]interface{})
  13. param["EdbCode"] = futureGoodEdbCode
  14. urlStr := `future_good/add`
  15. resp, err = postRefreshEdbData(param, urlStr)
  16. return
  17. }
  18. // RefreshEdbData 刷新指标数据
  19. func RefreshEdbData(futureGoodEdbInfoId int, futureGoodEdbCode, startDate string) (resp *models.BaseResponse, err error) {
  20. param := make(map[string]interface{})
  21. param["FutureGoodEdbCode"] = futureGoodEdbCode
  22. param["FutureGoodEdbInfoId"] = futureGoodEdbInfoId
  23. param["StartDate"] = startDate
  24. urlStr := `future_good/refresh`
  25. resp, err = postRefreshEdbData(param, urlStr)
  26. return
  27. }
  28. // RefreshEdbRelation 刷新商品期货指标相关的数据
  29. func RefreshEdbRelation(futureGoodEdbInfoId int) (resp *models.BaseResponse, err error) {
  30. param := make(map[string]interface{})
  31. param["FutureGoodEdbInfoId"] = futureGoodEdbInfoId
  32. urlStr := `future_good/relation/refresh`
  33. resp, err = postRefreshEdbData(param, urlStr)
  34. return
  35. }
  36. // postRefreshEdbData 刷新指标数据
  37. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  38. postUrl := utils.EDB_LIB_URL + urlStr
  39. postData, err := json.Marshal(param)
  40. if err != nil {
  41. return
  42. }
  43. result, err := HttpPost(postUrl, string(postData), "application/json")
  44. if err != nil {
  45. return
  46. }
  47. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  48. err = json.Unmarshal(result, &resp)
  49. if err != nil {
  50. return
  51. }
  52. return resp, nil
  53. }
  54. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  55. body := ioutil.NopCloser(strings.NewReader(postData))
  56. client := &http.Client{}
  57. req, err := http.NewRequest("POST", url, body)
  58. if err != nil {
  59. return nil, err
  60. }
  61. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  62. if len(params) > 0 && params[0] != "" {
  63. contentType = params[0]
  64. }
  65. req.Header.Set("Content-Type", contentType)
  66. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  67. resp, err := client.Do(req)
  68. defer resp.Body.Close()
  69. b, err := ioutil.ReadAll(resp.Body)
  70. //fmt.Println("HttpPost:" + string(b))
  71. return b, err
  72. }