|
|
|
|
@ -4,17 +4,14 @@ import ( |
|
|
|
|
"fmt" |
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
"github.com/liangdas/mqant/log" |
|
|
|
|
"github.com/olivere/elastic/v7" |
|
|
|
|
"gorm.io/gorm" |
|
|
|
|
"math/rand" |
|
|
|
|
"server/call" |
|
|
|
|
"server/common" |
|
|
|
|
"server/config" |
|
|
|
|
"server/db" |
|
|
|
|
"server/modules/web/app" |
|
|
|
|
"server/modules/web/values" |
|
|
|
|
"server/pb" |
|
|
|
|
"server/util" |
|
|
|
|
"sort" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
@ -24,78 +21,60 @@ func WeekCardInfo(c *gin.Context) { |
|
|
|
|
a.Response() |
|
|
|
|
}() |
|
|
|
|
a.GetUID() |
|
|
|
|
if !a.CheckActivityExpire(common.ActivityIDWeekCard) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
resp := &values.ActivityWeekCardInfoResp{} |
|
|
|
|
resp := &values.WeekCardInfoResp{} |
|
|
|
|
a.Data = resp |
|
|
|
|
uid := a.UID |
|
|
|
|
cons := call.GetConfigWeekCard() |
|
|
|
|
_ = cons |
|
|
|
|
cardInfo := new(common.ActivityWeekCardData) |
|
|
|
|
if a.UID > 0 { |
|
|
|
|
cardInfo = call.GetUserWeekCard(a.UID) |
|
|
|
|
} |
|
|
|
|
var rewardList []int64 |
|
|
|
|
var err error |
|
|
|
|
_ = err |
|
|
|
|
//rewardList, err = util.GenerateRandomSequence(cons.RewardAmount, cons.MiniLimit, 5)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// log.Error("err:%v", err)
|
|
|
|
|
//}
|
|
|
|
|
//rewardList = append([]int64{cons.DayOneReward}, rewardList...)
|
|
|
|
|
//rewardList = append(rewardList, 0)
|
|
|
|
|
//if cardInfo.ID <= 0 || len(cardInfo.Rewards) == 0 {
|
|
|
|
|
// cardInfo.Day = 0
|
|
|
|
|
// rewardList, err = util.GenerateRandomSequence(cons.RewardAmount, cons.MiniLimit, 5)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// log.Error("err:%v", err)
|
|
|
|
|
// }
|
|
|
|
|
// rewardList = append([]int64{cons.DayOneReward}, rewardList...)
|
|
|
|
|
// rewardList = append(rewardList, 0)
|
|
|
|
|
// if cardInfo.ID <= 0 {
|
|
|
|
|
// cardInfo.Rewards = strings.Join(util.Int64SliceToStringSlice(rewardList), ",")
|
|
|
|
|
// if a.UID > 0 {
|
|
|
|
|
// db.Mysql().Create(cardInfo)
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// cardInfo.Rewards = strings.Join(util.Int64SliceToStringSlice(rewardList), ",")
|
|
|
|
|
// db.Mysql().Update(&common.ActivityWeekCardData{UID: a.UID}, map[string]interface{}{
|
|
|
|
|
// "rewards": cardInfo.Rewards,
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
if rewardList == nil { |
|
|
|
|
rewardList, _ = util.StringToInt64Slice(cardInfo.Rewards, ",") |
|
|
|
|
} |
|
|
|
|
if cardInfo.RechargeTime != 0 { |
|
|
|
|
resp.RechargeStatus = true |
|
|
|
|
} |
|
|
|
|
// step:签完7天就重置
|
|
|
|
|
if cardInfo.Day >= len(rewardList) { |
|
|
|
|
resp.RechargeStatus = false |
|
|
|
|
} |
|
|
|
|
for _, item := range rewardList { |
|
|
|
|
_ = item |
|
|
|
|
//resp.RewardList = append(resp.RewardList, values.WeekCardInfo{
|
|
|
|
|
// Min: cons.MiniLimit,
|
|
|
|
|
// Max: cons.RewardAmount - cons.MiniLimit*4,
|
|
|
|
|
// Val: item,
|
|
|
|
|
//})
|
|
|
|
|
} |
|
|
|
|
resp.Status = true |
|
|
|
|
if config.GetBase().Release { |
|
|
|
|
if util.IsSameDayTimeStamp(time.Now().Unix(), cardInfo.LastDraw) { |
|
|
|
|
resp.Status = false |
|
|
|
|
if cons == nil { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
weekCardData, err := call.GetWeekCardData(uid) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("get week card data err, %s", err.Error()) |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
resp.Amount = cons.Amount |
|
|
|
|
resp.List = make([]values.WeekCardDay, 0, len(cons.AwardDetails)) |
|
|
|
|
now := time.Now() |
|
|
|
|
for index := 1; index <= 7; index++ { |
|
|
|
|
var state int |
|
|
|
|
if weekCardData != nil && weekCardData.ID != 0 && weekCardData.BeginAt != 0 { // 已经购买周卡的情况下
|
|
|
|
|
resp.IsUnlock = true |
|
|
|
|
if util.IsSameDayTimeStamp(now.Unix(), weekCardData.AwardAt) { |
|
|
|
|
resp.Time = int64(util.GetZeroTime(now.AddDate(0, 0, 1)).Sub(now).Seconds()) |
|
|
|
|
} |
|
|
|
|
if weekCardData.AwardTimes >= index { |
|
|
|
|
state = 2 |
|
|
|
|
} else if weekCardData.AwardTimes < index { |
|
|
|
|
state = 0 |
|
|
|
|
if !util.IsSameDayTimeStamp(now.Unix(), weekCardData.AwardAt) && |
|
|
|
|
weekCardData.AwardTimes+1 == index { |
|
|
|
|
state = 1 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
award := cons.AwardDetails[index] |
|
|
|
|
var coinDesc string |
|
|
|
|
if award.AwardLower == award.AwardUpper || award.AwardUpper == 0 { |
|
|
|
|
coinDesc = fmt.Sprintf("₹%d", award.AwardLower) |
|
|
|
|
} else { |
|
|
|
|
coinDesc = fmt.Sprintf("₹%d-₹%d", award.AwardLower, award.AwardUpper) |
|
|
|
|
} |
|
|
|
|
resp.List = append(resp.List, values.WeekCardDay{ |
|
|
|
|
State: state, |
|
|
|
|
Coin: coinDesc, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
resp.RewardDay = cardInfo.Day |
|
|
|
|
resp.ChannelList = call.GetConfigPayChannelsByID(common.CurrencyINR) |
|
|
|
|
resp.ProductList = call.GetConfigPayProductByActivityID(common.ActivityIDWeekCard) |
|
|
|
|
if resp.Status && resp.RechargeStatus { |
|
|
|
|
call.PushRed(a.UID, pb.RedPointModule_RedPointWeekCard, 1) |
|
|
|
|
|
|
|
|
|
products := call.GetConfigPayProductByActivityID(common.ActivityIDWeekCard) |
|
|
|
|
for _, v := range products { |
|
|
|
|
if v.Amount == resp.Amount*common.DecimalDigits { |
|
|
|
|
resp.ProductId = v.ProductID |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
call.UploadActivityData(a.UID, common.ActivityIDSign, common.ActivityDataClick, 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func WeekCardDraw(c *gin.Context) { |
|
|
|
|
@ -103,123 +82,136 @@ func WeekCardDraw(c *gin.Context) { |
|
|
|
|
defer func() { |
|
|
|
|
a.Response() |
|
|
|
|
}() |
|
|
|
|
if !a.CheckActivityExpire(common.ActivityIDWeekCard) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
req := new(values.ActivityWeekCardDrawReq) |
|
|
|
|
req := new(values.WeekCardDrawReq) |
|
|
|
|
if !a.S(req) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
resp := &values.ActivityWeekCardDrawResp{} |
|
|
|
|
resp := &values.WeekCardDrawResp{} |
|
|
|
|
a.Data = resp |
|
|
|
|
// conf := call.GetConfigActivityWeekCard()
|
|
|
|
|
card := call.GetUserWeekCard(a.UID) |
|
|
|
|
if card.ID == 0 { |
|
|
|
|
uid := a.UID |
|
|
|
|
|
|
|
|
|
if !db.Redis().Lock(common.GetRedisKeyWeekCard(uid)) { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
now := time.Now().Unix() |
|
|
|
|
if config.GetBase().Release { |
|
|
|
|
if util.IsSameDayTimeStamp(now, card.LastDraw) { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
a.Msg = "today has reward" |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
defer func() { |
|
|
|
|
db.Redis().UnLock(common.GetRedisKeyWeekCard(uid)) |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
configWeekCard := call.GetConfigWeekCard() |
|
|
|
|
if configWeekCard == nil { |
|
|
|
|
log.Error("get week card config is nil") |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
a.Msg = "Illegal request" |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
rewards, _ := util.StringToInt64Slice(card.Rewards, ",") |
|
|
|
|
if card.Day >= len(rewards) { |
|
|
|
|
|
|
|
|
|
weekCardData, err := call.GetWeekCardData(uid) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("get week card data err, %s", err.Error()) |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
a.Msg = "The weekly card has been collected" |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
rows, err := db.Mysql().UpdateRes(&common.ActivityWeekCardData{UID: a.UID}, |
|
|
|
|
map[string]interface{}{"day": gorm.Expr("day + 1"), "last_draw": now}) |
|
|
|
|
if rows == 0 || err != nil { |
|
|
|
|
log.Error("err:%v", err) |
|
|
|
|
now := time.Now() |
|
|
|
|
if weekCardData.ID == 0 || weekCardData.BeginAt == 0 { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
a.Msg = "Not unlocked Week Card" |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
var reward int64 |
|
|
|
|
if card.Day < 6 { |
|
|
|
|
reward = rewards[card.Day] * common.DecimalDigits |
|
|
|
|
|
|
|
|
|
if util.IsSameDayTimeStamp(now.Unix(), weekCardData.AwardAt) { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
a.Msg = "Already received today" |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if req.Index != weekCardData.AwardTimes+1 { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
a.Msg = "Illegal request" |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
var awardCount int64 |
|
|
|
|
maxAwardCount := weekCardData.CardPrice * 2 * common.DecimalDigits // 总奖励金额
|
|
|
|
|
lessAwardCount := maxAwardCount - weekCardData.AwardTotal // 剩余奖励金额
|
|
|
|
|
if req.Index == 7 { // 最后一天
|
|
|
|
|
awardCount = int64(maxAwardCount - weekCardData.AwardTotal) |
|
|
|
|
} else { |
|
|
|
|
// 第几天折扣券
|
|
|
|
|
// 用户画像一:6天内充值3笔及以上用户
|
|
|
|
|
// 推送当前最高额度向上一档充值满减卷
|
|
|
|
|
// 用户画像二:只解锁周卡,未充值的玩家
|
|
|
|
|
// 推送当前额度向下一档充值满减卷,最低300
|
|
|
|
|
var list []common.CurrencyBalance |
|
|
|
|
q := elastic.NewBoolQuery() |
|
|
|
|
q.Filter(elastic.NewRangeQuery("time").Gte(card.RechargeTime)) |
|
|
|
|
q.Filter(elastic.NewRangeQuery("time").Lte(now)) |
|
|
|
|
q.Filter(elastic.NewRangeQuery("event").Gte(common.CurrencyEventReCharge)) |
|
|
|
|
q.Must(elastic.NewTermsQuery("uid", a.UID)) |
|
|
|
|
db.ES().QueryList(common.ESIndexBalance, 0, 100, q, &list, "time", true) |
|
|
|
|
up := false |
|
|
|
|
maxAmount := card.RechargeAmount |
|
|
|
|
if len(list) >= 3 { |
|
|
|
|
up = true |
|
|
|
|
for _, item := range list { |
|
|
|
|
if item.Value > maxAmount { |
|
|
|
|
maxAmount = item.Value |
|
|
|
|
} |
|
|
|
|
tryCount := 10 |
|
|
|
|
awardOffset := 0 |
|
|
|
|
lessMinAwardCount := 0 // 剩下每天的最低金额
|
|
|
|
|
lessMaxAwardCount := 0 // 剩下每天的最大金额
|
|
|
|
|
for index := req.Index + 1; index <= 7; index++ { |
|
|
|
|
lessMinAwardCount += configWeekCard.AwardDetails[index].AwardLower * common.DecimalDigits |
|
|
|
|
lessMaxAwardCount += configWeekCard.AwardDetails[index].AwardUpper * common.DecimalDigits |
|
|
|
|
} |
|
|
|
|
try: |
|
|
|
|
awardDetail := configWeekCard.AwardDetails[req.Index] |
|
|
|
|
if awardDetail.AwardUpper == awardDetail.AwardLower || awardDetail.AwardUpper == 0 { |
|
|
|
|
awardCount = int64(awardDetail.AwardUpper * common.DecimalDigits) |
|
|
|
|
} else { |
|
|
|
|
randValue := (awardDetail.AwardUpper - awardDetail.AwardLower) + awardOffset |
|
|
|
|
if randValue < 0 { |
|
|
|
|
randValue = 1 |
|
|
|
|
} |
|
|
|
|
awardCount = int64((rand.Intn(randValue) + awardDetail.AwardLower) * common.DecimalDigits) |
|
|
|
|
} |
|
|
|
|
log.Info("maxAmount:%v", maxAmount) |
|
|
|
|
ticket := call.GetConfigDiscountTicketByAmount(maxAmount) |
|
|
|
|
log.Info("ticket:%v", ticket) |
|
|
|
|
if ticket.ID > 0 { |
|
|
|
|
// 赠送优惠券
|
|
|
|
|
tickets := call.GetConfigDiscountTicket() |
|
|
|
|
sort.Slice(tickets, func(i, j int) bool { |
|
|
|
|
return tickets[i].RechargeAmount < tickets[j].RechargeAmount |
|
|
|
|
}) |
|
|
|
|
// 获取下一档
|
|
|
|
|
nextIdx := -1 |
|
|
|
|
for idx, item := range tickets { |
|
|
|
|
if item.RechargeAmount == maxAmount { |
|
|
|
|
if up { |
|
|
|
|
nextIdx = idx + 1 |
|
|
|
|
} else if item.RechargeAmount >= 300*common.DecimalDigits { |
|
|
|
|
nextIdx = idx - 1 |
|
|
|
|
} |
|
|
|
|
break |
|
|
|
|
if lessMinAwardCount+int(awardCount) > lessAwardCount || |
|
|
|
|
lessMaxAwardCount+int(awardCount) < lessAwardCount { // 当前金额+之后每天最低金额比剩余金额还大的时候
|
|
|
|
|
if tryCount > 0 { |
|
|
|
|
tryCount-- |
|
|
|
|
if lessMinAwardCount+int(awardCount) > lessAwardCount { |
|
|
|
|
awardOffset -= 10 * common.DecimalDigits |
|
|
|
|
} else if lessMaxAwardCount+int(awardCount) < lessAwardCount { |
|
|
|
|
awardOffset += 10 * common.DecimalDigits |
|
|
|
|
} |
|
|
|
|
goto try |
|
|
|
|
} |
|
|
|
|
var nextTicket = ticket |
|
|
|
|
if len(tickets) > nextIdx && nextIdx != -1 { |
|
|
|
|
nextTicket = tickets[nextIdx] |
|
|
|
|
} |
|
|
|
|
if up { |
|
|
|
|
call.AddUserDiscountTicket(a.UID, ticket.DiscountAmount, ticket.RechargeAmount, -1, 0, false) |
|
|
|
|
call.SendMailWithContent(a.UID, call.SystemTitle, fmt.Sprintf(call.EmailDiscount, ticket.DiscountAmount/common.DecimalDigits, ticket.RechargeAmount/common.DecimalDigits)) |
|
|
|
|
resp.DiscountTicket = ticket.DiscountAmount |
|
|
|
|
resp.Amount = ticket.RechargeAmount |
|
|
|
|
} else if nextTicket.ID > 0 { |
|
|
|
|
call.AddUserDiscountTicket(a.UID, nextTicket.DiscountAmount, nextTicket.RechargeAmount, -1, 1, false) |
|
|
|
|
call.SendMailWithContent(a.UID, call.SystemTitle, fmt.Sprintf(call.EmailDiscount, ticket.DiscountAmount/common.DecimalDigits, ticket.RechargeAmount/common.DecimalDigits)) |
|
|
|
|
resp.DiscountTicket = nextTicket.DiscountAmount |
|
|
|
|
resp.Amount = nextTicket.RechargeAmount |
|
|
|
|
} |
|
|
|
|
log.Error("uid:%d weekCard draw err, lessAward:%d lessMinAward:%d lessMaxAward:%d | awardCount:%d awardOffset:%d", uid, lessAwardCount, lessMinAwardCount, lessMaxAwardCount, awardCount, awardOffset) |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
resource := common.CurrencyResourceWeekCard |
|
|
|
|
if card.Day > 0 { |
|
|
|
|
resource = common.CurrencyResourceBonus |
|
|
|
|
} |
|
|
|
|
resp.Reward = reward |
|
|
|
|
if reward > 0 { |
|
|
|
|
call.UpdateCurrencyPro(&common.UpdateCurrency{ |
|
|
|
|
CurrencyBalance: &common.CurrencyBalance{ |
|
|
|
|
UID: a.UID, |
|
|
|
|
Type: common.CurrencyINR, |
|
|
|
|
Value: reward, |
|
|
|
|
Event: common.CurrencyEventActivityWeekCard, |
|
|
|
|
ChannelID: a.Channel, |
|
|
|
|
NeedBet: call.GetConfigCurrencyResourceNeedBet(resource, reward), |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if int(awardCount)+weekCardData.AwardTotal > maxAwardCount { |
|
|
|
|
awardCount = int64(maxAwardCount - weekCardData.AwardTotal) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if awardCount <= 0 { |
|
|
|
|
a.Code = values.CodeRetry |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_, err = call.UpdateCurrencyPro(&common.UpdateCurrency{ |
|
|
|
|
CurrencyBalance: &common.CurrencyBalance{ |
|
|
|
|
UID: a.UID, |
|
|
|
|
Type: common.CurrencyINR, |
|
|
|
|
Value: awardCount, |
|
|
|
|
Event: common.CurrencyEventActivityWeekCard, |
|
|
|
|
ChannelID: a.Channel, |
|
|
|
|
NeedBet: call.GetConfigCurrencyResourceNeedBet(common.CurrencyResourceBonus, awardCount, configWeekCard.BetMultiples), |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("update currency err, %s", err.Error()) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
updates := make(map[string]interface{}) |
|
|
|
|
if req.Index == 7 { |
|
|
|
|
updates["begin_at"] = 0 |
|
|
|
|
updates["award_times"] = 0 |
|
|
|
|
updates["award_at"] = 0 |
|
|
|
|
updates["award_total"] = 0 |
|
|
|
|
updates["card_price"] = 0 |
|
|
|
|
updates["award_total_history"] = gorm.Expr("`award_total_history` + ?", awardCount) |
|
|
|
|
} else { |
|
|
|
|
updates["award_total"] = gorm.Expr("award_total + ?", awardCount) |
|
|
|
|
updates["award_times"] = gorm.Expr("`award_times` + ?", 1) |
|
|
|
|
updates["award_at"] = now.Unix() |
|
|
|
|
updates["award_total_history"] = gorm.Expr("`award_total_history` + ?", awardCount) |
|
|
|
|
} |
|
|
|
|
err = db.Mysql().C().Model(&common.WeekCardData{}).Where("id = ?", weekCardData.ID).Updates(updates).Error |
|
|
|
|
if err != nil { |
|
|
|
|
log.Error("update week card data err, %s", err.Error()) |
|
|
|
|
} |
|
|
|
|
call.PushRed(a.UID, pb.RedPointModule_RedPointWeekCard, 0) |
|
|
|
|
call.UploadActivityData(a.UID, common.ActivityIDSign, common.ActivityDataJoin, reward) |
|
|
|
|
resp.Reward = awardCount |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|