base_from_smm.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package services
  2. import (
  3. "eta/eta_index_lib/logic"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/services/alarm_msg"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. func SmmIndexHandle(baseFilePath, renameFilePath, indexName, indexCode, unit, frequency string, excelDataMap map[string]string) {
  12. var err error
  13. errMsgList := make([]string, 0)
  14. defer func() {
  15. if len(errMsgList) > 0 {
  16. fmt.Println(fmt.Sprint("SMM有色实际数据处理失败,err:", strings.Join(errMsgList, "\n")))
  17. go alarm_msg.SendAlarmMsg(fmt.Sprint("SMM有色实际数据处理失败,err:", strings.Join(errMsgList, "\n")), 3)
  18. }
  19. }()
  20. indexObj := new(models.BaseFromSmmIndex)
  21. var indexId int64
  22. addDataList := make([]*models.BaseFromSmmData, 0)
  23. updateDataList := make([]*models.BaseFromSmmData, 0)
  24. exitDataMap := make(map[string]*models.BaseFromSmmData)
  25. // 修改指标信息
  26. if indexName == "" {
  27. utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
  28. return
  29. }
  30. //判断指标是否存在
  31. var isAdd int
  32. item, err := indexObj.GetSmmIndexItem(indexCode)
  33. if err != nil {
  34. if err.Error() == utils.ErrNoRow() {
  35. isAdd = 1
  36. } else {
  37. isAdd = -1
  38. fmt.Println("GetSmmIndexItem Err:" + err.Error())
  39. return
  40. }
  41. }
  42. if item != nil && item.BaseFromSmmIndexId > 0 {
  43. fmt.Println("item:", item)
  44. isAdd = 2
  45. } else {
  46. isAdd = 1
  47. }
  48. fmt.Println("isAdd:", isAdd)
  49. if !strings.Contains(frequency, "度") {
  50. frequency = frequency + "度"
  51. }
  52. if isAdd == 1 {
  53. indexObj.IndexCode = indexCode
  54. indexObj.IndexName = indexName
  55. indexObj.Unit = unit
  56. indexObj.Frequency = frequency
  57. indexObj.ModifyTime = time.Now()
  58. indexObj.CreateTime = time.Now()
  59. indexObj.BaseFileName = baseFilePath
  60. indexObj.RenameFileName = renameFilePath
  61. lastId, err := indexObj.AddBaseFromSmmIndex()
  62. if err != nil {
  63. fmt.Println("add err:" + err.Error())
  64. return
  65. }
  66. indexId = lastId
  67. go models.ModifySmmIndexSort(indexId)
  68. } else if isAdd == 2 {
  69. //获取已存在的所有数据
  70. exitDataList, err := models.GetBaseFromSmmDataBySmmCode(indexCode)
  71. if err != nil {
  72. fmt.Println("GetIndexDataList Err:" + err.Error())
  73. return
  74. }
  75. fmt.Println("exitDataListLen:", len(exitDataList))
  76. for _, v := range exitDataList {
  77. //dateStr := v.DataTime.Format(utils.FormatDate)
  78. exitDataMap[v.DataTime] = v
  79. }
  80. indexId = item.BaseFromSmmIndexId
  81. }
  82. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  83. for date, value := range excelDataMap {
  84. dateTime, e := time.ParseInLocation(utils.FormatDate, date, time.Local)
  85. if e != nil {
  86. fmt.Println("time.ParseInLocation Err:" + e.Error())
  87. return
  88. }
  89. if _, ok := exitDataMap[date]; !ok {
  90. if !strings.Contains(value, "#N/A") {
  91. var saveDataTime time.Time
  92. if strings.Contains(date, "00:00:00") {
  93. saveDataTime, err = time.Parse(utils.FormatDateTime, date)
  94. } else {
  95. saveDataTime, err = time.Parse(utils.FormatDate, date)
  96. }
  97. if err != nil {
  98. errMsg := "saveDataTime parse err:" + err.Error() + " dataTime:" + date
  99. fmt.Println(errMsg)
  100. continue
  101. }
  102. timestamp := saveDataTime.UnixNano() / 1e6
  103. dataItem := new(models.BaseFromSmmData)
  104. dataItem.BaseFromSmmIndexId = int(indexId)
  105. dataItem.IndexCode = indexCode
  106. dataItem.DataTime = dateTime.Format(utils.FormatDate)
  107. dataItem.Value = value
  108. dataItem.CreateTime = time.Now()
  109. dataItem.ModifyTime = time.Now()
  110. dataItem.DataTimestamp = timestamp
  111. addDataList = append(addDataList, dataItem)
  112. }
  113. } else {
  114. // 更新对应日期数据
  115. if strings.Contains(value, "#N/A") {
  116. continue
  117. }
  118. dataItem := new(models.BaseFromSmmData)
  119. dataItem.IndexCode = indexCode
  120. dataItem.DataTime = dateTime.Format(utils.FormatDate)
  121. dataItem.Value = value
  122. dataItem.ModifyTime = time.Now()
  123. updateDataList = append(updateDataList, dataItem)
  124. }
  125. }
  126. if len(addDataList) > 0 {
  127. err = models.AddBaseFromSmmData(addDataList)
  128. if err != nil {
  129. fmt.Println("AddBaseFromSmmData Err:" + err.Error())
  130. errMsgList = append(errMsgList, "AddBaseFromSmmData err: "+err.Error())
  131. }
  132. }
  133. // 已存在的日期数据更新
  134. if len(updateDataList) > 0 {
  135. err = models.MultiUpdateBaseFromSmmDataValue(updateDataList)
  136. if err != nil {
  137. fmt.Println("MultiUpdateBaseFromSmmDataValue Err:" + err.Error())
  138. errMsgList = append(errMsgList, "MultiUpdateBaseFromSmmDataValue err: "+err.Error())
  139. }
  140. }
  141. itemInfo, err := models.GetSmmIndexInfoMaxAndMinInfo(indexCode)
  142. if err == nil && item != nil {
  143. e := models.ModifySmmIndexMaxAndMinInfo(indexCode, itemInfo)
  144. if e != nil {
  145. fmt.Println("ModifySmmIndexMaxAndMinInfo Err:" + e.Error())
  146. errMsgList = append(errMsgList, "ModifySmmIndexMaxAndMinInfo err: "+e.Error())
  147. }
  148. }
  149. // 同步刷新ETA图库有色的指标
  150. {
  151. // 获取指标详情
  152. edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_YS, indexCode)
  153. if err != nil && err.Error() != utils.ErrNoRow() {
  154. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  155. }
  156. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  157. if edbInfo != nil {
  158. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  159. }
  160. }
  161. }