sync_edb_lib.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongtao3_watch/global"
  6. "hongze/hongtao3_watch/models"
  7. "hongze/hongtao3_watch/models/index"
  8. "hongze/hongtao3_watch/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. )
  13. // SyncEdbDataSci 同步红桃3的指标到ETA库
  14. func SyncEdbDataSci(baseFromSciIndex *index.BaseFromSciIndex) {
  15. indexObj := new(models.EdbInfo)
  16. var isAdd int
  17. item, err := indexObj.GetEdbInfoItem(baseFromSciIndex.IndexCode)
  18. if err != nil {
  19. if err == utils.ErrNoRow {
  20. isAdd = 1
  21. } else {
  22. isAdd = -1
  23. fmt.Println("GetEdbInfoItem Err:" + err.Error())
  24. return
  25. }
  26. }
  27. if item != nil && item.EdbInfoId > 0 {
  28. fmt.Println("item:", item)
  29. isAdd = 2
  30. } else {
  31. isAdd = 1 //
  32. }
  33. if isAdd == 1 { //新增
  34. return
  35. }
  36. param := make(map[string]interface{})
  37. param["EdbCode"] = baseFromSciIndex.IndexCode
  38. param["EdbInfoId"] = item.EdbInfoId
  39. param["StartDate"] = item.EndDate
  40. postRefreshEdbData(param)
  41. }
  42. type BaseResponse struct {
  43. Ret int
  44. Msg string
  45. ErrMsg string
  46. ErrCode string
  47. Data interface{}
  48. Success bool `description:"true 执行成功,false 执行失败"`
  49. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  50. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  51. }
  52. // postRefreshEdbData 刷新指标数据
  53. func postRefreshEdbData(param map[string]interface{}) (resp *BaseResponse, err error) {
  54. urlStr := "sci/refresh"
  55. EDB_LIB_URL := "http://47.102.213.75:8300/edbapi/"
  56. postUrl := EDB_LIB_URL + urlStr
  57. postData, err := json.Marshal(param)
  58. if err != nil {
  59. return
  60. }
  61. result, err := HttpPost(postUrl, string(postData), "application/json")
  62. if err != nil {
  63. return
  64. }
  65. if result != nil {
  66. global.LOG.Info(" Refresh Result: " + string(result))
  67. err = json.Unmarshal(result, &resp)
  68. if err != nil {
  69. return
  70. }
  71. return resp, nil
  72. }
  73. return nil, err
  74. }
  75. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  76. body := ioutil.NopCloser(strings.NewReader(postData))
  77. client := &http.Client{}
  78. req, err := http.NewRequest("POST", url, body)
  79. if err != nil {
  80. return nil, err
  81. }
  82. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  83. if len(params) > 0 && params[0] != "" {
  84. contentType = params[0]
  85. }
  86. req.Header.Set("Content-Type", contentType)
  87. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  88. resp, err := client.Do(req)
  89. if resp != nil {
  90. defer resp.Body.Close()
  91. b, err := ioutil.ReadAll(resp.Body)
  92. fmt.Println("HttpPost:" + string(b))
  93. return b, err
  94. }
  95. return nil, err
  96. }