userTinyVip.go 4.41 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
package cv

import (
	"hilo-algoCenter/common/mysql"
	"time"
)

// User 用户信息
type User struct {
	mysql.Entity
	ExternalId    mysql.Str
	Avatar        mysql.Str
	DefaultAvatar bool
	Nick          mysql.Str
	Sex           mysql.Sex
	Birthday      mysql.Timestamp
	Country       mysql.Str
	CountryIcon   mysql.Str
	Language      mysql.Str
	Description   mysql.Str
	Code          mysql.Str
	OriginCode    mysql.Str
	IsPush        mysql.OpenClose
	IsShowAge     mysql.OpenClose
	Status        mysql.Type
	DeviceType    mysql.Str
	LogoutTime    int64
}

// UserVip 用户Vip
type UserVip struct {
	mysql.Entity
	UserId              mysql.ID
	ExpireAt            time.Time //结束时间
	Type                mysql.Type
	Platform            mysql.Platform
	VipSubscribeOrderId mysql.ID //最后的订单ID
}

type CvUserTiny struct {
	Id           uint64 `json:"id,omitempty"`
	ExternalId   string `json:"externalId"`
	Avatar       string `json:"avatar"`
	Nick         string `json:"nick"`
	Sex          uint8  `json:"sex"`
	Code         string `json:"code"`
	Country      string `json:"country"`
	CountryIcon  string `json:"countryIcon"`
	IsPrettyCode bool   `json:"isPrettyCode"` // 是否靓号
	IsLogout     bool   `json:"isLogout"`     //是否注销 true:已经注销, false:没有注销
	//生日,如果是其它人用户信息,年龄则按照是否展示显示,如果是本人,年龄则按照是否存在展示
	Birthday *uint64 `json:"birthday"`
}

type CvUserTinyVip struct {
	CvUserTiny
	//是否VIP用户
	IsVip bool `json:"isVip"`
	//VIP用户过期时间(只有自己查询自己,才返回)
	VipExpireTime *int64 `json:"vipExpireTime"`
}

func GetUserTinyVips(userIds []uint64) ([]CvUserTinyVip, error) {
	userTinys, _, err := GetUserTinys(userIds)
	if err != nil {
		return nil, err
	}

	vipMap, err := BatchGetVips(userIds)
	if err != nil {
		return nil, err
	}

	userTinyVips := make([]CvUserTinyVip, 0, len(userTinys))
	for i, r := range userTinys {
		vipExpireTime, _ := vipMap[r.Id]
		userTinyVips = append(userTinyVips, CvUserTinyVip{
			CvUserTiny:    userTinys[i],
			IsVip:         vipExpireTime != nil,
			VipExpireTime: vipExpireTime,
		})
	}
	return userTinyVips, nil
}

// GetUserTinys 批量获取用户tiny信息
func GetUserTinys(userIds []mysql.ID) ([]CvUserTiny, []uint64, error) {
	if len(userIds) == 0 {
		return nil, nil, nil
	}
	var users []User
	if err := mysql.Db.Model(&User{}).Where("id IN ?", userIds).Find(&users).Error; err != nil {
		return nil, nil, err
	}
	userTinys := make([]CvUserTiny, 0, len(users))
	for _, r := range users {
		userTinys = append(userTinys, GetUserTinyBy(r))
	}
	return userTinys, userIds, nil
}

func GetUserTinyVipMap(userIds []uint64) (map[uint64]CvUserTinyVip, error) {
	userTinyVips, err := GetUserTinyVips(userIds)
	if err != nil {
		return nil, err
	}
	userTinyVipMap := map[uint64]CvUserTinyVip{}
	for i, r := range userTinyVips {
		userTinyVipMap[r.Id] = userTinyVips[i]
	}
	return userTinyVipMap, nil
}

func GetUserTinyBy(user User) CvUserTiny {
	return CvUserTiny{
		Id:           user.ID,
		Avatar:       IfLogoutStr(IfLogout(user.LogoutTime), "", user.Avatar),
		ExternalId:   user.ExternalId,
		Nick:         IfLogoutNick(IfLogout(user.LogoutTime), user.Code, user.Nick),
		Sex:          user.Sex,
		Code:         user.Code,
		Country:      user.Country,
		CountryIcon:  user.CountryIcon,
		IsPrettyCode: user.IsPrettyCode(),
		IsLogout:     IfLogout(user.LogoutTime),
		Birthday:     BirthdayToUint64(&user.Birthday),
	}
}

func IfLogoutStr(condition bool, trueVal, falseVal string) string {
	if condition {
		return trueVal
	}
	return falseVal
}

func IfLogoutNick(condition bool, code string, nick string) string {
	if condition {
		return "Hilo No." + code
	}
	return nick
}
func IfLogout(logoutTime int64) bool {
	return logoutTime > 0 && time.Now().Unix() > logoutTime
}

func BirthdayToUint64(birthday *mysql.Timestamp) *uint64 {
	if *birthday == 0 {
		return nil
	}
	return (*uint64)(birthday)
}

func (u User) IsPrettyCode() bool {
	return u.Code != u.OriginCode
}

func BatchGetVips(userIds []uint64) (map[uint64]*int64, error) {
	rows := make([]UserVip, 0)
	err := mysql.Db.Where("user_id IN ?", userIds).Find(&rows).Error
	if err != nil {
		return nil, err
	}

	result := make(map[uint64]*int64, 0)
	for _, i := range userIds {
		result[i] = nil
	}
	now := time.Now()
	for _, i := range rows {
		if i.ExpireAt.After(now) {
			ts := i.ExpireAt.Unix()
			result[i.UserId] = &ts
		}
	}
	return result, nil
}