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.
300 lines
7.4 KiB
300 lines
7.4 KiB
package handler |
|
|
|
import ( |
|
"fmt" |
|
"github.com/gin-gonic/gin" |
|
"github.com/liangdas/mqant/log" |
|
"gorm.io/gorm" |
|
"math" |
|
"math/rand" |
|
"server/call" |
|
"server/common" |
|
"server/db" |
|
"server/modules/web/app" |
|
"server/modules/web/values" |
|
"server/util" |
|
"time" |
|
) |
|
|
|
func PddCfg(c *gin.Context) { |
|
a := app.NewApp(c) |
|
defer func() { |
|
a.Response() |
|
}() |
|
|
|
req := new(values.PddCfgReq) |
|
if !a.S(req) { |
|
return |
|
} |
|
uid := a.UID |
|
|
|
if !db.Redis().Lock(common.GetRedisKeyPdd(uid)) { |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
defer func() { |
|
db.Redis().UnLock(common.GetRedisKeyPdd(uid)) |
|
}() |
|
|
|
resp := &values.PddCfgResp{} |
|
a.Data = resp |
|
pdd := call.GetConfigPdd() |
|
if pdd == nil { |
|
return |
|
} |
|
pddData, err := call.GetPddData(uid) |
|
if err != nil { |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
channel := call.GetChannelByID(a.Channel) |
|
if channel != nil { |
|
resp.ShareLink = channel.URL + "?code=" + call.GetShareInfo(a.UID).Share |
|
} |
|
resp.WithdrawalAmount = pdd.WithdrawalAmount |
|
resp.LotteryCount = pddData.Spin |
|
resp.UserAmount = pddData.Amount |
|
resp.Countdown = pddData.ExpiredAt - time.Now().Unix() |
|
resp.Awards = pdd.PddCfgStr.PddAwards |
|
if pddData.Amount == "" { |
|
resp.InitialAmount = make([]string, 4) |
|
for index := range resp.InitialAmount { |
|
var realValue float64 |
|
switch index { |
|
case 0: |
|
realValue = float64(rand.Intn(pdd.WithdrawalAmount*30) + pdd.WithdrawalAmount*10) |
|
case 1: |
|
realValue = float64(rand.Intn(pdd.WithdrawalAmount*40) + pdd.WithdrawalAmount*40) |
|
case 2: |
|
realValue = float64(rand.Intn(pdd.WithdrawalAmount*30) + pdd.WithdrawalAmount*60) |
|
case 3: |
|
realValue = float64(rand.Intn(pdd.WithdrawalAmount*4) + pdd.WithdrawalAmount*95) |
|
} |
|
resp.InitialAmount[index] = fmt.Sprintf("%.2f", realValue/100) |
|
if index == 3 { |
|
resp.UserAmount = resp.InitialAmount[index] |
|
err = db.Mysql().C().Model(&common.PddDataNew{}).Where("id = ?", pddData.ID).Updates(map[string]interface{}{ |
|
"amount": resp.InitialAmount[index], |
|
}).Error |
|
if err != nil { |
|
log.Error("update amount err, %s", err.Error()) |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
err = db.Mysql().C().Model(&common.PddLotteryHistory{}).Create(&common.PddLotteryHistory{ |
|
UID: uid, |
|
ExpiredAt: pddData.ExpiredAt, |
|
AddAmount: resp.UserAmount, |
|
Amount: resp.UserAmount, |
|
CreatedAt: time.Now().Unix(), |
|
}).Error |
|
if err != nil { |
|
log.Error("create pdd history err, %s", err.Error()) |
|
} |
|
|
|
} |
|
} |
|
} |
|
historyDB := db.Mysql().C().Model(&common.PddLotteryHistory{}). |
|
Where("uid = ? and expired_at = ?", uid, pddData.ExpiredAt). |
|
Order("created_at desc") |
|
if req.Page != 0 && req.PageSize != 0 { |
|
historyDB = historyDB.Offset((req.Page - 1) * req.PageSize).Limit(req.PageSize) |
|
} |
|
err = historyDB.Find(&resp.LotteryHistory).Error |
|
if err != nil { |
|
log.Error("get lottery history err, %s", err.Error()) |
|
} |
|
if len(resp.LotteryHistory) == 0 && pddData.Amount == "" { |
|
resp.LotteryHistory = []common.PddLotteryHistory{ |
|
{ |
|
UID: uid, |
|
ExpiredAt: pddData.ExpiredAt, |
|
AddAmount: resp.UserAmount, |
|
Amount: resp.UserAmount, |
|
CreatedAt: time.Now().Unix(), |
|
}, |
|
} |
|
} |
|
log.Debug("resp:%+v", *resp) |
|
} |
|
|
|
func PddLottery(c *gin.Context) { |
|
a := app.NewApp(c) |
|
defer func() { |
|
a.Response() |
|
}() |
|
|
|
req := new(values.PddLotteryReq) |
|
if !a.S(req) { |
|
return |
|
} |
|
uid := a.UID |
|
|
|
if !db.Redis().Lock(common.GetRedisKeyPdd(uid)) { |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
defer func() { |
|
db.Redis().UnLock(common.GetRedisKeyPdd(uid)) |
|
}() |
|
|
|
resp := &values.PddLotteryResp{} |
|
a.Data = resp |
|
pdd := call.GetConfigPdd() |
|
if pdd == nil { |
|
return |
|
} |
|
pddData, err := call.GetPddData(uid) |
|
if err != nil { |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
if pddData.Amount == "" { |
|
a.Code = values.CodeParam |
|
a.Msg = "The event has expired, please reopen it." |
|
return |
|
} |
|
if pddData.Spin <= 0 { |
|
a.Code = values.CodeParam |
|
a.Msg = "Not enough spin." |
|
return |
|
} |
|
userAmount := util.ToFloat64(pddData.Amount) |
|
if util.ToInt(userAmount) == pdd.WithdrawalAmount { |
|
a.Code = values.CodeParam |
|
return |
|
} |
|
updateValues := map[string]interface{}{ |
|
"spin": gorm.Expr("spin - 1"), |
|
} |
|
var award string |
|
amountFinal := pddData.Amount |
|
lessAmount := float64(pdd.WithdrawalAmount) - userAmount |
|
if pddData.InviterAmount >= int64(pdd.WithdrawalAmount)*20 { |
|
award = fmt.Sprintf("%v", lessAmount) |
|
amountFinal = fmt.Sprintf("%d", pdd.WithdrawalAmount) |
|
updateValues["amount"] = amountFinal |
|
} else { |
|
var addAmount float64 |
|
if lessAmount > 1 { |
|
addAmountMax := lessAmount - 1 |
|
if addAmountMax <= 0.01 { |
|
addAmount = 0.01 |
|
} else { |
|
maxCents := int(math.Round(addAmountMax * 100)) |
|
if maxCents <= 0 { |
|
maxCents = 1 // 确保至少为1分钱 |
|
} |
|
randValue := rand.Intn(maxCents) + 1 |
|
addAmount = float64(randValue) / float64(100) |
|
} |
|
} else { // 0.03 |
|
randValue := rand.Intn(10) |
|
if randValue < 7 { // 70%给0.01 |
|
addAmount = 0.01 |
|
} else if randValue < 9 { // 20%给0.02 |
|
addAmount = 0.02 |
|
} else { // 10%给0.03 |
|
addAmount = 0.03 |
|
} |
|
} |
|
award = fmt.Sprintf("%.2f", addAmount) |
|
userAmount += addAmount |
|
if userAmount > float64(pdd.WithdrawalAmount) { |
|
userAmount = float64(pdd.WithdrawalAmount) |
|
} |
|
amountFinal = fmt.Sprintf("%.2f", userAmount) |
|
updateValues["amount"] = amountFinal |
|
} |
|
err = db.Mysql().C().Model(&common.PddDataNew{}).Where("id = ?", pddData.ID).Updates(updateValues).Error |
|
if err != nil { |
|
log.Error("update pdd data err, %s", err.Error()) |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
history := common.PddLotteryHistory{ |
|
UID: uid, |
|
ExpiredAt: pddData.ExpiredAt, |
|
AddAmount: award, |
|
Amount: amountFinal, |
|
CreatedAt: time.Now().Unix(), |
|
} |
|
err = db.Mysql().C().Model(&common.PddLotteryHistory{}).Create(&history).Error |
|
if err != nil { |
|
log.Error("create history err, %s", err.Error()) |
|
} |
|
resp.LotteryHistory = history |
|
resp.LessTimes = pddData.Spin - 1 |
|
resp.Amount = amountFinal |
|
resp.Award = award |
|
} |
|
|
|
func PddDraw(c *gin.Context) { |
|
a := app.NewApp(c) |
|
defer func() { |
|
a.Response() |
|
}() |
|
|
|
req := new(values.PddDrawReq) |
|
if !a.S(req) { |
|
return |
|
} |
|
uid := a.UID |
|
|
|
if !db.Redis().Lock(common.GetRedisKeyPdd(uid)) { |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
defer func() { |
|
db.Redis().UnLock(common.GetRedisKeyPdd(uid)) |
|
}() |
|
|
|
resp := &values.PddDrawResp{} |
|
a.Data = resp |
|
pdd := call.GetConfigPdd() |
|
if pdd == nil { |
|
return |
|
} |
|
pddData, err := call.GetPddData(uid) |
|
if err != nil { |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
userAmount := util.ToFloat64(pddData.Amount) |
|
if util.ToInt(userAmount) < pdd.WithdrawalAmount { |
|
a.Code = values.CodeParam |
|
a.Msg = "Insufficient amount." |
|
return |
|
} |
|
now := time.Now() |
|
updateValues := map[string]interface{}{ |
|
"amount": "", |
|
"spin": 0, |
|
"expired_at": now.AddDate(0, 0, pdd.Cycle).Unix(), |
|
"inviter_amount": 0, |
|
"free_spin_at": 0, |
|
} |
|
err = db.Mysql().C().Model(&common.PddDataNew{}).Where("id = ?", pddData.ID).Updates(updateValues).Error |
|
if err != nil { |
|
log.Error("update pdd data err, %s", err.Error()) |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
_, err = call.UpdateCurrencyPro(&common.UpdateCurrency{ |
|
CurrencyBalance: &common.CurrencyBalance{ |
|
UID: a.UID, |
|
Event: common.CurrencyEventActivityPdd, |
|
Type: common.CurrencyINR, |
|
ChannelID: a.Channel, |
|
Value: int64(pdd.WithdrawalAmount), |
|
NeedBet: call.GetConfigCurrencyResourceNeedBet(common.CurrencyResourceBonus, int64(pdd.WithdrawalAmount), pdd.BetMultiples), |
|
}, |
|
}) |
|
if err != nil { |
|
log.Error("update currency err, %s", err.Error()) |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
resp.Amount = pdd.WithdrawalAmount |
|
}
|
|
|