group.go 3.14 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
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
}