groupBlacklist.go 1.38 KB
Newer Older
hujiebin's avatar
hujiebin 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
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
)

type GroupBlacklist struct {
	mysql.Entity
	ImGroupId string
	UserId    uint64
	Imei      string
	Ip        string
}

func AddBlacklist(model *domain.Model, gb *GroupBlacklist) error {
	return model.Db.Create(gb).Error
}

func RemoveBlacklist(model *domain.Model, gb *GroupBlacklist) error {
	return model.Db.Where(gb).Delete(&GroupBlacklist{}).Error
}

func (g *GroupBlacklist) FindUser(model *domain.Model) error {
	return model.Db.Where(g).First(g).Error
}

func InGroupBlackList(model *domain.Model, groupId, imei, ip string, userId uint64) bool {
	g := GroupBlacklist{ImGroupId: groupId, UserId: userId}
	err := g.FindUser(model)
	if err == nil {
		return true
	}
	if imei != "" {
		g := GroupBlacklist{ImGroupId: groupId, Imei: imei}
		err := g.FindUser(model)
		if err == nil {
			return true
		}
	}
	if ip != "" {
		g := GroupBlacklist{ImGroupId: groupId, Ip: ip}
		err := g.FindUser(model)
		if err == nil {
			return true
		}
	}
	return false
}

func FindGroupBlackList(model *domain.Model, groupId string) ([]GroupBlacklist, error) {
	result := make([]GroupBlacklist, 0)
	err := model.Db.Where(&GroupBlacklist{ImGroupId: groupId}).Find(&result).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, err
		}
	}
	return result, nil
}