瀏覽代碼

中石油新加坡-Bloomberg

hsun 10 月之前
父節點
當前提交
ee63502862

+ 5 - 0
config/config.go

@@ -9,6 +9,7 @@ type Config struct {
 	Business Business `mapstructure:"business" json:"business" yaml:"business"`
 	Smm      Smm      `mapstructure:"smm" json:"smm" yaml:"smm"`
 	Xiangyu  Xiangyu  `mapstructure:"xiangyu" json:"xiangyu" yaml:"xiangyu"`
+	PCSG     PCSG     `mapstructure:"pcsg" json:"pcsg" yaml:"pcsg"`
 }
 
 // Serve gin服务配置
@@ -111,3 +112,7 @@ type Xiangyu struct {
 	IndexKey              string `mapstructure:"index-key" json:"index-key" yaml:"index-key" description:"统一平台秘钥"`
 	IndexSyncTarget       string `mapstructure:"index-sync-target" json:"index-sync-target" yaml:"index-sync-target" description:"指标同步平台编码"`
 }
+
+type PCSG struct {
+	BloombergApiUrl string `mapstructure:"bloomberge-api-url" json:"bloomberge-api-url" yaml:"bloomberge-api-url" description:"彭博API服务地址"`
+}

+ 49 - 0
controller/index_data/pcsg_index.go

@@ -0,0 +1,49 @@
+package index_data
+
+import (
+	"eta/eta_bridge/controller/resp"
+	indexDataService "eta/eta_bridge/services/index_data"
+	"github.com/gin-gonic/gin"
+)
+
+// PCSGIndexController 中石油新加坡指标
+type PCSGIndexController struct{}
+
+// GetBloombergDailyIndex
+// @Description 获取日度指标
+// @Success 200 {string} string "获取成功"
+// @Router /bloomberg/daily_index [post]
+func (this *PCSGIndexController) GetBloombergDailyIndex(c *gin.Context) {
+	data, e := indexDataService.GetPCSGBloombergDailyIndex()
+	if e != nil {
+		resp.FailMsg("获取失败", "获取中石油新加坡日度指标失败, err: "+e.Error(), c)
+		return
+	}
+	resp.OkData("获取成功", data, c)
+}
+
+// GetBloombergWeeklyIndex
+// @Description 获取周度指标
+// @Success 200 {string} string "获取成功"
+// @Router /bloomberg/weekly_index [post]
+func (this *PCSGIndexController) GetBloombergWeeklyIndex(c *gin.Context) {
+	data, e := indexDataService.GetPCSGBloombergWeeklyIndex()
+	if e != nil {
+		resp.FailMsg("获取失败", "获取中石油新加坡周度指标失败, err: "+e.Error(), c)
+		return
+	}
+	resp.OkData("获取成功", data, c)
+}
+
+// GetBloombergMonthlyIndex
+// @Description 获取月度指标
+// @Success 200 {string} string "获取成功"
+// @Router /bloomberg/monthly_index [post]
+func (this *PCSGIndexController) GetBloombergMonthlyIndex(c *gin.Context) {
+	data, e := indexDataService.GetPCSGBloombergMonthlyIndex()
+	if e != nil {
+		resp.FailMsg("获取失败", "获取中石油新加坡月度指标失败, err: "+e.Error(), c)
+		return
+	}
+	resp.OkData("获取成功", data, c)
+}

+ 1 - 0
init_serve/router.go

@@ -21,5 +21,6 @@ func InitRouter() (r *gin.Engine) {
 	routers.InitEtaTrial(rBase)
 	routers.InitIndexData(rBase)
 	routers.InitXiangyu(rBase)
+	routers.InitPCSG(rBase)
 	return
 }

+ 102 - 0
models/pcsg/bloomberg.go

@@ -0,0 +1,102 @@
+package pcsg
+
+import (
+	"strings"
+	"time"
+)
+
+// PythonBloombergDailyPriceData 日度-价格数据
+type PythonBloombergDailyPriceData struct {
+	IDENTIFIER    string  `json:"IDENTIFIER" description:"指标编码"`
+	PX_YEST_CLOSE float64 `json:"PX_YEST_CLOSE" description:"数据值"`
+	PX_CLOSE_DT   string  `json:"PX_CLOSE_DT" description:"数据日期"`
+	NAME          string  `json:"NAME" description:"指标名称"`
+}
+
+// PythonBloombergGeneralData 通用数据
+type PythonBloombergGeneralData struct {
+	IDENTIFIER     string  `json:"IDENTIFIER" description:"指标编码"`
+	NAME           string  `json:"NAME" description:"指标名称"`
+	PX_LAST        float64 `json:"PX_LAST" description:"数据值"`
+	LAST_UPDATE_DT string  `json:"LAST_UPDATE_DT" description:"数据日期"`
+}
+
+// PythonBloombergDailyResult 日度指标API响应体
+type PythonBloombergDailyResult struct {
+	Code int                            `json:"code"`
+	Msg  string                         `json:"msg"`
+	Data PythonBloombergDailyResultData `json:"data"`
+}
+
+// PythonBloombergDailyResultData 日度指标API响应数据
+type PythonBloombergDailyResultData struct {
+	PriceData   []PythonBloombergDailyPriceData `json:"price_data" description:"价格数据"`
+	GeneralData []PythonBloombergGeneralData    `json:"general_data" description:"一般数据"`
+}
+
+// PythonBloombergWeeklyResult 周度指标API响应体
+type PythonBloombergWeeklyResult struct {
+	Code int                          `json:"code"`
+	Msg  string                       `json:"msg"`
+	Data []PythonBloombergGeneralData `json:"data"`
+}
+
+// PythonBloombergMonthlyResult 月度指标API响应体
+type PythonBloombergMonthlyResult struct {
+	Code int                          `json:"code"`
+	Msg  string                       `json:"msg"`
+	Data []PythonBloombergGeneralData `json:"data"`
+}
+
+// BaseFromBloombergApiIndexAndData Bloomberg原始指标及数据
+type BaseFromBloombergApiIndexAndData struct {
+	BaseFromBloombergIndexId int                             `description:"指标ID"`
+	IndexCode                string                          `description:"指标编码"`
+	IndexName                string                          `description:"指标名称"`
+	Unit                     string                          `description:"单位"`
+	Source                   string                          `description:"来源"`
+	Frequency                string                          `description:"频度"`
+	CreateTime               time.Time                       `description:"创建时间"`
+	ModifyTime               time.Time                       `description:"修改时间"`
+	Data                     []BaseFromBloombergApiIndexData `description:"数据列表"`
+}
+
+// BaseFromBloombergApiIndexData Bloomberg原始指标数据
+type BaseFromBloombergApiIndexData struct {
+	DataTime time.Time `description:"数据日期"`
+	Value    float64   `description:"数据值"`
+}
+
+func FormatPythonBloombergDailyPriceData2Base(origin PythonBloombergDailyPriceData) (item BaseFromBloombergApiIndexAndData) {
+	if origin.IDENTIFIER == "" {
+		return
+	}
+	item.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
+	item.IndexName = strings.TrimSpace(origin.NAME)
+	item.Frequency = "日度"
+	item.Unit = "无"
+	item.Data = make([]BaseFromBloombergApiIndexData, 0)
+	t, _ := time.ParseInLocation(time.DateOnly, origin.PX_CLOSE_DT, time.Local)
+	item.Data = append(item.Data, BaseFromBloombergApiIndexData{
+		DataTime: t,
+		Value:    origin.PX_YEST_CLOSE,
+	})
+	return
+}
+
+func FormatPythonBloombergGeneralData2Base(origin PythonBloombergGeneralData, frequency string) (item BaseFromBloombergApiIndexAndData) {
+	if origin.IDENTIFIER == "" {
+		return
+	}
+	item.IndexCode = strings.TrimSpace(origin.IDENTIFIER)
+	item.IndexName = strings.TrimSpace(origin.NAME)
+	item.Frequency = frequency
+	item.Unit = "无"
+	item.Data = make([]BaseFromBloombergApiIndexData, 0)
+	t, _ := time.ParseInLocation(time.DateOnly, origin.LAST_UPDATE_DT, time.Local)
+	item.Data = append(item.Data, BaseFromBloombergApiIndexData{
+		DataTime: t,
+		Value:    origin.PX_LAST,
+	})
+	return
+}

+ 1 - 0
models/request/index_data/pcsg_index.go

@@ -0,0 +1 @@
+package index_data

+ 16 - 0
routers/pcsg.go

@@ -0,0 +1,16 @@
+package routers
+
+import (
+	"eta/eta_bridge/controller/index_data"
+	"eta/eta_bridge/middleware"
+	"github.com/gin-gonic/gin"
+)
+
+// InitPCSG 中石油新加坡
+func InitPCSG(r *gin.RouterGroup) {
+	control := new(index_data.PCSGIndexController)
+	group := r.Group("pcsg/").Use(middleware.InternalToken())
+	group.POST("bloomberg/daily_index", control.GetBloombergDailyIndex)
+	group.POST("bloomberg/weekly_index", control.GetBloombergWeeklyIndex)
+	group.POST("bloomberg/monthly_index", control.GetBloombergMonthlyIndex)
+}

+ 198 - 0
services/index_data/pcsg_bloomberg.go

@@ -0,0 +1,198 @@
+package index_data
+
+import (
+	"bytes"
+	"encoding/json"
+	"eta/eta_bridge/global"
+	"eta/eta_bridge/models/pcsg"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+)
+
+var (
+	PCSGBloombergPythonApiDaily   = "/api/bloomberg/daily_data"
+	PCSGBloombergPythonApiWeekly  = "/api/bloomberg/weekly_data"
+	PCSGBloombergPythonApiMonthly = "/api/bloomberg/monthly_data"
+)
+
+// GetPCSGBloombergDailyIndex 获取彭博日度指标
+func GetPCSGBloombergDailyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
+	apiData, e := CurlPCSGBloombergDailyApi()
+	if e != nil {
+		err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
+		return
+	}
+	if len(apiData.PriceData) > 0 {
+		for _, v := range apiData.PriceData {
+			t := pcsg.FormatPythonBloombergDailyPriceData2Base(v)
+			if t.IndexCode != "" {
+				indexes = append(indexes, t)
+			}
+		}
+	}
+	if len(apiData.GeneralData) > 0 {
+		frequency := "日度"
+		for _, v := range apiData.GeneralData {
+			t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
+			if t.IndexCode != "" {
+				indexes = append(indexes, t)
+			}
+		}
+	}
+	return
+}
+
+// GetPCSGBloombergWeeklyIndex 获取彭博周度指标
+func GetPCSGBloombergWeeklyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
+	apiData, e := CurlPCSGBloombergWeeklyApi()
+	if e != nil {
+		err = fmt.Errorf("GetPCSGBloombergWeeklyIndex err: %s", e.Error())
+		return
+	}
+	if len(apiData) == 0 {
+		return
+	}
+	frequency := "周度"
+	for _, v := range apiData {
+		t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
+		if t.IndexCode != "" {
+			indexes = append(indexes, t)
+		}
+	}
+	return
+}
+
+// GetPCSGBloombergMonthlyIndex 获取彭博月度指标
+func GetPCSGBloombergMonthlyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
+	apiData, e := CurlPCSGBloombergMonthlyApi()
+	if e != nil {
+		err = fmt.Errorf("GetPCSGBloombergMonthlyIndex err: %s", e.Error())
+		return
+	}
+	if len(apiData) == 0 {
+		return
+	}
+	frequency := "月度"
+	for _, v := range apiData {
+		t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
+		if t.IndexCode != "" {
+			indexes = append(indexes, t)
+		}
+	}
+	return
+}
+
+// CurlPCSGBloombergDailyApi 请求日度指标接口
+func CurlPCSGBloombergDailyApi() (resultData pcsg.PythonBloombergDailyResultData, err error) {
+	if global.CONFIG.PCSG.BloombergApiUrl == "" {
+		err = fmt.Errorf("服务地址为空")
+		return
+	}
+	url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiDaily)
+
+	resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
+	if e != nil {
+		err = fmt.Errorf("http post err: %s", e.Error())
+		return
+	}
+	defer resp.Body.Close()
+
+	b, e := ioutil.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
+	}
+
+	result := new(pcsg.PythonBloombergDailyResult)
+	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
+	}
+	resultData = result.Data
+	return
+}
+
+// CurlPCSGBloombergWeeklyApi 请求周度指标接口
+func CurlPCSGBloombergWeeklyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
+	if global.CONFIG.PCSG.BloombergApiUrl == "" {
+		err = fmt.Errorf("服务地址为空")
+		return
+	}
+	url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiWeekly)
+
+	resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
+	if e != nil {
+		err = fmt.Errorf("http post err: %s", e.Error())
+		return
+	}
+	defer resp.Body.Close()
+
+	b, e := ioutil.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
+	}
+
+	result := new(pcsg.PythonBloombergWeeklyResult)
+	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
+	}
+	resultData = result.Data
+	return
+}
+
+// CurlPCSGBloombergMonthlyApi 请求月度指标接口
+func CurlPCSGBloombergMonthlyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
+	if global.CONFIG.PCSG.BloombergApiUrl == "" {
+		err = fmt.Errorf("服务地址为空")
+		return
+	}
+	url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiMonthly)
+
+	resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
+	if e != nil {
+		err = fmt.Errorf("http post err: %s", e.Error())
+		return
+	}
+	defer resp.Body.Close()
+
+	b, e := ioutil.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
+	}
+
+	result := new(pcsg.PythonBloombergMonthlyResult)
+	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
+	}
+	resultData = result.Data
+	return
+}