base_from_sci.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // HandleSciIndex 处理卓创(红桃3)的Excel数据
  12. func HandleSciIndex(indexNameList, thirdIndexIdList, frequencyList, unitList []string, dataMap map[string]map[string]string) {
  13. // 卓创(红桃3)指标id列表
  14. sciIndexModel := new(models.BaseFromSciIndex)
  15. list, err := sciIndexModel.GetAllIndex()
  16. if err != nil {
  17. fmt.Println("获取指标失败:", err)
  18. return
  19. }
  20. allIndexMap := make(map[string]string)
  21. for _, v := range list {
  22. allIndexMap[v.IndexCode] = ``
  23. }
  24. // 需要入库的指标下标
  25. needAddIndexKeyList := make([]int, 0)
  26. needAddIndexMap := make(map[string]int, 0)
  27. for key, v := range thirdIndexIdList {
  28. if _, ok := allIndexMap[v]; !ok {
  29. if _, ok2 := needAddIndexMap[v]; !ok2 {
  30. needAddIndexKeyList = append(needAddIndexKeyList, key)
  31. needAddIndexMap[v] = key
  32. }
  33. }
  34. }
  35. //fmt.Println(needAddIndexKeyList)
  36. //return
  37. addSciIndexList := make([]*models.BaseFromSciIndex, 0)
  38. // 新的指标入库
  39. for _, key := range needAddIndexKeyList {
  40. tmpSciIndex := &models.BaseFromSciIndex{
  41. //BaseFromSciIndexId: 0,
  42. //ClassifyId: 0,
  43. IndexCode: thirdIndexIdList[key],
  44. IndexName: indexNameList[key],
  45. Frequency: frequencyList[key],
  46. Unit: unitList[key],
  47. //StartDate: time.Time{},
  48. //EndDate: time.Time{},
  49. CreateTime: time.Now(),
  50. ModifyTime: time.Now(),
  51. }
  52. addSciIndexList = append(addSciIndexList, tmpSciIndex)
  53. }
  54. //fmt.Println(addSciIndexList)
  55. if len(addSciIndexList) > 0 {
  56. err = sciIndexModel.BatchAdd(addSciIndexList)
  57. if err != nil {
  58. fmt.Println("批量添加指标失败:", err)
  59. return
  60. }
  61. fmt.Println("添加成功")
  62. }
  63. // 红桃3实际数据处理
  64. HandleSciData(dataMap)
  65. }
  66. // HandleData 红桃3实际数据处理
  67. func HandleSciData(dataMap map[string]map[string]string) {
  68. errMsgList := make([]string, 0)
  69. defer func() {
  70. if len(errMsgList) > 0 {
  71. go alarm_msg.SendAlarmMsg(fmt.Sprint("红桃3实际数据处理失败,err:", strings.Join(errMsgList, "\n")), 3)
  72. }
  73. }()
  74. // 获取所有的指标
  75. sciIndexModel := new(models.BaseFromSciIndex)
  76. list, err := sciIndexModel.GetAllIndex()
  77. if err != nil {
  78. fmt.Println("获取指标失败:", err)
  79. return
  80. }
  81. allIndexMap := make(map[string]*models.BaseFromSciIndex)
  82. for _, v := range list {
  83. allIndexMap[v.IndexCode] = v
  84. }
  85. sciIndexDataModel := new(models.BaseFromSciData)
  86. for indexCode, data := range dataMap {
  87. indexInfo, ok := allIndexMap[indexCode]
  88. if !ok {
  89. fmt.Println("找不到该指标:", indexCode)
  90. continue
  91. }
  92. indexDataList, err := sciIndexDataModel.GetIndexDataList(indexCode)
  93. if err != nil {
  94. errMsgList = append(errMsgList, fmt.Sprint("查找卓创基础指标失败,指标编码:", indexCode, ";错误原因:", err.Error()))
  95. continue
  96. }
  97. indexDataExistMap := make(map[string]*models.BaseFromSciData)
  98. for _, indexData := range indexDataList {
  99. indexDataExistMap[indexData.DataTime] = indexData
  100. }
  101. addSciDataList := make([]*models.BaseFromSciData, 0)
  102. for currDate, currVal := range data {
  103. currDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, currDate, time.Local)
  104. if tmpErr != nil {
  105. errMsgList = append(errMsgList, fmt.Sprint("时间格式化失败,指标编码:", currDate, ";错误原因:", tmpErr.Error()))
  106. continue
  107. }
  108. timestamp := currDataTime.UnixNano() / 1e6
  109. sciData, ok := indexDataExistMap[currDate]
  110. //判断是否存在数据,如果不存在,那么插入数据,存在的话那么修改数据
  111. if !ok {
  112. tmpBaseFromSciData := &models.BaseFromSciData{
  113. //SciDataId: 0,
  114. BaseFromSciIndexId: indexInfo.BaseFromSciIndexId,
  115. IndexCode: indexInfo.IndexCode,
  116. DataTime: currDate,
  117. Value: currVal,
  118. DataTimestamp: timestamp,
  119. CreateTime: time.Now(),
  120. ModifyTime: time.Now(),
  121. }
  122. indexDataExistMap[currDate] = tmpBaseFromSciData
  123. addSciDataList = append(addSciDataList, tmpBaseFromSciData)
  124. } else {
  125. // 更新数据
  126. existValue := sciData.Value
  127. if existValue != currVal {
  128. //fmt.Println("existValue:", existValue, ";====;currVal:", currVal)
  129. sciData.Value = currVal
  130. sciData.ModifyTime = time.Now()
  131. // 如果是已经入库了数据,那么就更新,否则只是单纯更改内存数据,而不去更新数据库
  132. if sciData.SciDataId > 0 {
  133. tmpErr = sciData.Update([]string{"Value", "ModifyTime"})
  134. if tmpErr != nil {
  135. errMsgList = append(errMsgList, fmt.Sprint("指标数据更新失败,指标编码:", indexCode, ",时间:", currDate, ";错误原因:", tmpErr.Error()))
  136. continue
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if len(addSciDataList) > 0 {
  143. err = sciIndexDataModel.BatchAdd(addSciDataList)
  144. if err != nil {
  145. errMsgList = append(errMsgList, fmt.Sprint("批量添加数据失败,指标编码:", indexCode, err.Error()))
  146. continue
  147. }
  148. }
  149. indexInfo.ModifyTime = time.Now()
  150. indexInfo.Update([]string{"ModifyTime"})
  151. // 同步刷新ETA图库红桃3的指标
  152. {
  153. // 获取指标详情
  154. edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI, indexInfo.IndexCode)
  155. if err != nil && err.Error() != utils.ErrNoRow() {
  156. errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
  157. continue
  158. }
  159. // 已经加入到指标库的话,那么就去更新ETA指标库吧
  160. if edbInfo != nil {
  161. go logic.RefreshBaseEdbInfo(edbInfo, ``)
  162. }
  163. }
  164. }
  165. }