From f34d88466a397d7287d2e698d8ae668ae5504516 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 16:48:17 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/cron.go | 3 +- cron/gift_cron/send_gift_redis.go | 2 +- cron/group_cron/group_list.go | 28 +++ domain/model/res_m/country.go | 7 + domain/service/group_s/group_list.go | 245 +++++++++++++++++++++++++++ route/group_r/group_list.go | 199 +++++++++++++++++----- 6 files changed, 443 insertions(+), 41 deletions(-) create mode 100644 cron/group_cron/group_list.go create mode 100644 domain/service/group_s/group_list.go diff --git a/cron/cron.go b/cron/cron.go index 6acb656..985e303 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -19,7 +19,8 @@ func Init() { group_cron.GroupPowerExpClear() // 清理家族经验/等级 group_cron.GroupPowerMonthRankAct() //group_cron.GroupInEventInit() // 进房事件 - group_cron.CreateGroup() // + //group_cron.CreateGroup() // group_cron.CalcGroupSupport() // 群组扶持计算 //group_cron.CalcGroupSupport_OldData() + group_cron.GroupCountryListSort() } diff --git a/cron/gift_cron/send_gift_redis.go b/cron/gift_cron/send_gift_redis.go index 1a93f16..73d941a 100644 --- a/cron/gift_cron/send_gift_redis.go +++ b/cron/gift_cron/send_gift_redis.go @@ -111,7 +111,7 @@ func groupSupportAddConsume(model *domain.Model, sendGiftEvent *gift_ev.SendGift } _, _, period := group_m.GetSupportLevelTime(time.Now()) // 钻石数 - diamond := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum + diamond := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) keyDiamond := rediskey.GetGroupSupportConsumeSummary(period) _, err := model.RedisCluster.ZIncrBy(context.Background(), keyDiamond, float64(diamond), sendGiftEvent.SceneUid).Result() if err != nil { diff --git a/cron/group_cron/group_list.go b/cron/group_cron/group_list.go new file mode 100644 index 0000000..d35947c --- /dev/null +++ b/cron/group_cron/group_list.go @@ -0,0 +1,28 @@ +package group_cron + +import ( + "git.hilo.cn/hilo-common/domain" + "github.com/robfig/cron" + "hilo-group/domain/service/group_s" +) + +// 清理家族经验和等级 +func GroupCountryListSort() { + + // 常用国家-每15分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 + spec := "0 */15 * * * ?" + c := cron.New() + _ = c.AddFunc(spec, func() { + var model = domain.CreateModelNil() + group_s.SortGroupCommonCountryList(model) + }) + + // 非常用国家-每60分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 + spec2 := "0 15 * * * ?" + _ = c.AddFunc(spec2, func() { + var model = domain.CreateModelNil() + group_s.SortGroupNotCommonCountryList(model) + }) + + c.Start() +} diff --git a/domain/model/res_m/country.go b/domain/model/res_m/country.go index b139ad9..6d23b29 100644 --- a/domain/model/res_m/country.go +++ b/domain/model/res_m/country.go @@ -213,3 +213,10 @@ func GetLangeByCountry(db *gorm.DB, country mysql.Str) (string, error) { return "", myerr.WrapErr(err) } } + +//获取所有国家名字列表 +func GetCountryNameList(model *domain.Model) ([]string, error) { + res := make([]string, 0) + err := model.DB().Select("distinct(name) name").Pluck("name", &res).Error + return res, myerr.WrapErr(err) +} diff --git a/domain/service/group_s/group_list.go b/domain/service/group_s/group_list.go new file mode 100644 index 0000000..fe6a416 --- /dev/null +++ b/domain/service/group_s/group_list.go @@ -0,0 +1,245 @@ +package group_s + +import ( + "context" + "git.hilo.cn/hilo-common/_const/rediskey" + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/resource/redisCli" + "github.com/go-redis/redis/v8" + "hilo-group/_const/enum/gift_e" + "hilo-group/cv/gift_cv" + "hilo-group/domain/cache/room_c" + "hilo-group/domain/model/group_m" + "hilo-group/domain/model/res_m" + "sort" + "strconv" + "time" +) + +func SortGroupCommonCountryList(model *domain.Model) { + // 常用的国家 + countryMap := map[string]struct{}{"India": {}, "Indonesia": {}, "Iraq": {}, "KSA": {}, "Kuwait": {}, "Pakistan": {}, "Turkey": {}} + for country, _ := range countryMap { + sortGroupList, err := GetGroupSortList(model, country) + if err != nil { + model.Log.Errorf("SortGroupCommonCountryList err:%v", err) + return + } + // 写入redis + err = setToRedis(model, country, sortGroupList) + if err != nil { + model.Log.Errorf("SortGroupCommonCountryList country:%v, len(sortGroupList):%v, err:%v", country, len(sortGroupList), err) + return + } + time.Sleep(time.Second * 3) + } +} + +func SortGroupNotCommonCountryList(model *domain.Model) { + // 常用的国家 + countryMap := map[string]struct{}{"India": {}, "Indonesia": {}, "Iraq": {}, "KSA": {}, "Kuwait": {}, "Pakistan": {}, "Turkey": {}} + // 取所有的国家名字 + allCountryList, err := res_m.GetCountryNameList(model) + if err != nil { + model.Log.Errorf("SortGroupNotCommonCountryList err:%v", err) + return + } + for _, country := range allCountryList { + if _, ok := countryMap[country]; ok { + continue + } + // 计算非常用国家 + sortGroupList, err := GetGroupSortList(model, country) + if err != nil { + model.Log.Errorf("SortGroupNotCommonCountryList err:%v", err) + return + } + // 写入redis + err = setToRedis(model, country, sortGroupList) + if err != nil { + model.Log.Errorf("SortGroupNotCommonCountryList country:%v, len(sortGroupList):%v, err:%v", country, len(sortGroupList), err) + return + } + time.Sleep(time.Second * 5) + } +} + +func setToRedis(model *domain.Model, country string, groupList []string) error { + // 写入redis + key := rediskey.GetGroupCountrySortList(country) + for idx, group := range groupList { + err := redisCli.GetRedis().ZRemRangeByRank(context.Background(), key, int64(idx), int64(idx)).Err() // 先删除旧的 + if err != nil { + model.Log.Errorf("setToRedis SortGroup key:%v, idx:%v, err:%v", key, idx, err) + return err + } + // 插入 + err = redisCli.GetRedis().ZAdd(context.Background(), key, &redis.Z{Score: float64(idx), Member: group}).Err() + if err != nil { + model.Log.Errorf("setToRedis SortGroup key:%v, idx:%v, group:%s, err:%v", key, idx, group, err) + return err + } + if idx%1000 == 0 { + time.Sleep(time.Millisecond * 50) + } + } + return nil +} + +// 计算国家群组列表排序 +func GetGroupSortList(model *domain.Model, country string) ([]string, error) { + bannedGroups, err := group_m.GetBannedGroupsMap(model) + if err != nil { + return nil, err + } + beginTime := time.Now() + groups, banCount, visitCount, err := GetCandidatesByCountry(model, bannedGroups, country) + if err != nil { + return nil, err + } + endTime := time.Now() + model.Log.Infof("GroupCountryListSort: candidates size = %d, takes %d ms banned = %d, visitCount size = %d", + len(groups), endTime.Sub(beginTime).Milliseconds(), banCount, len(visitCount)) + + // 获取麦上有人的所有群组及麦上人数 + micGroupNum, err := group_m.GetMicHasInGroupNum(model) + if err != nil { + return nil, err + } + model.Log.Infof("GroupCountryListSort, micGroupNum : %v", micGroupNum) + model.Log.Infof("GroupCountryListSort cost2:%v", time.Now().Sub(beginTime)) + + sortedGroupIds := make([]string, 0) + diamondGroupIds := make([]string, 0) + for i, _ := range groups { + // 麦上没人也放出来 + sortedGroupIds = append(sortedGroupIds, i) + // 麦上有人才计算流水 + if micGroupNum[i] > 0 { + diamondGroupIds = append(diamondGroupIds, i) + } + } + + now := time.Now() + bTime := now.Add(-time.Minute * 30) + g := gift_cv.GiftOperate{SceneType: gift_e.GroupSceneType} + diamonds, err := g.GetRangeConsumeSummaryV2(bTime, now, diamondGroupIds) + if err != nil { + return nil, err + } + model.Log.Infof("GroupCountryListSort, diamonds in 30 mins: %v", diamonds) + model.Log.Infof("GroupCountryListSort cost3:%v", time.Now().Sub(beginTime)) + + supportLevels, err := NewGroupService(model.MyContext).GetWeekMaxSupportLevelMap() + if err != nil { + return nil, err + } + model.Log.Infof("GroupCountryListSort, supportLevels : %v", supportLevels) + model.Log.Infof("GroupCountryListSort cost4:%v", time.Now().Sub(beginTime)) + + // 排序优先级2022-07-25 + sort.Slice(sortedGroupIds, func(i, j int) bool { + gi := sortedGroupIds[i] + gj := sortedGroupIds[j] + + // 1、按麦上人数多少排序 + if micGroupNum[gi] > micGroupNum[gj] { + return true + } else if micGroupNum[gi] < micGroupNum[gj] { + return false + } + + // 2、麦上人数相同,按30分钟内送礼钻石数排序 + if diamonds[gi] > diamonds[gj] { + return true + } else if diamonds[gi] < diamonds[gj] { + return false + } + // 3. 根据热度排序groupInUserDuration + if visitCount[gi] > visitCount[gj] { + return true + } else if visitCount[gi] < visitCount[gj] { + return false + } + + // * Final resort: 群组CODE,短号优先,然后按字母序 + return len(groups[gi].Code) < len(groups[gj].Code) || len(groups[gi].Code) == len(groups[gj].Code) && groups[gi].Code < groups[gj].Code + }) + model.Log.Infof("GroupCountryListSort cost5:%v", time.Now().Sub(beginTime)) + return sortedGroupIds, nil +} + +// 国家群候选:没有密码且没被封禁的群, 有国家 +func GetCandidatesByCountry(model *domain.Model, bannedGroups map[string]uint64, country string) (map[string]*group_m.GroupInfo, int, map[string]int64, error) { + noPwdGroups, err := group_m.FindOwnerCountryGroups(model, country) + if err != nil { + return nil, 0, nil, err + } + var groupIds []string + for _, v := range noPwdGroups { + groupIds = append(groupIds, v.ImGroupId) // imGroupId + } + + //roomVisitCount, err := room_c.GetAllRoomVisitCount() + roomVisitCount, err := room_c.MGetRoomVisitCount(groupIds) + if err != nil { + return nil, 0, nil, err + } + gameRoom := group_m.GetGameGroupsMap(model) + + banCount := 0 + groups := make(map[string]*group_m.GroupInfo, 0) + visitCount := make(map[string]int64) + for i, v := range noPwdGroups { + // 过滤掉被封禁的群 + if bannedGroups[v.ImGroupId] != 0 { + banCount++ + continue + } + // 过滤游戏房 + if gameRoom[v.ImGroupId] { + continue + } + + // 先从二级缓存中找 + if c, ok := roomVisitCount[v.ImGroupId]; ok { + if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 { + //model.Log.Debugf("getPopularCandidates, from roomVisitCount %s(%s) - %d", v.ImGroupId, v.Code, vc) + + groups[v.ImGroupId] = &noPwdGroups[i] + visitCount[v.ImGroupId] = vc + } + } else { + // 如果没有,就从roomVisit中取 + if vc, err := room_c.GetSetRoomVisitCount(v.ImGroupId); err == nil && vc > 0 { + model.Log.Infof("getPopularCandidates, from roomVisit %s(%s) - %d", v.ImGroupId, v.Code, vc) + + groups[v.ImGroupId] = &noPwdGroups[i] + visitCount[v.ImGroupId] = vc + } + } + } + return groups, banCount, visitCount, nil +} + +func GetVisitCount(groupIds []string) (map[string]int64, error) { + roomVisitCount, err := room_c.MGetRoomVisitCount(groupIds) + if err != nil { + return nil, err + } + visitCount := make(map[string]int64) + for _, v := range groupIds { + // 先从二级缓存中找 + if c, ok := roomVisitCount[v]; ok { + if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 { + visitCount[v] = vc + } + } else { + // 如果没有,就从roomVisit中取 + if vc, err := room_c.GetSetRoomVisitCount(v); err == nil && vc > 0 { + visitCount[v] = vc + } + } + } + return visitCount, nil +} diff --git a/route/group_r/group_list.go b/route/group_r/group_list.go index ed244d7..e4dd222 100644 --- a/route/group_r/group_list.go +++ b/route/group_r/group_list.go @@ -1,10 +1,13 @@ package group_r import ( + "context" "fmt" + "git.hilo.cn/hilo-common/_const/rediskey" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/resource/mysql" + "git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/utils" "github.com/gin-gonic/gin" @@ -18,7 +21,6 @@ import ( "hilo-group/cv/user_cv" "hilo-group/domain/cache/group_c" "hilo-group/domain/cache/res_c" - "hilo-group/domain/cache/room_c" "hilo-group/domain/model/game_m" "hilo-group/domain/model/group_m" "hilo-group/domain/model/noble_m" @@ -1251,7 +1253,7 @@ func GetGroupByCountry(c *gin.Context) (*mycontext.MyContext, error) { //model.Log.Infof("GetGroupByCountry: page size = %d, page index = %d, banMap %v", pageSize, pageIndex, bannedGroups) beginTime := time.Now() - groups, banCount, visitCount, err := getCandidatesByCountry(model, bannedGroups, countryShortName) + groups, banCount, visitCount, err := group_s.GetCandidatesByCountry(model, bannedGroups, countryShortName) if err != nil { return myContext, err } @@ -1461,57 +1463,176 @@ func GetGroupByCountry(c *gin.Context) (*mycontext.MyContext, error) { return myContext, nil } -// 国家群候选:没有密码且没被封禁的群, 有国家 -func getCandidatesByCountry(model *domain.Model, bannedGroups map[string]uint64, country string) (map[string]*group_m.GroupInfo, int, map[string]int64, error) { - noPwdGroups, err := group_m.FindOwnerCountryGroups(model, country) +func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { + myContext := mycontext.CreateMyContext(c.Keys) + myUserId, err := req.GetUserId(c) if err != nil { - return nil, 0, nil, err + return myContext, err } - var groupIds []string - for _, v := range noPwdGroups { - groupIds = append(groupIds, v.ImGroupId) // imGroupId + + countryShortName := c.Query("countryShortName") + if countryShortName == "" { + return myContext, myerr.NewSysError("countryShortName 为必填项") } + pageSize, err := strconv.Atoi(c.Query("pageSize")) + if err != nil || pageSize <= 0 { + pageSize = 10 + } + pageIndex, err := strconv.Atoi(c.Query("pageIndex")) + if err != nil || pageIndex <= 0 { + pageIndex = 1 + } + + model := domain.CreateModelContext(myContext) - //roomVisitCount, err := room_c.GetAllRoomVisitCount() - roomVisitCount, err := room_c.MGetRoomVisitCount(groupIds) + // 从redis分页取群组id + sortedGroupIds := make([]string, 0, pageSize) + beginPos := int64(pageSize * (pageIndex - 1)) + endPos := int64(pageSize*pageIndex - 1) + key := rediskey.GetGroupCountrySortList(countryShortName) + zList, err := redisCli.GetRedis().ZRangeWithScores(context.Background(), key, beginPos, endPos).Result() if err != nil { - return nil, 0, nil, err + model.Log.Errorf("GetGroupByCountry err:%v", err) + return myContext, err + } + for _, v := range zList { + sortedGroupIds = append(sortedGroupIds, v.Member.(string)) } - gameRoom := group_m.GetGameGroupsMap(model) - banCount := 0 - groups := make(map[string]*group_m.GroupInfo, 0) - visitCount := make(map[string]int64) - for i, v := range noPwdGroups { - // 过滤掉被封禁的群 - if bannedGroups[v.ImGroupId] != 0 { - banCount++ - continue - } - // 过滤游戏房 - if gameRoom[v.ImGroupId] { - continue + hotGroupList := make([]*group_m.GroupInfo, 0) + // * 同语言区 ^ 麦上有人 + 开放群 - 需要等级的群 + for _, i := range sortedGroupIds { + gInfo, err := group_m.GetGroupInfo(model, i) + if err != nil { + model.Log.Errorf("GetGroupByCountry err:%v", err) + return myContext, err } + hotGroupList = append(hotGroupList, gInfo) + } + total, err := redisCli.GetRedis().ZCard(context.Background(), key).Uint64() + if err != nil { + model.Log.Errorf("GetGroupByCountry err:%v", err) + return myContext, err + } - // 先从二级缓存中找 - if c, ok := roomVisitCount[v.ImGroupId]; ok { - if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 { - //model.Log.Debugf("getPopularCandidates, from roomVisitCount %s(%s) - %d", v.ImGroupId, v.Code, vc) + // 返回结果 + result := make([]group_cv.PopularGroupInfo, 0) - groups[v.ImGroupId] = &noPwdGroups[i] - visitCount[v.ImGroupId] = vc - } - } else { - // 如果没有,就从roomVisit中取 - if vc, err := room_c.GetSetRoomVisitCount(v.ImGroupId); err == nil && vc > 0 { - model.Log.Infof("getPopularCandidates, from roomVisit %s(%s) - %d", v.ImGroupId, v.Code, vc) + groupIds := make([]string, 0) + owners := make([]uint64, 0) + for _, i := range hotGroupList { + groupIds = append(groupIds, i.ImGroupId) + owners = append(owners, i.Owner) + } + powerIds, powerNames, powerNameplates, powerGrades, err := group_power_cv.BatchGetGroupPower(model.Db, owners) + if err != nil { + return myContext, err + } + groupMedals, err := group_m.BatchGetMedals(model.Db, groupIds) + if err != nil { + return myContext, err + } + resMedal, err := res_m.MedalGetAllMap(model.Db) + if err != nil { + return myContext, err + } - groups[v.ImGroupId] = &noPwdGroups[i] - visitCount[v.ImGroupId] = vc + countryInfo, err := res_c.GetCountryIconMap(model) + if err != nil { + return myContext, err + } + + rr := rocket_m.RocketResult{} + maxStageMap, err := rr.GetMaxStage(model.DB(), groupIds) + if err != nil { + return myContext, err + } + + roomCount, err := group_m.BatchGetRoomCount(model, groupIds) + if err != nil { + return nil, err + } + // 正在进行的游戏 + games := game_m.GetNotEndGamesMap(model) + // 扶持等级 + supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMap() + if err != nil { + return myContext, err + } + visitCount, err := group_s.GetVisitCount(groupIds) + if err != nil { + model.Log.Errorf("GetGroupByCountry err:%v", err) + return myContext, err + } + + for _, i := range hotGroupList { + var maxStage *uint16 = nil + if s, ok := maxStageMap[i.ImGroupId]; ok { + maxStage = &s + } + + medals := make([]medal_cv.PicElement, 0) + if m, ok := groupMedals[i.ImGroupId]; ok { + for _, j := range m { + mId := uint32(j) + if e, ok := resMedal[mId]; ok { + medals = append(medals, medal_cv.PicElement{ + PicUrl: e.PicUrl, + }) + } } } + // 补上房间流水勋章 + var pe *medal_cv.PicElement + _, pe, err = medal_cv.GetGroupConsumeMedal(model, i.ImGroupId) + if err != nil { + model.Log.Infof("GetGroupByCountry: GetGroupConsumeMedal: %s", err.Error()) + } else if pe != nil { + medals = append(medals, medal_cv.PicElement{PicUrl: pe.PicUrl}) + } + + var password *string = nil + if len(i.Password) > 0 && i.Owner != myUserId { + emptyStr := "" + password = &emptyStr + } + result = append(result, group_cv.PopularGroupInfo{ + GroupInfo: group_cv.GroupInfo{ + GroupBasicInfo: group_cv.GroupBasicInfo{ + GroupId: i.TxGroupId, + Name: i.Name, + Introduction: i.Introduction, + Notification: i.Notification, + FaceUrl: i.FaceUrl, + Code: i.Code, + CountryIcon: countryInfo[i.Country], + Password: password, + SupportLevel: supportLevels[i.ImGroupId], + GroupInUserDuration: visitCount[i.ImGroupId], + MicNumType: int(i.MicNumType), + GroupMedals: medals, + }, + HasOnMic: micGroupNum[i.ImGroupId] > 0, + GroupPowerId: powerIds[i.Owner], + GroupPowerName: powerNames[i.Owner], + GroupPowerNameplate: powerNameplates[i.Owner], + GroupPower: group_cv.GroupPower{ + Id: powerIds[i.Owner], + Name: powerNames[i.Owner], + Nameplate: powerNameplates[i.Owner], + Grade: powerGrades[i.Owner], + }, + }, + MicUsers: []user_cv.CvUserTiny{}, + RoomUserCount: uint(roomCount[i.ImGroupId]), + MaxStage: maxStage, + GameTypes: games[i.TxGroupId], + }) } - return groups, banCount, visitCount, nil + + model.Log.Infof("GetGroupByCountry cost6:%v", time.Now().Sub(beginTime)) + resp.ResponsePageOk(c, result, uint(total), pageIndex) + return myContext, nil } // @Tags 资源 -- 2.22.0 From 7998a65ae0120c1a1102e3416a478882e0d2d279 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:24:36 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/group_cron/group_list.go | 4 ++-- domain/model/group_m/mic.go | 34 ++++++++++++++++++++++++++++++++++ route/group_r/group_list.go | 6 +++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cron/group_cron/group_list.go b/cron/group_cron/group_list.go index d35947c..e3574f4 100644 --- a/cron/group_cron/group_list.go +++ b/cron/group_cron/group_list.go @@ -10,7 +10,7 @@ import ( func GroupCountryListSort() { // 常用国家-每15分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 - spec := "0 */15 * * * ?" + spec := "0 */2 * * * ?" c := cron.New() _ = c.AddFunc(spec, func() { var model = domain.CreateModelNil() @@ -18,7 +18,7 @@ func GroupCountryListSort() { }) // 非常用国家-每60分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 - spec2 := "0 15 * * * ?" + spec2 := "0 28 * * * ?" _ = c.AddFunc(spec2, func() { var model = domain.CreateModelNil() group_s.SortGroupNotCommonCountryList(model) diff --git a/domain/model/group_m/mic.go b/domain/model/group_m/mic.go index 20ba960..3dce551 100644 --- a/domain/model/group_m/mic.go +++ b/domain/model/group_m/mic.go @@ -920,6 +920,40 @@ func GetMicHasInGroupNum(model *domain.Model) (map[string]int64, error) { return resultGroupUuids, nil } +// 获取麦上人数 +func GetMicHasInGroupNumByList(model *domain.Model, groupIds []string) (map[string]int64, error) { + resultGroupUuids := make(map[string]int64, len(groupIds)) + micHasInKey := redis_key.GetPrefixGroupMicHasIn() + for _, group := range groupIds { + isMem, err := redisCli.GetRedis().SIsMember(context.Background(), micHasInKey, group).Result() + if err != nil { + model.Log.Errorf("GetMicHasInGroupNumByList err:%v", err) + return nil, myerr.WrapErr(err) + } + if isMem { + s := strings.Replace(micHasInScript, "{key}", micHasInKey, -1) + s = strings.Replace(s, "{remKey}", group, -1) + for i := 1; i <= MaxMicNum; i++ { + s = strings.Replace(s, "{key"+strconv.Itoa(i)+"}", redis_key.GetPrefixGroupMicUser(group, i), -1) + } + //r, err := redis2.NewScript(s).Run(context.Background(), redisCli.GetRedis(), []string{}).Result() + sha1, err := model.Redis.ScriptLoad(model, s).Result() + if err != nil { + return nil, myerr.WrapErr(err) + } + micNum, err := model.Redis.EvalSha(model, sha1, nil, nil).Int64() + if err != nil { + return nil, myerr.WrapErr(err) + } + //d := r.(int64) + if micNum > 0 { + resultGroupUuids[group] = micNum + } + } + } + return resultGroupUuids, nil +} + //获取麦上有人的群组&&麦上的数(有时间性,目前是24小时) func GetMicHasInPeriodGroupUser() (map[string][]uint64, error) { //清理超过12小时的 diff --git a/route/group_r/group_list.go b/route/group_r/group_list.go index e4dd222..3d36459 100644 --- a/route/group_r/group_list.go +++ b/route/group_r/group_list.go @@ -1564,6 +1564,11 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { model.Log.Errorf("GetGroupByCountry err:%v", err) return myContext, err } + // 获取麦上人数 + micGroupNum, err := group_m.GetMicHasInGroupNum(model) + if err != nil { + return myContext, err + } for _, i := range hotGroupList { var maxStage *uint16 = nil @@ -1630,7 +1635,6 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { }) } - model.Log.Infof("GetGroupByCountry cost6:%v", time.Now().Sub(beginTime)) resp.ResponsePageOk(c, result, uint(total), pageIndex) return myContext, nil } -- 2.22.0 From d91bafe133eb56143f14c43188a7435d58725267 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:35:15 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/group_cron/group_list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cron/group_cron/group_list.go b/cron/group_cron/group_list.go index e3574f4..dd56c6a 100644 --- a/cron/group_cron/group_list.go +++ b/cron/group_cron/group_list.go @@ -10,7 +10,7 @@ import ( func GroupCountryListSort() { // 常用国家-每15分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 - spec := "0 */2 * * * ?" + spec := "0 */15 * * * ?" c := cron.New() _ = c.AddFunc(spec, func() { var model = domain.CreateModelNil() @@ -18,7 +18,7 @@ func GroupCountryListSort() { }) // 非常用国家-每60分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 - spec2 := "0 28 * * * ?" + spec2 := "0 43 * * * ?" _ = c.AddFunc(spec2, func() { var model = domain.CreateModelNil() group_s.SortGroupNotCommonCountryList(model) -- 2.22.0 From 2671b9f04dca33757e7ccec0bca85677896a97ce Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:36:27 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/group_cron/group_list.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cron/group_cron/group_list.go b/cron/group_cron/group_list.go index dd56c6a..7f26ad7 100644 --- a/cron/group_cron/group_list.go +++ b/cron/group_cron/group_list.go @@ -14,14 +14,18 @@ func GroupCountryListSort() { c := cron.New() _ = c.AddFunc(spec, func() { var model = domain.CreateModelNil() + model.Log.Infof("GroupCountryListSort Common start") group_s.SortGroupCommonCountryList(model) + model.Log.Infof("GroupCountryListSort Common end") }) // 非常用国家-每60分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 spec2 := "0 43 * * * ?" _ = c.AddFunc(spec2, func() { var model = domain.CreateModelNil() + model.Log.Infof("GroupCountryListSort not Common start") group_s.SortGroupNotCommonCountryList(model) + model.Log.Infof("GroupCountryListSort not Common end") }) c.Start() -- 2.22.0 From 72b685e00173a706bb7a550591276bc77cdd3601 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:39:56 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/group_cron/group_list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cron/group_cron/group_list.go b/cron/group_cron/group_list.go index 7f26ad7..c6175f2 100644 --- a/cron/group_cron/group_list.go +++ b/cron/group_cron/group_list.go @@ -20,7 +20,7 @@ func GroupCountryListSort() { }) // 非常用国家-每60分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 - spec2 := "0 43 * * * ?" + spec2 := "0 46 * * * ?" _ = c.AddFunc(spec2, func() { var model = domain.CreateModelNil() model.Log.Infof("GroupCountryListSort not Common start") -- 2.22.0 From 2d9e6c6654d68b8d21a6e035532e0373e27a4fae Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:50:50 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/service/group_s/group_list.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/domain/service/group_s/group_list.go b/domain/service/group_s/group_list.go index fe6a416..b8e1af9 100644 --- a/domain/service/group_s/group_list.go +++ b/domain/service/group_s/group_list.go @@ -4,7 +4,6 @@ import ( "context" "git.hilo.cn/hilo-common/_const/rediskey" "git.hilo.cn/hilo-common/domain" - "git.hilo.cn/hilo-common/resource/redisCli" "github.com/go-redis/redis/v8" "hilo-group/_const/enum/gift_e" "hilo-group/cv/gift_cv" @@ -68,13 +67,13 @@ func setToRedis(model *domain.Model, country string, groupList []string) error { // 写入redis key := rediskey.GetGroupCountrySortList(country) for idx, group := range groupList { - err := redisCli.GetRedis().ZRemRangeByRank(context.Background(), key, int64(idx), int64(idx)).Err() // 先删除旧的 + err := model.RedisCluster.ZRemRangeByRank(context.Background(), key, int64(idx), int64(idx)).Err() // 先删除旧的 if err != nil { model.Log.Errorf("setToRedis SortGroup key:%v, idx:%v, err:%v", key, idx, err) return err } // 插入 - err = redisCli.GetRedis().ZAdd(context.Background(), key, &redis.Z{Score: float64(idx), Member: group}).Err() + err = model.RedisCluster.ZAdd(context.Background(), key, &redis.Z{Score: float64(idx), Member: group}).Err() if err != nil { model.Log.Errorf("setToRedis SortGroup key:%v, idx:%v, group:%s, err:%v", key, idx, group, err) return err -- 2.22.0 From 5c011de9f50c129a32b796156b4150a55de63944 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:54:13 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/res_m/country.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain/model/res_m/country.go b/domain/model/res_m/country.go index 6d23b29..fdf93c4 100644 --- a/domain/model/res_m/country.go +++ b/domain/model/res_m/country.go @@ -217,6 +217,6 @@ func GetLangeByCountry(db *gorm.DB, country mysql.Str) (string, error) { //获取所有国家名字列表 func GetCountryNameList(model *domain.Model) ([]string, error) { res := make([]string, 0) - err := model.DB().Select("distinct(name) name").Pluck("name", &res).Error + err := model.DB().Table("res_country").Select("distinct(name) name").Pluck("name", &res).Error return res, myerr.WrapErr(err) } -- 2.22.0 From 7ee9713ea2b22feb050d778f4d83f8d435ceff6f Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 17:54:27 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/group_cron/group_list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cron/group_cron/group_list.go b/cron/group_cron/group_list.go index 3eb531d..82f63e2 100644 --- a/cron/group_cron/group_list.go +++ b/cron/group_cron/group_list.go @@ -20,7 +20,7 @@ func GroupCountryListSort() { }) // 非常用国家-每60分钟计算国家房间列表排序 /v1/imGroup/country [get] 接口 - spec2 := "0 51 * * * ?" + spec2 := "0 59 * * * ?" _ = c.AddFunc(spec2, func() { var model = domain.CreateModelNil() model.Log.Infof("GroupCountryListSort not Common start") -- 2.22.0 From 56caba873744b898dad16e3303d8130d0f7a7e80 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 18:13:07 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/group_m/support.go | 18 +++++++++++++++ domain/service/group_s/group.go | 34 ++++++++++++++++++++++++++++ domain/service/group_s/group_list.go | 10 ++++---- route/group_r/group_list.go | 4 ++-- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/domain/model/group_m/support.go b/domain/model/group_m/support.go index 3bfe45a..f8e6472 100644 --- a/domain/model/group_m/support.go +++ b/domain/model/group_m/support.go @@ -57,6 +57,24 @@ func GetAllGroupSupportResult(db *gorm.DB, period string) (map[string]uint8, err return result, nil } +func GetGroupSupportResult(db *gorm.DB, period string, groupIds []string) (map[string]uint8, error) { + if len(period) <= 0 { + return nil, nil + } + gsr := GroupSupportResult{ + Period: period, + } + rows := make([]GroupSupportResult, 0) + if err := db.Where(gsr).Where("group_uid in (?)", groupIds).Find(&rows).Error; err != nil { + return nil, err + } + result := make(map[string]uint8) + for _, i := range rows { + result[i.GroupUid] = i.Grade + } + return result, nil +} + //群组扶持奖励,利益分配者 type GroupSupportAwardAdmin struct { mysql.Entity diff --git a/domain/service/group_s/group.go b/domain/service/group_s/group.go index 25cba03..00d75f4 100644 --- a/domain/service/group_s/group.go +++ b/domain/service/group_s/group.go @@ -65,6 +65,40 @@ func (s *GroupService) GetSupportLevelMap(now time.Time) (map[string]uint8, erro return result, nil } +// 取本周最高的扶持等级 +func (s *GroupService) GetWeekMaxSupportLevelMapByIds(groupIds []string) (map[string]uint8, error) { + return s.GetSupportLevelMapByIds(groupIds, time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY)) +} + +func (s *GroupService) GetSupportLevelMapByIds(groupIds []string, now time.Time) (map[string]uint8, error) { + model := domain.CreateModel(s.svc.CtxAndDb) + + _, _, period := group_m.GetSupportLevelTime(now) + + levels, err := GetAllSupportLevel(model, period) + if err != nil { + return nil, err + } + model.Log.Debugf("GetSupportLevelMapByIds, GET %s: %v", period, levels) + + result := make(map[string]uint8, 0) + if len(levels) > 0 { + for g, l := range levels { + le, err := strconv.ParseUint(l, 10, 8) + if err == nil { + result[g] = uint8(le) + } + } + } else { + result, err = group_m.GetGroupSupportResult(model.DB(), period, groupIds) + if err == nil { + ret, err := SaveAllSupportLevel(model, period, result) + model.Log.Infof("GetSupportLevelMapByIds SAVE ret = %d, err: %v", ret, err) + } + } + return result, nil +} + func SaveAllSupportLevel(model *domain.Model, date string, levels map[string]uint8) (int64, error) { values := make(map[string]interface{}, 0) for g, l := range levels { diff --git a/domain/service/group_s/group_list.go b/domain/service/group_s/group_list.go index b8e1af9..5d6e71d 100644 --- a/domain/service/group_s/group_list.go +++ b/domain/service/group_s/group_list.go @@ -129,11 +129,11 @@ func GetGroupSortList(model *domain.Model, country string) ([]string, error) { model.Log.Infof("GroupCountryListSort, diamonds in 30 mins: %v", diamonds) model.Log.Infof("GroupCountryListSort cost3:%v", time.Now().Sub(beginTime)) - supportLevels, err := NewGroupService(model.MyContext).GetWeekMaxSupportLevelMap() - if err != nil { - return nil, err - } - model.Log.Infof("GroupCountryListSort, supportLevels : %v", supportLevels) + //supportLevels, err := NewGroupService(model.MyContext).GetWeekMaxSupportLevelMap() + //if err != nil { + // return nil, err + //} + //model.Log.Infof("GroupCountryListSort, supportLevels : %v", supportLevels) model.Log.Infof("GroupCountryListSort cost4:%v", time.Now().Sub(beginTime)) // 排序优先级2022-07-25 diff --git a/route/group_r/group_list.go b/route/group_r/group_list.go index 3b8706c..185f1a8 100644 --- a/route/group_r/group_list.go +++ b/route/group_r/group_list.go @@ -736,7 +736,7 @@ func GetRecentGroup(c *gin.Context) (*mycontext.MyContext, error) { } myService := domain.CreateService(myContext) - result, _, err := group_cv.BuildJoinedGroupInfo(myService, userId, groupIds, 30, 1,roomEnterTime) + result, _, err := group_cv.BuildJoinedGroupInfo(myService, userId, groupIds, 30, 1, roomEnterTime) if err != nil { return myContext, err } @@ -1555,7 +1555,7 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { // 正在进行的游戏 games := game_m.GetNotEndGamesMap(model) // 扶持等级 - supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMap() + supportLevels, err := group_s.NewGroupService(myContext).GetWeekMaxSupportLevelMapByIds(groupIds) if err != nil { return myContext, err } -- 2.22.0 From 33a16ca0f2b9773161c441a2cf0877c57ff14e00 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 18:16:46 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- route/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/route/router.go b/route/router.go index 406bf10..c16fb82 100644 --- a/route/router.go +++ b/route/router.go @@ -106,7 +106,7 @@ func InitRouter() *gin.Engine { imGroup.POST("/mgr/clearScreen", wrapper(group_r.GroupMgrClearScreen)) imGroup.GET("/online/users", wrapper(group_r.GroupInUsers)) imGroup.GET("/online/users/new", wrapper(group_r.GroupInUserNew)) - imGroup.GET("/country", wrapper(group_r.GetGroupByCountry)) + imGroup.GET("/country", wrapper(group_r.GetGroupByCountryV2)) imGroup.GET("/country/prior", wrapper(group_r.GroupountryPrior)) // imGroup.POST("/theme/custom", wrapper(group_r.GroupThemeAdd)) -- 2.22.0 From 32e54187d2a815a66bbfc52d69b744d60ba26c1f Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 28 Aug 2023 18:34:39 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- route/group_r/group_list.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/route/group_r/group_list.go b/route/group_r/group_list.go index 657b286..d7ac7ba 100644 --- a/route/group_r/group_list.go +++ b/route/group_r/group_list.go @@ -7,7 +7,6 @@ import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/resource/mysql" - "git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/utils" "github.com/gin-gonic/gin" @@ -1490,7 +1489,7 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { beginPos := int64(pageSize * (pageIndex - 1)) endPos := int64(pageSize*pageIndex - 1) key := rediskey.GetGroupCountrySortList(countryShortName) - zList, err := redisCli.GetRedis().ZRangeWithScores(context.Background(), key, beginPos, endPos).Result() + zList, err := model.RedisCluster.ZRangeWithScores(context.Background(), key, beginPos, endPos).Result() if err != nil { model.Log.Errorf("GetGroupByCountry err:%v", err) return myContext, err @@ -1509,7 +1508,7 @@ func GetGroupByCountryV2(c *gin.Context) (*mycontext.MyContext, error) { } hotGroupList = append(hotGroupList, gInfo) } - total, err := redisCli.GetRedis().ZCard(context.Background(), key).Uint64() + total, err := model.RedisCluster.ZCard(context.Background(), key).Uint64() if err != nil { model.Log.Errorf("GetGroupByCountry err:%v", err) return myContext, err -- 2.22.0 From 04bd5726b9b096bf85200a8a3962553df1439c6f Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Tue, 29 Aug 2023 10:14:48 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E6=89=B6=E6=8C=81?= =?UTF-8?q?=E6=97=A7=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/cron.go | 2 +- cron/gift_cron/send_gift_redis.go | 2 +- cron/group_cron/group_support.go | 151 +++++++++++++++--------------- domain/model/gift_m/gift.go | 4 +- 4 files changed, 82 insertions(+), 77 deletions(-) diff --git a/cron/cron.go b/cron/cron.go index 985e303..3363b4b 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -21,6 +21,6 @@ func Init() { //group_cron.GroupInEventInit() // 进房事件 //group_cron.CreateGroup() // group_cron.CalcGroupSupport() // 群组扶持计算 - //group_cron.CalcGroupSupport_OldData() + group_cron.CalcGroupSupport_OldData() group_cron.GroupCountryListSort() } diff --git a/cron/gift_cron/send_gift_redis.go b/cron/gift_cron/send_gift_redis.go index 35f6c55..eca0712 100644 --- a/cron/gift_cron/send_gift_redis.go +++ b/cron/gift_cron/send_gift_redis.go @@ -95,7 +95,7 @@ func groupPowerStar(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) { // 群组扶持增加流水数据 func groupSupportAddConsume(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) { - if time.Now().Unix() <= 1692843600 { + if time.Now().Unix() <= 1693275900 { return } model.Log.Infof("groupSupportAddConsume UserId:%d, sendGiftEvent:%+v", sendGiftEvent.SendUserId, sendGiftEvent) diff --git a/cron/group_cron/group_support.go b/cron/group_cron/group_support.go index 933e58e..649a63a 100644 --- a/cron/group_cron/group_support.go +++ b/cron/group_cron/group_support.go @@ -1,13 +1,18 @@ package group_cron import ( + "context" "encoding/json" + "git.hilo.cn/hilo-common/_const/rediskey" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/config" + "git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/sdk/tencentyun" "git.hilo.cn/hilo-common/utils" "github.com/robfig/cron" + "hilo-group/_const/enum/gift_e" "hilo-group/_const/enum/group_e" + "hilo-group/domain/model/gift_m" "hilo-group/domain/model/group_m" "hilo-group/domain/service/group_s" "time" @@ -87,76 +92,76 @@ func sendGroupSupportH5(model *domain.Model) error { } // 群组扶持计算-旧数据 -//func CalcGroupSupport_OldData() { -// if !config.IsMaster() { -// return -// } -// c := cron.New() -// spec := "0 20 11 25 8 ?" -// _ = c.AddFunc(spec, func() { -// defer utils.CheckGoPanic() -// var model = domain.CreateModelNil() -// //开始 -// model.Log.Infof("CalcGroupSupport_OldData start") -// -// beginTime, endTime, period := group_m.GetSupportLevelTime(time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY)) -// //beginTime, _, period := group_m.GetSupportLevelTime(time.Now()) -// //endTime := time.Unix(1692843600, 0) -// -// g := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, Model: model} -// records, err := g.BatchGetConsumeByRange(beginTime, endTime) -// if err != nil { -// model.Log.Errorf("CalcGroupSupport_OldData beginTime:%v, endTime:%v, err:%v", beginTime, endTime, err) -// return -// } -// // 流水写入redis -// keyDiamond := rediskey.GetGroupSupportConsumeSummary(period) -// for _, r := range records { -// if r.SceneUid == "" || r.Consume <= 0 { -// continue -// } -// _, err = model.RedisCluster.ZIncrBy(context.Background(), keyDiamond, float64(r.Consume), r.SceneUid).Result() -// if err != nil { -// model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, val:%d, member:%s, err:%v", -// keyDiamond, r.Consume, r.SceneUid, err) -// } -// } -// err = redisCli.SetExpire(model.RedisCluster, keyDiamond, time.Hour*24*14) // 保留两周 -// if err != nil { -// model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, err:%v", keyDiamond, err) -// return -// } -// // 支持者写入redis -// for _, r := range records { -// if r.SceneUid == "" || r.C <= 0 { -// continue -// } -// // 支持者列表 -// support := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, SceneUid: r.SceneUid, Model: model} -// uids, err := support.BatchGetSupportList(beginTime, endTime) -// if err != nil { -// model.Log.Errorf("CalcGroupSupport_OldData beginTime:%v, endTime:%v, imGroupId:%v, err:%v", beginTime, endTime, r.SceneUid, err) -// continue -// } -// if len(uids) <= 0 { -// continue -// } -// // 支持者数量 -// keySupportNum := rediskey.GetGroupSupportCountSupporter(period, r.SceneUid) -// for _, uid := range uids { -// err = model.RedisCluster.SAdd(context.Background(), keySupportNum, uid).Err() -// if err != nil { -// model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, UserId:%d, err:%v", keySupportNum, uid, err) -// } -// } -// err = redisCli.SetExpire(model.RedisCluster, keySupportNum, time.Hour*24*14) // 保留两周 -// if err != nil { -// model.Log.Errorf("AddSendGiftEventAsync groupSupport key:%s, err:%v", keySupportNum, err) -// } -// } -// -// model.Log.Infof("CalcGroupSupport_OldData end") -// }) -// -// c.Start() -//} +func CalcGroupSupport_OldData() { + if !config.IsMaster() { + return + } + c := cron.New() + spec := "0 28 10 29 8 ?" + _ = c.AddFunc(spec, func() { + defer utils.CheckGoPanic() + var model = domain.CreateModelNil() + //开始 + model.Log.Infof("CalcGroupSupport_OldData start") + + //beginTime, endTime, period := group_m.GetSupportLevelTime(time.Now().AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY)) + beginTime, _, period := group_m.GetSupportLevelTime(time.Now()) + endTime := time.Unix(1693275900, 0) + + g := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, Model: model} + records, err := g.BatchGetConsumeByRange(beginTime, endTime) + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData beginTime:%v, endTime:%v, err:%v", beginTime, endTime, err) + return + } + // 流水写入redis + keyDiamond := rediskey.GetGroupSupportConsumeSummary(period) + for _, r := range records { + if r.SceneUid == "" || r.Consume <= 0 { + continue + } + _, err = model.RedisCluster.ZIncrBy(context.Background(), keyDiamond, float64(r.Consume), r.SceneUid).Result() + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, val:%d, member:%s, err:%v", + keyDiamond, r.Consume, r.SceneUid, err) + } + } + err = redisCli.SetExpire(model.RedisCluster, keyDiamond, time.Hour*24*14) // 保留两周 + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, err:%v", keyDiamond, err) + return + } + // 支持者写入redis + for _, r := range records { + if r.SceneUid == "" || r.C <= 0 { + continue + } + // 支持者列表 + support := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, SceneUid: r.SceneUid, Model: model} + uids, err := support.BatchGetSupportList(model, beginTime, endTime) + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData beginTime:%v, endTime:%v, imGroupId:%v, err:%v", beginTime, endTime, r.SceneUid, err) + continue + } + if len(uids) <= 0 { + continue + } + // 支持者数量 + keySupportNum := rediskey.GetGroupSupportCountSupporter(period, r.SceneUid) + for _, uid := range uids { + err = model.RedisCluster.SAdd(context.Background(), keySupportNum, uid).Err() + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, UserId:%d, err:%v", keySupportNum, uid, err) + } + } + err = redisCli.SetExpire(model.RedisCluster, keySupportNum, time.Hour*24*14) // 保留两周 + if err != nil { + model.Log.Errorf("AddSendGiftEventAsync groupSupport key:%s, err:%v", keySupportNum, err) + } + } + + model.Log.Infof("CalcGroupSupport_OldData end") + }) + + c.Start() +} diff --git a/domain/model/gift_m/gift.go b/domain/model/gift_m/gift.go index f3e5192..c97725f 100644 --- a/domain/model/gift_m/gift.go +++ b/domain/model/gift_m/gift.go @@ -182,9 +182,9 @@ func (g *GiftOperate) BatchGetConsumeByRange(beginTime, endTime time.Time) ([]Sc return rows, nil } -func (g *GiftOperate) BatchGetSupportList(beginTime, endTime time.Time) ([]uint64, error) { +func (g *GiftOperate) BatchGetSupportList(model *domain.Model, beginTime, endTime time.Time) ([]uint64, error) { userIds := make([]uint64, 0) - err := g.DB().Model(g). + err := model.DB().Model(g). Select("DISTINCT(send_user_id) AS user_id"). Where(g).Where("created_time BETWEEN ? AND ?", beginTime, endTime).Pluck("user_id", &userIds).Error if err != nil { -- 2.22.0 From 1c6f29d4c52e74b535167442b40ccec483811ae4 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Tue, 29 Aug 2023 10:15:25 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E7=BE=A4=E7=BB=84=E6=89=B6=E6=8C=81?= =?UTF-8?q?=E6=97=A7=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/group_cron/group_support.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cron/group_cron/group_support.go b/cron/group_cron/group_support.go index 649a63a..96515c6 100644 --- a/cron/group_cron/group_support.go +++ b/cron/group_cron/group_support.go @@ -97,7 +97,7 @@ func CalcGroupSupport_OldData() { return } c := cron.New() - spec := "0 28 10 29 8 ?" + spec := "0 27 10 29 8 ?" _ = c.AddFunc(spec, func() { defer utils.CheckGoPanic() var model = domain.CreateModelNil() -- 2.22.0