@@ -0,0 +1,203 @@
|
||||
package exchcodeser
|
||||
|
||||
import (
|
||||
"91porn-server/models/v/backpackmod"
|
||||
"91porn-server/models/v/videocoupon"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/stderr"
|
||||
"91porn-server/models/commod"
|
||||
"91porn-server/models/l/exchlogmod"
|
||||
"91porn-server/models/v/exchcodemod"
|
||||
"91porn-server/models/v/usermod"
|
||||
"91porn-server/models/v/walletmod"
|
||||
)
|
||||
|
||||
// 兑换码兑换
|
||||
func CodeExchange(uid uint64, exchangeCode string) (code stderr.Code, data interface{}, err error) {
|
||||
// 获取该兑换码
|
||||
cMod, err := exchcodemod.GetExchangeCodeByCode(exchangeCode)
|
||||
if err != nil || cMod == nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
return
|
||||
}
|
||||
u, err := usermod.FindUserByUID(uid)
|
||||
if err != nil || u == nil {
|
||||
code = stderr.UserIsNotExists
|
||||
return
|
||||
}
|
||||
if cMod.Channel == "XUA01" && cMod.Channel != u.DistrictCode {
|
||||
code = stderr.ExchangeCodeInvalidAuthority
|
||||
return
|
||||
}
|
||||
// 无该兑换码或者不是此app的兑换码
|
||||
if cMod == nil || cMod.App != exchcodemod.AppTypeYSVideo || cMod.Authority == exchcodemod.AuthorityFilmVip {
|
||||
code = stderr.ExchangeCodeInvalidCode
|
||||
return
|
||||
}
|
||||
var now = time.Now()
|
||||
// 未生效
|
||||
if cMod.EffectiveAt.After(now) {
|
||||
code = stderr.ExchangeCodeIsNotEffective
|
||||
return
|
||||
}
|
||||
// 已过期
|
||||
if cMod.InvalidAt.Before(now) {
|
||||
code = stderr.ExchangeCodeIsExpired
|
||||
return
|
||||
}
|
||||
//使用次数已满
|
||||
if cMod.UsableNum <= 0 {
|
||||
code = stderr.ExchangeCodeInvalidCode
|
||||
return
|
||||
}
|
||||
// 同一个的兑换码不能兑换两次
|
||||
lMod, err := exchlogmod.GetLogByUIDAndCode(uid, cMod.Code)
|
||||
if err != nil || (!lMod.ID.IsZero() && cMod.BatchNum != "YS-dzwj") {
|
||||
code = stderr.ExchangeCodeRepeatExchange
|
||||
return
|
||||
}
|
||||
// 兑换
|
||||
if code, err = exchange(uid, cMod); err != nil || code != stderr.Success {
|
||||
return
|
||||
}
|
||||
return doReward(uid, cMod.Authority, cMod.Reward, cMod.RewardCount)
|
||||
}
|
||||
|
||||
// 兑换并记录日志
|
||||
func exchange(uid uint64, cMod *exchcodemod.ExchCode) (code stderr.Code, err error) {
|
||||
if err = exchcodemod.Exchange(cMod.Code); err != nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
return
|
||||
}
|
||||
// 插入日志
|
||||
var logMod = exchlogmod.ExchangeLog{
|
||||
Code: cMod.Code,
|
||||
UserID: uid,
|
||||
BatchNum: cMod.BatchNum,
|
||||
Channel: cMod.Channel,
|
||||
Authority: cMod.Authority,
|
||||
Reward: cMod.Reward,
|
||||
RewardCount: cMod.RewardCount,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if err = exchlogmod.Insert(logMod); err != nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
return
|
||||
}
|
||||
code = stderr.Success
|
||||
return
|
||||
}
|
||||
|
||||
// 发放奖品
|
||||
func doReward(uid uint64, eType exchcodemod.AuthorityType, reward int, rewardCount int) (code stderr.Code, data interface{}, err error) {
|
||||
// 兼容之前的数据
|
||||
if rewardCount == 0 {
|
||||
rewardCount = 1
|
||||
}
|
||||
now := time.Now()
|
||||
// 获取用户信息
|
||||
u, err := usermod.FindUserByUID(uid)
|
||||
if err != nil || u == nil {
|
||||
code = stderr.UserIsNotExists
|
||||
return
|
||||
}
|
||||
switch eType {
|
||||
case exchcodemod.AuthorityGold:
|
||||
if _, err := walletmod.CreditAmount(nil, int64(reward*rewardCount), uid); err != nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
return code, nil, err
|
||||
}
|
||||
case exchcodemod.AuthorityShortVideoVip, exchcodemod.AuthorityFilmVip, exchcodemod.AuthoritySuper: // vip增加天数
|
||||
var now = time.Now()
|
||||
var vipExpireDate = time.Time{}
|
||||
if u.VipExpireDate.Before(now) {
|
||||
vipExpireDate = now.AddDate(0, 0, reward*rewardCount)
|
||||
} else {
|
||||
vipExpireDate = u.VipExpireDate.AddDate(0, 0, reward*rewardCount)
|
||||
}
|
||||
vipLevel := usermod.RenewVIPLevel(u.VipExpireDate, now, u.VipLevel, 1)
|
||||
var update = usermod.UserSelector{VipExpireDate: &vipExpireDate, VipLevel: &vipLevel}
|
||||
if u, err = usermod.Update(uid, update); err != nil || u == nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
return code, nil, err
|
||||
}
|
||||
data = u
|
||||
case exchcodemod.Authority3dPermanentVIP:
|
||||
s := usermod.ThreeDayVIP_SnapVip
|
||||
permanentVIPLevel := 2
|
||||
permanentVIPExpireDate := now.AddDate(30, 0, 0)
|
||||
if u, err := usermod.UpdateSnapVip(u.UID, usermod.UserSelector{SnapVip: &s, VipLevel: &permanentVIPLevel, VipExpireDate: &permanentVIPExpireDate, OriginVipLevel: &u.VipLevel, OriginVipExpire: &u.VipExpireDate}); err != nil || u == nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
return code, nil, err
|
||||
}
|
||||
case exchcodemod.AuthorityVideoCoupon: // 观影券
|
||||
for i := 0; i < rewardCount; i++ {
|
||||
videoCoupon := videocoupon.UserGoldVideoCoupon{
|
||||
UID: uid,
|
||||
Num: reward,
|
||||
Used: false,
|
||||
Source: videocoupon.GoldVideoCouponSourceExchange,
|
||||
UpdatedAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
if err := videocoupon.InsertOne(videoCoupon); err != nil {
|
||||
code = stderr.ExchangeCodeExchangeFailed
|
||||
continue
|
||||
}
|
||||
backpack := backpackmod.Backpack{
|
||||
UID: uid,
|
||||
GoodsType: backpackmod.ExchangeCoupon,
|
||||
GoodsValue: int64(videoCoupon.Num),
|
||||
GoodsOrigin: string(videocoupon.GoldVideoCouponSourceExchange),
|
||||
Status: backpackmod.Unused,
|
||||
ExpiredTime: now.AddDate(0, 0, 7),
|
||||
CreateTime: now,
|
||||
}
|
||||
backpackmod.AddGoods(nil, uid, backpack)
|
||||
}
|
||||
default:
|
||||
code = stderr.ExchangeCodeInvalidCode
|
||||
return
|
||||
}
|
||||
code = stderr.Success
|
||||
return
|
||||
}
|
||||
|
||||
// 查询用户兑换记录
|
||||
func UserExchageRecord(uid uint64, page commod.Page) (stderr.Code, interface{}, error) {
|
||||
total, data, err := exchlogmod.GetExchangeLogList(exchlogmod.FilterDoc{
|
||||
UserID: &uid,
|
||||
}, page)
|
||||
if err != nil {
|
||||
return stderr.ExchangeCodeExchangeFailed, nil, nil
|
||||
}
|
||||
for i := range data {
|
||||
// 兼容兑换量
|
||||
if data[i].RewardCount == 0 {
|
||||
data[i].RewardCount = 1
|
||||
}
|
||||
switch data[i].Authority {
|
||||
case exchcodemod.AuthorityShortVideoVip:
|
||||
data[i].Desc = "视频VIPx" + strconv.Itoa(data[i].RewardCount)
|
||||
case exchcodemod.AuthorityFilmVip:
|
||||
data[i].Desc = "影视VIP(影院)" + strconv.Itoa(data[i].RewardCount)
|
||||
case exchcodemod.AuthoritySuper:
|
||||
data[i].Desc = "超级VIPx" + strconv.Itoa(data[i].RewardCount)
|
||||
case exchcodemod.AuthorityGold:
|
||||
data[i].Desc = "兑换金币x" + strconv.Itoa(data[i].Reward*data[i].RewardCount)
|
||||
case exchcodemod.Authority3dPermanentVIP:
|
||||
data[i].Desc = "3天VIPx" + strconv.Itoa(data[i].Reward*data[i].RewardCount)
|
||||
case exchcodemod.AuthorityVideoCoupon:
|
||||
data[i].Desc = "观影券" + strconv.Itoa(data[i].Reward) + "x" + strconv.Itoa(data[i].RewardCount) + "张"
|
||||
}
|
||||
}
|
||||
return stderr.Success, struct {
|
||||
Data []exchlogmod.ExchangeLog `json:"data"`
|
||||
Total int64 `json:"total"`
|
||||
}{
|
||||
Data: data,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user