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.
425 lines
12 KiB
425 lines
12 KiB
package lottery_ticket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golangkit/formatime"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/shopspring/decimal"
|
|
"recook/configs"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/libs/lottery"
|
|
"recook/internal/model/lottery_ticket"
|
|
"recook/internal/model/user"
|
|
"recook/tools"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Lottery struct {
|
|
}
|
|
|
|
type replyItem struct {
|
|
Id uint `json:"id"`
|
|
Name string `json:"name"`
|
|
Icon string `json:"icon"`
|
|
Number string `json:"number"`
|
|
Now lottery_ticket.LotteryIssue `json:"now"`
|
|
Last lottery_ticket.LotteryIssue `json:"last"`
|
|
}
|
|
|
|
// @Style 彩票列表
|
|
func (l *Lottery) List(c *gin.Context) {
|
|
lotterys := []lottery_ticket.Lottery{}
|
|
dbc.DB.Model(&lottery_ticket.Lottery{}).Find(&lotterys)
|
|
result := []replyItem{}
|
|
if len(lotterys) == 0 {
|
|
back.Suc(c, "操作成功", result)
|
|
return
|
|
}
|
|
Numbers := []string{}
|
|
for _, lottery := range lotterys {
|
|
Numbers = append(Numbers, lottery.NowNumber)
|
|
Numbers = append(Numbers, lottery.LastNumber)
|
|
}
|
|
|
|
lotteryIssues := []lottery_ticket.LotteryIssue{}
|
|
dbc.DB.Model(&lottery_ticket.LotteryIssue{}).Where("number in (?)", Numbers).Find(&lotteryIssues)
|
|
lotteryIssueMap := map[string]lottery_ticket.LotteryIssue{}
|
|
for _, issue := range lotteryIssues {
|
|
key := fmt.Sprintf("%v|%v", issue.LotteryId, issue.Number)
|
|
lotteryIssueMap[key] = issue
|
|
}
|
|
for _, lottery := range lotterys {
|
|
nowKey := fmt.Sprintf("%v|%v", lottery.Id, lottery.NowNumber)
|
|
lastKey := fmt.Sprintf("%v|%v", lottery.Id, lottery.LastNumber)
|
|
result = append(result, replyItem{
|
|
Id: lottery.Id,
|
|
Name: lottery.Name,
|
|
Icon: lottery.Icon,
|
|
Number: lottery.NowNumber,
|
|
Now: lotteryIssueMap[nowKey],
|
|
Last: lotteryIssueMap[lastKey],
|
|
})
|
|
}
|
|
|
|
back.Suc(c, "操作成功", result)
|
|
return
|
|
}
|
|
|
|
type argsInfo struct {
|
|
LotteryId uint `json:"lotteryId" form:"lotteryId"`
|
|
}
|
|
|
|
// @Style 彩票详情
|
|
func (l *Lottery) Info(c *gin.Context) {
|
|
args := argsInfo{}
|
|
if err := tools.Params(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.LotteryId <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
|
|
lottery := lottery_ticket.Lottery{}
|
|
dbc.DB.Model(&lottery).Where("id = ?", args.LotteryId).First(&lottery)
|
|
if lottery.Id <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
result := replyItem{
|
|
Id: lottery.Id,
|
|
Name: lottery.Name,
|
|
Icon: lottery.Icon,
|
|
Number: lottery.NowNumber,
|
|
}
|
|
|
|
lotteryIssues := []lottery_ticket.LotteryIssue{}
|
|
dbc.DB.Model(&lottery_ticket.LotteryIssue{}).Where("lottery_id = ? and number in (?)", lottery.Id, []string{lottery.NowNumber, lottery.LastNumber}).Find(&lotteryIssues)
|
|
for _, issue := range lotteryIssues {
|
|
if issue.Number == lottery.NowNumber {
|
|
result.Now = issue
|
|
} else {
|
|
result.Last = issue
|
|
}
|
|
}
|
|
|
|
back.Suc(c, "操作成功", result)
|
|
return
|
|
}
|
|
|
|
type argsHistory struct {
|
|
LotteryId uint `json:"lotteryId" form:"lotteryId"`
|
|
tools.Page
|
|
}
|
|
|
|
// @Style 彩票历史开奖记录
|
|
func (l *Lottery) History(c *gin.Context) {
|
|
args := argsHistory{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.LotteryId <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
info := lottery_ticket.Lottery{}
|
|
dbc.DB.First(&info, "id = ?", args.LotteryId)
|
|
if info.Id <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
lotteryIssues := []lottery_ticket.LotteryIssue{}
|
|
count := uint(0)
|
|
dbc.DB.Model(&lottery_ticket.LotteryIssue{}).Where("lottery_id = ? and status > ?", args.LotteryId, 1).Count(&count)
|
|
start := args.GetStart()
|
|
if start >= count {
|
|
back.Suc(c, "操作成功", gin.H{
|
|
"data": lotteryIssues,
|
|
"total": count,
|
|
})
|
|
return
|
|
}
|
|
dbc.DB.Model(&lottery_ticket.LotteryIssue{}).Where("lottery_id = ? and status > ?", args.LotteryId, 1).
|
|
Offset(args.GetStart()).Limit(args.GetLimit()).Order("id desc").Find(&lotteryIssues)
|
|
back.Suc(c, "操作成功", gin.H{
|
|
"data": lotteryIssues,
|
|
"total": count,
|
|
})
|
|
return
|
|
|
|
}
|
|
|
|
type argsOrderAdd struct {
|
|
LotteryId uint `json:"lotteryId" form:"lotteryId"`
|
|
Item []lottery.BetItem `json:"item" form:"item"`
|
|
Num int `json:"num" form:"num"`
|
|
}
|
|
|
|
// @Style 购买彩票
|
|
func (l *Lottery) Bet(c *gin.Context) {
|
|
args := argsOrderAdd{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.LotteryId <= 0 || args.Num <= 0 || len(args.Item) == 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
userId := l.getUserId(c)
|
|
userinfo := user.Information{}
|
|
dbc.DB.Find(&userinfo, "id = ?", userId)
|
|
if userinfo.RealInfoStatus != user.RealInfoPass {
|
|
back.Fail(c, "账号尚未进行实名认证")
|
|
return
|
|
}
|
|
// 当前期数信息获取
|
|
lotteryInfo := lottery_ticket.Lottery{}
|
|
dbc.DB.Find(&lotteryInfo, "id = ?", args.LotteryId)
|
|
issue := lottery_ticket.LotteryIssue{}
|
|
dbc.DB.Find(&issue, "lottery_id = ? and number = ?", lotteryInfo.Id, lotteryInfo.NowNumber)
|
|
|
|
// 验证时间
|
|
if issue.StopTime.Time.Unix() <= time.Now().Unix()+120 {
|
|
back.Fail(c, "开奖前2分钟停止兑换")
|
|
return
|
|
}
|
|
|
|
// 验证玩法,计算金额
|
|
if err := lottery.Lottery.BetList(lotteryInfo.Code, &args.Item); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
// 计算总金额
|
|
total := 0
|
|
for _, item := range args.Item {
|
|
total = item.Money
|
|
}
|
|
|
|
// 验证余额
|
|
userWallet := user.Wallet{}
|
|
dbc.DB.Find(&userWallet, "user_id = ?", userId)
|
|
if userWallet.Coin.Cmp(decimal.NewFromInt(int64(total))) < 0 {
|
|
back.Fail(c, "瑞币不足")
|
|
return
|
|
}
|
|
|
|
// 下单
|
|
err := dbc.DB.Transaction(func(tx *gorm.DB) error {
|
|
// 扣除瑞币
|
|
row := tx.Model(&user.Wallet{}).Where("id = ? and coin >= ?", userWallet.ID, total).Update("coin", gorm.Expr("coin - ?", total)).RowsAffected
|
|
if row != 1 {
|
|
return errors.New("瑞币不足")
|
|
}
|
|
|
|
// 彩票购买
|
|
//LotteryTicket
|
|
betIds := []interface{}{}
|
|
for _, item := range args.Item {
|
|
jsonAnteCode, _ := json.Marshal(item.AnteCode)
|
|
dataInfo := lottery_ticket.LotteryTicket{
|
|
Uid: userWallet.UserID,
|
|
OrderId: lottery.Lottery.GetOrderId(),
|
|
Code: string(jsonAnteCode),
|
|
Num: args.Num,
|
|
CTime: formatime.NewSecondFrom(time.Now()),
|
|
Status: 1,
|
|
Money: decimal.NewFromInt(int64(item.Money)),
|
|
Number: lotteryInfo.NowNumber,
|
|
LotteryId: lotteryInfo.Id,
|
|
BetType: item.PlayType,
|
|
BetTime: formatime.NewSecondFrom(time.Now()),
|
|
Phone: userinfo.Phone,
|
|
IdCard: userinfo.IDCard,
|
|
RealName: userinfo.RealName,
|
|
}
|
|
tx.Create(&dataInfo)
|
|
if dataInfo.Id <= 0 {
|
|
return errors.New("网络错误")
|
|
}
|
|
betIds = append(betIds, fmt.Sprintf("%v|%v", lotteryInfo.Id, dataInfo.Id))
|
|
|
|
// 瑞币记录
|
|
coinHistory := user.CoinHistory{
|
|
UserID: userWallet.UserID,
|
|
CoinType: user.PurchasedTypeForCoinHistory,
|
|
CoinNum: decimal.NewFromInt(int64(item.Money * -1)),
|
|
OrderId: dataInfo.Id,
|
|
OrderAmount: decimal.NewFromInt(int64(item.Money)),
|
|
Remark: "彩票兑换",
|
|
}
|
|
dbc.DB.Create(&coinHistory)
|
|
if coinHistory.ID <= 0 {
|
|
return errors.New("网络错误")
|
|
}
|
|
walletData := user.WalletCoinList{
|
|
UserID: userWallet.UserID,
|
|
Number: int64(item.Money),
|
|
Title: "彩票兑换",
|
|
Comment: "彩票兑换",
|
|
OrderID: dataInfo.Id,
|
|
}
|
|
tx.Create(&walletData)
|
|
if walletData.ID <= 0 {
|
|
return errors.New("网络错误")
|
|
}
|
|
}
|
|
// 进入队列
|
|
dbc.Rds.RPush(configs.Config_lottery_redis_key, betIds...)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "操作成功", nil)
|
|
return
|
|
}
|
|
|
|
type replyOrder struct {
|
|
Id uint `json:"id"`
|
|
LotteryId uint `json:"lotteryId"`
|
|
LotteryName string `json:"LotteryName"`
|
|
Number string `json:"number"`
|
|
BetTime string `json:"betTime"`
|
|
Status int `json:"status"`
|
|
OrderId string `json:"orderId"`
|
|
}
|
|
|
|
// @Style 订单列表
|
|
func (l *Lottery) Order(c *gin.Context) {
|
|
userId := l.getUserId(c)
|
|
args := tools.Page{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, "操作成功")
|
|
return
|
|
}
|
|
list := []lottery_ticket.LotteryTicket{}
|
|
count := uint(0)
|
|
dbc.DB.Model(&lottery_ticket.LotteryTicket{}).Where("uid = ?", userId).Count(&count)
|
|
result := []replyOrder{}
|
|
if args.GetStart() >= count {
|
|
back.Suc(c, "操作成功", gin.H{
|
|
"data": result,
|
|
"total": count,
|
|
})
|
|
return
|
|
}
|
|
dbc.DB.Where("uid = ?", userId).Offset(args.GetStart()).Limit(args.GetLimit()).Order("id desc").Find(&list)
|
|
|
|
lotteryLists := []lottery_ticket.Lottery{}
|
|
dbc.DB.Find(&lotteryLists)
|
|
lotteryMap := map[uint]lottery_ticket.Lottery{}
|
|
for _, lotteryList := range lotteryLists {
|
|
lotteryMap[lotteryList.Id] = lotteryList
|
|
}
|
|
for _, item := range list {
|
|
result = append(result, replyOrder{
|
|
Id: item.Id,
|
|
LotteryId: item.LotteryId,
|
|
LotteryName: lotteryMap[item.LotteryId].Name,
|
|
Number: item.Number,
|
|
BetTime: item.BetTime.Time.Format("2006-01-02 15:04:05"),
|
|
Status: item.Status,
|
|
OrderId: item.OrderId,
|
|
})
|
|
}
|
|
|
|
back.Suc(c, "操作成功", gin.H{
|
|
"data": result,
|
|
"total": count,
|
|
})
|
|
return
|
|
}
|
|
|
|
type argsOrderInfo struct {
|
|
OrderId string `json:"orderId" form:"orderId"`
|
|
}
|
|
|
|
type replyOrderInfo struct {
|
|
OrderId string `json:"orderId"`
|
|
LotteryName string `json:"lotteryName"`
|
|
Number string `json:"number"`
|
|
Status int `json:"status"`
|
|
Money string `json:"money"`
|
|
TicketMoney string `json:"ticketMoney"`
|
|
IsBombBonus int `json:"isBombBonus"`
|
|
Code []string `json:"code"`
|
|
BetType int `json:"betType"`
|
|
Num int `json:"num"`
|
|
BetTime formatime.Second `json:"betTime"`
|
|
StopTime formatime.Second `json:"stopTime"`
|
|
UserProfile replyOrderInfoUserProfile `json:"userProfile"`
|
|
}
|
|
|
|
type replyOrderInfoUserProfile struct {
|
|
Name string `json:"name"`
|
|
IdCard string `json:"idCard"`
|
|
Mobile string `json:"mobile"`
|
|
}
|
|
|
|
func (l *Lottery) OrderInfo(c *gin.Context) {
|
|
userId := l.getUserId(c)
|
|
args := argsOrderInfo{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.OrderId == "" {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
orderInfo := lottery_ticket.LotteryTicket{}
|
|
dbc.DB.First(&orderInfo, "order_id = ?", args.OrderId)
|
|
if orderInfo.Id <= 0 || orderInfo.Uid != uint(userId) {
|
|
back.Fail(c, "订单错误")
|
|
return
|
|
}
|
|
|
|
issue := lottery_ticket.LotteryIssue{}
|
|
dbc.DB.First(&issue, "lottery_id = ? and number = ?", orderInfo.LotteryId, orderInfo.Number)
|
|
|
|
lotteryInfo := lottery_ticket.Lottery{}
|
|
dbc.DB.First(&lotteryInfo, "id = ?", orderInfo.LotteryId)
|
|
|
|
code := []string{}
|
|
json.Unmarshal([]byte(orderInfo.Code), &code)
|
|
result := replyOrderInfo{
|
|
OrderId: orderInfo.OrderId,
|
|
LotteryName: lotteryInfo.Name,
|
|
Number: orderInfo.Number,
|
|
Status: orderInfo.Status,
|
|
Money: orderInfo.Money.String(),
|
|
Code: code,
|
|
BetType: orderInfo.BetType,
|
|
Num: orderInfo.Num,
|
|
BetTime: orderInfo.BetTime,
|
|
StopTime: issue.StopTime,
|
|
TicketMoney: orderInfo.TicketMoney.String(),
|
|
IsBombBonus: orderInfo.IsBombBonus,
|
|
UserProfile: replyOrderInfoUserProfile{
|
|
Name: orderInfo.RealName,
|
|
IdCard: orderInfo.IdCard,
|
|
Mobile: orderInfo.Phone,
|
|
},
|
|
}
|
|
|
|
back.Suc(c, "操作成功", result)
|
|
return
|
|
}
|
|
|
|
func (l *Lottery) getUserId(c *gin.Context) uint {
|
|
userId, _ := strconv.Atoi(c.Request.Header.Get("X-Recook-ID"))
|
|
userLogin := user.Login{}
|
|
dbc.DB.First(&userLogin, "id = ?", userId)
|
|
return userLogin.UserID
|
|
}
|