Browse Source

新增有色数据批量初始化

tuoling805 1 year ago
parent
commit
88fd2be684

+ 50 - 0
controllers/data_init.go

@@ -86,3 +86,53 @@ func (this *DataInitController) MySteelChemicalBase() {
 	br.Ret = 200
 	br.Msg = "保存成功"
 }
+
+// @Title 初始化有色指标到数据源
+// @Description 初始化有色指标到数据源
+// @Param   FileName    query   string true       "文件名称"
+// @Success Ret=200
+// @router /smm/to_data_source [get]
+func (this *DataInitController) SmmToDataSource() {
+	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.InitSmmIndexToDataSource(filePath)
+	br.Ret = 200
+	br.Msg = "保存成功"
+}
+
+// @Title 初始化有色指标-由数据源批量初始化到指标库
+// @Description 初始化有色指标-由数据源批量初始化到指标库
+// @Param   FileName    query   string true       "文件名称"
+// @Success Ret=200
+// @router /smm/add/to_index_lib [get]
+func (this *DataInitController) SmmToIndexLib() {
+	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.InitBaseIndexDataFromDataSourceSmm(filePath)
+	br.Ret = 200
+	br.Msg = "保存成功"
+}

+ 22 - 0
models/base_from_smm_classify.go

@@ -0,0 +1,22 @@
+package models
+
+import "time"
+
+type BaseFromSmmClassifyResp struct {
+	Ret     int    `json:"Ret"`
+	Msg     string `json:"Msg"`
+	ErrMsg  string `json:"ErrMsg"`
+	ErrCode string `json:"ErrCode"`
+	Data    struct {
+		ClassifyId      int       `orm:"column(classify_id);pk"`
+		ClassifyName    string    `description:"分类名称"`
+		ParentId        int       `description:"父级id"`
+		SysUserId       int       `description:"创建人id"`
+		SysUserRealName string    `description:"创建人姓名"`
+		Level           int       `description:"层级"`
+		Sort            int       `description:"排序字段,越小越靠前,默认值:10"`
+		ModifyTime      time.Time `description:"修改时间"`
+		CreateTime      time.Time `description:"创建时间"`
+	} `json:"Data"`
+	Success bool `json:"Success"`
+}

+ 34 - 0
models/base_from_smm_index.go

@@ -0,0 +1,34 @@
+package models
+
+import "time"
+
+type BaseFromSmmIndexResp struct {
+	Ret     int    `json:"Ret"`
+	Msg     string `json:"Msg"`
+	ErrMsg  string `json:"ErrMsg"`
+	ErrCode string `json:"ErrCode"`
+	Data    struct {
+		BaseFromSmmIndexId int64 `orm:"column(base_from_smm_index_id);pk"`
+		ClassifyId         int
+		Interface          string
+		Name               string
+		IndexCode          string
+		IndexName          string
+		Type1              string `orm:"column(type_1)"`
+		Type2              string `orm:"column(type_2)"`
+		Type3              string `orm:"column(type_3)"`
+		Frequency          string
+		Unit               string
+		ApiStartTime       string
+		ApiUpdateTime      string
+		StartTime          string
+		FinishTime         string
+		BaseFileName       string
+		RenameFileName     string
+		StartDate          string
+		EndDate            string
+		CreateTime         time.Time
+		ModifyTime         time.Time
+	} `json:"Data"`
+	Success bool `json:"Success"`
+}

+ 215 - 21
services/init_base_index.go

@@ -185,27 +185,6 @@ func InitBaseIndexData(dataPath string) {
 					}
 				}
 				fmt.Println("add index success:" + indexCode)
-
-				//刷新指标
-				{
-					switch source {
-					case "wind", "万得":
-						method = "wind/refresh"
-					case "ths", "同花顺":
-						method = "ths/refresh"
-					case "彭博":
-						method = "pb/refresh"
-					case "":
-
-					}
-					if method != `` {
-						refreshMap := make(map[string]interface{})
-						refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
-						refreshMap["EdbCode"] = indexCode
-						refreshMap["StartDate"] = "1990-01-01"
-						PostEdbLib(refreshMap, method)
-					}
-				}
 			} else {
 				fmt.Println("data is empty")
 				fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
@@ -428,6 +407,221 @@ func InitBaseIndexDataFromMysteel(filePath string) {
 	}
 }
 
