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

152 regels
4.3 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. nowScore = utils.Float64ToStr(results[0].ScoreValue)
  78. } else {
  79. nowScore = "60"
  80. }
  81. indexes := make([]string, 0, limit)
  82. // 5. 构造分页索引列表 查询历史蛋蛋分
  83. for i := 0; i < limit; i++ {
  84. var tempDays int
  85. if getLastWeek {
  86. tempDays = 7 * ((page-1)*limit + i + 1)
  87. } else {
  88. tempDays = 7 * ((page-1)*limit + i)
  89. }
  90. tempTime := now.AddDate(0, 0, -tempDays)
  91. tempYear, tempWeek := tempTime.ISOWeek()
  92. // 判断跳转到的时间是否大于最老索引时间
  93. if tempYear == oldestYear {
  94. if tempWeek < oldestWeek {
  95. break
  96. }
  97. } else if tempYear < oldestYear {
  98. break
  99. }
  100. // 判断是否超过最新索引时间
  101. if tempYear > newestYear {
  102. continue
  103. }
  104. if tempYear == newestYear && tempWeek > newestWeek {
  105. continue
  106. }
  107. tempIndex := es2.GetAppointIndexFromAlias(utils.IntToStr(tempYear), utils.IntToStr(tempWeek))
  108. indexes = append(indexes, tempIndex)
  109. }
  110. // 构建查询条件
  111. recordBoolQuery := elastic.NewBoolQuery()
  112. recordBoolQuery.Filter(elastic.NewTermQuery("uid", uid))
  113. searchRecordResult, err := es.EsClient.Search().
  114. Index(indexes...).
  115. Query(boolQuery).
  116. Pretty(true).
  117. Do(context.Background())
  118. if searchRecordResult == nil {
  119. return nil, "", 0, errors.New("failed to get egg score flows")
  120. }
  121. //var recordResults []md.UserEggFlowReqRespList
  122. // 检查是否有结果
  123. if searchRecordResult.Hits.TotalHits.Value != 0 {
  124. // 解析结果
  125. for _, hit := range searchRecordResult.Hits.Hits {
  126. var doc md.UserEggFlowReqRespList
  127. err = json.Unmarshal(hit.Source, &doc)
  128. if err != nil {
  129. return nil, "", 0, errors.New("failed to unmarshal")
  130. }
  131. results = append(results, doc)
  132. }
  133. }
  134. list := make([]md.EggPointRecordNode, len(results))
  135. for i, result := range results {
  136. year, week, startAt, endAt := GetWeekInfo(result.UpdatedAt)
  137. list[i].Score = utils.Float64ToStr(result.ScoreValue)
  138. list[i].Year = year
  139. list[i].Week = week
  140. list[i].StartAt = startAt
  141. list[i].EndAt = endAt
  142. }
  143. return list, nowScore, indexNum, nil
  144. }