蛋蛋星球-客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

161 rader
4.5 KiB

  1. package svc
  2. import (
  3. "applet/app/md"
  4. "applet/app/utils"
  5. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  6. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  8. "context"
  9. "encoding/json"
  10. "errors"
  11. "github.com/olivere/elastic/v7"
  12. "time"
  13. )
  14. func GetEggPointRecordBase(now time.Time, uid int64, page int, limit int) ([]md.EggPointRecordNode, string, int, error) {
  15. aliasName := md2.EggEnergyUserEggScoreEsAlias
  16. // 1. 判断当周分数是否生效
  17. getLastWeek := true
  18. if now.Weekday() > time.Wednesday || (now.Weekday() == time.Wednesday && now.Hour() > 14) {
  19. getLastWeek = false
  20. }
  21. nowIndex := md2.EggEnergyUserEggScoreEsAlias + "_" + es2.GetLatestEffectiveIndexFromAlias(now)
  22. // 2. 计算有效周数数量
  23. aliases, err := es.EsClient.Aliases().
  24. Index(aliasName). // 指定别名
  25. Do(context.Background())
  26. if err != nil {
  27. return nil, "", 0, err
  28. }
  29. // 3. 获取最老有效索引及年份周数
  30. indexNum := 0
  31. oldestIndex := aliasName + "_" + "999999"
  32. newestIndex := aliasName + "_" + "000000"
  33. for key, _ := range aliases.Indices {
  34. if key < oldestIndex {
  35. oldestIndex = key
  36. }
  37. if key > newestIndex {
  38. newestIndex = key
  39. }
  40. // 计算可供查询的周数
  41. if key <= nowIndex {
  42. indexNum++
  43. }
  44. }
  45. if indexNum < 1 {
  46. return nil, "60", 0, errors.New("no data")
  47. }
  48. oldestYearStr, oldestWeekStr := GetYearsAndWeekStr(oldestIndex)
  49. oldestYear := utils.StrToInt(oldestYearStr)
  50. oldestWeek := utils.StrToInt(oldestWeekStr)
  51. newestYearStr, newestWeekStr := GetYearsAndWeekStr(newestIndex)
  52. newestYear := utils.StrToInt(newestYearStr)
  53. newestWeek := utils.StrToInt(newestWeekStr)
  54. // 4. 获取当期有效蛋蛋分
  55. boolQuery := elastic.NewBoolQuery()
  56. boolQuery.Filter(elastic.NewTermQuery("uid", uid))
  57. searchResult, err := es.EsClient.Search().
  58. Index(nowIndex).
  59. Query(boolQuery).
  60. Pretty(true).
  61. Do(context.Background())
  62. var results []md.UserEggFlowReqRespList
  63. nowScore := "60"
  64. if searchResult != nil {
  65. var results []md.UserEggFlowReqRespList
  66. if searchResult.Hits.TotalHits.Value != 0 {
  67. // 解析结果
  68. for _, hit := range searchResult.Hits.Hits {
  69. var doc md.UserEggFlowReqRespList
  70. err = json.Unmarshal(hit.Source, &doc)
  71. if err != nil {
  72. return nil, "", 0, errors.New("failed to unmarshal")
  73. }
  74. results = append(results, doc)
  75. }
  76. }
  77. if len(results) > 0 {
  78. nowScore = utils.Float64ToStr(results[0].ScoreValue)
  79. if nowScore == "0" {
  80. nowScore = "60"
  81. }
  82. }
  83. }
  84. var indexes []string
  85. // 5. 构造分页索引列表 查询历史蛋蛋分
  86. for i := 0; i < limit && i < indexNum; i++ {
  87. var tempDays int
  88. if getLastWeek {
  89. // 未到周三下午两点,从上上周开始查询
  90. tempDays = 7 * ((page-1)*limit + i + 2)
  91. } else {
  92. // 已过周三下午两点,从上周开始查询
  93. tempDays = 7 * ((page-1)*limit + i + 1)
  94. }
  95. tempTime := now.AddDate(0, 0, -tempDays)
  96. tempYear, tempWeek := tempTime.ISOWeek()
  97. // 判断跳转到的时间是否大于最老索引时间
  98. if tempYear == oldestYear {
  99. if tempWeek < oldestWeek {
  100. break
  101. }
  102. } else if tempYear < oldestYear {
  103. break
  104. }
  105. // 判断是否超过最新索引时间
  106. if tempYear > newestYear {
  107. continue
  108. }
  109. if tempYear == newestYear && tempWeek > newestWeek {
  110. continue
  111. }
  112. tempIndex := es2.GetAppointIndexFromAlias(utils.IntToStr(tempYear), utils.IntToStr(tempWeek))
  113. indexes = append(indexes, tempIndex)
  114. }
  115. // 构建查询条件
  116. recordBoolQuery := elastic.NewBoolQuery()
  117. recordBoolQuery.Filter(elastic.NewTermQuery("uid", uid))
  118. searchRecordResult, err := es.EsClient.Search().
  119. Index(indexes...).
  120. Query(boolQuery).
  121. Pretty(true).
  122. Do(context.Background())
  123. if searchRecordResult == nil {
  124. return nil, "", 0, errors.New("failed to get egg score flows")
  125. }
  126. //var recordResults []md.UserEggFlowReqRespList
  127. // 检查是否有结果
  128. if searchRecordResult.Hits.TotalHits.Value != 0 {
  129. // 解析结果
  130. for _, hit := range searchRecordResult.Hits.Hits {
  131. var doc md.UserEggFlowReqRespList
  132. err = json.Unmarshal(hit.Source, &doc)
  133. if err != nil {
  134. return nil, "", 0, errors.New("failed to unmarshal")
  135. }
  136. if doc.ScoreValue == 0 {
  137. doc.ScoreValue = 60
  138. }
  139. results = append(results, doc)
  140. }
  141. }
  142. list := make([]md.EggPointRecordNode, len(results))
  143. for i, result := range results {
  144. year, week, startAt, endAt := GetWeekInfo(result.UpdatedAt)
  145. list[i].Score = utils.Float64ToStr(result.ScoreValue)
  146. list[i].Year = year
  147. list[i].Week = week
  148. list[i].StartAt = startAt
  149. list[i].EndAt = endAt
  150. }
  151. return list, nowScore, indexNum, nil
  152. }