group.go 5.31 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package group_c

import (
	"context"
hujiebin's avatar
hujiebin committed
5 6 7
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
8
	"git.hilo.cn/hilo-common/resource/redisCli"
hujiebin's avatar
hujiebin committed
9
	"github.com/go-redis/redis/v8"
hujiebin's avatar
hujiebin committed
10
	"hilo-group/_const/redis_key"
hujiebin's avatar
hujiebin committed
11 12
	"hilo-group/_const/redis_key/group_k"
	"hilo-group/domain/model/group_m"
hujiebin's avatar
hujiebin committed
13 14 15 16
	"strings"
	"time"
)

hujiebin's avatar
hujiebin committed
17 18 19 20
func init() {
	group_m.FuncAddEditGroupCd = AddEditGroupCd
}

hujiebin's avatar
hujiebin committed
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
func GetGroupMemberCount(groupId string) (int, error) {
	key := redis_key.GetGroupMemCountKey(groupId)
	return redisCli.RedisClient.Get(context.Background(), key).Int()
}

func SetGroupMemberCount(groupId string, count uint, ttl time.Duration) error {
	key := redis_key.GetGroupMemCountKey(groupId)
	return redisCli.RedisClient.Set(context.Background(), key, count, ttl).Err()
}

func ClearGroupMemberCount(groupId string) error {
	key := redis_key.GetGroupMemCountKey(groupId)
	return redisCli.RedisClient.Del(context.Background(), key).Err()
}

func getGroupMemberKey(groupId string) string {
	return strings.Replace(redis_key.GroupMemberPrefix, "{groupId}", groupId, -1)
}

func getGroupConsumeKey(groupId string) string {
	return strings.Replace(redis_key.GroupConsumePrefix, "{groupId}", groupId, -1)
}

func GetGroupMember(groupId string) ([]string, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SMembers(context.Background(), key).Result()
}

func GetGroupMemberCard(groupId string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SCard(context.Background(), key).Result()
}

func GetGroupMemberAsMap(groupId string) (map[string]struct{}, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SMembersMap(context.Background(), key).Result()
}

func SetExists(groupId string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.Exists(context.Background(), key).Result()
}

64
func addGroupMember(groupId string, extIds []string) (int64, error) {
hujiebin's avatar
hujiebin committed
65 66 67 68
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SAdd(context.Background(), key, extIds).Result()
}

hujiebin's avatar
hujiebin committed
69
func AddGroupMember(groupId string, extIds []string) (int64, error) {
70
	key := getGroupMemberKey(groupId)
hujiebin's avatar
hujiebin committed
71
	return redisCli.RedisClient.SAdd(context.Background(), key, extIds).Result()
72 73
}

hujiebin's avatar
hujiebin committed
74 75 76 77 78 79 80 81 82 83 84 85
func RemoveGroupMember(groupId string, externalId string) (int64, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.SRem(context.Background(), key, externalId).Result()
}

func SetGroupMemberTTL(groupId string, ttl time.Duration) (bool, error) {
	key := getGroupMemberKey(groupId)
	return redisCli.RedisClient.Expire(context.Background(), key, ttl).Result()
}

func GetGroupConsume(groupId string) (uint64, error) {
	key := getGroupConsumeKey(groupId)
86
	return redisCli.RedisClusterClient.Get(context.Background(), key).Uint64()
hujiebin's avatar
hujiebin committed
87 88 89 90
}

func SetGroupConsume(groupId string, consume uint64, ttl time.Duration) error {
	key := getGroupConsumeKey(groupId)
91
	return redisCli.RedisClusterClient.Set(context.Background(), key, consume, ttl).Err()
hujiebin's avatar
hujiebin committed
92 93 94 95
}

func ClearGroupConsume(groupId string) error {
	key := getGroupConsumeKey(groupId)
96
	return redisCli.RedisClusterClient.Del(context.Background(), key).Err()
hujiebin's avatar
hujiebin committed
97
}
hujiebin's avatar
hujiebin committed
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 142 143 144 145 146 147 148 149 150

// 增加编辑用户cd
// 6小时cd
func AddEditGroupCd(model *domain.Model, imGroupId mysql.Str) error {
	key := group_k.GetEditGroupCDKey(imGroupId)
	return model.Redis.SetEX(model, key, 1, time.Hour*6).Err()
}

// 是否编辑群组cd中
func IsEditGroupCd(model *domain.Model, imGroupId mysql.Str) bool {
	key := group_k.GetEditGroupCDKey(imGroupId)
	n, err := model.Redis.Exists(model, key).Result()
	if err != nil {
		model.Log.Errorf("IsEditGroupCd fail:%v", err)
	}
	return n > 0
}

//
func GetGroupInfoCache(model *domain.Model, imGroupId mysql.Str) (*group_m.GroupInfo, error) {
	key := group_k.GetGroupInfoKey(imGroupId)
	data, err := model.Redis.Get(model, key).Bytes()
	if err != nil {
		if err == redis.Nil {
			return nil, nil
		}
		model.Log.Errorf("GetGroupInfo fail,imGroupId:%v,err:%v", imGroupId, err)
		return nil, err
	}
	res := new(group_m.GroupInfo)
	err = json.Unmarshal(data, &res)
	if err != nil {
		model.Log.Errorf("GetGroupInfo fail,imGroupId:%v,err:%v", imGroupId, err)
		return nil, err
	}
	return res, nil
}

//
func SetGroupInfoCache(model *domain.Model, info *group_m.GroupInfo) error {
	key := group_k.GetGroupInfoKey(info.ImGroupId)
	data, err := json.Marshal(info)
	if err != nil {
		model.Log.Errorf("SetGroupInfo fail,info:%+v,err:%v", info, err)
		return err
	}
	err = model.Redis.Set(model, key, data, time.Minute*5).Err()
	if err != nil {
		model.Log.Errorf("SetGroupInfo fail,info:%+v,err:%v", info, err)
		return err
	}
	return nil
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

// 增加领取群组扶持ip次数
func IncrGroupSupportAwardIp(model *domain.Model, ip mysql.Str) (times int64, err error) {
	key := group_k.GetGroupSupportAwardIpKey(ip)
	times, err = model.Redis.Incr(model, key).Result()
	if err != nil {
		model.Log.Errorf("IncrGroupSupportAwardIp fail,ip:%v,err:%v", ip, err)
	} else {
		model.Redis.Expire(model, key, time.Hour*24*6) // 1周1次,ttl=6天吧
	}
	return
}

// 获取领取群组扶持ip次数
func GetGroupSupportAwardIpTimes(model *domain.Model, ip mysql.Str) (times int64, err error) {
	key := group_k.GetGroupSupportAwardIpKey(ip)
	times, err = model.Redis.Get(model, key).Int64()
	if err != nil && err != redis.Nil {
		model.Log.Errorf("GetGroupSupportAwardIpTimes fail,ip:%v,err:%v", ip, err)
		return
	}
	return times, nil
}