2025-01-23 18:16:27 +08:00

278 lines
6.1 KiB
Go

package system
import (
"f5"
"fmt"
"main/constant"
"main/model/system"
"net/http"
"q5"
"github.com/gin-gonic/gin"
)
type GiftCodeApi struct {
}
func (aca *GiftCodeApi) GenCode(c *gin.Context) {
var maxbatchid int32 = 0
gameid := q5.SafeToInt32(c.DefaultQuery("gameid", "1"))
typeid := q5.SafeToInt32(c.DefaultQuery("type", ""))
gifttype := new(system.GiftType)
db := f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table("t_gift_type").Take(gifttype, "gameid = ? AND gift_type = ?", gameid, typeid)
if db.Error != nil {
if !f5.IsOrmErrRecordNotFound(db.Error) {
f5.RspErr2(c, 1, db.Error.Error())
} else {
f5.RspErr2(c, 1, "type error")
}
return
}
db = f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table("t_gift_code").Where("gameid = ?", gameid).Select("max(batch_id)")
err := db.Error
if err != nil {
if !f5.IsOrmErrRecordNotFound(err) {
f5.RspErr2(c, 1, err.Error())
return
}
} else {
var count int64 = 0
if db.Count(&count); count > 0 {
db.Scan(&maxbatchid)
}
}
if maxbatchid >= 0xFFF {
f5.RspErr2(c, 1, "batchid overflow")
return
}
batchid := maxbatchid + 1
count := q5.SafeToInt32(c.DefaultQuery("count", "1"))
if count < 1 {
count = 1
}
if count > 5000 {
count = 5000
}
tryCount := 0
postfix := fmt.Sprintf("%03x", batchid)
codelist := map[string]int{}
nowsecs := int32(f5.GetApp().GetRealSeconds())
batchcodeinfo := []*system.GiftCode{}
for i := 0; i < int(count); {
if tryCount > 10000*10 {
f5.RspErr2(c, 1, "internal error")
return
}
code := genCode(7) + postfix
_, exist := codelist[code]
if exist {
tryCount++
continue
}
codelist[code] = 1
i++
p := new(system.GiftCode)
p.GameId = gameid
p.GiftCode = code
p.Batch_id = batchid
p.GiftType = typeid
p.Limit = gifttype.Limit
p.Count = 0
p.Content = gifttype.Content
p.CreateTime = nowsecs
p.ModifyTime = nowsecs
batchcodeinfo = append(batchcodeinfo, p)
}
err = f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table("t_gift_code").Create(batchcodeinfo).Error
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "成功生成",
"batchid": batchid,
})
}
func (this *GiftCodeApi) List(c *gin.Context) {
req := struct {
GameId int32 `json:"gameid"`
BatchId int32 `json:"batch_id"`
PageDto system.PageDto `json:"page_dto"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
sql := fmt.Sprintf("SELECT * FROM t_gift_code WHERE 1=1 AND gameid = %d AND batch_id = %d", req.GameId, req.BatchId)
params := []string{}
result := []*system.GiftCode{}
f5.GetGoStyleDb().PageQuery(
constant.ACCOUNT_DB,
q5.ToInt32(req.PageDto.PageSize),
q5.ToInt32(req.PageDto.Page),
sql,
params,
f5.GetDbFilter().Comp([]f5.DbQueryFilter{}...),
"",
func(err error, pg *f5.Pagination) {
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
f5.UnmarshalModelList(pg.Rows, &result)
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "获取成功",
"data": result,
"total": pg.Total,
"total_page": pg.TotalPages,
"cur_page": pg.CurrentPage,
})
})
}
func (this *GiftCodeApi) DownloadFile(c *gin.Context) {
batchid := q5.SafeToInt32(c.DefaultQuery("batchid", ""))
if batchid < 1 {
f5.RspErr2(c, 1, "batchid error")
return
}
sql := fmt.Sprintf("SELECT * FROM t_activation_code WHERE 1=1 AND batch_id = %d", batchid)
f5.GetGoStyleDb().RawQuery(
constant.ACCOUNT_DB,
sql,
[]string{},
func(err error, ds *f5.DataSet) {
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
data := []struct {
BatchId int32 `json:"batch_id"`
Code string `json:"activation_code"`
}{}
for ds.Next() {
p := q5.NewSliceElement(&data)
p.BatchId = q5.SafeToInt32(ds.GetByName("batch_id"))
p.Code = ds.GetByName("activation_code")
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "",
"data": data,
})
})
}
func (this *GiftCodeApi) ListType(c *gin.Context) {
req := struct {
GameId int32 `json:"gameid"`
PageDto system.PageDto `json:"page_dto"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
sql := fmt.Sprintf("SELECT * FROM t_gift_type WHERE 1=1 AND gameid = %d", req.GameId)
params := []string{}
result := []*system.GiftType{}
f5.GetGoStyleDb().PageQuery(
constant.ACCOUNT_DB,
q5.ToInt32(req.PageDto.PageSize),
q5.ToInt32(req.PageDto.Page),
sql,
params,
f5.GetDbFilter().Comp([]f5.DbQueryFilter{}...),
"",
func(err error, pg *f5.Pagination) {
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
f5.UnmarshalModelList(pg.Rows, &result)
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "获取成功",
"data": result,
"total": pg.Total,
"total_page": pg.TotalPages,
"cur_page": pg.CurrentPage,
})
})
}
func (this *GiftCodeApi) AddType(c *gin.Context) {
req := struct {
GameId int32 `json:"gameid"`
Limit int32 `json:"limit"`
Content string `json:"content"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
var maxtype int32 = 0
db := f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table("t_gift_type").Where("gameid = ?", req.GameId).Select("max(gift_type)")
err := db.Error
if err != nil {
if !f5.IsOrmErrRecordNotFound(err) {
f5.RspErr2(c, 1, err.Error())
return
}
} else {
var count int64 = 0
if db.Count(&count); count > 0 {
db.Scan(&maxtype)
}
}
if maxtype >= 0xFFFF {
f5.RspErr2(c, 1, "type overflow")
return
}
nowsecs := int32(f5.GetApp().GetRealSeconds())
p := new(system.GiftType)
p.GiftType = maxtype + 1
p.GameId = req.GameId
p.Content = req.Content
p.Limit = req.Limit
if p.Limit > 1 {
p.Limit = 1
}
p.CreateTime = nowsecs
p.ModifyTime = nowsecs
err = f5.GetApp().GetOrmDb(constant.ACCOUNT_DB).Table(p.TableName()).Create(p).Error
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "成功生成",
})
}