userTinyVipLike.go 2.92 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
package cv

import (
	"hilo-algoCenter/common/mysql"
	"hilo-algoCenter/protocol/userProxy"
)

type CvUserTinyVipLike struct {
	CvUserTinyVip
	//是否喜欢别人
	IsLike bool `json:"isLike"`
	//别人是否喜欢我
	IsLikeMe bool `json:"isLikeMe"`
}

func GetUserTinyVipLikes(myUserId uint64, userIds []uint64) ([]CvUserTinyVipLike, error) {
	if len(userIds) == 0 {
		return []CvUserTinyVipLike{}, nil
	}
	userTinyVipMap, err := GetUserTinyVipMap(userIds)
	if err != nil {
		return nil, err
	}
	iLikeMap, err := CheckILike(myUserId, userIds)
	if err != nil {
		return nil, err
	}
	//
	likeMeMap, err := CheckLikeMe(myUserId, userIds)
	if err != nil {
		return nil, err
	}
	results := make([]CvUserTinyVipLike, 0, len(userIds))
	for _, r := range userIds {
		results = append(results, CvUserTinyVipLike{
			CvUserTinyVip: userTinyVipMap[r],
			IsLike:        iLikeMap[r],
			IsLikeMe:      likeMeMap[r],
		})
	}
	return results, nil
}


// GetOtherUserInfo 获取otherUserId的信息
// param myUserId:用来判断喜欢/被喜欢等关系
// param otherUserId:需要获取的用户
func GetOtherUserInfo(myUserId, otherUserId uint64) (*userProxy.User, error) {
	otherUser, err := GetUserTinyVipLikes(myUserId, []uint64{otherUserId})
	if err != nil {
		return nil, err
	}
	if len(otherUser) <= 0 {
		return nil, err
	}
	otherUserInfo := otherUser[0]
	var birthday uint64
	if otherUserInfo.Birthday != nil {
		birthday = *otherUserInfo.Birthday
	}
	return &userProxy.User{
		Id:          otherUserInfo.Id,
		ExternalId:  otherUserInfo.ExternalId,
		Nick:        otherUserInfo.Nick,
		Avatar:      otherUserInfo.Avatar,
		Country:     otherUserInfo.Country,
		CountryIcon: otherUserInfo.CountryIcon,
		Birthday:    birthday,
		IsVip:       otherUserInfo.IsVip,
		IsLike:      otherUserInfo.IsLike,
		IsLikeMe:    otherUserInfo.IsLikeMe,
	}, nil
}

//用户喜欢
type UserLike struct {
	mysql.Entity
	UserId     mysql.ID
	LikeUserId mysql.ID
	SceneType  mysql.Type
}

func CheckILike(myUserId uint64, userIds []uint64) (map[uint64]bool, error) {
	var userLikes []UserLike
	if err := mysql.Db.Model(&UserLike{}).Where(&UserLike{
		UserId: myUserId,
	}).Where("like_user_id in (?)", userIds).Find(&userLikes).Error; err != nil {
		return nil, err
	}
	m := map[uint64]struct{}{}
	for _, r := range userLikes {
		m[r.LikeUserId] = struct{}{}
	}
	result := make(map[uint64]bool, len(userIds))
	for _, r := range userIds {
		_, flag := m[r]
		result[r] = flag
	}
	return result, nil
}

func CheckLikeMe(myUserId uint64, userIds []uint64) (map[uint64]bool, error) {
	var userLikes []UserLike
	if err := mysql.Db.Model(&UserLike{}).Where(&UserLike{
		LikeUserId: myUserId,
	}).Where("user_id in (?)", userIds).Find(&userLikes).Error; err != nil {
		return nil, err
	}
	m := map[uint64]struct{}{}
	for _, r := range userLikes {
		m[r.UserId] = struct{}{}
	}
	result := make(map[uint64]bool, len(userIds))
	for _, r := range userIds {
		_, flag := m[r]
		result[r] = flag
	}
	return result, nil
}