印度包网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

291 lines
8.3 KiB

package handler
import (
"fmt"
"server/call"
"server/common"
"server/config"
"server/db"
"server/modules/web/app"
"server/modules/web/values"
"server/pb"
"server/util"
"time"
"github.com/gin-gonic/gin"
"github.com/liangdas/mqant/log"
"github.com/olivere/elastic/v7"
)
func GetVipInfo(c *gin.Context) {
a := app.NewApp(c)
defer func() {
a.Response()
}()
req := new(values.VipInfoReq)
ok, _ := util.CheckRequestBody(c)
if ok && !a.S(req) {
return
}
if req.Num > 100 {
req.Num = 100
}
resp := &values.VipInfoResp{
List: call.GetConfigVIP(),
}
a.Data = resp
a.GetUID()
if a.UID == 0 {
return
}
resp.Info = call.GetVIP(a.UID)
con := call.GetConfigVIPByLevel(resp.Info.Level)
if con == nil {
return
}
if resp.Info.Profit < 0 {
resp.Info.Profit = 0
}
reset := false
now := time.Now()
if config.GetBase().Release {
reset = !util.IsSameDayTimeStamp(now.Unix(), resp.Info.ProfitTime)
if reset {
if util.GetZeroTime(now).Unix()-resp.Info.ProfitTime > 24*3600 { // 超时未领取直接归零
db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"profit_time": now.Unix(), "cashback": 0, "profit": 0},
fmt.Sprintf("uid = %v and profit_time = %v", a.UID, resp.Info.ProfitTime))
resp.Info.Cashback = 0
resp.Info.Profit = 0
} else {
cashback := resp.Info.Profit * con.Cashback / 1000
if cashback < 0 {
cashback = 0
}
resp.Info.Cashback = cashback
db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"profit_time": now.Unix(), "cashback": cashback, "profit": 0},
fmt.Sprintf("uid = %v and profit_time = %v", a.UID, resp.Info.ProfitTime))
}
}
} else {
reset = util.GetNext5MinUnix()-resp.Info.ProfitTime >= 5*60
if reset {
if util.GetNext5MinUnix()-resp.Info.ProfitTime > 2*5*60 { // 超时未领取直接归零
db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"profit_time": now.Unix(), "cashback": 0, "profit": 0},
fmt.Sprintf("uid = %v and profit_time = %v", a.UID, resp.Info.ProfitTime))
resp.Info.Cashback = 0
resp.Info.Profit = 0
} else {
cashback := resp.Info.Profit * con.Cashback / 1000
if cashback < 0 {
cashback = 0
}
resp.Info.Cashback = cashback
db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"profit_time": now.Unix(), "cashback": cashback, "profit": 0},
fmt.Sprintf("uid = %v and profit_time = %v", a.UID, resp.Info.ProfitTime))
}
}
}
if resp.Info.Cashback == 0 && resp.Info.Level > 0 {
con := call.GetVipCon(a.UID)
resp.Info.Cashback = resp.Info.Profit * con.Cashback / 1000
if config.GetBase().Release {
resp.NextCashback = util.GetZeroTime(now.AddDate(0, 0, 1)).Unix() - now.Unix()
} else {
resp.NextCashback = util.GetNext5MinUnix() - now.Unix()
}
}
if resp.Info.Cashback < 0 {
resp.Info.Cashback = 0
}
resp.CashbackList = []common.CurrencyBalance{}
q := elastic.NewBoolQuery()
q.Filter(elastic.NewTermQuery("uid", a.UID))
q.Filter(elastic.NewTermQuery("event", common.CurrencyEventVIPCashback))
resp.TotalCashback = db.ES().SumByInt64(common.ESIndexBalance, "value", q)
db.ES().QueryList(common.ESIndexBalance, req.Page, req.Num, q, &resp.CashbackList, "time", false)
reset = !util.IsSameDayTimeStamp(now.Unix(), resp.Info.ProfitTime)
if reset && resp.Info.Cashback > 0 {
call.PushRed(a.UID, pb.RedPointModule_RedPointVipCashback, uint32(1))
}
if hasVipBonus(resp.Info) {
call.PushRed(a.UID, pb.RedPointModule_RedPointVipReward, uint32(1))
}
}
func hasVipBonus(vip *common.VipData) bool {
// 遍历所有VIP级别
for level := 1; level <= vip.Level; level++ {
// 计算标志位
flag := int64(1 << level)
// 检查该级别的奖励是否已经领取
if vip.Draws&flag == 0 {
return true
}
}
return false
}
func DrawVipBonus(c *gin.Context) {
a := app.NewApp(c)
defer func() {
a.Response()
}()
req := new(values.DrawVipBonusReq)
if !a.S(req) {
return
}
cons := call.GetConfigVIP()
if len(cons) == 0 {
a.Code = values.CodeRetry
return
}
if req.Level < 0 || req.Level > cons[len(cons)-1].Level {
a.Code = values.CodeParam
a.Msg = "VIP level is insufficient"
return
}
vip := call.GetVIP(a.UID)
if vip.Level < req.Level {
a.Code = values.CodeParam
a.Msg = "VIP level is insufficient"
return
}
flag := int64(1 << req.Level)
if vip.Draws&flag > 0 {
a.Code = values.CodeParam
a.Msg = "reward has been claimed"
return
}
con := call.GetConfigVIPByLevel(req.Level)
if con == nil {
a.Code = values.CodeParam
a.Msg = "VIP level is insufficient"
return
}
bonus := con.Bonus
if bonus <= 0 {
a.Code = values.CodeParam
a.Msg = "इनम अमय"
return
}
newDraws := vip.Draws | flag
rows, err := db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"draws": newDraws}, fmt.Sprintf("uid = %d and draws = %v", a.UID, vip.Draws))
if err != nil {
log.Error("err:%v", err)
a.Code = values.CodeRetry
return
}
if rows == 0 {
a.Code = values.CodeRetry
return
}
_, err = call.UpdateCurrencyPro(&common.UpdateCurrency{
CurrencyBalance: &common.CurrencyBalance{
UID: a.UID,
Event: common.CurrencyEventVIPBonus,
Type: common.CurrencyINR,
ChannelID: a.Channel,
Value: bonus,
NeedBet: call.GetConfigCurrencyResourceNeedBet(common.CurrencyResourceBonus, bonus),
},
})
if err != nil {
log.Error("err:%v", err)
a.Code = values.CodeRetry
return
}
vip = call.GetVIP(a.UID)
if !hasVipBonus(vip) {
call.PushRed(a.UID, pb.RedPointModule_RedPointVipReward, uint32(0))
}
}
func DrawVipCashback(c *gin.Context) {
a := app.NewApp(c)
defer func() {
a.Response()
}()
vip := call.GetVIP(a.UID)
cashback := vip.Cashback
con := call.GetConfigVIPByLevel(vip.Level)
reset := false
now := time.Now()
update := map[string]interface{}{"cashback": 0}
if config.GetBase().Release {
reset = !util.IsSameDayTimeStamp(now.Unix(), vip.ProfitTime)
if reset {
if util.GetZeroTime(now).Unix()-vip.ProfitTime > 24*3600 { // 超时未领取直接归零
db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"profit_time": now.Unix(), "cashback": 0, "profit": 0},
fmt.Sprintf("uid = %v and profit_time = %v", a.UID, vip.ProfitTime))
a.Code = values.CodeParam
a.Msg = "इनम अमय"
return
} else {
cashback = vip.Profit * con.Cashback / 1000
update["profit"] = 0
update["profit_time"] = time.Now().Unix()
}
}
} else {
reset = util.GetNext5MinUnix()-vip.ProfitTime >= 5*60
if reset {
if util.GetNext5MinUnix()-vip.ProfitTime > 2*5*60 { // 超时未领取直接归零
db.Mysql().UpdateResW(&common.VipData{}, map[string]interface{}{"profit_time": now.Unix(), "cashback": 0, "profit": 0},
fmt.Sprintf("uid = %v and profit_time = %v", a.UID, vip.ProfitTime))
a.Code = values.CodeParam
a.Msg = "इनम अमय"
return
} else {
cashback = vip.Profit * con.Cashback / 1000
update["profit"] = 0
update["profit_time"] = time.Now().Unix()
}
}
}
if cashback <= 0 {
a.Code = values.CodeParam
a.Msg = "इनम अमय"
return
}
q := elastic.NewBoolQuery()
q.Filter(elastic.NewTermQuery("uid", a.UID))
q.Filter(elastic.NewTermQuery("event", common.CurrencyEventVIPCashback))
totalCashback := db.ES().SumByInt64(common.ESIndexBalance, "value", q)
rows, err := db.Mysql().UpdateResW(&common.VipData{}, update, fmt.Sprintf("uid = %v and cashback = %v", a.UID, vip.Cashback))
if err != nil || rows == 0 {
log.Error("err:%v", err)
a.Code = values.CodeParam
a.Msg = "इनम अमय"
return
}
_, err = call.UpdateCurrencyPro(&common.UpdateCurrency{
CurrencyBalance: &common.CurrencyBalance{
UID: a.UID,
Event: common.CurrencyEventVIPCashback,
Type: common.CurrencyINR,
ChannelID: a.Channel,
Value: cashback,
NeedBet: call.GetConfigCurrencyResourceNeedBet(common.CurrencyResourceBonus, cashback),
},
})
if err != nil {
log.Error("err:%v", err)
a.Code = values.CodeRetry
return
}
resp := values.DrawCashbackResp{
TotalCashback: totalCashback + cashback,
Balance: 0,
}
if config.GetBase().Release {
resp.NextCashback = util.GetZeroTime(now.AddDate(0, 0, 1)).Unix() - now.Unix()
} else {
resp.NextCashback = util.GetNext5MinUnix() - now.Unix()
}
a.Data = resp
call.PushRed(a.UID, pb.RedPointModule_RedPointVipCashback, uint32(0))
}