蛋蛋星球-客户端
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.
 
 
 
 
 
 

137 lines
3.9 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. for key, _ := range aliases.Indices {
  39. if key < oldestIndex {
  40. oldestIndex = key
  41. }
  42. }
  43. oldestYearStr, oldestWeekStr := GetYearsAndWeekStr(oldestIndex)
  44. oldestYear := utils.StrToInt(oldestYearStr)
  45. oldestWeek := utils.StrToInt(oldestWeekStr)
  46. // 4. 获取当期有效蛋蛋分
  47. nowIndex := es2.GetLatestEffectiveIndexFromAlias(now)
  48. boolQuery := elastic.NewBoolQuery()
  49. boolQuery.Filter(elastic.NewTermQuery("uid", uid))
  50. searchResult, err := es.EsClient.Search().
  51. Index(nowIndex).
  52. Query(boolQuery).
  53. Pretty(true).
  54. Do(context.Background())
  55. if searchResult == nil {
  56. return nil, "", 0, errors.New("failed to get current egg score")
  57. }
  58. var results []md.UserEggFlowReqRespList
  59. if searchResult.Hits.TotalHits.Value != 0 {
  60. // 解析结果
  61. for _, hit := range searchResult.Hits.Hits {
  62. var doc md.UserEggFlowReqRespList
  63. err = json.Unmarshal(hit.Source, &doc)
  64. if err != nil {
  65. return nil, "", 0, errors.New("failed to unmarshal")
  66. }
  67. results = append(results, doc)
  68. }
  69. }
  70. nowScore := utils.Float64ToStr(results[0].ScoreValue)
  71. indexes := make([]string, 0, limit)
  72. // 5. 构造分页索引列表 查询历史蛋蛋分
  73. for i := 0; i < limit; i++ {
  74. var tempDays int
  75. if getLastWeek {
  76. tempDays = 7 * ((page-1)*limit + i + 1)
  77. } else {
  78. tempDays = 7 * ((page-1)*limit + i)
  79. }
  80. tempTime := now.AddDate(0, 0, -tempDays)
  81. tempYear, tempWeek := tempTime.ISOWeek()
  82. // 判断跳转到的时间是否大于最老索引时间
  83. if tempYear == oldestYear {
  84. if tempWeek < oldestWeek {
  85. break
  86. }
  87. } else if tempYear < oldestYear {
  88. break
  89. }
  90. tempIndex := es2.GetAppointIndexFromAlias(utils.IntToStr(tempYear), utils.IntToStr(tempWeek))
  91. indexes = append(indexes, tempIndex)
  92. }
  93. // 构建查询条件
  94. recordBoolQuery := elastic.NewBoolQuery()
  95. recordBoolQuery.Filter(elastic.NewTermQuery("uid", uid))
  96. searchRecordResult, err := es.EsClient.Search().
  97. Index(indexes...).
  98. Query(boolQuery).
  99. Pretty(true).
  100. Do(context.Background())
  101. if searchRecordResult == nil {
  102. return nil, "", 0, errors.New("failed to get egg score flows")
  103. }
  104. var recordResults []md.UserEggFlowReqRespList
  105. // 检查是否有结果
  106. if searchRecordResult.Hits.TotalHits.Value != 0 {
  107. // 解析结果
  108. for _, hit := range searchResult.Hits.Hits {
  109. var doc md.UserEggFlowReqRespList
  110. err = json.Unmarshal(hit.Source, &doc)
  111. if err != nil {
  112. return nil, "", 0, errors.New("failed to unmarshal")
  113. }
  114. recordResults = append(recordResults, doc)
  115. }
  116. }
  117. list := make([]md.EggPointRecordNode, len(results))
  118. for i, result := range recordResults {
  119. year, week, startAt, endAt := GetWeekInfo(result.UpdatedAt)
  120. list[i].Score = utils.Float64ToStr(result.ScoreValue)
  121. list[i].Year = year
  122. list[i].Week = week
  123. list[i].StartAt = startAt
  124. list[i].EndAt = endAt
  125. }
  126. return list, nowScore, indexNum, nil
  127. }