+// 初始化基础指标数据-有色
+func InitBaseIndexDataFromDataSourceSmm(filePath string) {
+	var err error
+
+	defer func() {
+		if err != nil {
+			fmt.Println("InitBaseIndexDataFromDataSourceSmm Err:" + err.Error())
+			utils.FileLog.Info("InitBaseIndexDataFromDataSourceSmm Err:" + err.Error())
+		}
+	}()
+
+	//读取excel
+	path, err := filepath.Abs(os.Args[0])
+	if err != nil {
+		fmt.Println(err)
+	}
+
+	dir := filepath.Dir(path)
+	fmt.Println("dir:" + dir)
+	dataPath := dir + filePath
+	fmt.Println("dataPath:" + dataPath)
+	f, err := excelize.OpenFile(dataPath)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	defer func() {
+		// Close the spreadsheet.
+		if err := f.Close(); err != nil {
+			fmt.Println(err)
+		}
+	}()
+	rows, err := f.GetRows("Sheet1")
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	fmt.Println("rows len:", len(rows))
+	for rk, row := range rows {
+		if rk > 0 {
+			var classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source string
+			for ck, colCell := range row {
+				switch ck {
+				case 0:
+					classifyFirst = colCell
+				case 1:
+					classifySecond = colCell
+				case 2:
+					classifyThree = colCell
+				case 3:
+					indexCode = colCell
+				case 4:
+					indexName = colCell
+				case 5:
+					frequency = colCell
+				case 6:
+					unit = colCell
+				case 7:
+					source = colCell
+				}
+			}
+
+			if classifyFirst != "" &&
+				classifySecond != "" &&
+				classifyThree != "" &&
+				indexCode != "" &&
+				indexName != "" &&
+				unit != "" &&
+				frequency != "" &&
+				source != "" {
+
+				//判断指标是否存在
+				method := "smm/index_detail/from_data_source"
+				indexSearchMap := make(map[string]interface{})
+				indexSearchMap["IndexCode"] = indexCode
+				indexResult, err := PostEdbLib(indexSearchMap, method)
+				if err != nil {
+					fmt.Println("判断指标是否存在失败,Err:" + err.Error())
+					return
+				}
+
+				smmIndexResp := new(models.BaseFromSmmIndexResp)
+				err = json.Unmarshal(indexResult, &smmIndexResp)
+				if err != nil {
+					utils.FileLog.Info("判断指标是否存在失败:" + err.Error())
+					fmt.Println("判断指标是否存在失败,Err:" + err.Error())
+					return
+				}
+
+				if smmIndexResp.Ret != 200 {
+					fmt.Println("判断指标是否存在失败,Err:" + smmIndexResp.ErrMsg)
+					//utils.FileLog.Info("判断指标是否存在失败:" + err.Error())
+					//return
+					continue
+				}
+
+				if smmIndexResp.Data.BaseFromSmmIndexId <= 0 {
+					fmt.Println("指标:" + indexCode + ";不存在")
+					continue
+				}
+
+				method = "classify/get_or_add"
+				classifyFirstMap := make(map[string]interface{})
+				classifyFirstMap["ClassifyName"] = classifyFirst
+				classifyFirstMap["ParentId"] = 0
+				classifyFirstMap["Level"] = 0
+				classifyFirstMap["ClassifyType"] = 0
+				result, err := PostEdbLib(classifyFirstMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化分类1失败:" + err.Error())
+					return
+				}
+
+				resp := new(models.ClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类1失败:" + err.Error())
+					return
+				}
+
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类1失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+
+				classifySecondMap := make(map[string]interface{})
+				classifySecondMap["ClassifyName"] = classifySecond
+				classifySecondMap["ParentId"] = resp.Data.ClassifyId
+				classifySecondMap["Level"] = 1
+				classifySecondMap["ClassifyType"] = 0
+				result, err = PostEdbLib(classifySecondMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+				resp = new(models.ClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+
+				classifyThreeMap := make(map[string]interface{})
+				classifyThreeMap["ClassifyName"] = classifyThree
+				classifyThreeMap["ParentId"] = resp.Data.ClassifyId
+				classifyThreeMap["Level"] = 2
+				classifyThreeMap["ClassifyType"] = 0
+				result, err = PostEdbLib(classifyThreeMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化分类3失败:" + err.Error())
+					return
+				}
+				resp = new(models.ClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类3失败:" + err.Error())
+					return
+				}
+
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+				method = "edb_info/add"
+				indexMap := make(map[string]interface{})
+				indexMap["Source"] = IndexSourceMap[source]
+				indexMap["EdbCode"] = indexCode
+				indexMap["EdbName"] = indexName
+				indexMap["Frequency"] = frequency
+				indexMap["Unit"] = unit
+				indexMap["ClassifyId"] = resp.Data.ClassifyId
+				result, err = PostEdbLib(indexMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
+					return
+				}
+
+				indexResp := new(models.EdbInfoResp)
+				err = json.Unmarshal(result, &indexResp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+
+				if indexResp.Ret != 200 {
+					if strings.Contains(indexResp.Msg, "新增指标失败") {
+						continue
+					} else {
+						fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
+						utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
+						return
+					}
+				}
+				fmt.Println("add index success:" + indexCode)
+				//刷新指标
+				{
+					refreshMethod := "smm/refresh"
+					refreshMap := make(map[string]interface{})
+					refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
+					refreshMap["EdbCode"] = indexCode
+					refreshMap["StartDate"] = "1990-01-01"
+					PostEdbLib(refreshMap, refreshMethod)
+				}
+			} else {
+				fmt.Println("data is empty")
+				fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
+			}
+		}
+	}
+}
+
 /*
 // PostEdbLib 调用指标接口
 func PostEdbLib(param map[string]interface{}, method string) (resp *models.BaseResponse, err error) {

+ 163 - 0
services/init_smm_index.go

@@ -0,0 +1,163 @@
+package services
+
+import (
+	"encoding/json"
+	"eta/eta_data_init/models"
+	"eta/eta_data_init/utils"
+	"fmt"
+	"github.com/xuri/excelize/v2"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+// InitSmmIndexToDataSource 初始化数据源-有色指标数据
+func InitSmmIndexToDataSource(filePath string) {
+	var err error
+
+	defer func() {
+		if err != nil {
+			fmt.Println("InitBaseIndexData Err:" + err.Error())
+		}
+	}()
+
+	//读取excel
+	path, err := filepath.Abs(os.Args[0])
+	if err != nil {
+		fmt.Println(err)
+	}
+
+	dir := filepath.Dir(path)
+	fmt.Println("dir:" + dir)
+	dataPath := dir + filePath
+	fmt.Println("dataPath:" + dataPath)
+	f, err := excelize.OpenFile(dataPath)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	defer func() {
+		// Close the spreadsheet.
+		if err := f.Close(); err != nil {
+			fmt.Println(err)
+		}
+	}()
+	rows, err := f.GetRows("Sheet1")
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	classifyMethod := "smm/smm_classify/get_or_add"
+	indexMethod := "smm/add/index/to_data_source"
+	fmt.Println("rows len:", len(rows))
+	for rk, row := range rows {
+		if rk > 0 {
+			var classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source string
+			for ck, colCell := range row {
+				switch ck {
+				case 0:
+					classifyFirst = colCell
+				case 1:
+					classifySecond = colCell
+				case 2:
+					classifyThree = colCell
+				case 3:
+					indexCode = colCell
+				case 4:
+					indexName = colCell
+				case 5:
+					frequency = colCell
+				case 6:
+					unit = colCell
+				case 7:
+					source = colCell
+				}
+			}
+
+			if classifySecond != "" &&
+				classifyThree != "" &&
+				indexCode != "" &&
+				indexName != "" &&
+				unit != "" &&
+				frequency != "" {
+
+				classifySecondMap := make(map[string]interface{})
+				classifySecondMap["ClassifyName"] = classifySecond
+				classifySecondMap["ParentId"] = 0
+				classifySecondMap["Level"] = 0
+				classifySecondMap["SysUserId"] = utils.OpUserId
+				classifySecondMap["SysUserRealName"] = utils.OpUserRealName
+				result, err := PostEdbLib(classifySecondMap, classifyMethod)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+				resp := new(models.BaseFromSmmClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+
+				classifyThreeMap := make(map[string]interface{})
+				classifyThreeMap["ClassifyName"] = classifyThree
+				classifyThreeMap["ParentId"] = resp.Data.ClassifyId
+				classifyThreeMap["Level"] = 1
+				classifyThreeMap["SysUserId"] = utils.OpUserId
+				classifyThreeMap["SysUserRealName"] = utils.OpUserRealName
+				result, err = PostEdbLib(classifyThreeMap, classifyMethod)
+				if err != nil {
+					utils.FileLog.Info("初始化分类3失败:" + err.Error())
+					return
+				}
+				resp = new(models.BaseFromSmmClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类3失败:" + err.Error())
+					return
+				}
+
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+
+				indexMap := make(map[string]interface{})
+				indexMap["EdbCode"] = indexCode
+				indexMap["ClassifyId"] = resp.Data.ClassifyId
+				indexMap["SysUserId"] = utils.OpUserId
+				indexMap["SysUserRealName"] = utils.OpUserRealName
+				result, err = PostEdbLib(indexMap, indexMethod)
+				if err != nil {
+					utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
+					return
+				}
+
+				indexResp := new(models.EdbInfoResp)
+				err = json.Unmarshal(result, &indexResp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+
+				if indexResp.Ret != 200 {
+					if strings.Contains(indexResp.Msg, "新增指标失败") {
+						continue
+					} else {
+						fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
+						utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
+						return
+					}
+				}
+				fmt.Println("add index success:" + indexCode)
+			} else {
+				fmt.Println("data is empty")
+				fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
+			}
+		}
+	}
+}

+ 1 - 0
services/task.go

@@ -23,6 +23,7 @@ func Task() {
 	IndexSourceMap["wind"] = utils.DATA_SOURCE_WIND
 	IndexSourceMap["钢联"] = utils.DATA_SOURCE_MYSTEEL_CHEMICAL
 	IndexSourceMap["彭博"] = utils.DATA_SOURCE_PB
+	IndexSourceMap["SMM"] = utils.DATA_SOURCE_YS
 
 	fmt.Println("task end")
 }