vip.go 9.26 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
package user_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/user_e"
	"hilo-group/myerr"
	"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 (user *User) CheckVip() (bool, error) {
	rows := make([]UserVip, 0)
	err := user.Model.Db.Where("user_id = ? AND expire_at >= NOW()", user.ID).Find(&rows).Error
	if err != nil {
		return false, err
	}
	if len(rows) > 0 {
		return true, nil
	}
	return false, nil
}

// 检查某用户是否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
}

iamhujiebin's avatar
iamhujiebin committed
62
// 分批获取
hujiebin's avatar
hujiebin committed
63
func BatchGetVips(userIds []uint64) (map[uint64]*int64, error) {
iamhujiebin's avatar
iamhujiebin committed
64
	result := make(map[uint64]*int64, 0)
hujiebin's avatar
hujiebin committed
65
	rows := make([]UserVip, 0)
iamhujiebin's avatar
iamhujiebin committed
66 67 68 69 70
	end := 500
	if end > len(userIds) {
		end = len(userIds)
	}
	start := 0
hujiebin's avatar
hujiebin committed
71
	for start < len(userIds) {
iamhujiebin's avatar
iamhujiebin committed
72
		tmp := make([]UserVip, 0)
iamhujiebin's avatar
iamhujiebin committed
73
		err := mysql.Db.Where("user_id IN ?", userIds[start:end]).Find(&tmp).Error
iamhujiebin's avatar
iamhujiebin committed
74 75 76 77 78 79 80 81 82 83
		if err != nil {
			return nil, err
		}
		if err != nil {
			return result, err
		} else {
			rows = append(rows, tmp...)
		}
		start += 500
		end += 500
hujiebin's avatar
hujiebin committed
84 85 86
		if end > len(userIds) {
			end = len(userIds)
		}
hujiebin's avatar
hujiebin committed
87
		//mylogrus.MyLog.Infof("BatchGetVips start:%v-end:%v", start, end)
hujiebin's avatar
hujiebin committed
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	}

	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
}

// 查询当前有效的vips(google)
func GetValidVipsGoogle(db *gorm.DB, userId uint64) (*UserVip, error) {
	rows := make([]UserVip, 0)
	err := db.Table("user_vip AS u").
		Joins("INNER JOIN vip_subscribe_order_id AS s ON u.vip_subscribe_order_id = s.id").
		Where("platform = ? and expire_at >= NOW()", user_e.Google).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	if len(rows) > 0 {
		return &rows[0], nil
	}
	return nil, nil
}

//初始化用户Vip, 存在则抛出错误
func InitUserVip(model *domain.Model, userId uint64) (*UserVip, error) {
	var n int64
	if err := model.Db.Model(&UserVip{}).Where(&UserVip{
		UserId: userId,
	}).Count(&n).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	if n > 0 {
		return nil, myerr.NewSysErrorF("该用户已经拥有/曾经拥有vip")
	}
	return &UserVip{
		Model:  model,
		UserId: userId,
	}, nil
}

//获取UserVip 不存在则抛出错误
func GetUserVip(model *domain.Model, userId uint64) (*UserVip, error) {
	userVip := UserVip{}
	if err := model.Db.Where(&UserVip{
		UserId: userId,
	}).First(&userVip).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, myerr.NewSysErrorF("userVip userId:%v 不存在", userId)
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	userVip.Model = model
	return &userVip, nil
}

func GetUserVipNil(model *domain.Model, userId uint64) (*UserVip, error) {
	userVip := UserVip{}
	if err := model.Db.Where(&UserVip{
		UserId: userId,
	}).First(&userVip).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	userVip.Model = model
	return &userVip, nil
}

//设置userVip来源于管理者
func (userVip *UserVip) SetOriginByMgr(expireAt time.Time) *UserVip {
	userVip.Type = user_e.UserVipTypeGive
	userVip.Platform = 0
	userVip.VipSubscribeOrderId = 0
	userVip.ExpireAt = expireAt
	return userVip
}

//设置userVip来源于管理人
func (userVip *UserVip) AddDaysByMgr(days uint64) *UserVip {
	userVip.Type = user_e.UserVipTypeGive
	userVip.Platform = 0
	userVip.VipSubscribeOrderId = 0
	userVip.ExpireAt = userVip.ExpireAt.AddDate(0, 0, int(days))
	return userVip
}

//删除userVip来源于管理人,支付购买的,无法删除
func (userVip *UserVip) DelByMgr() (*UserVip, error) {
	if userVip.Type == user_e.UserVipTypeBuy {
		return nil, myerr.NewSysError("真金白银购买的VIP不能删除")
	} else {
		if userVip.ExpireAt.After(time.Now()) {
			userVip.ExpireAt = time.Now()
		}
	}
	return userVip, nil
}

func MakeVip(db *gorm.DB, uvType user_e.UserVipType, userId uint64, expiredTime time.Time, subscribeOrderId uint64, platform mysql.Platform) error {
	uv := UserVip{
		UserId:              userId,
		ExpireAt:            expiredTime,
		Type:                uvType,
		Platform:            platform,
		VipSubscribeOrderId: subscribeOrderId,
	}
	return db.Clauses(clause.OnConflict{
		Columns: []clause.Column{{Name: "user_id"}},
		DoUpdates: clause.Assignments(map[string]interface{}{
			"type":                   uvType,
			"platform":               platform, // TODO:如何处理切换平台订阅的问题?
			"expire_at":              expiredTime,
			"vip_subscribe_order_id": subscribeOrderId}),
	}).Create(&uv).Error
}

