|
@@ -0,0 +1,304 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "eta/eta_mini_crm_ht/utils"
|
|
|
+ httpClient "eta/eta_mini_crm_ht/utils/http"
|
|
|
+ "fmt"
|
|
|
+ "github.com/rdlucklib/rdluck_tools/paging"
|
|
|
+ "io"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ SourceListUrl = "/v1/edb/business/edb/source/list"
|
|
|
+ DataListUrl = "/v1/edb/business/edb/list"
|
|
|
+ DataDetailUrl = "/v1/edb/business/edb/data/list"
|
|
|
+
|
|
|
+ DeleteIndexesUrl = "/v1/edb/business/edb/del"
|
|
|
+)
|
|
|
+
|
|
|
+//type IndexInfo struct {
|
|
|
+// IndexCode string `json:"indexCode"`
|
|
|
+// IndexName string `json:"indexName"`
|
|
|
+// Frequency string `json:"frequency"`
|
|
|
+// SourceName string `json:"sourceName"`
|
|
|
+// LatestDataDate string `json:"latestDataDate"`
|
|
|
+// LastPushTime string `json:"lastPushTime"`
|
|
|
+// Remark string `json:"remark"`
|
|
|
+// Unit string `json:"unit"`
|
|
|
+// DataList []interface{} `json:"dataList"`
|
|
|
+//}
|
|
|
+
|
|
|
+type ETAResponse struct {
|
|
|
+ Ret int
|
|
|
+ Msg string
|
|
|
+ ErrMsg string
|
|
|
+ ErrCode string
|
|
|
+ Data json.RawMessage
|
|
|
+}
|
|
|
+
|
|
|
+func createETARequest() (client *httpClient.HttpClient, err error) {
|
|
|
+ client = httpClient.DefaultClient()
|
|
|
+ signHeader, signErr := utils.GenerateSignatureAndHeaders()
|
|
|
+ if signErr != nil {
|
|
|
+ err = signErr
|
|
|
+ utils.FileLog.Error("创建eta请求接口失败,生成签名失败:%v", signErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ structMap, err := client.StructToMap(signHeader)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("创建eta请求接口失败,生成签名请求头失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ client.AddHeader(structMap)
|
|
|
+ return
|
|
|
+}
|
|
|
+func GetDataList(request IndexDataListRequest) (baseFromBusinessIndexDataResp *BaseFromBusinessIndexDataResp, err error) {
|
|
|
+ if utils.ETA_ADDRESS == "" {
|
|
|
+ err = errors.New("未配置ETA_API地址")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ url := fmt.Sprintf("%s%s?%s", utils.ETA_ADDRESS, DataDetailUrl, request.ToString())
|
|
|
+ client, err := createETARequest()
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("创建eta请求接口失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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 ETAResponse
|
|
|
+ err = json.Unmarshal(body, &htRes)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("解析应答信息失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if htRes.Ret == 200 {
|
|
|
+ err = json.Unmarshal(htRes.Data, &baseFromBusinessIndexDataResp)
|
|
|
+ } else {
|
|
|
+ err = errors.New(htRes.ErrMsg)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+func GetSourceList() (businessSourceList []*EdbBusinessSource, err error) {
|
|
|
+ if utils.ETA_ADDRESS == "" {
|
|
|
+ err = errors.New("未配置ETA_API地址")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ url := fmt.Sprintf("%s%s", utils.ETA_ADDRESS, SourceListUrl)
|
|
|
+ client, err := createETARequest()
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("创建eta请求接口失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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 ETAResponse
|
|
|
+ err = json.Unmarshal(body, &htRes)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("解析应答信息失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if htRes.Ret == 200 {
|
|
|
+ err = json.Unmarshal(htRes.Data, &businessSourceList)
|
|
|
+ } else {
|
|
|
+ err = errors.New(htRes.ErrMsg)
|
|
|
+ }
|
|
|
+ if err == nil {
|
|
|
+ var filterList []*EdbBusinessSource
|
|
|
+ for _, v := range businessSourceList {
|
|
|
+ if strings.HasPrefix(v.SourceName, "一期-") {
|
|
|
+ filterList = append(filterList, v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ businessSourceList = filterList
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+type IndexListRequest struct {
|
|
|
+ PageSize int
|
|
|
+ CurrentIndex int
|
|
|
+ SourceName string
|
|
|
+ Frequency string
|
|
|
+ SortColumn string
|
|
|
+ SortOrder string
|
|
|
+ KeyWord string
|
|
|
+ SysSource string
|
|
|
+}
|
|
|
+type IndexDataListRequest struct {
|
|
|
+ PageSize int
|
|
|
+ CurrentIndex int
|
|
|
+ IndexCode string
|
|
|
+}
|
|
|
+
|
|
|
+func (ilr *IndexListRequest) ToString() string {
|
|
|
+ return fmt.Sprintf("PageSize=%d&CurrentIndex=%d&SortColumn=%s&SortOrder=%s&SourceName=%s&KeyWord=%s&Frequency=%s&SysSource=%s",
|
|
|
+ ilr.PageSize, ilr.CurrentIndex, ilr.SortColumn, ilr.SortOrder, ilr.SourceName, ilr.KeyWord, ilr.Frequency, ilr.SysSource)
|
|
|
+}
|
|
|
+func (ilr *IndexDataListRequest) ToString() string {
|
|
|
+ return fmt.Sprintf("PageSize=%d&CurrentIndex=%d&IndexCode=%s",
|
|
|
+ ilr.PageSize, ilr.CurrentIndex, ilr.IndexCode)
|
|
|
+}
|
|
|
+
|
|
|
+func GetIndexList() (list []string, err error) {
|
|
|
+ indexReq := IndexListRequest{
|
|
|
+ PageSize: 500,
|
|
|
+ CurrentIndex: 1,
|
|
|
+ SysSource: "ht_mini_crm",
|
|
|
+ }
|
|
|
+ for {
|
|
|
+ resp, fetchErr := GetList(indexReq)
|
|
|
+ if fetchErr != nil {
|
|
|
+ utils.FileLog.Error("获取指标列表信息失败", fetchErr.Error())
|
|
|
+ err = errors.New("获取指标列表信息失败:" + fetchErr.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range resp.List {
|
|
|
+ list = append(list, v.IndexCode)
|
|
|
+ }
|
|
|
+ if indexReq.CurrentIndex*indexReq.PageSize >= resp.Paging.Totals {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexReq.CurrentIndex += 1
|
|
|
+ }
|
|
|
+}
|
|
|
+func GetList(indexRequest IndexListRequest) (businessIndexResp *BaseFromBusinessIndexResp, err error) {
|
|
|
+ if utils.ETA_ADDRESS == "" {
|
|
|
+ err = errors.New("未配置ETA_API地址")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ url := fmt.Sprintf("%s%s?%s", utils.ETA_ADDRESS, DataListUrl, indexRequest.ToString())
|
|
|
+ client, err := createETARequest()
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("创建eta请求接口失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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 ETAResponse
|
|
|
+ err = json.Unmarshal(body, &htRes)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("解析应答信息失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if htRes.Ret == 200 {
|
|
|
+ err = json.Unmarshal(htRes.Data, &businessIndexResp)
|
|
|
+ } else {
|
|
|
+ err = errors.New(htRes.ErrMsg)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+type EdbBusinessSource struct {
|
|
|
+ EdbBusinessSourceId int64 `orm:"column(edb_business_source_id);pk"`
|
|
|
+ SourceName string `description:"来源名称"` // 来源名称
|
|
|
+ CreateTime time.Time `description:"创建时间"` // 创建时间
|
|
|
+}
|
|
|
+type BaseFromBusinessIndexItem struct {
|
|
|
+ IndexCode string `description:"指标编码"`
|
|
|
+ IndexName string `description:"指标名称"`
|
|
|
+ Unit string `description:"单位"`
|
|
|
+ Frequency string `description:"频度"`
|
|
|
+ SourceName string `description:"数据来源名称"`
|
|
|
+ StartDate time.Time `description:"指标开始时间"`
|
|
|
+ EndDate time.Time `description:"指标最新时间"`
|
|
|
+ CreateTime time.Time `description:"入库时间"`
|
|
|
+}
|
|
|
+
|
|
|
+type BaseFromBusinessIndexDataItem struct {
|
|
|
+ ID string
|
|
|
+ EdbDataId int `orm:"column(edb_data_id);pk"`
|
|
|
+ IndexCode string
|
|
|
+ DataTime string
|
|
|
+ Value float64
|
|
|
+}
|
|
|
+type BaseFromBusinessIndexResp struct {
|
|
|
+ Paging *paging.PagingItem
|
|
|
+ List []*BaseFromBusinessIndexItem
|
|
|
+ LastUpdateTime string
|
|
|
+}
|
|
|
+type BaseFromBusinessIndexDataResp struct {
|
|
|
+ Paging *paging.PagingItem
|
|
|
+ List []*BaseFromBusinessIndexDataItem
|
|
|
+ LastUpdateTime string
|
|
|
+}
|
|
|
+type DelBusinessIndexResp struct {
|
|
|
+ IsDeleteEdbCodeList []string `description:"已经删除了的指标编码"`
|
|
|
+ NoDeleteEdbCodeList []string `description:"未删除的指标编码"`
|
|
|
+}
|
|
|
+
|
|
|
+type DelBusinessIndexReq struct {
|
|
|
+ IndexCodeList []string `description:"已经删除了的指标编码"`
|
|
|
+}
|
|
|
+
|
|
|
+func DeleteIndexes(indexCodes []string) (deleteBusinessIndexResp *DelBusinessIndexResp, err error) {
|
|
|
+ if utils.ETA_ADDRESS == "" {
|
|
|
+ err = errors.New("未配置ETA_API地址")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ url := fmt.Sprintf("%s%s", utils.ETA_ADDRESS, DeleteIndexesUrl)
|
|
|
+ client, err := createETARequest()
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("创建eta请求接口失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp, err := client.Post(url, &DelBusinessIndexReq{
|
|
|
+ IndexCodeList: 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 ETAResponse
|
|
|
+ err = json.Unmarshal(body, &htRes)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("解析应答信息失败:%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if htRes.Ret == 200 {
|
|
|
+ err = json.Unmarshal(htRes.Data, &deleteBusinessIndexResp)
|
|
|
+ } else {
|
|
|
+ err = errors.New(htRes.ErrMsg)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|