|
@@ -0,0 +1,252 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "eta_gn/eta_index_lib/models"
|
|
|
+ "eta_gn/eta_index_lib/services/alarm_msg"
|
|
|
+ "eta_gn/eta_index_lib/utils"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ BridgeApiGnIndexDataUrl = "/api/index_data/gn/edb/data/list" // 获取指标数据API
|
|
|
+ BridgeApiGnNewIndexUrl = "/api/index_data/gn/new_index" // 获取增量指标API
|
|
|
+ BridgeApiGnMenuListUrl = "/api/index_data/gn/menu_list" // 获取指标目录API
|
|
|
+)
|
|
|
+
|
|
|
+// GetGnIndexDataListFromBridge
|
|
|
+// @Description: 从桥接服务获取指标数据
|
|
|
+// @param param
|
|
|
+// @return indexDataList
|
|
|
+// @return err
|
|
|
+func GetGnIndexDataListFromBridge(param models.BridgeGnIndexDataParams) (indexDataList []models.BridgeGnIndexAndData, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ b, _ := json.Marshal(param)
|
|
|
+ tips := fmt.Sprintf("桥接服务-获取国能指标数据失败, err: %s, params: %s", err.Error(), string(b))
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiGnIndexDataUrl)
|
|
|
+ data, e := json.Marshal(param)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("data json marshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ body := io.NopCloser(strings.NewReader(string(data)))
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("POST", url, body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("Authorization", checkToken)
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ b, e := io.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(b) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(string(b))
|
|
|
+ // 生产环境解密
|
|
|
+ if utils.RunMode == "release" {
|
|
|
+ str := string(b)
|
|
|
+ str = strings.Trim(str, `"`)
|
|
|
+ b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(models.BridgeGnResultIndexData)
|
|
|
+ if e = json.Unmarshal(b, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Code != 200 {
|
|
|
+ err = fmt.Errorf("result: %s", string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexDataList = result.Data
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetGnNewIndexFromBridge 从桥接服务获取增量指标
|
|
|
+func GetGnNewIndexFromBridge() (indexData []models.BridgeJiaYueIndexAndData, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("桥接服务-获取国能增量指标失败, err: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiGnNewIndexUrl)
|
|
|
+ body := io.NopCloser(strings.NewReader(""))
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("POST", url, body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("Authorization", checkToken)
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ b, e := io.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(b) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 生产环境解密
|
|
|
+ if utils.RunMode == "release" {
|
|
|
+ str := string(b)
|
|
|
+ str = strings.Trim(str, `"`)
|
|
|
+ b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(models.BridgeJiaYueResultNewIndexData)
|
|
|
+ if e = json.Unmarshal(b, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Code != 200 {
|
|
|
+ err = fmt.Errorf("result: %s", string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexData = result.Data
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetGnMenuListFromBridge 从桥接服务获取指标目录列表
|
|
|
+func GetGnMenuListFromBridge() (indexData []models.BridgeJiaYueIndexMenuData, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tips := fmt.Sprintf("桥接服务-获取国能增量指标失败, err: %s", err.Error())
|
|
|
+ utils.FileLog.Info(tips)
|
|
|
+ go alarm_msg.SendAlarmMsg(tips, 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ url := fmt.Sprint(utils.EtaBridgeUrl, BridgeApiGnMenuListUrl)
|
|
|
+ body := io.NopCloser(strings.NewReader(""))
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("POST", url, body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("Authorization", checkToken)
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ b, e := io.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(b) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 生产环境解密
|
|
|
+ if utils.RunMode == "release" {
|
|
|
+ str := string(b)
|
|
|
+ str = strings.Trim(str, `"`)
|
|
|
+ b = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(models.BridgeJiaYueResultMenuListData)
|
|
|
+ if e = json.Unmarshal(b, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Code != 200 {
|
|
|
+ err = fmt.Errorf("result: %s", string(b))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexData = result.Data
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// TransGnFrequency 频度转换
|
|
|
+func TransGnFrequency(origin string) string {
|
|
|
+ mapping := map[string]string{
|
|
|
+ "日": "日度",
|
|
|
+ "周": "周度",
|
|
|
+ "旬": "旬度",
|
|
|
+ "半月": "旬度",
|
|
|
+ "月": "月度",
|
|
|
+ "季": "季度",
|
|
|
+ "半年": "半年度",
|
|
|
+ "年": "年度",
|
|
|
+ "日度": "日度",
|
|
|
+ "周度": "周度",
|
|
|
+ "旬度": "旬度",
|
|
|
+ "月度": "月度",
|
|
|
+ "季度": "季度",
|
|
|
+ "半年度": "半年度",
|
|
|
+ "年度": "年度",
|
|
|
+ }
|
|
|
+ return mapping[origin]
|
|
|
+}
|
|
|
+
|
|
|
+// GetGnParentMenusByMenu 获取指定目录的父级目录
|
|
|
+func GetGnParentMenusByMenu(menu models.BridgeJiaYueIndexMenuData, menus []models.BridgeJiaYueIndexMenuData, level int) (results []models.BridgeJiaYueIndexMenuWithLevel) {
|
|
|
+ results = make([]models.BridgeJiaYueIndexMenuWithLevel, 0)
|
|
|
+ for _, m := range menus {
|
|
|
+ if menu.ParentId == m.Id {
|
|
|
+ results = append(results, models.BridgeJiaYueIndexMenuWithLevel{
|
|
|
+ Level: level,
|
|
|
+ Menu: m,
|
|
|
+ })
|
|
|
+ ps := GetGnParentMenusByMenu(m, menus, level+1)
|
|
|
+ if len(ps) > 0 {
|
|
|
+ results = append(results, ps...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|