package group_m import ( "context" "encoding/json" redisV8 "github.com/go-redis/redis/v8" "gorm.io/gorm" "hilo-user/common" "hilo-user/common/rediskey" "hilo-user/domain" "hilo-user/myerr" "hilo-user/myerr/bizerr" "hilo-user/resource/mysql" "hilo-user/resource/redisCli" ) type GroupRoles struct { mysql.Entity UserId uint64 ImGroupId string Role common.GroupRoleType } func (this *GroupInfo) TableName() string { return "group_info" } //发言,注意(发言是在麦位上) type MicUser struct { model *domain.Model //群组uuid GroupUuid string //麦位 I int //麦中的人 ExternalId string // UserId uint64 //静音 true:静音,false:没有静音 Forbid bool //上麦的的时间戳 Timestamp int64 } //记录麦位上有谁。用于 type UserInMic struct { //群组uuid GroupUuid string //麦位 I int //userId UserId uint64 } // 查询用户在IM群组中的角色 func GetRoleInGroup(model *domain.Model, userId uint64, imGroupId string) (uint16, error) { r := GroupRoles{} err := model.Db.Where(&GroupRoles{ UserId: userId, ImGroupId: imGroupId}).First(&r).Error if err != nil { if err != gorm.ErrRecordNotFound { return 0, err } else { return 0, nil } } return r.Role, nil } func GetByTxGroupId(model *domain.Model, txGroupId string) (*GroupInfo, error) { if len(txGroupId) <= 0 { return nil, bizerr.GroupNotFound } res := new(GroupInfo) err := model.Db.Where(&GroupInfo{TxGroupId: txGroupId}).First(&res).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil, myerr.WrapErr(bizerr.GroupNotFound) } else { return nil, myerr.WrapErr(err) } } return res, nil } //获取用户在哪个麦位上。没有不在麦上,则是nil func GetMicUserByExternalId(model *domain.Model, externalId string) (*MicUser, error) { if str, err := redisCli.GetRedis().Get(context.Background(), rediskey.GetPrefixGroupUserInMic(externalId)).Result(); err != nil { if err != redisV8.Nil { return nil, myerr.WrapErr(err) } else { return nil, nil } } else { if userInMic, err := strToUserInMic(str); err != nil { return nil, err } else { return GetMicUser(model, userInMic.GroupUuid, userInMic.I) } } } //麦位上没人,返回nil func GetMicUser(model *domain.Model, groupUuid string, i int) (*MicUser, error) { if i < 1 || i > 30 { return nil, myerr.NewSysErrorF("麦序不对,不在范围值内 i:%v", i) } str, err := redisCli.GetRedis().Get(context.Background(), rediskey.GetPrefixGroupMicUser(groupUuid, i)).Result() if err != nil { if err == redisV8.Nil { return nil, nil } else { return nil, myerr.WrapErr(err) } } var micUser MicUser err = json.Unmarshal([]byte(str), &micUser) if err != nil { return nil, err } return &MicUser{ model: model, GroupUuid: groupUuid, I: i, ExternalId: micUser.ExternalId, UserId: micUser.UserId, Forbid: micUser.Forbid, Timestamp: micUser.Timestamp, }, nil } func strToUserInMic(str string) (UserInMic, error) { userInMic := UserInMic{} if err := json.Unmarshal([]byte(str), &userInMic); err != nil { return UserInMic{}, myerr.WrapErr(err) } return userInMic, nil }