123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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
- }
|