base_edb_lib.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models"
  5. "eta/eta_hub/utils"
  6. "io"
  7. "net/http"
  8. "strings"
  9. )
  10. type PushEdbResponse struct {
  11. Ret int
  12. Msg string
  13. ErrMsg string
  14. ErrCode string
  15. Data BaseFromBusinessIndexResp
  16. }
  17. // BaseFromBusinessIndexResp
  18. // @Description: 外部指标(商家系统)表
  19. type BaseFromBusinessIndexResp struct {
  20. IndexCode string `description:"指标编码"`
  21. IndexName string `description:"指标名称"`
  22. Unit string `description:"单位"`
  23. Frequency string `description:"频度"`
  24. SourceName string `description:"数据来源名称"`
  25. }
  26. // PushEdb
  27. // @Description:处理外部(商家)指标的接口
  28. // @author: Roc
  29. // @datetime 2024-04-28 11:10:44
  30. // @param paramStr string
  31. // @return resp *PushEdbResponse
  32. // @return err error
  33. func PushEdb(paramStr string) (resp *PushEdbResponse, err error) {
  34. _, resultByte, err := postEdbLib(paramStr, "/business_index/handle")
  35. err = json.Unmarshal(resultByte, &resp)
  36. if err != nil {
  37. return
  38. }
  39. return
  40. }
  41. type DelBusinessIndexResponse struct {
  42. Ret int
  43. Msg string
  44. ErrMsg string
  45. ErrCode string
  46. Data DelBusinessIndexResp
  47. }
  48. // DelBusinessIndexResp
  49. // @Description: 外部指标(商家系统)表
  50. type DelBusinessIndexResp struct {
  51. IsDeleteEdbCodeList []string `description:"已经删除了的指标编码"`
  52. NoDeleteEdbCodeList []string `description:"未删除的指标编码"`
  53. }
  54. // DelBusinessIndex
  55. // @Description: 删除外部(商家)指标的接口
  56. // @author: Roc
  57. // @datetime 2024-5-31 15:26:19
  58. // @param paramStr string
  59. // @return resp *models.BaseResponse
  60. // @return err error
  61. func DelBusinessIndex(paramStr string) (resp *DelBusinessIndexResponse, err error) {
  62. _, resultByte, err := postEdbLib(paramStr, "/business_index/index/del")
  63. err = json.Unmarshal(resultByte, &resp)
  64. if err != nil {
  65. return
  66. }
  67. return
  68. }
  69. // DelBusinessIndexData
  70. // @Description:删除外部(商家)指标的明细数据接口
  71. // @author: Roc
  72. // @datetime 2024-5-31 15:26:19
  73. // @param paramStr string
  74. // @return resp *models.BaseResponse
  75. // @return err error
  76. func DelBusinessIndexData(paramStr string) (resp *models.BaseResponse, err error) {
  77. _, resultByte, err := postEdbLib(paramStr, "/business_index/data/del")
  78. err = json.Unmarshal(resultByte, &resp)
  79. if err != nil {
  80. return
  81. }
  82. return
  83. }
  84. // postRefreshEdbData 刷新指标数据
  85. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  86. postUrl := utils.EDB_LIB_URL + urlStr
  87. postData, err := json.Marshal(param)
  88. if err != nil {
  89. return
  90. }
  91. result, err := HttpPost(postUrl, string(postData), "application/json")
  92. if err != nil {
  93. return
  94. }
  95. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  96. err = json.Unmarshal(result, &resp)
  97. if err != nil {
  98. return
  99. }
  100. return resp, nil
  101. }
  102. // postEdbLib
  103. // @Description: post请求指标服务
  104. // @author: Roc
  105. // @datetime 2024-04-28 11:08:59
  106. // @param paramStr string
  107. // @param urlStr string
  108. // @return resp *models.BaseResponse
  109. // @return result []byte
  110. // @return err error
  111. func postEdbLib(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  112. postUrl := utils.EDB_LIB_URL + urlStr
  113. result, err = HttpPost(postUrl, paramStr, "application/json")
  114. if err != nil {
  115. return
  116. }
  117. err = json.Unmarshal(result, &resp)
  118. if err != nil {
  119. return
  120. }
  121. return
  122. }
  123. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  124. body := io.NopCloser(strings.NewReader(postData))
  125. client := &http.Client{}
  126. req, err := http.NewRequest("POST", url, body)
  127. if err != nil {
  128. return nil, err
  129. }
  130. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  131. if len(params) > 0 && params[0] != "" {
  132. contentType = params[0]
  133. }
  134. req.Header.Set("Content-Type", contentType)
  135. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  136. resp, err := client.Do(req)
  137. if err != nil {
  138. return nil, err
  139. }
  140. defer resp.Body.Close()
  141. b, err := io.ReadAll(resp.Body)
  142. utils.FileLog.Debug("HttpPost:" + string(b))
  143. return b, err
  144. }