package services import ( "eta/eta_index_lib/models" "eta/eta_index_lib/utils" "fmt" "github.com/mozillazg/go-pinyin" "strings" ) var HisugarIndexCodeMap = make(map[string]string) var HisugarIndexMap = make(map[string]*models.BaseFromHisugarIndex) func HandleHisugarIndex(list []*models.BaseFromHisugarIndexReq) (err error) { allCode, e := models.GetBaseFromHisugarIndex() if e != nil { err = e fmt.Println("select Code err:", err) utils.FileLog.Info("GetBaseFromHisugarIndex err:", err) return } for _, item := range allCode { HisugarIndexCodeMap[item.IndexName] = item.IndexCode HisugarIndexMap[item.IndexName] = item } for _, v := range list { indexCode, needAdd := HisugarIndexCodeGenerator(v.IndexName, v.IndexNameStr, v.MarketName) if needAdd { item := models.BaseFromHisugarIndex{ IndexCode: indexCode, IndexName: v.IndexName, ClassifyId: v.ClassifyId, Unit: v.Unit, Frequency: v.Frequency, Describe: v.Describe, Sort: v.Sort, CreateTime: v.CreateTime, ModifyTime: v.ModifyTime, } id, e := models.AddBaseFromHisugarIndex(&item) if e != nil { err = e fmt.Println("AddBaseFromHisugarIndexMuti err:", err) utils.FileLog.Info("AddBaseFromHisugarIndexMuti err:", err) return } item.BaseFromHisugarIndexId = int(id) HisugarIndexMap[item.IndexName] = &item } //获取指标数据信息 data, e := models.GetBaseFromHisugarData(indexCode, v.DataTime) if e != nil && e.Error() != utils.ErrNoRow() { err = e fmt.Println("select err:", err) utils.FileLog.Info("GetBaseFromTradeSci99IndexAll err:", err) } if data != nil { if data.Value != v.Value { // 更新 fmt.Println("更新指标:", indexCode+v.DataTime) utils.FileLog.Info("更新指标:", indexCode+v.DataTime) e = models.UpdateBaseFromHisugarData(v.Value, indexCode, v.DataTime) if e != nil { err = e fmt.Println("Error update into database:", err) utils.FileLog.Info("Error update into database:", err) return } } } else { // 新增 dataItem := models.BaseFromHisugarData{ BaseFromHisugarIndexId: HisugarIndexMap[v.IndexName].BaseFromHisugarIndexId, IndexCode: indexCode, DataTime: v.DataTime, Value: v.Value, CreateTime: v.CreateTime, ModifyTime: v.ModifyTime, } fmt.Println("新增数据:", indexCode+v.DataTime) utils.FileLog.Info("新增数据:", indexCode+v.DataTime) _,e = models.AddBaseFromHisugarData(&dataItem) if e != nil { err = e fmt.Println("Error inserting into database:", err) utils.FileLog.Info("Error inserting into database:", err) return } } } return } func HisugarIndexCodeGenerator(indexName, indexCodeStr, marketSampleName string) (indexCode string, needAdd bool) { strResult := "" indexCodeStr = indexName indexCode, _ = HisugarIndexCodeMap[indexName] if indexCode == "" { //首字母 a := pinyin.NewArgs() a.Fallback = func(r rune, a pinyin.Args) []string { return []string{string(r)} } indexCodeStr = strings.Replace(indexCodeStr, "(", "", -1) indexCodeStr = strings.Replace(indexCodeStr, ")", "", -1) rows := pinyin.Pinyin(indexCodeStr, a) for i := 0; i < len(rows); i++ { //strResult += rows[i][0] if len(rows[i]) != 0 { str := rows[i][0] pi := str[0:1] strResult += pi } } // 处理括号内名称 if marketSampleName != "" { if province, ok := ProvinceMap[marketSampleName]; ok { strResult += province } else { a := pinyin.NewArgs() rows := pinyin.LazyPinyin(marketSampleName, a) for i := 0; i < len(rows); i++ { strResult += rows[i] } if len(rows) == 0 { strResult += marketSampleName } } } // 去除特殊符号 strResult = strings.Replace(strResult, " ", "", -1) strResult = strings.Replace(strResult, "-", "", -1) strResult = strings.Replace(strResult, "/", "", -1) strResult = strings.Replace(strResult, "#", "", -1) strResult = strings.Replace(strResult, ":", "", -1) strResult = strings.Replace(strResult, "(", "", -1) strResult = strings.Replace(strResult, ")", "", -1) needAdd = true strResult = "ftkj" + strResult indexCode = strings.Replace(strResult, " ", "", -1) HisugarIndexCodeMap[indexName] = indexCode } return }