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 GetVipInfoNew(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.VipInfoReq) if !a.S(req) { return } if req.Num > 100 { req.Num = 100 } resp := &values.VipInfoResp{ List: call.GetConfigVIPWithout0(), } a.Data = resp a.GetUID() if a.UID == 0 { return } resp.Info = call.GetVIP(a.UID) var levelBonus []int for level := 1; level <= resp.Info.Level; level++ { flag := int64(1 << level) if resp.Info.Draws&flag == 0 { levelBonus = append(levelBonus, level) } } con := call.GetConfigVIPByLevel(resp.Info.Level) if con == nil { return } var weekBonus bool if con.BonusWeek != 0 { // 有周奖励 if resp.Info.WeekBonusAt == 0 { // 可以领取 weekBonus = true } else { weekBonus = !util.IsSameWeek(time.Now().Unix(), resp.Info.WeekBonusAt) } } for _, v := range resp.List { if util.SliceContain(levelBonus, v.Level) { // 可领取 v.BonusDraw = 1 } else { if v.Level <= resp.Info.Level { v.BonusDraw = 2 } } if v.Level == resp.Info.Level { if weekBonus { v.BonusWeekDraw = 1 } else { v.BonusWeekDraw = 2 } } } return } 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 DrawVipBonusNew(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 } updateValues := make(map[string]interface{}) var bonus int64 con := call.GetConfigVIPByLevel(req.Level) if con == nil { a.Code = values.CodeParam a.Msg = "VIP level is insufficient" return } if !req.Week { flag := int64(1 << req.Level) if vip.Draws&flag > 0 { a.Code = values.CodeParam a.Msg = "reward has been claimed" return } newDraws := vip.Draws | flag updateValues["draws"] = newDraws bonus = con.Bonus } else { // 计算是不是同个周 if vip.WeekBonusAt != 0 { if util.IsSameWeek(time.Now().Unix(), vip.WeekBonusAt) { a.Code = values.CodeParam a.Msg = "reward has been claimed" return } } updateValues["week_bonus_at"] = time.Now().Unix() bonus = con.BonusWeek } if bonus <= 0 { a.Code = values.CodeParam a.Msg = "Reward invalid" return } rows, err := db.Mysql().UpdateResW(&common.VipData{}, updateValues, fmt.Sprintf("uid = %d", a.UID)) if err != nil { log.Error("err:%v", err) a.Code = values.CodeRetry return } if rows == 0 { a.Code = values.CodeRetry return } event := common.CurrencyEventVIPBonus if req.Week { event = common.CurrencyEventVIPWeekBonus } _, err = call.UpdateCurrencyPro(&common.UpdateCurrency{ CurrencyBalance: &common.CurrencyBalance{ UID: a.UID, Event: event, Type: common.CurrencyINR, ChannelID: a.Channel, Value: bonus, NeedBet: call.GetConfigCurrencyResourceNeedBet(common.CurrencyResourceVipBonus, 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 VipBalanceHistory(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.VipBonusHistoryReq) if !a.S(req) { return } if req.PageSize > 100 { log.Error("balance req num:%v", req.PageSize) req.PageSize = 100 } resp := values.BalanceHisResp{} q := elastic.NewBoolQuery() q.Filter(elastic.NewTermQuery("uid", a.UID)) q.Filter(elastic.NewTermsQuery("event", common.CurrencyEventVIPBonus, common.CurrencyEventVIPWeekBonus)) var startAt, endAt time.Time if req.Month { startAt = util.GetFirstDateOfMonth(time.Now()) endAt = startAt.AddDate(0, 1, 0) q.Filter(elastic.NewRangeQuery("time").Gte(startAt.Unix()).Lt(endAt.Unix())) } else { player, _ := call.GetUserXInfo(a.UID, "birth") q.Filter(elastic.NewRangeQuery("time").Gte(player.Birth)) } resp.Count, _ = db.ES().QueryList(common.ESIndexBalance, req.Page, req.PageSize, q, &resp.List, "id", false) resp.TotalAmount = db.ES().SumByInt64(common.ESIndexBalance, "value", q) a.Data = resp } 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 = "Reward invalid" 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 = "Reward invalid" 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 = "Reward invalid" 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 = "Reward invalid" 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 = "Reward invalid" 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)) }