diff --git a/_const/redis_key/redisPrefix.go b/_const/redis_key/redisPrefix.go index 57d5e58263323437b0f5b393d85c55f3fbd651e7..b1a22ce6e2974559edaef1b3c0db8994ee1399d6 100644 --- a/_const/redis_key/redisPrefix.go +++ b/_const/redis_key/redisPrefix.go @@ -261,6 +261,9 @@ const groupPowerDiamond = "group_diamond_{period}" // 国家Icon const countryIcon = "contry_icon" +// 国家Area +const countryArea = "contry:area" + // 群贡献前三 const groupTop3Consume = "group_top3_consume_{period}_{groupId}" @@ -778,6 +781,10 @@ func GetCountryIconKey() string { return countryIcon } +func GetCountryAreaKey() string { + return countryArea +} + func GetGroupTop3ConsumeKey(period string, groupId string) string { return strings.Replace(strings.Replace(groupTop3Consume, "{period}", period, -1), "groupId", groupId, -1) } diff --git a/domain/cache/res_c/country.go b/domain/cache/res_c/country.go index e6cc993a7a801a0872d7427c55161c2e0fd4a830..3e5744da6c4c92fe621cdf65d0a331ddc5152390 100644 --- a/domain/cache/res_c/country.go +++ b/domain/cache/res_c/country.go @@ -36,3 +36,32 @@ func SaveAllCountryIcon(model *domain.Model, icons map[string]string) (int64, er } return ret, err } + +func GetCountryAreaMap(model *domain.Model) (map[string]string, error) { + m, err := GetAllCountryArea(model) + if err != nil || len(m) <= 0 { + m, err = res_m.GetAllCountriesArea(model) + if err != nil { + return nil, err + } + if len(m) <= 0 { + return map[string]string{}, nil + } + SaveAllCountryArea(model, m) + } + return m, nil +} + +func GetAllCountryArea(model *domain.Model) (map[string]string, error) { + key := redis_key.GetCountryAreaKey() + return model.Redis.HGetAll(context.Background(), key).Result() +} + +func SaveAllCountryArea(model *domain.Model, data map[string]string) (int64, error) { + key := redis_key.GetCountryAreaKey() + ret, err := model.Redis.HSet(context.Background(), key, data).Result() + if err == nil { + model.Redis.Expire(context.Background(), key, time.Minute*10) + } + return ret, err +} diff --git a/domain/model/res_m/country.go b/domain/model/res_m/country.go index 7017ea86be284aba4c0a876d9c6988fa7a5602d1..b139ad9fb6d09125a84d49af2c19344ceae03b39 100644 --- a/domain/model/res_m/country.go +++ b/domain/model/res_m/country.go @@ -23,6 +23,7 @@ type ResCountry struct { StandardShortName mysql.Str AreaCode mysql.Str AreaCodeName mysql.Str + Area mysql.Str } type ResLanguage struct { @@ -81,6 +82,22 @@ func GetAllCountries(model *domain.Model) (map[string]string, error) { return result, nil } +// 获取所有国家的Area信息 +func GetAllCountriesArea(model *domain.Model) (map[string]string, error) { + var countrys []ResCountry + if err := model.Db.Model(&ResCountry{}).Where(&ResCountry{ + Status: mysql.USER, + }).Find(&countrys).Error; err != nil { + return nil, myerr.WrapErr(err) + } + + result := make(map[string]string, 0) + for _, i := range countrys { + result[i.Name] = i.Area + } + return result, nil +} + func GetAllCountryByFilter(model *domain.Model, shortNames []string) ([]ResCountry, error) { var countrys []ResCountry if err := model.Db.Model(&ResCountry{}).Where(&ResCountry{ @@ -196,4 +213,3 @@ func GetLangeByCountry(db *gorm.DB, country mysql.Str) (string, error) { return "", myerr.WrapErr(err) } } - diff --git a/route/group_r/group_list.go b/route/group_r/group_list.go index 5a31d97031ea7bbfb83a2a119dd36bb03771245b..8b60bba8baf3a357df1c5a828e01292824c3fe95 100644 --- a/route/group_r/group_list.go +++ b/route/group_r/group_list.go @@ -1,6 +1,7 @@ package group_r import ( + "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/resource/mysql" @@ -155,6 +156,20 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { //} //logstr += " |" + // 获取国家信息 + _, area, err := user_m.GetUserCountryArea(model, myUserId) + if err != nil { + model.Log.Errorf("GetUserCountryArea 获取国家资源错误 userId:%d, err:%v", myUserId, err) + return myContext, err + } + myArea := fmt.Sprintf("%d", area) + // 国家区域信息 + resAreaMap, err := res_c.GetCountryAreaMap(model) + if err != nil { + return myContext, err + } + areaScore := make(map[string]int) + countryScore := make(map[string]int) if len(myCountry) > 0 { for _, i := range sortedGroupIds { @@ -163,6 +178,13 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { } else { countryScore[i] = 0 } + if cArea, ok := resAreaMap[groups[i].Country]; ok { + if myArea == cArea { + areaScore[i] = 1 + } else { + areaScore[i] = 0 + } + } } } model.Log.Infof("GetPopularGroups, countryScore[*]: %v,cost:%v", countryScore, time.Now().Sub(start)) @@ -192,6 +214,15 @@ func GetPopularGroups(c *gin.Context) (*mycontext.MyContext, error) { } else if countryScore[gi] < countryScore[gj] { return false } + // 不是我的国家,按区域排序 + if countryScore[gi] == 0 { + if areaScore[gi] > areaScore[gj] { + return true + } else if areaScore[gi] < areaScore[gj] { + return false + } + } + // 2、按麦上人数多少排序 if micGroupNum[gi] > micGroupNum[gj] { return true