black.go 4.09 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
package user_m

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

//腾讯云的拉黑名单,临时表
type UserBlackTencentyunTmp struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserExternal  mysql.Str
	BlockExternal mysql.Str
}

//黑名单
type UserBlock struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	BlockUserId   mysql.ID
}

func initUserBlock(model *domain.Model, userId mysql.ID) *UserBlock {
	return &UserBlock{
		Model:  model,
		UserId: userId,
	}
}

//拉黑
func (ub *UserBlock) block(blockUserId mysql.ID) (*UserBlock, error) {
	err := ub.Db.Where(&UserBlock{
		UserId:      ub.UserId,
		BlockUserId: blockUserId,
	}).First(ub).Error
	//已经拉黑了
	if err == nil {
		ub.SetLasyLoad()
		return ub, nil
		//return nil, myerr.NewWaring("已经标记拉黑")
	} else if err == gorm.ErrRecordNotFound {
		ub.BlockUserId = blockUserId
		return ub, nil
	} else {
		return nil, myerr.WrapErr(err)
	}
}

//取消拉黑
func (ub *UserBlock) blockCancel(blockUserId mysql.ID) (*UserBlock, error) {
	err := ub.Db.Where(&UserBlock{
		UserId:      ub.UserId,
		BlockUserId: blockUserId,
	}).First(ub).Error
	//
	if err == nil {
		ub.SetDel()
		return ub, nil
	} else if err == gorm.ErrRecordNotFound {
		return nil, myerr.NewWaring("没有拉黑的记录")
	} else {
		return nil, myerr.WrapErr(err)
	}
}

//检查是否存在拉黑
/*func CheckBlock(model *domain.Model, userId mysql.ID, blockUserId mysql.ID) (bool, error) {
	var n int64
	if err := model.Db.Model(&UserBlock{}).Where(&UserBlock{
		UserId:      userId,
		BlockUserId: blockUserId,
	}).Count(&n).Error; err != nil {
		return false, myerr.WrapErr(err)
	}
	return n > 0, nil
}*/

//检查是否存在拉黑(无论是我拉黑别人,还是别人拉黑我), true:拉黑, false:不拉黑
func CheckBlockOr(model *domain.Model, userId mysql.ID, blockUserId mysql.ID) (bool, error) {
	var n int64
	if err := model.Db.Model(&UserBlock{}).Where(&UserBlock{
		UserId:      userId,
		BlockUserId: blockUserId,
	}).Count(&n).Error; err != nil {
		return false, myerr.WrapErr(err)
	}
	if n == 0 {
		if err := model.Db.Model(&UserBlock{}).Where(&UserBlock{
			BlockUserId: userId,
			UserId:      blockUserId,
		}).Count(&n).Error; err != nil {
			return false, myerr.WrapErr(err)
		}
	}
	return n > 0, nil
}

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

//检查互相拉黑的成员
func FilterBlock(model *domain.Model, userId mysql.ID, otherUserIds []mysql.ID) ([]mysql.ID, error) {
	if len(otherUserIds) == 0 {
		return otherUserIds, nil
	}
	userBlocks1 := []UserBlock{}
	if err := model.Db.Model(&UserBlock{}).Where(&UserBlock{
		UserId: userId,
	}).Where("block_user_id in (?)", otherUserIds).Find(&userBlocks1).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	userBlocks2 := []UserBlock{}
	if err := model.Db.Model(&UserBlock{}).Where(&UserBlock{
		BlockUserId: userId,
	}).Where("user_id in (?)", otherUserIds).Find(&userBlocks2).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}

	blockSet := map[uint64]struct{}{}
	for i, _ := range userBlocks1 {
		blockSet[userBlocks1[i].BlockUserId] = struct{}{}
	}
	for i, _ := range userBlocks2 {
		blockSet[userBlocks2[i].UserId] = struct{}{}
	}

	//
	resultUserIds := make([]mysql.ID, 0, len(otherUserIds))
	for i, r := range otherUserIds {
		if _, flag := blockSet[r]; !flag {
			resultUserIds = append(resultUserIds, otherUserIds[i])
		}
	}
	return resultUserIds, nil
}

// 获取用户拉黑的用户ids
func GetBlockUserIds(model *domain.Model, userId mysql.ID) ([]mysql.ID, error) {
	var userBlocks []UserBlock
	if err := model.Db.Where(&UserBlock{
		UserId: userId,
	}).Find(&userBlocks).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	var userIds []mysql.ID
	for _, v := range userBlocks {
		userIds = append(userIds, v.BlockUserId)
	}
	return userIds, nil
}