瀏覽代碼

兼容钢联API导入数据源

hsun 8 月之前
父節點
當前提交
9e184db83d
共有 4 個文件被更改,包括 85 次插入2 次删除
  1. 27 1
      controllers/data_init.go
  2. 9 0
      routers/commentsRouter.go
  3. 13 0
      services/base.go
  4. 36 1
      services/init_mysteel_index.go

+ 27 - 1
controllers/data_init.go

@@ -60,7 +60,7 @@ func (this *DataInitController) MySteelChemicalToSource() {
 
 	filePath := "/docs/" + fileName
 	fmt.Println("filePath:" + filePath)
-	services.InitMysteelChemicalIndex(filePath)
+	services.InitMysteelChemicalIndex(filePath, false)
 	br.Ret = 200
 	br.Msg = "初始化成功"
 }
@@ -259,3 +259,29 @@ func (this *DataInitController) InitPCSGBloombergRun3() {
 	br.Ret = 200
 	br.Msg = "初始化成功"
 }
+
+// MySteelChemicalApiBase
+// @Title 钢联API-初始化至数据源
+// @Description 钢联API-初始化至数据源
+// @Param   FileName    query   string true       "文件名称"
+// @Success Ret=200
+// @router /mysteel_chemical/api_base [get]
+func (this *DataInitController) MySteelChemicalApiBase() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	fileName := this.GetString("FileName")
+	if fileName == "" {
+		br.Msg = "文件名称不能为空"
+		return
+	}
+
+	filePath := "/docs/" + fileName
+	fmt.Println("filePath:" + filePath)
+	services.InitMysteelChemicalIndex(filePath, true)
+	br.Ret = 200
+	br.Msg = "初始化成功"
+}

+ 9 - 0
routers/commentsRouter.go

@@ -79,6 +79,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_data_init/controllers:DataInitController"] = append(beego.GlobalControllerRouter["eta/eta_data_init/controllers:DataInitController"],
+        beego.ControllerComments{
+            Method: "MySteelChemicalApiBase",
+            Router: `/mysteel_chemical/api_base`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_data_init/controllers:DataInitController"] = append(beego.GlobalControllerRouter["eta/eta_data_init/controllers:DataInitController"],
         beego.ControllerComments{
             Method: "MySteelChemicalBase",

+ 13 - 0
services/base.go

@@ -47,3 +47,16 @@ func HttpPost(url, postData string, params ...string) ([]byte, error) {
 	fmt.Println("HttpPost:" + string(b))
 	return b, err
 }
+
+func PostHandleMysteelIndexEdbLib(param map[string][]map[string]interface{}, method string) (result []byte, err error) {
+	postUrl := utils.EDB_LIB_URL + method
+	postData, err := json.Marshal(param)
+	if err != nil {
+		return
+	}
+	result, err = HttpPost(postUrl, string(postData), "application/json")
+	if err != nil {
+		return
+	}
+	return
+}

+ 36 - 1
services/init_mysteel_index.go

@@ -13,7 +13,7 @@ import (
 
 // InitMysteelChemicalIndex
 // 初始化钢联化工指标数据(添加到数据源)
-func InitMysteelChemicalIndex(filePath string) {
+func InitMysteelChemicalIndex(filePath string, isApi bool) {
 	var err error
 
 	defer func() {
@@ -31,6 +31,7 @@ func InitMysteelChemicalIndex(filePath string) {
 	dir := filepath.Dir(path)
 	fmt.Println("dir:" + dir)
 	dataPath := dir + filePath
+	//dataPath = "./docs/gl_api_test.xlsx"
 	fmt.Println("dataPath:" + dataPath)
 	f, err := excelize.OpenFile(dataPath)
 	if err != nil {
@@ -50,6 +51,7 @@ func InitMysteelChemicalIndex(filePath string) {
 	}
 	classifyMethod := "mysteel_chemical/mysteel_classify/get_or_add"
 	indexMethod := "mysteel_chemical/add/mysteel/index"
+	apiHandleMethod := "mysteel_chemical/handle/api/mysteel/index"
 	fmt.Println("rows len:", len(rows))
 	for rk, row := range rows {
 		if rk > 0 {
@@ -184,6 +186,39 @@ func InitMysteelChemicalIndex(filePath string) {
 					}
 				}
 				fmt.Println("add index success:" + indexCode)
+
+				// 请求API初始化数据
+				if !isApi {
+					continue
+				}
+				fmt.Println("请求API刷新数据")
+
+				handleReq := make(map[string][]map[string]interface{})
+				handleMap := make(map[string]interface{})
+				handleMap["IndexCode"] = indexCode
+				handleReq["List"] = append(handleReq["List"], handleMap)
+				res, e := PostHandleMysteelIndexEdbLib(handleReq, apiHandleMethod)
+				if e != nil {
+					tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib写入指标失败, code: %s, err: %v, result: %s", indexCode, e, string(res))
+					fmt.Println(tips)
+					utils.FileLog.Info(tips)
+					return
+				}
+				handleResp := new(models.EdbInfoResp)
+				if e = json.Unmarshal(res, &handleResp); e != nil {
+					tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib解析resp失败, code: %s, err: %v, result: %s", indexCode, e, string(res))
+					fmt.Println(tips)
+					utils.FileLog.Info(tips)
+					return
+				}
+				if handleResp.Ret != 200 {
+					tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib写入指标失败, code: %s, Msg: %s, ErrMsg: %s", indexCode, handleResp.Msg, handleResp.ErrMsg)
+					fmt.Println(tips)
+					utils.FileLog.Info(tips)
+					return
+				}
+
+				fmt.Println("handle index success:" + indexCode)
 			} else {
 				fmt.Println("data is empty")
 				fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)