@@ -0,0 +1,73 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/models/rechargepackagemod"
|
||||
"videoweb/pub/models/usermod"
|
||||
"videoweb/pub/pubdef"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// ActivateVip 支付成功后为用户开通/续期会员。
|
||||
// 若用户已有未过期会员,则从当前到期时间向后顺延;否则从当前时刻起算。
|
||||
// 同时将赠送金币(pkg.GiftCoin)计入用户余额。
|
||||
func ActivateVip(ctx context.Context, userId int64, pkg *rechargepackagemod.RechargePackage) (time.Time, *srvdef.SrvErr) {
|
||||
if pkg == nil {
|
||||
return time.Time{}, srvdef.New(srvdef.ErrParamError, "会员套餐不能为空", "")
|
||||
}
|
||||
user, serr := usermod.FindOneUser(ctx, bson.M{usermod.FN.User.Id: userId})
|
||||
if serr != nil {
|
||||
log.Error("payment:ActivateVip FindOneUser failed", log.Any("userId", userId), log.E(serr))
|
||||
return time.Time{}, serr
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
duration := time.Duration(pkg.DurationDays) * 24 * time.Hour
|
||||
vipType := vipTypeFromDurationDays(pkg.DurationDays)
|
||||
|
||||
baseTime := now
|
||||
if user.CurrentVipExpire.After(now) {
|
||||
baseTime = user.CurrentVipExpire
|
||||
}
|
||||
newExpire := baseTime.Add(duration)
|
||||
|
||||
setDoc := bson.M{
|
||||
usermod.FN.User.VipType: vipType,
|
||||
usermod.FN.User.CurrentVipExpire: newExpire,
|
||||
usermod.FN.User.ShowCardId: pkg.Id,
|
||||
usermod.FN.User.ShowCardName: pkg.Name,
|
||||
usermod.FN.User.UpdatedAt: now,
|
||||
}
|
||||
updateDoc := bson.M{"$set": setDoc}
|
||||
if pkg.GiftCoin > 0 {
|
||||
updateDoc["$inc"] = bson.M{usermod.FN.User.Recharge: pkg.GiftCoin}
|
||||
}
|
||||
|
||||
if serr = usermod.UpdateOneUser(ctx, bson.M{usermod.FN.User.Id: userId}, updateDoc); serr != nil {
|
||||
log.Error("payment:ActivateVip UpdateOneUser failed",
|
||||
log.Any("userId", userId), log.Any("packageId", pkg.Id), log.E(serr))
|
||||
return time.Time{}, serr
|
||||
}
|
||||
|
||||
log.Info("payment:ActivateVip success",
|
||||
log.Any("userId", userId),
|
||||
log.Any("packageId", pkg.Id),
|
||||
log.Any("vipType", vipType),
|
||||
log.Any("newExpire", newExpire),
|
||||
)
|
||||
return newExpire, nil
|
||||
}
|
||||
|
||||
func vipTypeFromDurationDays(days int64) pubdef.VipType {
|
||||
if days >= 365 {
|
||||
return pubdef.VipTypeYear
|
||||
}
|
||||
if days > 0 {
|
||||
return pubdef.VipTypeMonth
|
||||
}
|
||||
return pubdef.VipTypeNone
|
||||
}
|
||||
Reference in New Issue
Block a user