Browse Source

中石油彭博

hsun 9 months ago
parent
commit
c62e5ab851
4 changed files with 57 additions and 242 deletions
  1. 25 38
      controller/index_data/pcsg_index.go
  2. 21 12
      models/pcsg/bloomberg.go
  3. 1 4
      routers/pcsg.go
  4. 10 188
      services/index_data/pcsg_bloomberg.go

+ 25 - 38
controller/index_data/pcsg_index.go

@@ -2,60 +2,47 @@ package index_data
 
 import (
 	"eta/eta_bridge/controller/resp"
+	"eta/eta_bridge/global"
 	indexDataService "eta/eta_bridge/services/index_data"
+	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/go-playground/validator/v10"
 )
 
 // PCSGIndexController 中石油新加坡指标
 type PCSGIndexController struct{}
 
-// GetBloombergDailyIndex
-// @Description 获取日度指标
+// GetBloombergIndexDataGeneral
+// @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)
+// @Router /bloomberg/index_data/general [post]
+func (this *PCSGIndexController) GetBloombergIndexDataGeneral(c *gin.Context) {
+	var req struct {
+		TaskKey   string `json:"TaskKey" form:"TaskKey" description:"来源"`
+		Frequency string `json:"Frequency" form:"Frequency" description:"频度"`
+	}
+	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
 	}
-	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)
+	fmt.Println(req)
+	if req.TaskKey == "" {
+		resp.Fail(fmt.Sprintf("参数有误, TaskKey: %s", req.TaskKey), 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)
+	if req.Frequency == "" {
+		resp.Fail(fmt.Sprintf("参数有误, Frequency: %s", req.Frequency), c)
 		return
 	}
-	resp.OkData("获取成功", data, c)
-}
 
-// GetBloombergDailyIndexRun3
-// @Description 获取日度指标Run3
-// @Success 200 {string} string "获取成功"
-// @Router /bloomberg/daily_index_run3 [post]
-func (this *PCSGIndexController) GetBloombergDailyIndexRun3(c *gin.Context) {
-	data, e := indexDataService.GetPCSGBloombergDailyIndexRun3()
+	data, e := indexDataService.GetPCSGBloombergGeneralIndex(req.TaskKey, req.Frequency)
 	if e != nil {
-		resp.FailMsg("获取失败", "获取中石油新加坡日度指标(Run3)失败, err: "+e.Error(), c)
+		resp.FailMsg("获取失败", "GetPCSGBloombergGeneralIndex, err: "+e.Error(), c)
 		return
 	}
 	resp.OkData("获取成功", data, c)

+ 21 - 12
models/pcsg/bloomberg.go

@@ -1,17 +1,20 @@
 package pcsg
 
 import (
+	"eta/eta_bridge/utils"
 	"strings"
 	"time"
 )
 
 // PythonBloombergGeneralData 通用数据格式
 type PythonBloombergGeneralData struct {
+	NAME                 string   `json:"NAME" description:"指标名称, 可能为null"`
 	IDENTIFIER           string   `json:"IDENTIFIER" description:"指标编码"`
 	PX_LAST_EOD          *float64 `json:"PX_LAST_EOD" description:"数据值, 可能为null"`
-	PX_VOLUME_EOD        *float64 `json:"PX_VOLUME_EOD" description:"数据值(Run3), 可能为null"`
+	PX_VOLUME_EOD        *float64 `json:"PX_VOLUME_EOD" description:"数据值, 可能为null"`
+	PX_LAST              *float64 `json:"PX_LAST" description:"数据值, 可能为null"`
 	LAST_UPDATE_DATE_EOD string   `json:"LAST_UPDATE_DATE_EOD" description:"数据日期, 可能为null"`
-	NAME                 string   `json:"NAME" description:"指标名称, 可能为null"`
+	DATE                 string   `json:"DATE" description:"数据日期, 可能为null"`
 }
 
 // PythonBloombergGeneralResult API响应体
@@ -49,20 +52,26 @@ func FormatPythonBloombergGeneralData2Base(origin PythonBloombergGeneralData, fr
 	item.Frequency = frequency
 	item.Unit = "无"
 	item.Data = make([]BaseFromBloombergApiIndexData, 0)
-	// 值可能为nil
+
+	// 数据来源的值和日期对应字段不同
+	var dataTime time.Time
+	var val *float64
 	if origin.LAST_UPDATE_DATE_EOD != "" && origin.PX_LAST_EOD != nil {
-		t, _ := time.ParseInLocation(time.DateOnly, origin.LAST_UPDATE_DATE_EOD, time.Local)
-		item.Data = append(item.Data, BaseFromBloombergApiIndexData{
-			DataTime: t,
-			Value:    *origin.PX_LAST_EOD,
-		})
+		dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.LAST_UPDATE_DATE_EOD, time.Local)
+		val = origin.PX_LAST_EOD
 	}
-	// 可能是从Run3过来的, 数据值的字段为PX_VOLUME_EOD
 	if origin.LAST_UPDATE_DATE_EOD != "" && origin.PX_VOLUME_EOD != nil {
-		t, _ := time.ParseInLocation(time.DateOnly, origin.LAST_UPDATE_DATE_EOD, time.Local)
+		dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.LAST_UPDATE_DATE_EOD, time.Local)
+		val = origin.PX_VOLUME_EOD
+	}
+	if origin.DATE != "" && origin.PX_LAST != nil {
+		dataTime, _ = time.ParseInLocation(utils.FormatDate, origin.DATE, time.Local)
+		val = origin.PX_LAST
+	}
+	if !dataTime.IsZero() && val != nil {
 		item.Data = append(item.Data, BaseFromBloombergApiIndexData{
-			DataTime: t,
-			Value:    *origin.PX_VOLUME_EOD,
+			DataTime: dataTime,
+			Value:    *val,
 		})
 	}
 	return

