vip.go 1.41 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3
package user_m

import (
hujiebin's avatar
hujiebin committed
4 5
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
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
	"gorm.io/gorm"
	"hilo-user/_const/enum/user_e"
	"time"
)

//用户Vip
type UserVip struct {
	mysql.Entity
	*domain.Model       `gorm:"-"`
	UserId              mysql.ID
	ExpireAt            time.Time          //结束时间
	Type                user_e.UserVipType //来源类型
	Platform            mysql.Platform
	VipSubscribeOrderId mysql.ID //最后的订单ID
}

// 检查某用户是否Vip
func IsVip(userId uint64) (bool, *int64, error) {
	uv, err := GetVip(mysql.Db, userId)
	if err != nil {
		return false, nil, err
	}
	if uv == nil {
		return false, nil, nil
	}
	ts := uv.ExpireAt.Unix()
	return true, &ts, nil
}

func GetVip(db *gorm.DB, userId uint64) (*UserVip, error) {
	rows := make([]UserVip, 0)
	err := db.Where("user_id = ? AND expire_at >= NOW()", userId).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	if len(rows) > 0 {
		return &rows[0], nil
	}
	return nil, nil
}
hujiebin's avatar
hujiebin committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

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
}