groupBanned.go 1.25 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
iamhujiebin's avatar
iamhujiebin committed
6 7
	"github.com/bluele/gcache"
	"time"
hujiebin's avatar
hujiebin committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
)

type GroupBanned struct {
	mysql.Entity
	ImGroupId string
	MgrId     uint64
	RuleId    uint64
}

func (banned *GroupBanned) Set(model *domain.Model) error {
	return model.Db.Where(banned).Create(banned).Error
}

func (banned *GroupBanned) Get(model *domain.Model) error {
	return model.Db.Where(banned).First(banned).Error
}

func (banned *GroupBanned) Delete(model *domain.Model) error {
	return model.Db.Where(banned).Delete(&GroupBanned{}).Error
}

iamhujiebin's avatar
iamhujiebin committed
29 30
var bannedGroupCache = gcache.New(100).LRU().Build()

hujiebin's avatar
hujiebin committed
31
func GetBannedGroups(model *domain.Model) ([]GroupBanned, error) {
iamhujiebin's avatar
iamhujiebin committed
32 33 34 35
	key := "banned"
	if data, err := bannedGroupCache.Get(key); err == nil {
		return data.([]GroupBanned), nil
	}
hujiebin's avatar
hujiebin committed
36
	result := make([]GroupBanned, 0)
37
	err := model.DB().Find(&result).Error
hujiebin's avatar
hujiebin committed
38 39 40
	if err != nil {
		return nil, err
	}
iamhujiebin's avatar
iamhujiebin committed
41
	bannedGroupCache.SetWithExpire(key, result, time.Minute*5)
hujiebin's avatar
hujiebin committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	return result, nil
}

func GetBannedGroupsMap(model *domain.Model) (map[string]uint64, error) {
	r, err := GetBannedGroups(model)
	if err != nil {
		return nil, err
	}
	result := make(map[string]uint64, 0)
	for _, i := range r {
		result[i.ImGroupId] = i.MgrId
	}
	return result, nil
}