Przeglądaj źródła

fix:指标同步接口实现

Roc 1 rok temu
rodzic
commit
2d77b3f81e

+ 3 - 0
config/config.go

@@ -93,4 +93,7 @@ type Xiangyu struct {
 	UserAuthHost         string `mapstructure:"user-auth-host" json:"user-auth-host" yaml:"user-auth-host" description:"用户认证平台地址"`
 	UserKey              string `mapstructure:"user-key" json:"user-key" yaml:"user-key" description:"统一平台秘钥"`
 	DefaultRoleId        int    `mapstructure:"default-role-id" json:"default-role-id" yaml:"default-role-id" description:"默认的角色id"`
+	IndexSyncHost        string `mapstructure:"index-sync-host" json:"index-sync-host" yaml:"index-sync-host" description:"指标同步平台地址"`
+	IndexSyncAuth        string `mapstructure:"index-sync-auth" json:"index-sync-auth" yaml:"index-sync-auth" description:"指标同步平台鉴权token"`
+	IndexSyncTarget      string `mapstructure:"index-sync-target" json:"index-sync-target" yaml:"index-sync-target" description:"指标同步平台编码"`
 }

+ 39 - 0
controller/xiangyu/index.go

@@ -0,0 +1,39 @@
+package xiangyu
+
+import (
+	"eta/eta_bridge/controller/resp"
+	"eta/eta_bridge/global"
+	"eta/eta_bridge/logic/xiangyu"
+	xiangyuSrc "eta/eta_bridge/services/xiangyu"
+	"github.com/gin-gonic/gin"
+	"github.com/go-playground/validator/v10"
+)
+
+// PushData
+// @Description: 数据推送
+// @author: Roc
+// @receiver xc
+// @datetime 2024-02-27 17:53:24
+// @param c *gin.Context
+func (xc *XiangyuController) PushData(c *gin.Context) {
+	var req xiangyuSrc.PushDataParamReq
+	if e := c.Bind(&req); e != nil {
+		err, ok := e.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+e.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", err.Translate(global.Trans), c)
+		return
+	}
+
+	err, errMsg := xiangyu.PushData(req)
+
+	if err != nil {
+		resp.FailData(errMsg, err.Error(), c)
+		return
+	}
+	resp.OkData("同步成功", "", c)
+
+	return
+}

+ 24 - 0
logic/xiangyu/index.go

@@ -0,0 +1,24 @@
+package xiangyu
+
+import (
+	"eta/eta_bridge/services/xiangyu"
+)
+
+// LoginEta
+// @Description: 获取eta的session
+// @author: Roc
+// @datetime 2024-01-23 17:44:15
+// @param code string
+// @return resp response.LoginResp
+// @return err error
+// @return errMsg string
+func PushData(data xiangyu.PushDataParamReq) (err error, errMsg string) {
+	//  获取xiangyu token
+
+	errMsg = `同步失败`
+	data.TableCode = "CY_DATA_ASSETS_INFO" // 固定字段
+	_, err = xiangyu.PushData(data)
+	//fmt.Println(resp)
+
+	return
+}

+ 52 - 0
models/request/xiangyu/index.go

@@ -0,0 +1,52 @@
+package xiangyu
+
+// PushDataParamReq
+// @Description: 业务报文
+type PushDataParamReq struct {
+	SerialID    string                 `json:"serialID" description:"流水号"`
+	TableCode   string                 `json:"tableCode" description:"数据表编码"`
+	Total       int                    `json:"total" description:"本次落表数据总数"`
+	IsEmailWarn int                    `json:"isEmailWarn" description:"是否发送预警邮件,(1-是 0-否)"`
+	Data        []PushDataParamDataReq `json:"data" description:"报文体,指标数据列表"`
+}
+
+// PushDataParamDataReq
+// @Description: 指标数据结构
+type PushDataParamDataReq struct {
+	SourceIndexCode      string `json:"source_index_code" description:"上游来源指标ID"`
+	IndexCode            string `json:"Index_code" description:""`
+	IndexName            string `json:"index_name" description:""`
+	IndexShortName       string `json:"index_short_name" description:""`
+	FrequenceName        string `json:"frequence_name" description:""`
+	UnitName             string `json:"unit_name" description:""`
+	CountryName          string `json:"country_name" description:""`
+	ProvinceName         string `json:"province_name" description:""`
+	AreaName             string `json:"area_name" description:""`
+	CityName             string `json:"city_name" description:""`
+	CountyName           string `json:"county_name" description:""`
+	RegionName           string `json:"region_name" description:""`
+	CompanyName          string `json:"company_name" description:""`
+	BreedName            string `json:"breed_name" description:""`
+	MaterialName         string `json:"material_name" description:""`
+	SpecName             string `json:"spec_name" description:""`
+	MarketName           string `json:"market_name" description:""`
+	DerivativeType       string `json:"derivative_type" description:""`
+	ContractName         string `json:"contract_name" description:""`
+	AuthKindName         string `json:"auth_kind_name" description:""`
+	CustomSmallClassName string `json:"custom_small_class_name" description:""`
+	AssetBeginDate       string `json:"asset_begin_date" description:""`
+	AssetEndDate         string `json:"asset_end_date" description:""`
+	CreateUser           string `json:"create_user" description:""`
+	IndexCreateTime      string `json:"index_create_time" description:""`
+	UpdateUser           string `json:"update_user" description:""`
+	DetailUpdateTime     string `json:"detail_update_time" description:""`
+	IndexUpdateTime      string `json:"index_update_time" description:""`
+	DutyDept             string `json:"duty_dept" description:""`
+	BusinessDept         string `json:"business_dept" description:""`
+	OrginSource          string `json:"orgin_source" description:""`
+	OrginSysSource       string `json:"orgin_sys_source" description:""`
+	SysSource            string `json:"sys_source" description:""`
+	SourceType           string `json:"source_type" description:""`
+	EtlTime              string `json:"etl_time" description:""`
+	Status               int    `json:"status" description:""`
+}

