123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package services
- import (
- "encoding/json"
- "errors"
- "eta/eta_mini_crm_ht/utils"
- httpClient "eta/eta_mini_crm_ht/utils/http"
- "fmt"
- "io"
- )
- const (
- GetIndexInfoUrl = "/htApi/v1/index/getIndexInfo"
- PushIndexInfoUrl = "/htApi/v1/index/push"
- DeleteIndexSyncTaskUrl = "/htApi/v1/index/deleteTask"
- )
- type IndexInfo struct {
- IndexCode string
- IndexName string
- Frequency string
- SourceName string
- LatestDataDate string
- StartDate string
- EndDate string
- LastPushTime string
- Remark string
- Unit string
- DataList []interface{}
- }
- type PushIndex struct {
- IndexCode string `json:"indexCode"`
- }
- type DeleteIndexSyncTask struct {
- IndexCode []string `json:"indexCode"`
- }
- type HTResponse struct {
- Code string `json:"code"`
- Success bool `json:"success"`
- Message string `json:"message"`
- Data json.RawMessage `json:"data"`
- }
- func GetIndexInfo(indexCode string) (indexInfo *IndexInfo, err error) {
- client := httpClient.DefaultClient()
- if utils.HT_ADDRESS == "" {
- err = errors.New("未配置海通钢联地址")
- return
- }
- url := fmt.Sprintf("%s%s?indexCode=%s", utils.HT_ADDRESS, GetIndexInfoUrl, indexCode)
- resp, err := client.Get(url)
- if err != nil {
- utils.FileLog.Error("获取钢联指标信息失败", err.Error())
- return
- }
- defer func(Body io.ReadCloser) {
- closeErr := Body.Close()
- if closeErr != nil {
- utils.FileLog.Error("关闭Response失败:%v", closeErr)
- }
- }(resp.Body)
- body, _ := io.ReadAll(resp.Body)
- var htRes HTResponse
- err = json.Unmarshal(body, &htRes)
- if err != nil {
- utils.FileLog.Error("解析应答信息失败:%v", err)
- return
- }
- if htRes.Success {
- err = json.Unmarshal(htRes.Data, &indexInfo)
- } else {
- err = errors.New(htRes.Message)
- }
- return
- }
- func PushIndexInfo(indexCode string) (err error) {
- client := httpClient.DefaultClient()
- if utils.HT_ADDRESS == "" {
- err = errors.New("未配置海通钢联地址")
- return
- }
- url := fmt.Sprintf("%s%s?indexCode=%s", utils.HT_ADDRESS, PushIndexInfoUrl, indexCode)
- resp, err := client.Post(url, &PushIndex{
- IndexCode: indexCode,
- })
- if err != nil {
- utils.FileLog.Error("获取钢联指标信息失败", err.Error())
- return
- }
- defer func(Body io.ReadCloser) {
- closeErr := Body.Close()
- if closeErr != nil {
- utils.FileLog.Error("关闭Response失败:%v", closeErr)
- }
- }(resp.Body)
- body, _ := io.ReadAll(resp.Body)
- var htRes HTResponse
- err = json.Unmarshal(body, &htRes)
- if err != nil {
- utils.FileLog.Error("解析应答信息失败:%v", err)
- return
- }
- if !htRes.Success {
- err = errors.New(htRes.Message)
- }
- return
- }
- func DeleteSyncTask(indexCodes []string) (err error) {
- client := httpClient.DefaultClient()
- if utils.HT_ADDRESS == "" {
- err = errors.New("未配置海通钢联地址")
- return
- }
- url := fmt.Sprintf("%s%s", utils.HT_ADDRESS, DeleteIndexSyncTaskUrl)
- resp, err := client.Post(url, &DeleteIndexSyncTask{
- IndexCode: indexCodes,
- })
- if err != nil {
- utils.FileLog.Error("停止钢联指标失败", err.Error())
- return
- }
- defer func(Body io.ReadCloser) {
- closeErr := Body.Close()
- if closeErr != nil {
- utils.FileLog.Error("关闭Response失败:%v", closeErr)
- }
- }(resp.Body)
- body, _ := io.ReadAll(resp.Body)
- var htRes HTResponse
- err = json.Unmarshal(body, &htRes)
- if err != nil {
- utils.FileLog.Error("解析应答信息失败:%v", err)
- return
- }
- if !htRes.Success {
- err = errors.New(htRes.Message)
- }
- return
- }
|