ht_service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mini_crm_ht/utils"
  6. httpClient "eta/eta_mini_crm_ht/utils/http"
  7. "fmt"
  8. "io"
  9. )
  10. const (
  11. GetIndexInfoUrl = "/htApi/v1/index/getIndexInfo"
  12. PushIndexInfoUrl = "/htApi/v1/index/push"
  13. DeleteIndexSyncTaskUrl = "/htApi/v1/index/deleteTask"
  14. )
  15. type IndexInfo struct {
  16. IndexCode string
  17. IndexName string
  18. Frequency string
  19. SourceName string
  20. LatestDataDate string
  21. StartDate string
  22. EndDate string
  23. LastPushTime string
  24. Remark string
  25. Unit string
  26. DataList []interface{}
  27. }
  28. type PushIndex struct {
  29. IndexCode string `json:"indexCode"`
  30. }
  31. type DeleteIndexSyncTask struct {
  32. IndexCode []string `json:"indexCode"`
  33. }
  34. type HTResponse struct {
  35. Code string `json:"code"`
  36. Success bool `json:"success"`
  37. Message string `json:"message"`
  38. Data json.RawMessage `json:"data"`
  39. }
  40. func GetIndexInfo(indexCode string) (indexInfo *IndexInfo, err error) {
  41. client := httpClient.DefaultClient()
  42. if utils.HT_ADDRESS == "" {
  43. err = errors.New("未配置海通钢联地址")
  44. return
  45. }
  46. url := fmt.Sprintf("%s%s?indexCode=%s", utils.HT_ADDRESS, GetIndexInfoUrl, indexCode)
  47. resp, err := client.Get(url)
  48. if err != nil {
  49. utils.FileLog.Error("获取钢联指标信息失败", err.Error())
  50. return
  51. }
  52. defer func(Body io.ReadCloser) {
  53. closeErr := Body.Close()
  54. if closeErr != nil {
  55. utils.FileLog.Error("关闭Response失败:%v", closeErr)
  56. }
  57. }(resp.Body)
  58. body, _ := io.ReadAll(resp.Body)
  59. var htRes HTResponse
  60. err = json.Unmarshal(body, &htRes)
  61. if err != nil {
  62. utils.FileLog.Error("解析应答信息失败:%v", err)
  63. return
  64. }
  65. if htRes.Success {
  66. err = json.Unmarshal(htRes.Data, &indexInfo)
  67. } else {
  68. err = errors.New(htRes.Message)
  69. }
  70. return
  71. }
  72. func PushIndexInfo(indexCode string) (err error) {
  73. client := httpClient.DefaultClient()
  74. if utils.HT_ADDRESS == "" {
  75. err = errors.New("未配置海通钢联地址")
  76. return
  77. }
  78. url := fmt.Sprintf("%s%s?indexCode=%s", utils.HT_ADDRESS, PushIndexInfoUrl, indexCode)
  79. resp, err := client.Post(url, &PushIndex{
  80. IndexCode: indexCode,
  81. })
  82. if err != nil {
  83. utils.FileLog.Error("获取钢联指标信息失败", err.Error())
  84. return
  85. }
  86. defer func(Body io.ReadCloser) {
  87. closeErr := Body.Close()
  88. if closeErr != nil {
  89. utils.FileLog.Error("关闭Response失败:%v", closeErr)
  90. }
  91. }(resp.Body)
  92. body, _ := io.ReadAll(resp.Body)
  93. var htRes HTResponse
  94. err = json.Unmarshal(body, &htRes)
  95. if err != nil {
  96. utils.FileLog.Error("解析应答信息失败:%v", err)
  97. return
  98. }
  99. if !htRes.Success {
  100. err = errors.New(htRes.Message)
  101. }
  102. return
  103. }
  104. func DeleteSyncTask(indexCodes []string) (err error) {
  105. client := httpClient.DefaultClient()
  106. if utils.HT_ADDRESS == "" {
  107. err = errors.New("未配置海通钢联地址")
  108. return
  109. }
  110. url := fmt.Sprintf("%s%s", utils.HT_ADDRESS, DeleteIndexSyncTaskUrl)
  111. resp, err := client.Post(url, &DeleteIndexSyncTask{
  112. IndexCode: indexCodes,
  113. })
  114. if err != nil {
  115. utils.FileLog.Error("停止钢联指标失败", err.Error())
  116. return
  117. }
  118. defer func(Body io.ReadCloser) {
  119. closeErr := Body.Close()
  120. if closeErr != nil {
  121. utils.FileLog.Error("关闭Response失败:%v", closeErr)
  122. }
  123. }(resp.Body)
  124. body, _ := io.ReadAll(resp.Body)
  125. var htRes HTResponse
  126. err = json.Unmarshal(body, &htRes)
  127. if err != nil {
  128. utils.FileLog.Error("解析应答信息失败:%v", err)
  129. return
  130. }
  131. if !htRes.Success {
  132. err = errors.New(htRes.Message)
  133. }
  134. return
  135. }