+ 9 - 0
routers/xiangyu.go

@@ -15,6 +15,9 @@ func InitXiangyu(r *gin.RouterGroup) {
 
 	// 需要内部鉴权的接口
 	initAuthXiangyu(r)
+
+	// 指标接口
+	initIndexXiangyu(r)
 }
 
 func initAuthXiangyu(r *gin.RouterGroup) {
@@ -24,3 +27,9 @@ func initAuthXiangyu(r *gin.RouterGroup) {
 	group.GET("auth/refreshToken", control.RefreshToken)
 	group.GET("auth/revokeToken", control.RevokeToken)
 }
+
+func initIndexXiangyu(r *gin.RouterGroup) {
+	control := new(xiangyu.XiangyuController)
+	group := r.Group("xy/").Use(middleware.InternalToken())
+	group.POST("index/pushData", control.PushData)
+}

+ 183 - 0
services/xiangyu/index.go

@@ -0,0 +1,183 @@
+package xiangyu
+
+import (
+	"encoding/json"
+	"errors"
+	"eta/eta_bridge/global"
+	"fmt"
+	"io"
+	"net/http"
+	"strings"
+)
+
+// IndexErrResp
+// @Description: 错误信息返回
+type IndexErrResp struct {
+	ErrCode string `json:"errcode" description:"失败编码 1001:缺少参数client_id, 2001:缺少参数access_token, 2006:缺少参数uid, 2002:参数access _token,不正确或过期, 1005:参数client_id非法"`
+	Msg     string `json:"msg" description:"失败信息"`
+}
+
+// PushResp
+// @Description: push数据返回
+type PushResp struct {
+	IndexErrResp
+	AccessToken  string `json:"access_token"`
+	RefreshToken string `json:"refresh_token"`
+	Uid          string `json:"uid"`
+	ExpiresIn    int    `json:"expires_in"`
+}
+
+type PushDataReq struct {
+	In0        In0              `json:"in0"`
+	Parameters PushDataParamReq `json:"parameters" description:"业务报文"`
+}
+
+// PushDataParamReq
+// @Description: 业务报文
+type PushDataParamReq struct {
+	SerialID    string                 `json:"serialID" description:"流水号"`
+	TableCode   string                 `json:"tableCode" description:"数据表编码"`
+	Total       int                    `json:"total" description:"本次落表数据总数"`
+	IsEmailWarn int                    `json:"isEmailWarn" description:"是否发送预警邮件,(1-是 0-否)"`
+	Data        []PushDataParamDataReq `json:"data" description:"报文体,指标数据列表"`
+}
+
+// PushDataParamDataReq
+// @Description: 指标数据结构
+type PushDataParamDataReq struct {
+	SourceIndexCode string `json:"source_index_code" description:"上游来源指标ID"`
+	IndexCode       string `json:"Index_code" description:""`
+	IndexName       string `json:"index_name" description:""`
+	IndexShortName  string `json:"index_short_name" description:""`
+	FrequenceName   string `json:"frequence_name" description:""`
+	UnitName        string `json:"unit_name" description:""`
+	//CountryName          string `json:"country_name" description:""`
+	//ProvinceName         string `json:"province_name" description:""`
+	//AreaName             string `json:"area_name" description:""`
+	//CityName             string `json:"city_name" description:""`
+	//CountyName           string `json:"county_name" description:""`
+	//RegionName           string `json:"region_name" description:""`
+	//CompanyName          string `json:"company_name" description:""`
+	//BreedName            string `json:"breed_name" description:""`
+	//MaterialName         string `json:"material_name" description:""`
+	//SpecName             string `json:"spec_name" description:""`
+	//MarketName           string `json:"market_name" description:""`
+	//DerivativeType       string `json:"derivative_type" description:""`
+	//ContractName         string `json:"contract_name" description:""`
+	//AuthKindName         string `json:"auth_kind_name" description:""`
+	//CustomSmallClassName string `json:"custom_small_class_name" description:""`
+	AssetBeginDate   string `json:"asset_begin_date" description:""`
+	AssetEndDate     string `json:"asset_end_date" description:""`
+	CreateUser       string `json:"create_user" description:""`
+	IndexCreateTime  string `json:"index_create_time" description:""`
+	UpdateUser       string `json:"update_user" description:""`
+	DetailUpdateTime string `json:"detail_update_time" description:""`
+	IndexUpdateTime  string `json:"index_update_time" description:""`
+	//DutyDept         string `json:"duty_dept" description:""`
+	//BusinessDept     string `json:"business_dept" description:""`
+	OrginSource    string `json:"orgin_source" description:""`
+	OrginSysSource string `json:"orgin_sys_source" description:""`
+	SysSource      string `json:"sys_source" description:""`
+	SourceType     string `json:"source_type" description:""`
+	Status         int    `json:"status" description:""`
+}
+
+type PushDataResp struct {
+	Out struct {
+		ReturnCode string `json:"returnCode"`
+		ReturnMsg  string `json:"returnMsg"`
+		Data       int    `json:"data"`
+	} `json:"out"`
+}
+
+// GetToken
+// @Description: 获取token信息
+// @author: Roc
+// @datetime 2024-01-23 15:40:56
+// @param code string
+// @return resp *GetTokenResp
+// @return err error
+func PushData(data PushDataParamReq) (resp *PushDataResp, err error) {
+	urlPath := `/DAQ/CY/ProxyServices/pushMarketPricePS`
+	req := PushDataReq{
+		In0: In0{
+			PageTotal: "",
+			PageNo:    "",
+			DocType:   "pushMarketPricePS",
+			Property:  "",
+			//DocCode:   getDocCode(),
+			Source: global.CONFIG.Xiangyu.SystemCode,
+			Target: global.CONFIG.Xiangyu.UserSyncTarget,
+		},
+		Parameters: data,
+	}
+
+	postData, err := json.Marshal(req)
+	if err != nil {
+		return
+	}
+	result, err := HttpPostIndex(urlPath, string(postData))
+	if err != nil {
+		return
+	}
+
+	//  解析响应结果
+	err = json.Unmarshal(result, &resp)
+	if err != nil {
+		return
+	}
+
+	if resp.Out.ReturnCode != "S" {
+		err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.Out.ReturnCode, resp.Out.ReturnMsg))
+		return
+	}
+
+	return
+}
+
+// HttpPostIndex
+// @Description: post请求
+// @author: Roc
+// @datetime 2024-02-27 18:45:30
+// @param urlPath string
+// @param postData string
+// @return []byte
+// @return error
+func HttpPostIndex(urlPath, postData string) ([]byte, error) {
+	if global.CONFIG.Xiangyu.IndexSyncHost == `` {
+		return nil, errors.New("数仓同步接口地址为空")
+	}
+	// 请求地址
+	postUrl := global.CONFIG.Xiangyu.IndexSyncHost + urlPath
+
+	body := io.NopCloser(strings.NewReader(postData))
+	client := &http.Client{}
+	req, err := http.NewRequest("POST", postUrl, body)
+	if err != nil {
+		return nil, err
+	}
+	req.Header.Set("content-Type", "application/json; charset=utf-8")
+	req.Header.Set("Accept-Encoding", "application/json; charset=utf-8")
+	req.Header.Set("Accept", "application/json; charset=utf-8")
+	req.Header.Set("Authorization", global.CONFIG.Xiangyu.IndexSyncAuth)
+
+	resp, err := client.Do(req)
+	if err != nil {
+		return nil, err
+	}
+	defer resp.Body.Close()
+	result, err := io.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+
+	// 日志记录
+	global.FILE_LOG.Debug("指标同步服务:地址:" + postUrl + ";\n请求参数:" + postData + ";\n返回参数:" + string(result))
+
+	//  解析返回参数,判断是否是json
+	if !json.Valid(result) {
+		err = errors.New("返回参数不是json格式")
+	}
+
+	return result, err
+}