base_from_sci.go 6.1 KB

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