groupPower.go 2.01 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
package groupPower_m

import (
	"gorm.io/gorm"
	"hilo-user/_const/enum/groupPower_e"
	"hilo-user/domain"
	"hilo-user/domain/model/group_m"
	"hilo-user/myerr"
	"hilo-user/resource/mysql"
)

type GroupPower struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	GroupUid      mysql.Str
	Name          mysql.Str
	Status        groupPower_e.GroupPowerStatus
}

type GroupPowerUser struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	GroupPowerId  mysql.ID
	UserId        mysql.ID
	Role          groupPower_e.GroupPowerUserRole
}

func GetGroupPowerOrErr(model *domain.Model, id uint64) (*GroupPower, error) {
	groupPower := GroupPower{}
	if err := model.Db.Model(&GroupPower{}).First(&groupPower, id).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	groupPower.Model = model
	return &groupPower, nil
}

//获取用户所在的国家势力信息,不存在则为nil
func GetGroupPowerUserOrNil(model *domain.Model, userId mysql.ID) (*GroupPowerUser, error) {
	groupPowerUser := GroupPowerUser{}
	if err := model.Db.Where(&GroupPowerUser{
		UserId: userId,
	}).First(&groupPowerUser).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	groupPowerUser.Model = model
	return &groupPowerUser, nil
}

// 查询用户加入的国家势力ID及名称(势力绑定的群组的名称)
func GetUserGroupPower(model *domain.Model, userId uint64) (uint64, string, error) {
	gpu, err := GetGroupPowerUserOrNil(model, userId)
	if err != nil {
		return 0, "", err
	}
	if gpu == nil || gpu.GroupPowerId == 0 {
		return 0, "", nil
	}
	gp, err := GetGroupPowerOrErr(model, gpu.GroupPowerId)
	if err != nil {
		return 0, "", err
	}

	powerName := ""
	if gp != nil && len(gp.GroupUid) > 0 {
		gi, err := group_m.GetGroupInfo(model, gp.GroupUid)
		if err != nil {
			return 0, "", err
		}
		if gi != nil {
			// 只要前15个字
			s := []rune(gi.Name)
			if len(s) <= 15 {
				powerName = string(s)
			} else {
				powerName = string(s[0:15])
			}
		}
	}
	return gpu.GroupPowerId, powerName, nil
}