浏览代码

Merge remote-tracking branch 'origin/eta/1.8.4' into debug

Roc 1 年之前
父节点
当前提交
b6fed81386
共有 6 个文件被更改,包括 216 次插入1 次删除
  1. 76 0
      controllers/edb.go
  2. 20 0
      models/data_manage/request.go
  3. 9 0
      routers/commentsRouter.go
  4. 91 0
      services/data/base_edb_lib.go
  5. 12 1
      utils/common.go
  6. 8 0
      utils/config.go

+ 76 - 0
controllers/edb.go

@@ -1,11 +1,13 @@
 package controllers
 
 import (
+	"encoding/json"
 	"eta/eta_hub/models"
 	"eta/eta_hub/models/data_manage"
 	"eta/eta_hub/services"
 	"eta/eta_hub/services/data"
 	"eta/eta_hub/utils"
+	"strings"
 )
 
 // EdbController 指标
@@ -277,3 +279,77 @@ func (this *EdbInfoController) TraceEdbInfo() {
 	br.Data = resp
 	br.Msg = "刷新成功"
 }
+
+// Push
+// @Title 指标数据
+// @Description 指标数据
+// @Success 200 {object} data_manage.EdbInfoItem
+// @router /push [post]
+func (this *EdbController) Push() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	body := this.Ctx.Input.RequestBody
+	var req data_manage.PushBusinessIndexReq
+	err := json.Unmarshal(body, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	if req.IndexCode == `` {
+		br.Msg = "指标编码不能为空"
+		return
+	}
+	if req.IndexName == `` {
+		br.Msg = "指标名称不能为空"
+		return
+	}
+	if req.Unit == `` {
+		br.Msg = "指标单位不能为空"
+		return
+	}
+	if req.Frequency == `` {
+		br.Msg = "指标频度不能为空"
+		return
+	}
+	if req.SourceName == `` {
+		br.Msg = "数据来源名称不能为空"
+		return
+	}
+
+	// 兼容频度缺少度的字段
+	if !strings.Contains(req.Frequency, "度") {
+		req.Frequency = req.Frequency + "度"
+	}
+	if !utils.VerifyFrequency(req.Frequency) {
+		br.Msg = "指标频度不合法:" + req.Frequency
+		return
+	}
+
+	reqJson, err := json.Marshal(req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+
+	respItem, err := data.PushEdb(string(reqJson))
+	if err != nil {
+		br.Msg = "处理失败"
+		br.ErrMsg = "处理失败,Err:" + err.Error()
+		return
+	}
+	if respItem.Ret != 200 {
+		br.Msg = respItem.Msg
+		br.ErrMsg = respItem.ErrMsg
+		return
+	}
+
+	//br.Data = edbData
+	br.Ret = 200
+	br.Msg = "处理成功"
+}

+ 20 - 0
models/data_manage/request.go

@@ -0,0 +1,20 @@
+package data_manage
+
+// PushBusinessIndexReq
+// @Description:  添加外部指标(商家)请求
+type PushBusinessIndexReq struct {
+	IndexCode  string               `description:"指标编码"`
+	IndexName  string               `description:"指标名称"`
+	Unit       string               `description:"单位"`
+	Frequency  string               `description:"频度"`
+	SourceName string               `description:"数据来源名称"`
+	Remark     string               `description:"备注字段"`
+	DataList   []AddBusinessDataReq `description:"指标数据"`
+}
+
+// AddBusinessDataReq
+// @Description: 外部指标(商家系统)数据
+type AddBusinessDataReq struct {
+	Value float64 `description:"值"`
+	Date  string  `description:"日期"`
+}

+ 9 - 0
routers/commentsRouter.go

@@ -88,6 +88,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_hub/controllers:EdbController"] = append(beego.GlobalControllerRouter["eta/eta_hub/controllers:EdbController"],
+        beego.ControllerComments{
+            Method: "Push",
+            Router: `/push`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_hub/controllers:EdbController"] = append(beego.GlobalControllerRouter["eta/eta_hub/controllers:EdbController"],
         beego.ControllerComments{
             Method: "SourceList",

+ 91 - 0
services/data/base_edb_lib.go

@@ -0,0 +1,91 @@
+package data
+
+import (
+	"encoding/json"
+	"eta/eta_hub/models"
+	"eta/eta_hub/utils"
+	"io"
+	"net/http"
+	"strings"
+)
+
+// PushEdb
+// @Description:处理外部(商家)指标的接口
+// @author: Roc
+// @datetime 2024-04-28 11:10:44
+// @param paramStr string
+// @return resp *models.BaseResponse
+// @return err error
+func PushEdb(paramStr string) (resp *models.BaseResponse, err error) {
+	_, resultByte, err := postEdbLib(paramStr, "/business_index/handle")
+	err = json.Unmarshal(resultByte, &resp)
+	if err != nil {
+		return
+	}
+	return
+}
+
+// postRefreshEdbData 刷新指标数据
+func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
+	postUrl := utils.EDB_LIB_URL + urlStr
+	postData, err := json.Marshal(param)
+	if err != nil {
+		return
+	}
+	result, err := HttpPost(postUrl, string(postData), "application/json")
+	if err != nil {
+		return
+	}
+	utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
+	err = json.Unmarshal(result, &resp)
+	if err != nil {
+		return
+	}
+	return resp, nil
+}
+
+// postEdbLib
+// @Description: post请求指标服务
+// @author: Roc
+// @datetime 2024-04-28 11:08:59
+// @param paramStr string
+// @param urlStr string
+// @return resp *models.BaseResponse
+// @return result []byte
+// @return err error
+func postEdbLib(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
+	postUrl := utils.EDB_LIB_URL + urlStr
+	result, err = HttpPost(postUrl, paramStr, "application/json")
+	if err != nil {
+		return
+	}
+	err = json.Unmarshal(result, &resp)
+	if err != nil {
+		return
+	}
+	
+	return
+}
+
+func HttpPost(url, postData string, params ...string) ([]byte, error) {
+	body := io.NopCloser(strings.NewReader(postData))
+	client := &http.Client{}
+	req, err := http.NewRequest("POST", url, body)
+	if err != nil {
+		return nil, err
+	}
+	contentType := "application/x-www-form-urlencoded;charset=utf-8"
+	if len(params) > 0 && params[0] != "" {
+		contentType = params[0]
+	}
+	req.Header.Set("Content-Type", contentType)
+	req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
+	resp, err := client.Do(req)
+	if err != nil {
+		return nil, err
+	}
+	defer resp.Body.Close()
+	b, err := io.ReadAll(resp.Body)
+	utils.FileLog.Debug("HttpPost:" + string(b))
+	return b, err
+}

+ 12 - 1
utils/common.go

@@ -1023,6 +1023,7 @@ func GetSign(nonce, timestamp string) (sign string) {
 	signStr = strings.Trim(signStr, "&")
 	fmt.Println("signStr:" + signStr)
 	sign = HmacSha256ToBase64(Secret, signStr)
+	fmt.Println("sign:" + sign)
 	return
 }
 
@@ -1153,4 +1154,14 @@ func (ms MapSorter) Swap(i, j int) {
 //	@return string
 func GetLikeKeyword(keyword string) string {
 	return `%` + keyword + `%`
-}
+}
+
+// VerifyFrequency
+// @Description: 校验频度是否合规
+// @author: Roc
+// @datetime 2024-04-26 13:30:22
+// @param frequency string 待校验的频度
+// @return bool
+func VerifyFrequency(frequency string) bool {
+	return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
+}

+ 8 - 0
utils/config.go

@@ -53,6 +53,10 @@ var (
 // 内部配置
 var (
 	EdbChartLibUrl string // 图库服务地址
+	// EDB_LIB_URL 公共指标库
+	EDB_LIB_URL         string
+	APP_EDB_LIB_NAME_EN string
+	EDB_LIB_Md5_KEY     string
 )
 
 func init() {
@@ -140,6 +144,10 @@ func init() {
 	// 内部配置
 	{
 		EdbChartLibUrl = config["edb_chart_lib_url"]
+		// 公共指标库相关
+		EDB_LIB_URL = config["edb_lib_url"]
+		APP_EDB_LIB_NAME_EN = config["app_edb_lib_name_en"]
+		EDB_LIB_Md5_KEY = config["edb_lib_md5_key"]
 	}
 
 }