package services import ( "eta/eta_index_lib/models" "eta/eta_index_lib/utils" "fmt" "github.com/mozillazg/go-pinyin" "strings" ) var IndexCodeMap = make(map[string]string) var IndexMap = make(map[string]*models.BaseFromOilchemIndex) func HandleOilchemIndex(list []*models.BaseFromOilchemIndexReq) (err error) { allCode, e := models.GetBaseFromOilchemIndex() if e != nil { err = e fmt.Println("select Code err:", err) utils.FileLog.Info("GetBaseFromOilchemIndex err:", err) return } for _, item := range allCode { IndexCodeMap[item.IndexName] = item.IndexCode IndexMap[item.IndexName] = item } for _, v := range list { indexCode, needAdd := OilchemIndexCodeGenerator(v.IndexName, v.IndexNameStr, v.MarketName) if needAdd { item := models.BaseFromOilchemIndex{ 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.AddBaseFromOilchemIndex(&item) if e != nil { err = e fmt.Println("AddBaseFromOilchemIndexMuti err:", err) utils.FileLog.Info("AddBaseFromOilchemIndexMuti err:", err) return } item.BaseFromOilchemIndexId = int(id) IndexMap[item.IndexName] = &item } //获取指标数据信息 data, e := models.GetBaseFromOilchemData(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.UpdateBaseFromOilchemData(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.BaseFromOilchemData{ BaseFromOilchemIndexId: IndexMap[v.IndexName].BaseFromOilchemIndexId, 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.AddBaseFromOilchemData(&dataItem) if e != nil { err = e fmt.Println("Error inserting into database:", err) utils.FileLog.Info("Error inserting into database:", err) return } } } return } func OilchemIndexCodeGenerator(indexName, indexCodeStr, marketSampleName string) (indexCode string, needAdd bool) { strResult := "" indexCode, _ = IndexCodeMap[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 = "lzzxw" + strResult indexCode = strings.Replace(strResult, " ", "", -1) IndexCodeMap[indexName] = indexCode } return } var ProvinceMap = map[string]string{ "上海": "shanghai", "云南": "yunnan", "内蒙古": "innermongolia", "北京": "beijing", "台湾": "taiwan", "吉林": "jilin", "四川": "sichuan", "天津": "tianjin", "宁夏": "ningxia", "安徽": "anhui", "山东": "shandong", "山西": "shanxi", "广东": "guangdong", "广西": "guangxi", "新疆": "xinjiang", "江苏": "jiangsu", "江西": "jiangxi", "河北": "hebei", "河南": "henan", "浙江": "zhejiang", "海南": "hainan", "湖北": "hubei", "湖南": "hunan", "澳门": "macao", "甘肃": "gansu", "福建": "fujian", "西藏": "tibet", "贵州": "guizhou", "辽宁": "liaoning", "重庆": "chongqing", "陕西": "shaanxi", "青海": "qinhai", "香港": "hongkong", "黑龙江": "heilongjiang", }