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

158 lines
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. // 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. // 未到周三下午两点,从上上周开始查询
  91. tempDays = 7 * ((page-1)*limit + i + 2)
  92. } else {
  93. // 已过周三下午两点,从上周开始查询
  94. tempDays = 7 * ((page-1)*limit + i + 1)
  95. }
  96. tempTime := now.AddDate(0, 0, -tempDays)
  97. tempYear, tempWeek := tempTime.ISOWeek()
  98. // 判断跳转到的时间是否大于最老索引时间
  99. if tempYear == oldestYear {
  100. if tempWeek < oldestWeek {
  101. break
  102. }
  103. } else if tempYear < oldestYear {
  104. break
  105. }
  106. // 判断是否超过最新索引时间
  107. if tempYear > newestYear {
  108. continue
  109. }
  110. if tempYear == newestYear && tempWeek > newestWeek {
  111. continue
  112. }
  113. tempIndex := es2.GetAppointIndexFromAlias(utils.IntToStr(tempYear), utils.IntToStr(tempWeek))
  114. indexes = append(indexes, tempIndex)
  115. }
  116. // 构建查询条件
  117. recordBoolQuery := elastic.NewBoolQuery()
  118. recordBoolQuery.Filter(elastic.NewTermQuery("uid", uid))
  119. searchRecordResult, err := es.EsClient.Search().
  120. Index(indexes...).
  121. Query(boolQuery).
  122. Pretty(true).
  123. Do(context.Background())
  124. if searchRecordResult == nil {
  125. return nil, "", 0, errors.New("failed to get egg score flows")
  126. }
  127. //var recordResults []md.UserEggFlowReqRespList
  128. // 检查是否有结果
  129. if searchRecordResult.Hits.TotalHits.Value != 0 {
  130. // 解析结果
  131. for _, hit := range searchRecordResult.Hits.Hits {
  132. var doc md.UserEggFlowReqRespList
  133. err = json.Unmarshal(hit.Source, &doc)
  134. if err != nil {
  135. return nil, "", 0, errors.New("failed to unmarshal")
  136. }
  137. results = append(results, doc)
  138. }
  139. }
  140. list := make([]md.EggPointRecordNode, len(results))
  141. for i, result := range results {
  142. year, week, startAt, endAt := GetWeekInfo(result.UpdatedAt)
  143. list[i].Score = utils.Float64ToStr(result.ScoreValue)
  144. list[i].Year = year
  145. list[i].Week = week
  146. list[i].StartAt = startAt
  147. list[i].EndAt = endAt
  148. }
  149. return list, nowScore, indexNum, nil
  150. }