base_edb_lib.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if err != nil {
  36. return
  37. }
  38. err = json.Unmarshal(resultByte, &resp)
  39. if err != nil {
  40. return
  41. }
  42. return
  43. }
  44. type DelBusinessIndexResponse struct {
  45. Ret int
  46. Msg string
  47. ErrMsg string
  48. ErrCode string
  49. Data DelBusinessIndexResp
  50. }
  51. // DelBusinessIndexResp
  52. // @Description: 外部指标(商家系统)表
  53. type DelBusinessIndexResp struct {
  54. IsDeleteEdbCodeList []string `description:"已经删除了的指标编码"`
  55. NoDeleteEdbCodeList []string `description:"未删除的指标编码"`
  56. }
  57. // DelBusinessIndex
  58. // @Description: 删除外部(商家)指标的接口
  59. // @author: Roc
  60. // @datetime 2024-5-31 15:26:19
  61. // @param paramStr string
  62. // @return resp *models.BaseResponse
  63. // @return err error
  64. func DelBusinessIndex(paramStr string) (resp *DelBusinessIndexResponse, err error) {
  65. _, resultByte, err := postEdbLib(paramStr, "/business_index/index/del")
  66. if err != nil {
  67. return
  68. }
  69. err = json.Unmarshal(resultByte, &resp)
  70. if err != nil {
  71. return
  72. }
  73. return
  74. }
  75. // DelBusinessIndexData
  76. // @Description:删除外部(商家)指标的明细数据接口
  77. // @author: Roc
  78. // @datetime 2024-5-31 15:26:19
  79. // @param paramStr string
  80. // @return resp *models.BaseResponse
  81. // @return err error
  82. func DelBusinessIndexData(paramStr string) (resp *models.BaseResponse, err error) {
  83. _, resultByte, err := postEdbLib(paramStr, "/business_index/data/del")
  84. if err != nil {
  85. return
  86. }
  87. err = json.Unmarshal(resultByte, &resp)
  88. if err != nil {
  89. return
  90. }
  91. return
  92. }
  93. // postRefreshEdbData 刷新指标数据
  94. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  95. postUrl := utils.EDB_LIB_URL + urlStr
  96. postData, err := json.Marshal(param)
  97. if err != nil {
  98. return
  99. }
  100. result, err := HttpPost(postUrl, string(postData), "application/json")
  101. if err != nil {
  102. return
  103. }
  104. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  105. err = json.Unmarshal(result, &resp)
  106. if err != nil {
  107. return
  108. }
  109. return resp, nil
  110. }
  111. // postEdbLib
  112. // @Description: post请求指标服务
  113. // @author: Roc
  114. // @datetime 2024-04-28 11:08:59
  115. // @param paramStr string
  116. // @param urlStr string
  117. // @return resp *models.BaseResponse
  118. // @return result []byte
  119. // @return err error
  120. func postEdbLib(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  121. postUrl := utils.EDB_LIB_URL + urlStr
  122. result, err = HttpPost(postUrl, paramStr, "application/json")
  123. if err != nil {
  124. return
  125. }
  126. err = json.Unmarshal(result, &resp)
  127. if err != nil {
  128. return
  129. }
  130. return
  131. }
  132. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  133. body := io.NopCloser(strings.NewReader(postData))
  134. client := &http.Client{}
  135. req, err := http.NewRequest("POST", url, body)
  136. if err != nil {
  137. return nil, err
  138. }
  139. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  140. if len(params) > 0 && params[0] != "" {
  141. contentType = params[0]
  142. }
  143. req.Header.Set("Content-Type", contentType)
  144. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  145. resp, err := client.Do(req)
  146. if err != nil {
  147. return nil, err
  148. }
  149. defer resp.Body.Close()
  150. b, err := io.ReadAll(resp.Body)
  151. utils.FileLog.Debug("HttpPost:" + string(b))
  152. return b, err
  153. }