|
@@ -0,0 +1,337 @@
|
|
|
+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"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ JiaYueSourceMap map[string]int
|
|
|
+)
|
|
|
+
|
|
|
+func init() {
|
|
|
+ JiaYueSourceMap = make(map[string]int)
|
|
|
+ JiaYueSourceMap["webisite"] = 1001
|
|
|
+ JiaYueSourceMap["website"] = 1001
|
|
|
+ JiaYueSourceMap["website_gf"] = 1001
|
|
|
+ JiaYueSourceMap["platts"] = 1002
|
|
|
+ JiaYueSourceMap["reuter"] = 1003
|
|
|
+ JiaYueSourceMap["reuter_vessel_q"] = 1003
|
|
|
+ JiaYueSourceMap["路透"] = 1003
|
|
|
+ JiaYueSourceMap["bloomberg"] = 1004
|
|
|
+ JiaYueSourceMap["bloomberg_tmp"] = 1004
|
|
|
+ JiaYueSourceMap["wind"] = 1005
|
|
|
+ JiaYueSourceMap["wind_p"] = 1005
|
|
|
+ JiaYueSourceMap["wind_stop"] = 1005
|
|
|
+ JiaYueSourceMap["wind_tmp"] = 1005
|
|
|
+}
|
|
|
+
|
|
|
+// InitJiaYueIndexData 初始化基础指标数据
|
|
|
+func InitJiaYueIndexData(dataPath string) {
|
|
|
+ var err error
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err.Error())
|
|
|
+ utils.FileLog.Info("InitJiaYueIndexData Err: " + err.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 读取excel
|
|
|
+ path, e := filepath.Abs(os.Args[0])
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("file abs err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ dir := filepath.Dir(path)
|
|
|
+ dataPath = dir + dataPath
|
|
|
+ fmt.Println("dataPath:" + dataPath)
|
|
|
+ //dataPath := dir + "/docs/东吴ETA同花顺指标20230925.xlsx"
|
|
|
+ f, e := excelize.OpenFile(dataPath)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("open file err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if e = f.Close(); e != nil {
|
|
|
+ err = fmt.Errorf("f close err: %s", e.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ rows, e := f.GetRows("Sheet1")
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("f GetRows err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("rows len:", len(rows))
|
|
|
+
|
|
|
+ type ExcelRows struct {
|
|
|
+ ClassifyFirst string
|
|
|
+ ClassifySecond string
|
|
|
+ ClassifyThird string
|
|
|
+ ClassifyFourth string
|
|
|
+ ClassifyFifth string
|
|
|
+ ClassifySixth string
|
|
|
+ Id string
|
|
|
+ IndexName string
|
|
|
+ IndexCode string
|
|
|
+ Unit string
|
|
|
+ Frequency string
|
|
|
+ SourceType string
|
|
|
+ }
|
|
|
+ for rk, row := range rows {
|
|
|
+ if rk <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var rowData ExcelRows
|
|
|
+ for ck, colCell := range row {
|
|
|
+ switch ck {
|
|
|
+ case 0:
|
|
|
+ rowData.ClassifyFirst = strings.TrimSpace(colCell)
|
|
|
+ case 1:
|
|
|
+ rowData.ClassifySecond = strings.TrimSpace(colCell)
|
|
|
+ case 2:
|
|
|
+ rowData.ClassifyThird = strings.TrimSpace(colCell)
|
|
|
+ case 3:
|
|
|
+ rowData.ClassifyFourth = strings.TrimSpace(colCell)
|
|
|
+ case 4:
|
|
|
+ rowData.ClassifyFifth = strings.TrimSpace(colCell)
|
|
|
+ case 5:
|
|
|
+ rowData.ClassifySixth = strings.TrimSpace(colCell)
|
|
|
+ case 6:
|
|
|
+ rowData.Id = strings.TrimSpace(colCell)
|
|
|
+ case 7:
|
|
|
+ rowData.IndexName = strings.TrimSpace(colCell)
|
|
|
+ case 8:
|
|
|
+ rowData.IndexCode = strings.TrimSpace(colCell)
|
|
|
+ case 9:
|
|
|
+ rowData.Unit = strings.TrimSpace(colCell)
|
|
|
+ case 10:
|
|
|
+ rowData.Frequency = strings.TrimSpace(colCell)
|
|
|
+ case 11:
|
|
|
+ rowData.SourceType = strings.TrimSpace(colCell)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if rowData.ClassifyFirst == "" || rowData.IndexName == "" || rowData.Id == "" || rowData.SourceType == "" || rowData.Frequency == "" {
|
|
|
+ fmt.Printf("row is empty, ClassifyFirst: %s, IndexName: %s, Id: %s, SourceType: %s, Frequency: %s\n", rowData.ClassifyFirst, rowData.IndexName, rowData.Id, rowData.SourceType, rowData.Frequency)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ sourceId, ok := JiaYueSourceMap[rowData.SourceType]
|
|
|
+ if !ok {
|
|
|
+ fmt.Printf("source is undefined, source: %s\n", rowData.SourceType)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指标编码, 路透彭博wind用指标编码, 没有的就忽略
|
|
|
+ edbCode := ""
|
|
|
+ if sourceId == 1003 || sourceId == 1004 || sourceId == 1005 {
|
|
|
+ edbCode = rowData.IndexCode
|
|
|
+ } else {
|
|
|
+ edbCode = rowData.Id
|
|
|
+ }
|
|
|
+ if edbCode == "" {
|
|
|
+ fmt.Printf("edb code empty\n")
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if rowData.Unit == "" {
|
|
|
+ rowData.Unit = "无"
|
|
|
+ }
|
|
|
+
|
|
|
+ var firstId, secondId, thirdId, fourthId, fifthId, lastId int
|
|
|
+ // 一级分类
|
|
|
+ method := "classify/get_or_add"
|
|
|
+ classifyFirstMap := make(map[string]interface{})
|
|
|
+ classifyFirstMap["ClassifyName"] = rowData.ClassifyFirst
|
|
|
+ classifyFirstMap["ParentId"] = 0
|
|
|
+ classifyFirstMap["Level"] = 0
|
|
|
+ classifyFirstMap["ClassifyType"] = 0
|
|
|
+ result, e := PostEdbLib(classifyFirstMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ClassifyFirst PostEdbLib err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp := new(models.ClassifyResp)
|
|
|
+ if err = json.Unmarshal(result, &resp); err != nil {
|
|
|
+ err = fmt.Errorf("ClassifyFirst json unmarshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ err = fmt.Errorf("ClassifyFirst resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ firstId = resp.Data.ClassifyId
|
|
|
+ lastId = firstId
|
|
|
+
|
|
|
+ // 二级分类
|
|
|
+ if rowData.ClassifySecond != "" {
|
|
|
+ classifySecondMap := make(map[string]interface{})
|
|
|
+ classifySecondMap["ClassifyName"] = rowData.ClassifySecond
|
|
|
+ classifySecondMap["ParentId"] = firstId
|
|
|
+ classifySecondMap["Level"] = 1
|
|
|
+ classifySecondMap["ClassifyType"] = 0
|
|
|
+ result, e = PostEdbLib(classifySecondMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp = new(models.ClassifyResp)
|
|
|
+ if err = json.Unmarshal(result, &resp); err != nil {
|
|
|
+ err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ secondId = resp.Data.ClassifyId
|
|
|
+ lastId = secondId
|
|
|
+ }
|
|
|
+
|
|
|
+ // 三级分类
|
|
|
+ if rowData.ClassifyThird != "" {
|
|
|
+ classifyThirdMap := make(map[string]interface{})
|
|
|
+ classifyThirdMap["ClassifyName"] = rowData.ClassifyThird
|
|
|
+ classifyThirdMap["ParentId"] = secondId
|
|
|
+ classifyThirdMap["Level"] = 2
|
|
|
+ classifyThirdMap["ClassifyType"] = 0
|
|
|
+ result, e = PostEdbLib(classifyThirdMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp = new(models.ClassifyResp)
|
|
|
+ if err = json.Unmarshal(result, &resp); err != nil {
|
|
|
+ err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ thirdId = resp.Data.ClassifyId
|
|
|
+ lastId = thirdId
|
|
|
+ }
|
|
|
+
|
|
|
+ // 四级分类
|
|
|
+ if rowData.ClassifyFourth != "" {
|
|
|
+ classifyFourthMap := make(map[string]interface{})
|
|
|
+ classifyFourthMap["ClassifyName"] = rowData.ClassifyFourth
|
|
|
+ classifyFourthMap["ParentId"] = thirdId
|
|
|
+ classifyFourthMap["Level"] = 3
|
|
|
+ classifyFourthMap["ClassifyType"] = 0
|
|
|
+ result, e = PostEdbLib(classifyFourthMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp = new(models.ClassifyResp)
|
|
|
+ if err = json.Unmarshal(result, &resp); err != nil {
|
|
|
+ err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fourthId = resp.Data.ClassifyId
|
|
|
+ lastId = fourthId
|
|
|
+ }
|
|
|
+
|
|
|
+ // 五级分类
|
|
|
+ if rowData.ClassifyFifth != "" {
|
|
|
+ classifyFifthMap := make(map[string]interface{})
|
|
|
+ classifyFifthMap["ClassifyName"] = rowData.ClassifyFifth
|
|
|
+ classifyFifthMap["ParentId"] = fourthId
|
|
|
+ classifyFifthMap["Level"] = 4
|
|
|
+ classifyFifthMap["ClassifyType"] = 0
|
|
|
+ result, e = PostEdbLib(classifyFifthMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp = new(models.ClassifyResp)
|
|
|
+ if err = json.Unmarshal(result, &resp); err != nil {
|
|
|
+ err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fifthId = resp.Data.ClassifyId
|
|
|
+ lastId = fifthId
|
|
|
+ }
|
|
|
+
|
|
|
+ // 六级分类
|
|
|
+ if rowData.ClassifySixth != "" {
|
|
|
+ classifySixthMap := make(map[string]interface{})
|
|
|
+ classifySixthMap["ClassifyName"] = rowData.ClassifySixth
|
|
|
+ classifySixthMap["ParentId"] = fifthId
|
|
|
+ classifySixthMap["Level"] = 5
|
|
|
+ classifySixthMap["ClassifyType"] = 0
|
|
|
+ result, e = PostEdbLib(classifySixthMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp = new(models.ClassifyResp)
|
|
|
+ if err = json.Unmarshal(result, &resp); err != nil {
|
|
|
+ err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if resp.Ret != 200 {
|
|
|
+ err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp.Msg, resp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ lastId = resp.Data.ClassifyId
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增指标
|
|
|
+ method = "edb_info/add"
|
|
|
+ indexMap := make(map[string]interface{})
|
|
|
+ indexMap["Source"] = sourceId
|
|
|
+ indexMap["EdbCode"] = edbCode
|
|
|
+ indexMap["EdbName"] = rowData.IndexName
|
|
|
+ indexMap["Frequency"] = rowData.Frequency
|
|
|
+ indexMap["Unit"] = rowData.Unit
|
|
|
+ indexMap["ClassifyId"] = lastId
|
|
|
+ result, e = PostEdbLib(indexMap, method)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("edb add PostEdbLib err: %s; result: %s", e.Error(), string(result))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ indexResp := new(models.EdbInfoResp)
|
|
|
+ if e = json.Unmarshal(result, &indexResp); e != nil {
|
|
|
+ err = fmt.Errorf("edb add unmarshal err: %s; result: %s", e.Error(), string(result))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if indexResp.Ret != 200 {
|
|
|
+ if strings.Contains(indexResp.Msg, "新增指标失败") {
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
+ err = fmt.Errorf("edb add resp msg: %s; errMsg: %s", indexResp.Msg, indexResp.ErrMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fmt.Println("edb add success:" + edbCode)
|
|
|
+
|
|
|
+ // 刷新指标
|
|
|
+ method = "jiayue_index/refresh"
|
|
|
+ refreshMap := make(map[string]interface{})
|
|
|
+ refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
|
|
|
+ refreshMap["EdbCode"] = indexResp.Data.EdbCode
|
|
|
+ refreshMap["Source"] = indexResp.Data.Source
|
|
|
+ refreshMap["StartDate"] = "1990-01-01"
|
|
|
+ refreshRes, e := PostEdbLib(refreshMap, method)
|
|
|
+ if e != nil {
|
|
|
+ utils.FileLog.Info("edb refresh err: %s; result: %s; IndexCode: %s", e.Error(), string(refreshRes), edbCode)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|