verify shopcart

This commit is contained in:
yangduo 2024-08-02 10:19:04 +08:00
parent 61f3bd7c67
commit e1ca58b8e8
2 changed files with 185 additions and 72 deletions

View File

@ -1,28 +1,28 @@
package shopcart package shopcart
import ( import (
"q5"
"f5" "f5"
"jccommon" "jccommon"
"main/constant"
"main/common" "main/common"
"main/constant"
. "main/global" . "main/global"
"q5"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type ShopCartApi struct { type ShopCartApi struct {
} }
func (this *ShopCartApi) List(c *gin.Context) { func (this *ShopCartApi) List(c *gin.Context) {
openId := c.MustGet("open_id").(string) openId := c.MustGet("open_id").(string)
rspObj := &struct { rspObj := &struct {
ErrCode int32 `json:"errcode"` ErrCode int32 `json:"errcode"`
ErrMsg string `json:"errmsg"` ErrMsg string `json:"errmsg"`
Data []struct{ Data []struct {
Event interface{} `json:"event"` Event interface{} `json:"event"`
Nft interface{} `json:"nft"` Nft interface{} `json:"nft"`
} `json:"data"` } `json:"data"`
}{} }{}
q5.NewSlice(&rspObj.Data, 0, 10) q5.NewSlice(&rspObj.Data, 0, 10)
@ -32,7 +32,7 @@ func (this *ShopCartApi) List(c *gin.Context) {
[][]string{ [][]string{
{"open_id", openId}, {"open_id", openId},
}, },
func (err error, ds* f5.DataSet) { func(err error, ds *f5.DataSet) {
if err != nil { if err != nil {
rspObj.ErrCode = 500 rspObj.ErrCode = 500
rspObj.ErrMsg = "server internal error" rspObj.ErrMsg = "server internal error"
@ -44,26 +44,47 @@ func (this *ShopCartApi) List(c *gin.Context) {
cartDb := common.NewShopCart() cartDb := common.NewShopCart()
if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil { if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil {
q5.Sort[common.ShopCartGoods](cartDb.Goods, q5.Sort[common.ShopCartGoods](cartDb.Goods,
func (a *common.ShopCartGoods, b *common.ShopCartGoods) bool{ func(a *common.ShopCartGoods, b *common.ShopCartGoods) bool {
return a.SortIdx < b.SortIdx return a.SortIdx < b.SortIdx
}) })
dirty := false dirty := false
newGoods := []*common.ShopCartGoods{} newGoods := []*common.ShopCartGoods{}
priceType := ""
priceContractAddress := ""
for _, val := range cartDb.Goods { for _, val := range cartDb.Goods {
var err1 error var err1 error
wherekv := [][]string{
{"net_id", q5.ToString(val.NetId)},
{"contract_address", q5.ToString(val.ContractAddress)},
{"token_id", q5.ToString(val.TokenId)},
{"status", jccommon.ORDER_STATUS_ACTIVE},
}
if priceType != "" {
wherekv = append(wherekv, []string{"src_price_item_type", priceType})
}
if priceContractAddress != "" {
wherekv = append(wherekv, []string{"src_price_contract_address", priceContractAddress})
}
f5.GetGoStyleDb().OrmSelectOne( f5.GetGoStyleDb().OrmSelectOne(
constant.BCNFT_DB, constant.BCNFT_DB,
"t_order", "t_order",
[][]string{ wherekv,
{"net_id", q5.ToString(val.NetId)}, func(err error, ds *f5.DataSet) {
{"contract_address", q5.ToString(val.ContractAddress)},
{"token_id", q5.ToString(val.TokenId)},
{"status", jccommon.ORDER_STATUS_ACTIVE},
},
func (err error, ds* f5.DataSet) {
err1 = err err1 = err
if err == nil { if err == nil {
if ds.Next() { if ds.Next() {
val.PriceType = ds.GetByName("src_price_item_type")
val.PriceContractAddress = ds.GetByName("src_price_contract_address")
val.OrderId = ds.GetByName("order_id")
if priceType == "" && priceContractAddress == "" {
priceType = val.PriceType
priceContractAddress = val.PriceContractAddress
}
p := new(common.NftDto) p := new(common.NftDto)
p.NetId = q5.ToInt32(ds.GetByName("net_id")) p.NetId = q5.ToInt32(ds.GetByName("net_id"))
p.ContractAddress = ds.GetByName("contract_address") p.ContractAddress = ds.GetByName("contract_address")
@ -73,9 +94,9 @@ func (this *ShopCartApi) List(c *gin.Context) {
q5.AppendSlice(&nfts, p) q5.AppendSlice(&nfts, p)
q5.AppendSlice(&newGoods, val) q5.AppendSlice(&newGoods, val)
} else { } else {
dirty = true
val.Flag = 1 val.Flag = 1
} }
dirty = true
} }
}) })
if err1 != nil { if err1 != nil {
@ -84,7 +105,7 @@ func (this *ShopCartApi) List(c *gin.Context) {
c.JSON(200, rspObj) c.JSON(200, rspObj)
return return
} }
}//end for goods } //end for goods
if dirty { if dirty {
cartDb.Goods = newGoods cartDb.Goods = newGoods
this.save(openId, cartDb) this.save(openId, cartDb)
@ -108,15 +129,15 @@ func (this *ShopCartApi) Add(c *gin.Context) {
accountAddress := c.MustGet("account_address").(string) accountAddress := c.MustGet("account_address").(string)
rspObj := &struct { rspObj := &struct {
ErrCode int32 `json:"errcode"` ErrCode int32 `json:"errcode"`
ErrMsg string `json:"errmsg"` ErrMsg string `json:"errmsg"`
}{} }{}
reqJson := struct { reqJson := struct {
NetId interface{} `json:"net_id"` NetId interface{} `json:"net_id"`
Tokens []struct { Tokens []struct {
TokenId interface{} `json:"token_id"` TokenId interface{} `json:"token_id"`
ContractAddress string `json:"contract_address"` ContractAddress string `json:"contract_address"`
} `json:"tokens"` } `json:"tokens"`
}{} }{}
if err := c.ShouldBindJSON(&reqJson); err != nil { if err := c.ShouldBindJSON(&reqJson); err != nil {
@ -138,7 +159,7 @@ func (this *ShopCartApi) Add(c *gin.Context) {
[][]string{ [][]string{
{"open_id", openId}, {"open_id", openId},
}, },
func (err error, ds* f5.DataSet) { func(err error, ds *f5.DataSet) {
if err != nil { if err != nil {
rspObj.ErrCode = 500 rspObj.ErrCode = 500
rspObj.ErrMsg = "server internal error" rspObj.ErrMsg = "server internal error"
@ -151,16 +172,91 @@ func (this *ShopCartApi) Add(c *gin.Context) {
if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil { if q5.DecodeJson(ds.GetByName("data"), cartDb) == nil {
} }
} }
priceType := ""
priceContractAddress := ""
for _, g := range cartDb.Goods {
inreq := false
for _, req := range reqJson.Tokens {
netId := q5.SafeToInt32(reqJson.NetId)
contractAddress := req.ContractAddress
tokenId := q5.SafeToString(req.TokenId)
if g.NetId == netId && g.ContractAddress == contractAddress && g.TokenId == tokenId {
inreq = true
break
}
}
if !inreq {
priceType = g.PriceType
priceContractAddress = g.PriceContractAddress
break
}
}
for _, val := range reqJson.Tokens { for _, val := range reqJson.Tokens {
netId := q5.SafeToInt32(reqJson.NetId) netId := q5.SafeToInt32(reqJson.NetId)
contractAddress := val.ContractAddress contractAddress := val.ContractAddress
tokenId := q5.SafeToString(val.TokenId) tokenId := q5.SafeToString(val.TokenId)
p := cartDb.GetGoods(netId, contractAddress, tokenId) p := cartDb.GetGoods(netId, contractAddress, tokenId)
if p != nil { if p != nil {
cartDb.CurrSortIdx += 1 var err1 error
p.SortIdx = cartDb.CurrSortIdx orderok := false
p.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds()) f5.GetGoStyleDb().OrmSelectOne(
dirty = true; constant.BCNFT_DB,
"t_order",
[][]string{
{"net_id", q5.ToString(netId)},
{"contract_address", contractAddress},
{"token_id", tokenId},
{"status", jccommon.ORDER_STATUS_ACTIVE},
},
func(err error, ds *f5.DataSet) {
err1 = err
if err != nil {
return
}
if !ds.Next() {
return
}
if ds.GetByName("seller_address") == accountAddress {
return
}
p.OrderId = ds.GetByName("order_id")
p.PriceType = ds.GetByName("src_price_item_type")
p.PriceContractAddress = ds.GetByName("src_price_contract_address")
if priceType == "" && priceContractAddress == "" {
priceType = p.PriceType
priceContractAddress = p.PriceContractAddress
orderok = true
} else if priceType != "" && priceContractAddress != "" &&
priceType == p.PriceType && priceContractAddress == p.PriceContractAddress {
orderok = true
}
})
if err1 != nil {
rspObj.ErrCode = 500
rspObj.ErrMsg = "server internal error"
c.JSON(200, rspObj)
return
}
if orderok {
cartDb.CurrSortIdx += 1
p.SortIdx = cartDb.CurrSortIdx
p.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds())
} else {
j := 0
for _, g := range cartDb.Goods {
if g != p {
cartDb.Goods[j] = g
j++
}
}
cartDb.Goods = cartDb.Goods[:j]
}
dirty = true
} else { } else {
var err1 error var err1 error
f5.GetGoStyleDb().OrmSelectOne( f5.GetGoStyleDb().OrmSelectOne(
@ -172,7 +268,7 @@ func (this *ShopCartApi) Add(c *gin.Context) {
{"token_id", tokenId}, {"token_id", tokenId},
{"status", jccommon.ORDER_STATUS_ACTIVE}, {"status", jccommon.ORDER_STATUS_ACTIVE},
}, },
func (err error, ds* f5.DataSet) { func(err error, ds *f5.DataSet) {
err1 = err err1 = err
if err == nil { if err == nil {
if ds.Next() { if ds.Next() {
@ -184,7 +280,21 @@ func (this *ShopCartApi) Add(c *gin.Context) {
g.TokenId = tokenId g.TokenId = tokenId
g.SortIdx = cartDb.CurrSortIdx g.SortIdx = cartDb.CurrSortIdx
g.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds()) g.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds())
q5.AppendSlice(&cartDb.Goods, g) g.OrderId = ds.GetByName("order_id")
g.PriceType = ds.GetByName("src_price_item_type")
g.PriceContractAddress = ds.GetByName("src_price_contract_address")
orderok := false
if priceType == "" && priceContractAddress == "" {
orderok = true
priceType = g.PriceType
priceContractAddress = g.PriceContractAddress
} else if priceType != "" && priceContractAddress != "" && priceType == g.PriceType && priceContractAddress == g.PriceContractAddress {
orderok = true
}
if orderok {
q5.AppendSlice(&cartDb.Goods, g)
}
} }
} else { } else {
} }
@ -196,7 +306,7 @@ func (this *ShopCartApi) Add(c *gin.Context) {
c.JSON(200, rspObj) c.JSON(200, rspObj)
return return
} }
dirty = true; dirty = true
} }
} }
if dirty { if dirty {
@ -211,15 +321,15 @@ func (this *ShopCartApi) Del(c *gin.Context) {
openId := c.MustGet("open_id").(string) openId := c.MustGet("open_id").(string)
rspObj := &struct { rspObj := &struct {
ErrCode int32 `json:"errcode"` ErrCode int32 `json:"errcode"`
ErrMsg string `json:"errmsg"` ErrMsg string `json:"errmsg"`
}{} }{}
reqJson := struct { reqJson := struct {
NetId interface{} `json:"net_id"` NetId interface{} `json:"net_id"`
Tokens []struct { Tokens []struct {
TokenId interface{} `json:"token_id"` TokenId interface{} `json:"token_id"`
ContractAddress string `json:"contract_address"` ContractAddress string `json:"contract_address"`
} `json:"tokens"` } `json:"tokens"`
}{} }{}
if err := c.ShouldBindJSON(&reqJson); err != nil { if err := c.ShouldBindJSON(&reqJson); err != nil {
@ -235,7 +345,7 @@ func (this *ShopCartApi) Del(c *gin.Context) {
[][]string{ [][]string{
{"open_id", openId}, {"open_id", openId},
}, },
func (err error, ds* f5.DataSet) { func(err error, ds *f5.DataSet) {
if err != nil { if err != nil {
rspObj.ErrCode = 500 rspObj.ErrCode = 500
rspObj.ErrMsg = "server internal error" rspObj.ErrMsg = "server internal error"
@ -276,8 +386,8 @@ func (this *ShopCartApi) Clear(c *gin.Context) {
openId := c.MustGet("open_id").(string) openId := c.MustGet("open_id").(string)
rspObj := &struct { rspObj := &struct {
ErrCode int32 `json:"errcode"` ErrCode int32 `json:"errcode"`
ErrMsg string `json:"errmsg"` ErrMsg string `json:"errmsg"`
}{} }{}
cartDb := common.NewShopCart() cartDb := common.NewShopCart()
@ -303,7 +413,7 @@ func (this *ShopCartApi) save(openId string, cartDb *common.ShopCart) {
{"createtime", q5.ToString(nowTime)}, {"createtime", q5.ToString(nowTime)},
{"modifytime", q5.ToString(nowTime)}, {"modifytime", q5.ToString(nowTime)},
}, },
func (err error, lastInsertId int64, rowsAffected int64) { func(err error, lastInsertId int64, rowsAffected int64) {
}) })
} }

View File

@ -8,33 +8,36 @@ import (
) )
type NftHomeMeta struct { type NftHomeMeta struct {
Name string `json:"name"` Name string `json:"name"`
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
Description string `json:"description"` Description string `json:"description"`
Image string `json:"image"` Image string `json:"image"`
ExternalLink string `json:"external_link"` ExternalLink string `json:"external_link"`
} }
type StreamPagination struct { type StreamPagination struct {
NextCursor string `json:"next_cursor"` NextCursor string `json:"next_cursor"`
PreviousCursor string `json:"previous_cursor"` PreviousCursor string `json:"previous_cursor"`
Remaining int32 `json:"remaining"` Remaining int32 `json:"remaining"`
Count int32 `json:"count"` Count int32 `json:"count"`
TotalCount int32 `json:"total_count"` TotalCount int32 `json:"total_count"`
} }
type ShopCartGoods struct { type ShopCartGoods struct {
NetId int32 `json:"net_id"` NetId int32 `json:"net_id"`
ContractAddress string `json:"address"` ContractAddress string `json:"address"`
TokenId string `json:"token_id"` TokenId string `json:"token_id"`
SortIdx uint32 `json:"idx"` SortIdx uint32 `json:"idx"`
AddTime int32 `json:"addtime"` AddTime int32 `json:"addtime"`
Flag int32 Flag int32
PriceType string
PriceContractAddress string
OrderId string
} }
type ShopCart struct { type ShopCart struct {
CurrSortIdx uint32 `json:"curr_sort_idx"` CurrSortIdx uint32 `json:"curr_sort_idx"`
Goods []*ShopCartGoods `json:"goods"` Goods []*ShopCartGoods `json:"goods"`
} }
type NftCache interface { type NftCache interface {
@ -43,32 +46,32 @@ type NftCache interface {
} }
type NftDto struct { type NftDto struct {
NetId int32 NetId int32
ContractAddress string ContractAddress string
TokenId string TokenId string
NftCache NftCache NftCache NftCache
Price string Price string
Payload interface{} Payload interface{}
Param1 int64 Param1 int64
LockTime int32 LockTime int32
} }
type App interface { type App interface {
Run(func(), func()) Run(func(), func())
} }
type RouterMgr interface{ type RouterMgr interface {
} }
type TaskMgr interface{ type TaskMgr interface {
} }
type CacheMgr interface{ type CacheMgr interface {
GetNfts([]*NftDto) GetNfts([]*NftDto)
GetIngameHero(string) (error, []interface{}) GetIngameHero(string) (error, []interface{})
} }
func (this* StreamPagination) FillPage(page *f5.StreamPagination) { func (this *StreamPagination) FillPage(page *f5.StreamPagination) {
if page.NextCursor != 0 { if page.NextCursor != 0 {
this.NextCursor = q5.ToString(page.NextCursor) this.NextCursor = q5.ToString(page.NextCursor)
} }
@ -80,12 +83,12 @@ func (this* StreamPagination) FillPage(page *f5.StreamPagination) {
this.Remaining = page.Remaining this.Remaining = page.Remaining
} }
func (this* NftDto) GetKey() string { func (this *NftDto) GetKey() string {
key := fmt.Sprintf("%d_%s_%s", this.NetId, this.ContractAddress, this.TokenId) key := fmt.Sprintf("%d_%s_%s", this.NetId, this.ContractAddress, this.TokenId)
return key return key
} }
func (this* ShopCart) GetGoods(netId int32, contractAddress string, tokenId string) *ShopCartGoods { func (this *ShopCart) GetGoods(netId int32, contractAddress string, tokenId string) *ShopCartGoods {
for _, val := range this.Goods { for _, val := range this.Goods {
if val.NetId == netId && val.ContractAddress == contractAddress && val.TokenId == tokenId { if val.NetId == netId && val.ContractAddress == contractAddress && val.TokenId == tokenId {
return val return val
@ -94,9 +97,9 @@ func (this* ShopCart) GetGoods(netId int32, contractAddress string, tokenId stri
return nil return nil
} }
func (this* ShopCart) DeleteExcessData() { func (this *ShopCart) DeleteExcessData() {
q5.Sort[ShopCartGoods](this.Goods, q5.Sort[ShopCartGoods](this.Goods,
func (a *ShopCartGoods, b *ShopCartGoods) bool{ func(a *ShopCartGoods, b *ShopCartGoods) bool {
return a.SortIdx > b.SortIdx return a.SortIdx > b.SortIdx
}) })
newGoods := []*ShopCartGoods{} newGoods := []*ShopCartGoods{}
@ -109,7 +112,7 @@ func (this* ShopCart) DeleteExcessData() {
} }
this.Goods = newGoods this.Goods = newGoods
q5.Sort[ShopCartGoods](this.Goods, q5.Sort[ShopCartGoods](this.Goods,
func (a *ShopCartGoods, b *ShopCartGoods) bool{ func(a *ShopCartGoods, b *ShopCartGoods) bool {
return a.SortIdx < b.SortIdx return a.SortIdx < b.SortIdx
}) })
} }