base_from_baiinfo.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // HandleBaiinfoIndex 处理百川盈弗的excel数据
  12. func HandleBaiinfoIndex(baseFilePath, renameFilePath, indexName, indexCode string, excelDataMap map[string]string) {
  13. var err error
  14. errMsgList := make([]string, 0)
  15. defer func() {
  16. if len(errMsgList) > 0 {
  17. go alarm_msg.SendAlarmMsg(fmt.Sprint("红桃3实际数据处理失败,err:", strings.Join(errMsgList, "\n")), 3)
  18. }
  19. }()
  20. indexObj := new(models.BaseFromBaiinfoIndex)
  21. var indexId int64
  22. addDataList := make([]*models.BaseFromBaiinfoData, 0)
  23. exitDataMap := make(map[string]*models.BaseFromBaiinfoData)
  24. // 修改指标信息
  25. if indexName == "" {
  26. utils.FileLog.Info("未刷新到指标数据:indexName:" + indexName)
  27. return
  28. }
  29. //判断指标是否存在
  30. var isAdd int
  31. item, err := indexObj.GetSmmIndexItem(indexCode)
  32. if err != nil {
  33. if err.Error() == utils.ErrNoRow() {
  34. isAdd = 1
  35. } else {
  36. isAdd = -1
  37. fmt.Println("GetSmmIndexItem Err:" + err.Error())
  38. return
  39. }
  40. }
  41. if item != nil && item.BaseFromBaiinfoIndexId > 0 {
  42. fmt.Println("item:", item)
  43. isAdd = 2
  44. } else {
  45. isAdd = 1
  46. }
  47. if isAdd == 1 {
  48. indexObj.IndexCode = indexCode
  49. indexObj.IndexName = indexName
  50. indexObj.ModifyTime = time.Now()
  51. indexObj.CreateTime = time.Now()
  52. indexObj.BaseFileName = baseFilePath
  53. indexObj.RenameFileName = renameFilePath
  54. lastId, err := indexObj.AddBaseFromBaiinfoIndex()
  55. if err != nil {
  56. fmt.Println("add err:" + err.Error())
  57. return
  58. }
  59. indexId = lastId
  60. //修改排序
  61. go models.ModifyBaiinfoIndexSort(indexId)
  62. } else if isAdd == 2 {
  63. //获取已存在的所有数据
  64. exitDataList, err := models.GetBaseFromBaiinfoDataByCode(indexCode)
  65. if err != nil {
  66. fmt.Println("GetIndexDataList Err:" + err.Error())
  67. return
  68. }
  69. fmt.Println("exitDataListLen:", len(exitDataList))
  70. for _, v := range exitDataList {
  71. dateStr := v.DataTime
  72. exitDataMap[dateStr] = v
  73. }
  74. indexId = item.BaseFromBaiinfoIndexId
  75. }
  76. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  77. for date, value := range excelDataMap {
  78. if findData, ok := exitDataMap[date]; !ok {
  79. _, err := time.ParseInLocation(utils.FormatDate, date, time.Local)
  80. if err != nil {
  81. fmt.Println("time.ParseInLocation Err:" + err.Error())
  82. return
  83. }
  84. if !strings.Contains(value, "#N/A") {
  85. var saveDataTime time.Time
  86. if strings.Contains(date, "00:00:00") {
  87. saveDataTime, err = time.Parse(utils.FormatDateTime, date)
  88. } else {
  89. saveDataTime, err = time.Parse(utils.FormatDate, date)
  90. }
  91. if err != nil {
  92. errMsg := "saveDataTime parse err:" + err.Error() + " dataTime:" + date
  93. fmt.Println(errMsg)
  94. continue
  95. }
  96. timestamp := saveDataTime.UnixNano() / 1e6
  97. dataItem := new(models.BaseFromBaiinfoData)
  98. dataItem.BaseFromBaiinfoIndexId = int(indexId)
  99. dataItem.IndexCode = indexCode
  100. dataItem.DataTime = date
  101. dataItem.Value = value
  102. dataItem.CreateTime = time.Now()
  103. dataItem.ModifyTime = time.Now()
  104. dataItem.DataTimestamp = timestamp
  105. addDataList = append(addDataList, dataItem)
  106. }
  107. } else {
  108. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  109. }
  110. }
  111. }
  112. if len(addDataList) > 0 {
  113. err = models.AddBaseFromBaiinfoData(addDataList)
  114. if err != nil {
  115. fmt.Println("dataObj.Add() Err:" + err.Error())
  116. }
  117. item, err := models.GetBaiinfoIndexInfoMaxAndMinInfo(indexCode)
  118. if err == nil && item != nil {
  119. go models.ModifyBaiinfoIndexMaxAndMinInfo(indexCode, item)
  120. }
  121. }
  122. // 同步刷新ETA图库红桃3的指标
  123. {
  124. // 获取指标详情
  125. edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_BAIINFO, indexCode)
  126. if err != nil && err.Error() != utils.ErrNoRow() {
  127. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  128. }
  129. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  130. if edbInfo != nil {
  131. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  132. }
  133. }
  134. }