base_from_baiinfo.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, terminalCode, 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. indexObj.TerminalCode = terminalCode
  55. lastId, err := indexObj.AddBaseFromBaiinfoIndex()
  56. if err != nil {
  57. fmt.Println("add err:" + err.Error())
  58. return
  59. }
  60. indexId = lastId
  61. //修改排序
  62. go models.ModifyBaiinfoIndexSort(indexId)
  63. } else if isAdd == 2 {
  64. if item.TerminalCode == `` && terminalCode != `` {
  65. item.TerminalCode = terminalCode
  66. item.Update([]string{"TerminalCode"})
  67. }
  68. //获取已存在的所有数据
  69. exitDataList, err := models.GetBaseFromBaiinfoDataByCode(indexCode)
  70. if err != nil {
  71. fmt.Println("GetIndexDataList Err:" + err.Error())
  72. return
  73. }
  74. fmt.Println("exitDataListLen:", len(exitDataList))
  75. for _, v := range exitDataList {
  76. dateStr := v.DataTime
  77. exitDataMap[dateStr] = v
  78. }
  79. indexId = item.BaseFromBaiinfoIndexId
  80. }
  81. // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
  82. for date, value := range excelDataMap {
  83. dateT, tmpErr := utils.DealExcelDate(date)
  84. if tmpErr != nil {
  85. fmt.Println("time.ParseInLocation Err:" + tmpErr.Error())
  86. return
  87. }
  88. date = dateT.Format(utils.FormatDate)
  89. if findData, 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.BaseFromBaiinfoData)
  104. dataItem.BaseFromBaiinfoIndexId = int(indexId)
  105. dataItem.IndexCode = indexCode
  106. dataItem.DataTime = date
  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. if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
  115. }
  116. }
  117. }
  118. if len(addDataList) > 0 {
  119. err = models.AddBaseFromBaiinfoData(addDataList)
  120. if err != nil {
  121. fmt.Println("dataObj.Add() Err:" + err.Error())
  122. }
  123. item, err := models.GetBaiinfoIndexInfoMaxAndMinInfo(indexCode)
  124. if err == nil && item != nil {
  125. go models.ModifyBaiinfoIndexMaxAndMinInfo(indexCode, item)
  126. }
  127. }
  128. // 同步刷新ETA图库红桃3的指标
  129. {
  130. // 获取指标详情
  131. edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_BAIINFO, indexCode)
  132. if err != nil && err.Error() != utils.ErrNoRow() {
  133. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  134. }
  135. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  136. if edbInfo != nil {
  137. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  138. }
  139. }
  140. }