base_from_oilchem.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 IndexCodeMap = make(map[string]string)
  10. var IndexMap = make(map[string]*models.BaseFromOilchemIndex)
  11. func HandleOilchemIndex(list []*models.BaseFromOilchemIndexReq) (err error) {
  12. allCode, e := models.GetBaseFromOilchemIndex()
  13. if e != nil {
  14. err = e
  15. fmt.Println("select Code err:", err)
  16. utils.FileLog.Info("GetBaseFromOilchemIndex err:", err)
  17. return
  18. }
  19. for _, item := range allCode {
  20. IndexCodeMap[item.IndexName] = item.IndexCode
  21. IndexMap[item.IndexName] = item
  22. }
  23. for _, v := range list {
  24. indexCode, needAdd := OilchemIndexCodeGenerator(v.IndexName, v.IndexNameStr, v.MarketName)
  25. if needAdd {
  26. item := models.BaseFromOilchemIndex{
  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.AddBaseFromOilchemIndex(&item)
  38. if e != nil {
  39. err = e
  40. fmt.Println("AddBaseFromOilchemIndexMuti err:", err)
  41. utils.FileLog.Info("AddBaseFromOilchemIndexMuti err:", err)
  42. return
  43. }
  44. item.BaseFromOilchemIndexId = int(id)
  45. IndexMap[item.IndexName] = &item
  46. }
  47. //获取指标数据信息
  48. data, e := models.GetBaseFromOilchemData(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.UpdateBaseFromOilchemData(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.BaseFromOilchemData{
  70. BaseFromOilchemIndexId: IndexMap[v.IndexName].BaseFromOilchemIndexId,
  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.AddBaseFromOilchemData(&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 OilchemIndexCodeGenerator(indexName, indexCodeStr, marketSampleName string) (indexCode string, needAdd bool) {
  91. strResult := ""
  92. indexCode, _ = IndexCodeMap[indexName]
  93. if indexCode == "" {
  94. //首字母
  95. a := pinyin.NewArgs()
  96. a.Fallback = func(r rune, a pinyin.Args) []string {
  97. return []string{string(r)}
  98. }
  99. indexCodeStr = strings.Replace(indexCodeStr, "(", "", -1)
  100. indexCodeStr = strings.Replace(indexCodeStr, ")", "", -1)
  101. rows := pinyin.Pinyin(indexCodeStr, a)
  102. for i := 0; i < len(rows); i++ {
  103. //strResult += rows[i][0]
  104. if len(rows[i]) != 0 {
  105. str := rows[i][0]
  106. pi := str[0:1]
  107. strResult += pi
  108. }
  109. }
  110. // 处理括号内名称
  111. if marketSampleName != "" {
  112. if province, ok := ProvinceMap[marketSampleName]; ok {
  113. strResult += province
  114. } else {
  115. a := pinyin.NewArgs()
  116. rows := pinyin.LazyPinyin(marketSampleName, a)
  117. for i := 0; i < len(rows); i++ {
  118. strResult += rows[i]
  119. }
  120. if len(rows) == 0 {
  121. strResult += marketSampleName
  122. }
  123. }
  124. }
  125. // 去除特殊符号
  126. strResult = strings.Replace(strResult, " ", "", -1)
  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. needAdd = true
  134. strResult = "lzzxw" + strResult
  135. indexCode = strings.Replace(strResult, " ", "", -1)
  136. IndexCodeMap[indexName] = indexCode
  137. }
  138. return
  139. }
  140. var ProvinceMap = map[string]string{
  141. "上海": "shanghai",
  142. "云南": "yunnan",
  143. "内蒙古": "innermongolia",
  144. "北京": "beijing",
  145. "台湾": "taiwan",
  146. "吉林": "jilin",
  147. "四川": "sichuan",
  148. "天津": "tianjin",
  149. "宁夏": "ningxia",
  150. "安徽": "anhui",
  151. "山东": "shandong",
  152. "山西": "shanxi",
  153. "广东": "guangdong",
  154. "广西": "guangxi",
  155. "新疆": "xinjiang",
  156. "江苏": "jiangsu",
  157. "江西": "jiangxi",
  158. "河北": "hebei",
  159. "河南": "henan",
  160. "浙江": "zhejiang",
  161. "海南": "hainan",
  162. "湖北": "hubei",
  163. "湖南": "hunan",
  164. "澳门": "macao",
  165. "甘肃": "gansu",
  166. "福建": "fujian",
  167. "西藏": "tibet",
  168. "贵州": "guizhou",
  169. "辽宁": "liaoning",
  170. "重庆": "chongqing",
  171. "陕西": "shaanxi",
  172. "青海": "qinhai",
  173. "香港": "hongkong",
  174. "黑龙江": "heilongjiang",
  175. }