+ 1 - 4
routers/pcsg.go

@@ -10,8 +10,5 @@ import (
 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)
-	group.POST("bloomberg/daily_index_run3", control.GetBloombergDailyIndexRun3)
+	group.POST("bloomberg/index_data/general", control.GetBloombergIndexDataGeneral)
 }

+ 10 - 188
services/index_data/pcsg_bloomberg.go

@@ -1,7 +1,6 @@
 package index_data
 
 import (
-	"bytes"
 	"encoding/json"
 	"eta/eta_bridge/global"
 	"eta/eta_bridge/models/pcsg"
@@ -10,16 +9,9 @@ import (
 	"net/http"
 )
 
-var (
-	PCSGBloombergPythonApiDaily     = "/api/bloomberg/daily_data"
-	PCSGBloombergPythonApiWeekly    = "/api/bloomberg/weekly_data"
-	PCSGBloombergPythonApiMonthly   = "/api/bloomberg/monthly_data"
-	PCSGBloombergPythonApiDailyRun3 = "/api/bloomberg/daily_data_run3"
-)
-
-// GetPCSGBloombergDailyIndex 获取彭博日度指标
-func GetPCSGBloombergDailyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
-	apiData, e := CurlPCSGBloombergDailyApi()
+// GetPCSGBloombergGeneralIndex 获取指标信息
+func GetPCSGBloombergGeneralIndex(taskKey, frequency string) (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
+	apiData, e := CurlPCSGBloombergGeneralDataApi(taskKey)
 	if e != nil {
 		err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
 		return
@@ -27,7 +19,6 @@ func GetPCSGBloombergDailyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndDa
 	if len(apiData) == 0 {
 		return
 	}
-	frequency := "日度"
 	for _, v := range apiData {
 		t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
 		if t.IndexCode != "" {
@@ -37,189 +28,20 @@ func GetPCSGBloombergDailyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndDa
 	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
-}
-
-// GetPCSGBloombergDailyIndexRun3 获取彭博日度指标(Run3)
-func GetPCSGBloombergDailyIndexRun3() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
-	apiData, e := CurlPCSGBloombergDailyRun3Api()
-	if e != nil {
-		err = fmt.Errorf("GetPCSGBloombergDailyIndexRun3 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.PythonBloombergGeneralData, 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.PythonBloombergGeneralResult)
-	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("服务地址为空")
+// CurlPCSGBloombergGeneralDataApi 请求通用格式数据接口
+func CurlPCSGBloombergGeneralDataApi(taskKey string) (resultData []pcsg.PythonBloombergGeneralData, err error) {
+	if taskKey == "" {
+		err = fmt.Errorf("任务Key为空")
 		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.PythonBloombergGeneralResult)
-	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.PythonBloombergGeneralResult)
-	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
-}
-
-// CurlPCSGBloombergDailyRun3Api 请求日度指标(Run3)接口
-func CurlPCSGBloombergDailyRun3Api() (resultData []pcsg.PythonBloombergGeneralData, err error) {
 	if global.CONFIG.PCSG.BloombergApiUrl == "" {
 		err = fmt.Errorf("服务地址为空")
 		return
 	}
-	url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiDailyRun3)
+	url := fmt.Sprintf("%s/api/bloomberg/general_data?task_key=%s", global.CONFIG.PCSG.BloombergApiUrl, taskKey)
 
-	resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
+	//resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
+	resp, e := http.Get(url)
 	if e != nil {
 		err = fmt.Errorf("http post err: %s", e.Error())
 		return