Commit f2d5e210 authored by chenweijian's avatar chenweijian

家族信息接口

parent db416e73
...@@ -22,6 +22,17 @@ const ( ...@@ -22,6 +22,17 @@ const (
GroupPowerUserRoleUser GroupPowerUserRole = 1 GroupPowerUserRoleUser GroupPowerUserRole = 1
//势力主 //势力主
GroupPowerUserRoleMgr GroupPowerUserRole = 2 GroupPowerUserRoleMgr GroupPowerUserRole = 2
//势力管理员
GroupPowerUserRoleAdmin GroupPowerUserRole = 3
)
//国家势力排行榜类型
type GroupPowerRankType = mysql.Type
const (
GroupPowerRankTypeFamous GroupPowerRankType = 1
GroupPowerRankTypeActive GroupPowerRankType = 2
GroupPowerRankTypeCharm GroupPowerRankType = 3
) )
//国家势力日志操作类型 //国家势力日志操作类型
......
package common
import (
"git.hilo.cn/hilo-common/resource/config"
"strings"
)
// 去除slice中的重复元素
func UniqueSliceUInt64(sliceIn []uint64) []uint64 {
sliceOut := make([]uint64, 0, len(sliceIn))
m := make(map[uint64]struct{}, len(sliceIn))
for _, i := range sliceIn {
if _, ok := m[i]; !ok {
m[i] = struct{}{}
sliceOut = append(sliceOut, i)
}
}
return sliceOut
}
// 补全url,区分处理oss和aws两种情况
func MakeFullUrl(url string) string {
if strings.HasPrefix(url, config.GetConfigOss().OSS_CDN) || strings.HasPrefix(url, config.GetConfigAws().AWS_CDN) {
return url
} else if strings.HasPrefix(url, "nextvideo/") {
return config.GetConfigOss().OSS_CDN + url
} else if strings.HasPrefix(url, config.GetConfigAws().AWS_DIR) {
return config.GetConfigAws().AWS_CDN + url
} else {
return url
}
}
package utime
import (
"github.com/jinzhu/now"
"time"
)
const (
Day = 86400
Layout = "2006-01-02 15:04:05"
LayoutDay = "2006-01-02"
LayoutDay2 = "20060102"
LayoutMonth = "2006-01"
)
// 获取时间所在周的第一天(周一) 00:00:00,和最后一天(周日)23:59:59
func WeekStartEnd(date time.Time) (start, end time.Time) {
w := 0
week := date.Weekday()
if week == time.Sunday {
w = 7
} else {
w = int(week)
}
startTmp := date.AddDate(0, 0, -w+1)
endTmp := date.AddDate(0, 0, 7-w)
start = time.Date(startTmp.Year(), startTmp.Month(), startTmp.Day(), 0, 0, 0, 0, startTmp.Location())
end = time.Date(endTmp.Year(), endTmp.Month(), endTmp.Day(), 23, 59, 59, 999, endTmp.Location())
return
}
// 当天开始结束
func DayStartEnd(date time.Time) (start, end time.Time) {
start = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
end = time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 999, date.Location())
return
}
// 获取时间所在月的第一天(1号) 00:00:00,和最后一天23:59:59
func MonthStartEnd(date time.Time) (start, end time.Time) {
start = time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, date.Location())
lastDay := now.New(start).EndOfMonth().Day()
end = time.Date(date.Year(), date.Month(), lastDay, 23, 59, 59, 999999999, date.Location())
return
}
// 增加年/月
// 因为golang原生的Time.AddDate增加月份的时候有bug
func AddDate(t time.Time, years int, months int) time.Time {
year, month, day := t.Date()
hour, min, sec := t.Clock()
// firstDayOfMonthAfterAddDate: years 年,months 月后的 那个月份的1号
firstDayOfMonthAfterAddDate := time.Date(year+years, month+time.Month(months), 1,
hour, min, sec, t.Nanosecond(), t.Location())
// firstDayOfMonthAfterAddDate 月份的最后一天
lastDay := now.New(firstDayOfMonthAfterAddDate).EndOfMonth().Day()
// 如果 t 的天 > lastDay,则设置为lastDay
// 如:t 为 2020-03-31 12:00:00 +0800,增加1个月,为4月31号
// 但是4月没有31号,则设置为4月最后一天lastDay(30号)
if day > lastDay {
day = lastDay
}
return time.Date(year+years, month+time.Month(months), day,
hour, min, sec, t.Nanosecond(), t.Location())
}
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"hilo-group/cv/user_cv" "hilo-group/cv/user_cv"
"hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/groupPower_m"
"hilo-group/domain/model/group_m" "hilo-group/domain/model/group_m"
"hilo-group/domain/model/user_m"
"hilo-group/myerr" "hilo-group/myerr"
"sort" "sort"
"strconv" "strconv"
...@@ -639,3 +640,19 @@ func GetAllPowerDiamond(period string) (map[string]string, error) { ...@@ -639,3 +640,19 @@ func GetAllPowerDiamond(period string) (map[string]string, error) {
key := redis_key.GetPrefixGroupPowerDiamond(period) key := redis_key.GetPrefixGroupPowerDiamond(period)
return redisCli.GetRedis().HGetAll(context.Background(), key).Result() return redisCli.GetRedis().HGetAll(context.Background(), key).Result()
} }
type GroupPowerInfo struct {
Info *groupPower_m.GroupPower `json:"info"`
Members []*GroupPowerUser `json:"members"`
Stars []*user_m.UserTiny `json:"stars"`
}
type GroupPowerUser struct {
User *user_m.UserTiny `json:"user"`
Role groupPower_e.GroupPowerUserRole `json:"role"`
}
type GroupPowerStart struct {
User *user_m.UserTiny `json:"user"`
RankingType groupPower_e.GroupPowerRankType `json:"rankingType"`
}
...@@ -6,10 +6,12 @@ import ( ...@@ -6,10 +6,12 @@ import (
redisV8 "github.com/go-redis/redis/v8" redisV8 "github.com/go-redis/redis/v8"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"hilo-group/_const/redis_key/user_k" "hilo-group/_const/redis_key/user_k"
"hilo-group/common"
"hilo-group/domain/cache" "hilo-group/domain/cache"
"hilo-group/domain/model/user_m" "hilo-group/domain/model/user_m"
"hilo-group/myerr" "hilo-group/myerr"
"hilo-group/myerr/bizerr" "hilo-group/myerr/bizerr"
"time"
) )
// 获取用户简要信息 // 获取用户简要信息
...@@ -113,3 +115,89 @@ func cacheUserTiny(model *domain.Model, user *user_m.User) error { ...@@ -113,3 +115,89 @@ func cacheUserTiny(model *domain.Model, user *user_m.User) error {
} }
return nil return nil
} }
// 获取用户简要信息
// param userIds 用户id列表
func GetUserTinyMap(model *domain.Model, userIds []mysql.ID, isDefAvatar bool) (map[mysql.ID]*user_m.UserTiny, error) {
redisNoIds := make([]mysql.ID, 0)
res := make(map[mysql.ID]*user_m.UserTiny)
for _, id := range userIds {
userTiny := new(user_m.UserTiny)
key := user_k.GetUserTinyKey(id)
err := cache.GetJSON(model, key, userTiny)
if err != nil && err != redisV8.Nil {
return nil, err
}
if err == redisV8.Nil {
redisNoIds = append(redisNoIds, id)
} else if userTiny.ID > 0 {
res[id] = userTiny
}
}
// 从db中读
users, err := user_m.GetUsers(model, redisNoIds)
if err != nil {
return nil, err
}
for _, u := range users {
userTiny := ToUserTinyBy(u)
err = cacheUserTiny(model, u)
if err != nil {
return nil, err
}
res[userTiny.ID] = userTiny
}
if isDefAvatar {
for _, v := range res {
if len(v.Avatar) <= 0 {
if v.Sex == mysql.MAN {
v.Avatar = common.MakeFullUrl(common.DefaultAvatarMan)
} else if v.Sex == mysql.WOMAN {
v.Avatar = common.MakeFullUrl(common.DefaultAvatarWoman)
}
}
}
}
return res, nil
}
func ToUserTinyBy(user *user_m.User) *user_m.UserTiny {
return &user_m.UserTiny{
ID: user.ID,
Avatar: IfLogoutStr(IfLogout(user.LogoutTime), "", user.Avatar),
ExternalId: user.ExternalId,
Nick: IfLogoutNick(IfLogout(user.LogoutTime), user.Code, user.Nick),
Sex: user.Sex,
Code: user.Code,
Country: user.Country,
CountryIcon: user.CountryIcon,
IsPrettyCode: user.IsPrettyCode(),
IsLogout: IfLogout(user.LogoutTime),
Birthday: BirthdayToUint64(&user.Birthday),
}
}
func IfLogout(logoutTime int64) bool {
return logoutTime > 0 && time.Now().Unix() > logoutTime
}
func BirthdayToUint64(birthday *mysql.Timestamp) *uint64 {
if *birthday == 0 {
return nil
}
return (*uint64)(birthday)
}
func IfLogoutStr(condition bool, trueVal, falseVal string) string {
if condition {
return trueVal
}
return falseVal
}
func IfLogoutNick(condition bool, code string, nick string) string {
if condition {
return "Hilo No." + code
}
return nick
}
package groupPower_m
import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm"
"hilo-group/common/utime"
"time"
)
type GroupPowerGrade struct {
GroupPowerId int64 `json:"group_power_id"`
Exp int64 `json:"exp"`
Grade int32 `json:"grade"`
ExpireAt time.Time `json:"expire_at"`
}
func (this *GroupPower) Get(model *domain.Model) (*GroupPower, error) {
group := new(GroupPower)
err := model.Db.Where(this).First(&group).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, err
}
grade := new(GroupPowerGrade)
err = model.Db.Model(&GroupPowerGrade{}).Where("group_power_id=? and expire_at > ?", this.ID, time.Now().Format(utime.Layout)).First(&grade).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
if grade.GroupPowerId > 0 {
group.Grade = mysql.Num(grade.Grade)
group.Exp = mysql.Num(grade.Exp)
group.NextExp = mysql.Num(grade.Exp)
}
return group, nil
}
func (gpu *GroupPowerUser) GetBy(model *domain.Model, limit int) ([]*GroupPowerUser, error) {
rows := make([]*GroupPowerUser, 0)
err := model.Db.Where(gpu).Order("field(`role`, 2, 3, 1)").Limit(limit).Find(&rows).Error
if err != nil {
return nil, err
}
return rows, nil
}
...@@ -26,6 +26,14 @@ type GroupPower struct { ...@@ -26,6 +26,14 @@ type GroupPower struct {
GroupUid mysql.Str GroupUid mysql.Str
Name mysql.Str Name mysql.Str
Status groupPower_e.GroupPowerStatus Status groupPower_e.GroupPowerStatus
Nameplate mysql.Str // 铭牌
Declaration mysql.Str // 宣言
Icon mysql.Str // 头像
Grade mysql.Num `gorm:"-"` // 等级
Exp mysql.Num `gorm:"-"` // 当前经验值
NextExp mysql.Num `gorm:"-"` // 升到下一级需要的经验值
GradeName mysql.Str // 等级称号
GradeMedal mysql.Str // 等级勋章图片
} }
type GroupPowerUser struct { type GroupPowerUser struct {
......
...@@ -118,3 +118,12 @@ func BatchGetUserByExtIds(model *domain.Model, extIds []string) ([]User, error) ...@@ -118,3 +118,12 @@ func BatchGetUserByExtIds(model *domain.Model, extIds []string) ([]User, error)
} }
return users, nil return users, nil
} }
//获取用户
func GetUsers(model *domain.Model, ids []mysql.ID) ([]*User, error) {
res := make([]*User, 0)
if err := model.Db.WithContext(model.Context).Where("id in (?)", ids).Find(&res).Error; err != nil {
return nil, myerr.WrapErr(err)
}
return res, nil
}
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"hilo-group/cv/medal_cv" "hilo-group/cv/medal_cv"
"hilo-group/cv/user_cv" "hilo-group/cv/user_cv"
"hilo-group/domain/cache/res_c" "hilo-group/domain/cache/res_c"
"hilo-group/domain/cache/user_c"
"hilo-group/domain/model/game_m" "hilo-group/domain/model/game_m"
"hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/groupPower_m"
"hilo-group/domain/model/group_m" "hilo-group/domain/model/group_m"
...@@ -692,3 +693,49 @@ func GroupPowerBillboardOwnerWeek(c *gin.Context) (*mycontext.MyContext, error) ...@@ -692,3 +693,49 @@ func GroupPowerBillboardOwnerWeek(c *gin.Context) (*mycontext.MyContext, error)
resp.ResponseOk(c, results) resp.ResponseOk(c, results)
return myContext, nil return myContext, nil
} }
// @Tags 家族
// @Summary 家族信息
// @Param id query int true "家族id"
// @Success 200 {object} group_power_cv.GroupPowerInfo
// @Router /v1/groupPower/info [get]
func GroupPowerInfo(c *gin.Context) (*mycontext.MyContext, error) {
myContext := mycontext.CreateMyContext(c.Keys)
//userId, err := req.GetUserId(c)
//if err != nil {
// return myContext, err
//}
familyId, err := strconv.ParseUint(c.Query("id"), 10, 64)
if err != nil {
return myContext, err
}
model := domain.CreateModelContext(myContext)
gp := &groupPower_m.GroupPower{Entity: mysql.Entity{ID: familyId}}
gpInfo, err := gp.Get(model)
if err != nil {
return myContext, err
}
gpU := &groupPower_m.GroupPowerUser{GroupPowerId: gpInfo.ID}
members, err := gpU.GetBy(model, 5)
if err != nil {
return myContext, err
}
uids := make([]uint64, 0, len(members))
for _, v := range members {
uids = append(uids, v.UserId)
}
//查找用户信息
//uids = common.UniqueSliceUInt64(uids)
userMap, err := user_c.GetUserTinyMap(domain.CreateModelContext(myContext), uids, true)
if err != nil {
return myContext, err
}
resMembers := make([]*group_power_cv.GroupPowerUser, 0, len(members))
for _, v := range members {
resMembers = append(resMembers, &group_power_cv.GroupPowerUser{User: userMap[v.UserId], Role: v.Role})
}
res := group_power_cv.GroupPowerInfo{Info: gpInfo, Members: resMembers}
resp.ResponseOk(c, res)
return myContext, nil
}
...@@ -126,6 +126,7 @@ func InitRouter() *gin.Engine { ...@@ -126,6 +126,7 @@ func InitRouter() *gin.Engine {
groupPower.GET("/team", wrapper(group_power_r.GroupPowerTeam)) groupPower.GET("/team", wrapper(group_power_r.GroupPowerTeam))
groupPower.GET("/billboard/week", wrapper(group_power_r.GroupPowerBillboardWeek)) groupPower.GET("/billboard/week", wrapper(group_power_r.GroupPowerBillboardWeek))
groupPower.GET("/billboard/owner/week", wrapper(group_power_r.GroupPowerBillboardOwnerWeek)) groupPower.GET("/billboard/owner/week", wrapper(group_power_r.GroupPowerBillboardOwnerWeek))
groupPower.GET("/info", wrapper(group_power_r.GroupPowerInfo))
} }
charge := v1.Group("/charge") charge := v1.Group("/charge")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment