base_from_hisugar.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package services
  2. import (
  3. "eta/eta_index_lib/models"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/mozillazg/go-pinyin"
  7. "strings"
  8. )
  9. var HisugarIndexCodeMap = make(map[string]string)
  10. var HisugarIndexMap = make(map[string]*models.BaseFromHisugarIndex)
  11. func HandleHisugarIndex(list []*models.BaseFromHisugarIndexReq) (err error) {
  12. allCode, e := models.GetBaseFromHisugarIndex()
  13. if e != nil {
  14. err = e
  15. fmt.Println("select Code err:", err)
  16. utils.FileLog.Info("GetBaseFromHisugarIndex err:", err)
  17. return
  18. }
  19. for _, item := range allCode {
  20. HisugarIndexCodeMap[item.IndexName] = item.IndexCode
  21. HisugarIndexMap[item.IndexName] = item
  22. }
  23. for _, v := range list {
  24. indexCode, needAdd := HisugarIndexCodeGenerator(v.IndexName, v.IndexNameStr, v.MarketName)
  25. if needAdd {
  26. item := models.BaseFromHisugarIndex{
  27. IndexCode: indexCode,
  28. IndexName: v.IndexName,
  29. ClassifyId: v.ClassifyId,
  30. Unit: v.Unit,
  31. Frequency: v.Frequency,
  32. Describe: v.Describe,
  33. Sort: v.Sort,
  34. CreateTime: v.CreateTime,
  35. ModifyTime: v.ModifyTime,
  36. }
  37. id, e := models.AddBaseFromHisugarIndex(&item)
  38. if e != nil {
  39. err = e
  40. fmt.Println("AddBaseFromHisugarIndexMuti err:", err)
  41. utils.FileLog.Info("AddBaseFromHisugarIndexMuti err:", err)
  42. return
  43. }
  44. item.BaseFromHisugarIndexId = int(id)
  45. HisugarIndexMap[item.IndexName] = &item
  46. }
  47. //获取指标数据信息
  48. data, e := models.GetBaseFromHisugarData(indexCode, v.DataTime)
  49. if e != nil && e.Error() != utils.ErrNoRow() {
  50. err = e
  51. fmt.Println("select err:", err)
  52. utils.FileLog.Info("GetBaseFromTradeSci99IndexAll err:", err)
  53. }
  54. if data != nil {
  55. if data.Value != v.Value {
  56. // 更新
  57. fmt.Println("更新指标:", indexCode+v.DataTime)
  58. utils.FileLog.Info("更新指标:", indexCode+v.DataTime)
  59. e = models.UpdateBaseFromHisugarData(v.Value, indexCode, v.DataTime)
  60. if e != nil {
  61. err = e
  62. fmt.Println("Error update into database:", err)
  63. utils.FileLog.Info("Error update into database:", err)
  64. return
  65. }
  66. }
  67. } else {
  68. // 新增
  69. dataItem := models.BaseFromHisugarData{
  70. BaseFromHisugarIndexId: HisugarIndexMap[v.IndexName].BaseFromHisugarIndexId,
  71. IndexCode: indexCode,
  72. DataTime: v.DataTime,
  73. Value: v.Value,
  74. CreateTime: v.CreateTime,
  75. ModifyTime: v.ModifyTime,
  76. }
  77. fmt.Println("新增数据:", indexCode+v.DataTime)
  78. utils.FileLog.Info("新增数据:", indexCode+v.DataTime)
  79. _,e = models.AddBaseFromHisugarData(&dataItem)
  80. if e != nil {
  81. err = e
  82. fmt.Println("Error inserting into database:", err)
  83. utils.FileLog.Info("Error inserting into database:", err)
  84. return
  85. }
  86. }
  87. }
  88. return
  89. }
  90. func HisugarIndexCodeGenerator(indexName, indexCodeStr, marketSampleName string) (indexCode string, needAdd bool) {
  91. strResult := ""
  92. indexCodeStr = indexName
  93. indexCode, _ = HisugarIndexCodeMap[indexName]
  94. if indexCode == "" {
  95. //首字母
  96. a := pinyin.NewArgs()
  97. a.Fallback = func(r rune, a pinyin.Args) []string {
  98. return []string{string(r)}
  99. }
  100. indexCodeStr = strings.Replace(indexCodeStr, "(", "", -1)
  101. indexCodeStr = strings.Replace(indexCodeStr, ")", "", -1)
  102. rows := pinyin.Pinyin(indexCodeStr, a)
  103. for i := 0; i < len(rows); i++ {
  104. //strResult += rows[i][0]
  105. if len(rows[i]) != 0 {
  106. str := rows[i][0]
  107. pi := str[0:1]
  108. strResult += pi
  109. }
  110. }
  111. // 处理括号内名称
  112. if marketSampleName != "" {
  113. if province, ok := ProvinceMap[marketSampleName]; ok {
  114. strResult += province
  115. } else {
  116. a := pinyin.NewArgs()
  117. rows := pinyin.LazyPinyin(marketSampleName, a)
  118. for i := 0; i < len(rows); i++ {
  119. strResult += rows[i]
  120. }
  121. if len(rows) == 0 {
  122. strResult += marketSampleName
  123. }
  124. }
  125. }
  126. // 去除特殊符号
  127. strResult = strings.Replace(strResult, " ", "", -1)
  128. strResult = strings.Replace(strResult, "-", "", -1)
  129. strResult = strings.Replace(strResult, "/", "", -1)
  130. strResult = strings.Replace(strResult, "#", "", -1)
  131. strResult = strings.Replace(strResult, ":", "", -1)
  132. strResult = strings.Replace(strResult, "(", "", -1)
  133. strResult = strings.Replace(strResult, ")", "", -1)
  134. needAdd = true
  135. strResult = "ftkj" + strResult
  136. indexCode = strings.Replace(strResult, " ", "", -1)
  137. HisugarIndexCodeMap[indexName] = indexCode
  138. }
  139. return
  140. }