superManager.go 2.06 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
6
	"gorm.io/gorm"
hujiebin's avatar
hujiebin committed
7
	"hilo-group/myerr"
hujiebin's avatar
hujiebin committed
8
	"strings"
hujiebin's avatar
hujiebin committed
9 10 11 12 13 14
)

type SuperManager struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
hujiebin's avatar
hujiebin committed
15 16
	IsAll         bool
	Countries     string
hujiebin's avatar
hujiebin committed
17 18 19 20 21 22 23 24 25 26 27 28
}

func IsSuperManager(model *domain.Model, userId mysql.ID) (bool, error) {
	var n int64
	if err := model.Db.Model(&SuperManager{}).Where(&SuperManager{
		UserId: userId,
	}).Count(&n).Error; err != nil {
		return false, myerr.WrapErr(err)
	}
	return n > 0, nil
}

hujiebin's avatar
hujiebin committed
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
// 对某人是否有超管权限
func IsSuperManagerV2(model *domain.Model, userId, targetUserId mysql.ID) (bool, error) {
	var man SuperManager
	if err := model.Db.Model(&SuperManager{}).Where(&SuperManager{
		UserId: userId,
	}).First(&man).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			model.Log.Errorf("IsSuperManagerV2 fail:%v", err)
		}
		return false, nil
	}
	if man.IsAll {
		return true, nil
	}
	targetUser, err := GetUser(model, targetUserId)
	if err != nil {
		return false, err
	}
	countries := strings.Split(man.Countries, ",")
	for _, c := range countries {
		if c == targetUser.Country {
			return true, nil
		}
	}
	return false, nil
}

hujiebin's avatar
hujiebin committed
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
func GetSuperManagerAll(model *domain.Model) ([]uint64, error) {
	superManagers := []SuperManager{}
	if err := model.Db.Model(&SuperManager{}).Find(&superManagers).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	userIds := make([]uint64, 0, len(superManagers))
	for i, _ := range superManagers {
		userIds = append(userIds, superManagers[i].UserId)
	}
	return userIds, nil
}

func GetSuperManagerMap(userIds []uint64) (map[uint64]bool, error) {
	if len(userIds) == 0 {
		return map[uint64]bool{}, nil
	}
	var superManagers []SuperManager
	if err := mysql.Db.Model(&SuperManager{}).Where("user_id in (?)", userIds).Find(&superManagers).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	//转换成map
	rs := map[uint64]bool{}
	for i, _ := range userIds {
		rs[userIds[i]] = false
	}
	for i, _ := range superManagers {
		rs[superManagers[i].UserId] = true
	}
	return rs, nil
hujiebin's avatar
hujiebin committed
85
}