|
- package svc
-
- import (
- "applet/app/md"
- "applet/app/utils"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
- "context"
- "encoding/json"
- "errors"
- "github.com/olivere/elastic/v7"
- "time"
- )
-
- func GetEggPointRecordBase(now time.Time, uid int64, page int, limit int) ([]md.EggPointRecordNode, string, int, error) {
- aliasName := md2.EggEnergyUserEggScoreEsAlias
- // 1. 判断当周分数是否生效
- getLastWeek := true
- if now.Weekday() > time.Wednesday || (now.Weekday() == time.Wednesday && now.Hour() > 14) {
- getLastWeek = false
- }
- nowIndex := md2.EggEnergyUserEggScoreEsAlias + "_" + es2.GetLatestEffectiveIndexFromAlias(now)
-
- // 2. 计算有效周数数量
- aliases, err := es.EsClient.Aliases().
- Index(aliasName). // 指定别名
- Do(context.Background())
- if err != nil {
- return nil, "", 0, err
- }
-
- // 3. 获取最老有效索引及年份周数
- indexNum := 0
- oldestIndex := aliasName + "_" + "999999"
- newestIndex := aliasName + "_" + "000000"
- for key, _ := range aliases.Indices {
- if key < oldestIndex {
- oldestIndex = key
- }
- if key > newestIndex {
- newestIndex = key
- }
- // 计算可供查询的周数
- if key <= nowIndex {
- indexNum++
- }
- }
- if indexNum < 1 {
- return nil, "60", 0, errors.New("no data")
- }
- oldestYearStr, oldestWeekStr := GetYearsAndWeekStr(oldestIndex)
- oldestYear := utils.StrToInt(oldestYearStr)
- oldestWeek := utils.StrToInt(oldestWeekStr)
- newestYearStr, newestWeekStr := GetYearsAndWeekStr(newestIndex)
- newestYear := utils.StrToInt(newestYearStr)
- newestWeek := utils.StrToInt(newestWeekStr)
-
- // 4. 获取当期有效蛋蛋分
- boolQuery := elastic.NewBoolQuery()
- boolQuery.Filter(elastic.NewTermQuery("uid", uid))
- searchResult, err := es.EsClient.Search().
- Index(nowIndex).
- Query(boolQuery).
- Pretty(true).
- Do(context.Background())
- var results []md.UserEggFlowReqRespList
- nowScore := "60"
- if searchResult != nil {
- var results []md.UserEggFlowReqRespList
- if searchResult.Hits.TotalHits.Value != 0 {
- // 解析结果
- for _, hit := range searchResult.Hits.Hits {
- var doc md.UserEggFlowReqRespList
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return nil, "", 0, errors.New("failed to unmarshal")
- }
- results = append(results, doc)
- }
- }
- if len(results) > 0 {
- nowScore = utils.Float64ToStr(results[0].ScoreValue)
- if nowScore == "0" {
- nowScore = "60"
- }
- }
- }
-
- var indexes []string
- // 5. 构造分页索引列表 查询历史蛋蛋分
- for i := 0; i < limit && i < indexNum; i++ {
- var tempDays int
- if getLastWeek {
- // 未到周三下午两点,从上上周开始查询
- tempDays = 7 * ((page-1)*limit + i + 2)
- } else {
- // 已过周三下午两点,从上周开始查询
- tempDays = 7 * ((page-1)*limit + i + 1)
- }
- tempTime := now.AddDate(0, 0, -tempDays)
- tempYear, tempWeek := tempTime.ISOWeek()
- // 判断跳转到的时间是否大于最老索引时间
- if tempYear == oldestYear {
- if tempWeek < oldestWeek {
- break
- }
- } else if tempYear < oldestYear {
- break
- }
- // 判断是否超过最新索引时间
- if tempYear > newestYear {
- continue
- }
- if tempYear == newestYear && tempWeek > newestWeek {
- continue
- }
- tempIndex := es2.GetAppointIndexFromAlias(utils.IntToStr(tempYear), utils.IntToStr(tempWeek))
- indexes = append(indexes, tempIndex)
- }
- // 构建查询条件
- recordBoolQuery := elastic.NewBoolQuery()
- recordBoolQuery.Filter(elastic.NewTermQuery("uid", uid))
- searchRecordResult, err := es.EsClient.Search().
- Index(indexes...).
- Query(boolQuery).
- Pretty(true).
- Do(context.Background())
- if searchRecordResult == nil {
- return nil, "", 0, errors.New("failed to get egg score flows")
- }
- //var recordResults []md.UserEggFlowReqRespList
- // 检查是否有结果
- if searchRecordResult.Hits.TotalHits.Value != 0 {
- // 解析结果
- for _, hit := range searchRecordResult.Hits.Hits {
- var doc md.UserEggFlowReqRespList
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- return nil, "", 0, errors.New("failed to unmarshal")
- }
- if doc.ScoreValue == 0 {
- doc.ScoreValue = 60
- }
- results = append(results, doc)
- }
- }
-
- list := make([]md.EggPointRecordNode, len(results))
- for i, result := range results {
- year, week, startAt, endAt := GetWeekInfo(result.UpdatedAt)
- list[i].Score = utils.Float64ToStr(result.ScoreValue)
- list[i].Year = year
- list[i].Week = week
- list[i].StartAt = startAt
- list[i].EndAt = endAt
- }
-
- return list, nowScore, indexNum, nil
- }
|