123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- 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
- JiaYueFrequencyMap map[string]string
- )
- 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
- JiaYueFrequencyMap = map[string]string{
- "日": "日度",
- "周": "周度",
- "旬": "旬度",
- "半月": "旬度",
- "月": "月度",
- "季": "季度",
- "半年": "半年度",
- "年": "年度",
- "日度": "日度",
- "周度": "周度",
- "旬度": "旬度",
- "月度": "月度",
- "季度": "季度",
- "半年度": "半年度",
- "年度": "年度",
- }
- //fmt.Println(JiaYueSourceMap)
- //fmt.Println(JiaYueFrequencyMap)
- }
- // 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 = "E:\\GoProjects\\src\\eta\\eta_data_init\\docs\\ttt.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
- }
- totalIndex := 0
- repeatCodes := make(map[string]int, 0)
- 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 e = json.Unmarshal(result, &resp); e != 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
- res2, e := PostEdbLib(classifySecondMap, method)
- if e != nil {
- err = fmt.Errorf("ClassifySecond PostEdbLib err: %s", e.Error())
- return
- }
- resp2 := new(models.ClassifyResp)
- if e = json.Unmarshal(res2, &resp2); e != nil {
- err = fmt.Errorf("ClassifySecond json unmarshal err: %s", e.Error())
- return
- }
- if resp2.Ret != 200 {
- err = fmt.Errorf("ClassifySecond resp msg: %s; errMsg: %s", resp2.Msg, resp2.ErrMsg)
- return
- }
- secondId = resp2.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
- res3, e := PostEdbLib(classifyThirdMap, method)
- if e != nil {
- err = fmt.Errorf("ClassifyThird PostEdbLib err: %s", e.Error())
- return
- }
- resp3 := new(models.ClassifyResp)
- if e = json.Unmarshal(res3, &resp3); e != nil {
- err = fmt.Errorf("ClassifyThird json unmarshal err: %s", e.Error())
- return
- }
- if resp3.Ret != 200 {
- err = fmt.Errorf("ClassifyThird resp msg: %s; errMsg: %s", resp3.Msg, resp3.ErrMsg)
- return
- }
- thirdId = resp3.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
- res4, e := PostEdbLib(classifyFourthMap, method)
- if e != nil {
- err = fmt.Errorf("ClassifyFourth PostEdbLib err: %s", e.Error())
- return
- }
- resp4 := new(models.ClassifyResp)
- if e = json.Unmarshal(res4, &resp4); e != nil {
- err = fmt.Errorf("ClassifyFourth json unmarshal err: %s", e.Error())
- return
- }
- if resp4.Ret != 200 {
- err = fmt.Errorf("ClassifyFourth resp msg: %s; errMsg: %s", resp4.Msg, resp4.ErrMsg)
- return
- }
- fourthId = resp4.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
- res5, e := PostEdbLib(classifyFifthMap, method)
- if e != nil {
- err = fmt.Errorf("ClassifyFifth PostEdbLib err: %s", e.Error())
- return
- }
- resp5 := new(models.ClassifyResp)
- if e = json.Unmarshal(res5, &resp5); e != nil {
- err = fmt.Errorf("ClassifyFifth json unmarshal err: %s", e.Error())
- return
- }
- if resp5.Ret != 200 {
- err = fmt.Errorf("ClassifyFifth resp msg: %s; errMsg: %s", resp5.Msg, resp5.ErrMsg)
- return
- }
- fifthId = resp5.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
- res6, e := PostEdbLib(classifySixthMap, method)
- if e != nil {
- err = fmt.Errorf("ClassifySixth PostEdbLib err: %s", e.Error())
- return
- }
- resp6 := new(models.ClassifyResp)
- if e = json.Unmarshal(res6, &resp6); e != nil {
- err = fmt.Errorf("ClassifySixth json unmarshal err: %s", e.Error())
- return
- }
- if resp6.Ret != 200 {
- err = fmt.Errorf("ClassifySixth resp msg: %s; errMsg: %s", resp6.Msg, resp6.ErrMsg)
- return
- }
- lastId = resp6.Data.ClassifyId
- }
- // 新增指标
- method = "edb_info/add"
- indexMap := make(map[string]interface{})
- indexMap["Source"] = sourceId
- indexMap["EdbCode"] = edbCode
- indexMap["EdbName"] = rowData.IndexName
- indexMap["Frequency"] = JiaYueFrequencyMap[rowData.Frequency]
- indexMap["Unit"] = rowData.Unit
- indexMap["ClassifyId"] = lastId
- indexRes, e := PostEdbLib(indexMap, method)
- if e != nil {
- err = fmt.Errorf("edb add PostEdbLib err: %s; result: %s", e.Error(), string(indexRes))
- return
- }
- indexResp := new(models.EdbInfoResp)
- if e = json.Unmarshal(indexRes, &indexResp); e != nil {
- err = fmt.Errorf("edb add unmarshal err: %s; result: %s", e.Error(), string(indexRes))
- 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)
- repeatCodes[indexResp.Data.EdbCode] += 1
- // 刷新指标
- 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)
- }
- totalIndex += 1
- }
- // 打印重复编码的指标
- repeatCodeArr := make([]string, 0)
- for k, v := range repeatCodes {
- if v > 1 {
- repeatCodeArr = append(repeatCodeArr, k)
- }
- }
- utils.FileLog.Info(fmt.Sprintf("初始化指标总数: %d, 重复编码的指标数: %d, 重复编码: %s", totalIndex, len(repeatCodeArr), strings.Join(repeatCodeArr, ",")))
- }
|