base_edb_lib.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_task/models"
  5. "eta_gn/eta_task/models/data_manage"
  6. "eta_gn/eta_task/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. func RefreshEdbData(edbInfoId, source, subSource int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  13. param := make(map[string]interface{})
  14. param["EdbCode"] = edbCode
  15. param["EdbInfoId"] = edbInfoId
  16. param["StartDate"] = startDate
  17. param["Source"] = source
  18. urlStr := ``
  19. urlStr = data_manage.GetEdbSourceRefreshMethodBySourceId(source) // 没有对应的从edb_source中取
  20. if urlStr == "" {
  21. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  22. return
  23. }
  24. resp, err = postRefreshEdbData(param, urlStr)
  25. return
  26. }
  27. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  28. param := make(map[string]interface{})
  29. param["EdbCode"] = edbCode
  30. param["EdbInfoId"] = edbInfoId
  31. param["StartDate"] = startDate
  32. resp, err = postRefreshEdbData(param, "calculate/refresh")
  33. return
  34. }
  35. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  36. param := make(map[string]interface{})
  37. param["EdbCode"] = edbCode
  38. param["EdbInfoId"] = edbInfoId
  39. param["StartDate"] = startDate
  40. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  41. return
  42. }
  43. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  44. postUrl := utils.EDB_LIB_URL + urlStr
  45. postData, err := json.Marshal(param)
  46. if err != nil {
  47. return
  48. }
  49. result, err := HttpPost(postUrl, string(postData), "application/json")
  50. if err != nil {
  51. return
  52. }
  53. err = json.Unmarshal(result, &resp)
  54. if err != nil {
  55. return
  56. }
  57. return resp, nil
  58. }
  59. func SetEdbSourceStat() (resp *models.BaseResponse, err error) {
  60. postUrl := utils.EDB_LIB_URL + "edb_stat/source_update"
  61. result, err := HttpPost(postUrl, "", "application/json")
  62. if err != nil {
  63. return
  64. }
  65. err = json.Unmarshal(result, &resp)
  66. if err != nil {
  67. return
  68. }
  69. return resp, nil
  70. }
  71. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  72. body := ioutil.NopCloser(strings.NewReader(postData))
  73. client := &http.Client{}
  74. req, err := http.NewRequest("POST", url, body)
  75. if err != nil {
  76. return nil, err
  77. }
  78. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  79. if len(params) > 0 && params[0] != "" {
  80. contentType = params[0]
  81. }
  82. req.Header.Set("Content-Type", contentType)
  83. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  84. resp, err := client.Do(req)
  85. if err != nil {
  86. return nil, err
  87. }
  88. defer resp.Body.Close()
  89. b, err := ioutil.ReadAll(resp.Body)
  90. return b, err
  91. }