groupRole.go 3.81 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"hilo-group/_const/enum/group_e"
)

type GroupRoles struct {
	mysql.Entity
	UserId    uint64
	ImGroupId string
	Role      group_e.GroupRoleType
}

func CreateGroupRole(model *domain.Model, imGroupId string, userId uint64, role group_e.GroupRoleType) error {
	return model.Db.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "user_id"}, {Name: "im_group_id"}},
		DoUpdates: clause.AssignmentColumns([]string{"role"}),
	}).Create(&GroupRoles{
		UserId:    userId,
		ImGroupId: imGroupId,
		Role:      role,
	}).Error
}

func RemoveGroupRole(model *domain.Model, imGroupId string, userId uint64) error {
	return model.Db.Where(&GroupRoles{ImGroupId: imGroupId, UserId: userId}).Delete(&GroupRoles{}).Error
}

// 查询用户在IM群组中的角色
func GetRoleInGroup(model *domain.Model, userId uint64, imGroupId string) (uint16, error) {
	r := GroupRoles{}
	err := model.Db.Where(&GroupRoles{
		UserId:    userId,
		ImGroupId: imGroupId}).First(&r).Error
	if err != nil {
		if err != gorm.ErrRecordNotFound {
			return 0, err
		} else {
			return 0, nil
		}
	}
	return r.Role, nil
}

// 取群中的指定角色,按时间排序
func FindRolesInGroup(db *gorm.DB, imGroupId string, role group_e.GroupRoleType) ([]uint64, error) {
	rows := make([]GroupRoles, 0)
	err := db.Model(&GroupRoles{}).Where(&GroupRoles{
		Role:      role,
		ImGroupId: imGroupId}).Order("created_time").Find(&rows).Error
	if err != nil {
		return nil, err
	}
	result := make([]uint64, 0)
	for _, i := range rows {
		result = append(result, i.UserId)
	}
	return result, nil
}

// 统计IM群组中某角色的个数
func GetRoleCountInGroup(model *domain.Model, imGroupId string, role group_e.GroupRoleType) (uint, error) {
	var count int64 = 0
	err := model.Db.Model(&GroupRoles{}).Where(&GroupRoles{
		Role:      role,
		ImGroupId: imGroupId}).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return uint(count), nil
}

// 统计用户在所有群中的角色
func GetRolesByUser(model *domain.Model, userId uint64) (map[string]uint16, error) {
	data := make([]GroupRoles, 0)
	err := model.Db.Where(&GroupRoles{UserId: userId}).Find(&data).Error
	if err != nil {
		return nil, err
	}

	result := make(map[string]uint16, 0)
	for _, i := range data {
		result[i.ImGroupId] = i.Role
	}
	return result, nil
}

// 查询群组中所有有角色的成员,由级别高到低、创建时间由早到晚排列
func GetRolesInGroup(model *domain.Model, groupId string) (map[uint64]uint16, []uint64, error) {
	data := make([]GroupRoles, 0)
	err := model.Db.Where(&GroupRoles{ImGroupId: groupId}).Order("role DESC, created_time").Find(&data).Error
	if err != nil {
		return nil, nil, err
	}

	result := make(map[uint64]uint16, 0)
	orders := make([]uint64, 0)
	for _, i := range data {
		orders = append(orders, i.UserId)
		result[i.UserId] = i.Role
	}
	return result, orders, nil
}

func IsRoleGreater(model *domain.Model, groupId string, userId1, userId2 uint64) (bool, error) {
	m, _, err := GetRolesInGroup(model, groupId)
	if err != nil {
		return false, err
	}
	return m[userId1] > m[userId2], nil
}

func GetGroupOwner(model *domain.Model, groupId string) (uint64, error) {
	gr := GroupRoles{ImGroupId: groupId, Role: group_e.GROUP_OWNER}
	err := model.Db.Where(&gr).First(&gr).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}
	return gr.UserId, nil
}

func GetGroups(db *gorm.DB, userId uint64, role group_e.GroupRoleType) ([]string, error) {
	gr := GroupRoles{UserId: userId, Role: role}
	rows := make([]GroupRoles, 0)
	err := db.Where(&gr).Order("created_time").Find(&rows).Error
	if err != nil {
		return nil, err
	}
	result := make([]string, 0)
	for _, i := range rows {
		result = append(result, i.ImGroupId)
	}
	return result, nil
}