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,18 +1,18 @@
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) {
@ -20,7 +20,7 @@ func (this *ShopCartApi) List(c *gin.Context) {
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"`
@ -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
f5.GetGoStyleDb().OrmSelectOne( wherekv := [][]string{
constant.BCNFT_DB,
"t_order",
[][]string{
{"net_id", q5.ToString(val.NetId)}, {"net_id", q5.ToString(val.NetId)},
{"contract_address", q5.ToString(val.ContractAddress)}, {"contract_address", q5.ToString(val.ContractAddress)},
{"token_id", q5.ToString(val.TokenId)}, {"token_id", q5.ToString(val.TokenId)},
{"status", jccommon.ORDER_STATUS_ACTIVE}, {"status", jccommon.ORDER_STATUS_ACTIVE},
}, }
func (err error, ds* f5.DataSet) {
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(
constant.BCNFT_DB,
"t_order",
wherekv,
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)
@ -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 {
var err1 error
orderok := false
f5.GetGoStyleDb().OrmSelectOne(
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 cartDb.CurrSortIdx += 1
p.SortIdx = cartDb.CurrSortIdx p.SortIdx = cartDb.CurrSortIdx
p.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds()) p.AddTime = q5.ToInt32(f5.GetApp().GetRealSeconds())
dirty = true; } 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,8 +280,22 @@ 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())
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) 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 {
@ -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"
@ -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

@ -30,6 +30,9 @@ type ShopCartGoods struct {
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 {
@ -57,18 +60,18 @@ 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
}) })
} }