蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

156 lines
4.4 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. // 2. 计算有效周数数量
  22. aliases, err := es.EsClient.Aliases().
  23. Index(aliasName). // 指定别名
  24. Do(context.Background())
  25. if err != nil {
  26. return nil, "", 0, err
  27. }
  28. indexNum := len(aliases.Indices)
  29. if getLastWeek == true {
  30. // 如果当期未生效 有效周数 - 1
  31. indexNum--
  32. }
  33. if indexNum < 1 {
  34. return nil, "", 0, errors.New("no data")
  35. }
  36. // 3. 获取最老有效索引及年份周数
  37. oldestIndex := aliasName + "_" + "999999"
  38. newestIndex := aliasName + "_" + "000000"
  39. for key, _ := range aliases.Indices {
  40. if key < oldestIndex {
  41. oldestIndex = key
  42. }
  43. if key > newestIndex {
  44. newestIndex = key
  45. }
  46. }
  47. oldestYearStr, oldestWeekStr := GetYearsAndWeekStr(oldestIndex)
  48. oldestYear := utils.StrToInt(oldestYearStr)
  49. oldestWeek := utils.StrToInt(oldestWeekStr)
  50. newestYearStr, newestWeekStr := GetYearsAndWeekStr(newestIndex)
  51. newestYear := utils.StrToInt(newestYearStr)
  52. newestWeek := utils.StrToInt(newestWeekStr)
  53. // 4. 获取当期有效蛋蛋分
  54. nowIndex := md2.EggEnergyUserEggScoreEsAlias + "_" + es2.GetLatestEffectiveIndexFromAlias(now)
  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. var nowScore string
  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. } else {
  80. nowScore = "60"
  81. }
  82. } else {
  83. nowScore = "60"
  84. }
  85. indexes := make([]string, 0, limit)
  86. // 5. 构造分页索引列表 查询历史蛋蛋分
  87. for i := 0; i < limit; i++ {
  88. var tempDays int
  89. if getLastWeek {
  90. tempDays = 7 * ((page-1)*limit + i + 1)
  91. } else {
  92. tempDays = 7 * ((page-1)*limit + i)
  93. }
  94. tempTime := now.AddDate(0, 0, -tempDays)
  95. tempYear, tempWeek := tempTime.ISOWeek()
  96. // 判断跳转到的时间是否大于最老索引时间
  97. if tempYear == oldestYear {
  98. if tempWeek < oldestWeek {
  99. break
  100. }
  101. } else if tempYear < oldestYear {
  102. break
  103. }
  104. // 判断是否超过最新索引时间
  105. if tempYear > newestYear {
  106. continue
  107. }
  108. if tempYear == newestYear && tempWeek > newestWeek {
  109. continue
  110. }
  111. tempIndex := es2.GetAppointIndexFromAlias(utils.IntToStr(tempYear), utils.IntToStr(tempWeek))
  112. indexes = append(indexes, tempIndex)
  113. }
  114. // 构建查询条件
  115. recordBoolQuery := elastic.NewBoolQuery()
  116. recordBoolQuery.Filter(elastic.NewTermQuery("uid", uid))
  117. searchRecordResult, err := es.EsClient.Search().
  118. Index(indexes...).
  119. Query(boolQuery).
  120. Pretty(true).
  121. Do(context.Background())
  122. if searchRecordResult == nil {
  123. return nil, "", 0, errors.New("failed to get egg score flows")
  124. }
  125. //var recordResults []md.UserEggFlowReqRespList
  126. // 检查是否有结果
  127. if searchRecordResult.Hits.TotalHits.Value != 0 {
  128. // 解析结果
  129. for _, hit := range searchRecordResult.Hits.Hits {
  130. var doc md.UserEggFlowReqRespList
  131. err = json.Unmarshal(hit.Source, &doc)
  132. if err != nil {
  133. return nil, "", 0, errors.New("failed to unmarshal")
  134. }
  135. results = append(results, doc)
  136. }
  137. }
  138. list := make([]md.EggPointRecordNode, len(results))
  139. for i, result := range results {
  140. year, week, startAt, endAt := GetWeekInfo(result.UpdatedAt)
  141. list[i].Score = utils.Float64ToStr(result.ScoreValue)
  142. list[i].Year = year
  143. list[i].Week = week
  144. list[i].StartAt = startAt
  145. list[i].EndAt = endAt
  146. }
  147. return list, nowScore, indexNum, nil
  148. }