func ExtendVip(db *gorm.DB, userId uint64, expireAt time.Time) error {
	return db.Model(&UserVip{}).Where("user_id = ?", userId).Update("expire_at", expireAt).Error
}

// 检查一批用户中有没有一个是Vip
func CheckVipByUserIds(userIds []uint64) (bool, error) {
	rows := make([]UserVip, 0)
	err := mysql.Db.Where("user_id IN ?", userIds).Find(&rows).Error
	if err != nil {
		return false, err
	}
	now := time.Now()
	for _, i := range rows {
		if i.ExpireAt.After(now) {
			return true, nil
		}
	}
	return false, nil
}

type VipSubscribeApple struct {
	mysql.Entity
	*domain.Model         `gorm:"-"`
	UserId                mysql.ID
	Receipt               mysql.Str
	TransactionId         mysql.Str
	OriginalTransactionId mysql.Str
}

func (v *VipSubscribeApple) Get() (*VipSubscribeApple, error) {
	rows := make([]VipSubscribeApple, 0)
	err := mysql.Db.Where(v).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	if len(rows) <= 0 {
		return nil, nil
	}
	return &rows[0], nil
}

// 添加ios支付凭证
// 1. 检查旧订单(同origin_transaction_id)
// 2. 处理完全相同的transaction_id订单,更新对应的user_id
// 3. 若无相同transaction_id,则插入新的记录
// return: vip_subscribe_apple的唯一id, 旧的单子, error
func (v *VipSubscribeApple) Add(model *domain.Model) (uint64, []VipSubscribeApple, error) {
	// 检查,订单是否已经存在
	// 订阅的单子,需要看original_transaction_id判断
	var olds []VipSubscribeApple
	if err := model.Db.Model(&VipSubscribeApple{}).Where(&VipSubscribeApple{
		//TransactionId: v.TransactionId,
		OriginalTransactionId: v.OriginalTransactionId,
	}).Find(&olds).Error; err != nil {
		return 0, nil, myerr.WrapErr(err)
	}
	for _, old := range olds {
		// 完全是拿旧的transactionId
		if old.TransactionId == v.TransactionId {
			if err := model.Db.Model(&VipSubscribeApple{}).Where("transaction_id = ?", v.TransactionId).Update("user_id", v.UserId).Error; err != nil {
				return 0, nil, err
			} else {
				return old.ID, olds, nil
			}
		}
	}
	if err := model.Db.Model(&VipSubscribeApple{}).Create(&v).Error; err != nil {
		return 0, nil, myerr.WrapErr(err)
	}
	return v.ID, olds, nil
}

type VipSubscribeGoogle struct {
	mysql.Entity
	*domain.Model  `gorm:"-"`
	UserId         mysql.ID
	PackageName    mysql.Str
	SubscriptionID mysql.Str
	PurchaseToken  mysql.Str
}

func GetGoogleSubscribe(model *domain.Model, packageName, subscriptionID, purchaseToken mysql.Str) (*VipSubscribeGoogle, error) {
	q := VipSubscribeGoogle{
		Model:       model,
		PackageName: packageName, SubscriptionID: subscriptionID, PurchaseToken: purchaseToken}
	return q.Get()
}

func BatchGetGoogleSubscribe(db *gorm.DB, ids []uint64) ([]VipSubscribeGoogle, error) {
	rows := make([]VipSubscribeGoogle, 0)
	err := db.Model(&VipSubscribeGoogle{}).Where("id IN ?", ids).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	return rows, nil
}

func (v *VipSubscribeGoogle) Get() (*VipSubscribeGoogle, error) {
	rows := make([]VipSubscribeGoogle, 0)
	err := v.Db.Where(v).Find(&rows).Error
	if err != nil {
		return nil, err
	}
	if len(rows) <= 0 {
		return nil, nil
	}
	return &rows[0], nil
}

func (v *VipSubscribeGoogle) Add() (uint64, error) {
	err := v.Db.Model(&VipSubscribeGoogle{}).Create(&v).Error
	if err == nil {
		return v.ID, nil
	}
	return 0, err
}

type GoogleSubscribeState struct {
	mysql.Entity
	*domain.Model              `gorm:"-"`
	PurchaseToken              mysql.Str
	OrderId                    mysql.Str
	CountryCode                mysql.Str
	PriceAmountMicros          int64
	PriceCurrencyCode          mysql.Str
	StartTimeMillis            int64
	ExpiryTimeMillis           int64
	AutoRenewing               bool
	LinkedPurchaseToken        mysql.Str
	PaymentState               int64
	CancelReason               int64
	UserCancellationTimeMillis int64
}

func (gss *GoogleSubscribeState) Set() (uint64, error) {
	err := gss.Db.Clauses(clause.OnConflict{
		UpdateAll: true,
	}).Create(gss).Error
	if err == nil {
		return gss.ID, nil
	}
	return 0, err
}