494 lines
12 KiB
Go
494 lines
12 KiB
Go
package market
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"mt"
|
|
"main/constant"
|
|
"main/common"
|
|
"main/service"
|
|
"jccommon"
|
|
. "main/global"
|
|
"fmt"
|
|
"strings"
|
|
"net/http"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type goodsDto struct {
|
|
Event interface{} `json:"event"`
|
|
Nft interface{} `json:"nft"`
|
|
InShopCart int32 `json:"in_shopcart"`
|
|
}
|
|
|
|
type MarketApi struct {
|
|
|
|
}
|
|
|
|
func (this *MarketApi) ProductList(c *gin.Context) {
|
|
openId := c.MustGet("open_id").(string)
|
|
cartDb := service.GetShopCartByOpenId(openId)
|
|
netId := q5.ToInt64(c.Param("net_id"))
|
|
reqJson := struct {
|
|
PageSize interface{} `json:"page_size"`
|
|
Cursor interface{} `json:"cursor"`
|
|
Search struct {
|
|
Name string `json:"name"`
|
|
} `json:"cursor"`
|
|
Filter struct {
|
|
PriceMin interface{} `json:"price_min"`
|
|
PriceMax interface{} `json:"price_max"`
|
|
ItemIds []interface{} `json:"item_ids"`
|
|
HeroRanks []interface{} `json:"hero_ranks"`
|
|
} `json:"filter"`
|
|
Sort struct {
|
|
Fields [] struct {
|
|
Name string `json:"name"`
|
|
Type interface{} `json:"type"`
|
|
} `json:"fields"`
|
|
} `json:"sort"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
pageSize := q5.AdjustRangeValue(q5.SafeToInt32(reqJson.PageSize), 1, 20)
|
|
cursor := q5.ToInt64(c.DefaultQuery("cursor", ""))
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_order A WHERE idx > %d AND net_id = %d AND status="%s"
|
|
`,
|
|
cursor, netId, jccommon.ORDER_STATUS_ACTIVE)
|
|
/*
|
|
if f5.IsTestEnv() {
|
|
sql = fmt.Sprintf(`
|
|
SELECT * FROM t_order A WHERE idx > %d AND net_id = %d
|
|
`,
|
|
cursor, netId)
|
|
}*/
|
|
|
|
orderBy := ""
|
|
/*
|
|
{
|
|
for _, val := range reqJson.Sort.Fields {
|
|
if val.Name == "price" && orderBy == "" {
|
|
t := q5.SafeToInt32(val.Type)
|
|
if t < 0 {
|
|
orderBy = " ORDER BY LENGTH(price) DESC, price DESC"
|
|
} else {
|
|
orderBy = " ORDER BY LENGTH(price) ASC, price ASC"
|
|
}
|
|
}
|
|
}
|
|
}*/
|
|
subFilters := []f5.DbQueryFilter{}
|
|
{
|
|
priceMin := q5.SafeToString(reqJson.Filter.PriceMin)
|
|
priceMax := q5.SafeToString(reqJson.Filter.PriceMax)
|
|
if !q5.IsPureNumber(priceMin) {
|
|
priceMin = ""
|
|
}
|
|
if !q5.IsPureNumber(priceMax) {
|
|
priceMax = ""
|
|
}
|
|
if priceMin != "" && priceMax != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().Custom(
|
|
fmt.Sprintf(`LENGTH(price) >= LENGTH('%s') AND price >= '%s' AND
|
|
LENGTH(price) <= LENGTH('%s') AND price <= '%s'`,
|
|
priceMin,
|
|
priceMin,
|
|
priceMax,
|
|
priceMax)).And())
|
|
} else if priceMin != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().Custom(
|
|
fmt.Sprintf(`LENGTH(price) >= LENGTH('%s') AND price >= '%s'`,
|
|
priceMin,
|
|
priceMin)).And())
|
|
} else if priceMax != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().Custom(
|
|
fmt.Sprintf(`LENGTH(price) <= LENGTH('%s') AND price <= '%s'`,
|
|
priceMax,
|
|
priceMax)).And())
|
|
}
|
|
}
|
|
{
|
|
itemIds := map[int32]int32{}
|
|
if reqJson.Search.Name != "" {
|
|
mt.Table.Item.Search(reqJson.Search.Name, itemIds)
|
|
}
|
|
for _, val := range(reqJson.Filter.ItemIds) {
|
|
itemId := q5.SafeToInt32(val)
|
|
itemIds[itemId] = 1
|
|
}
|
|
if len(itemIds) > 0 {
|
|
inSub := `item_id IN (`
|
|
i := 0
|
|
for key, _ := range(itemIds) {
|
|
if i == 0 {
|
|
inSub += q5.ToString(key)
|
|
} else {
|
|
inSub += "," + q5.ToString(key)
|
|
}
|
|
i += 1
|
|
}
|
|
inSub += ")"
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().Custom(inSub).And())
|
|
}
|
|
}
|
|
|
|
rspObj := struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Page common.StreamPagination `json:"page"`
|
|
Rows []struct{
|
|
Event interface{} `json:"event"`
|
|
Nft interface{} `json:"nft"`
|
|
InShopCart int32 `json:"in_shopcart"`
|
|
} `json:"rows"`
|
|
}{}
|
|
q5.NewSlice(&rspObj.Rows, 0, 10)
|
|
nfts := []*common.NftDto{}
|
|
f5.GetGoStyleDb().StreamPageQuery(
|
|
constant.BCNFT_DB,
|
|
pageSize,
|
|
cursor,
|
|
sql,
|
|
[]string{
|
|
},
|
|
f5.GetDbFilter().Comp(subFilters...),
|
|
orderBy,
|
|
func (err error, pagination *f5.StreamPagination) {
|
|
rspObj.Page.FillPage(pagination)
|
|
},
|
|
func (ds *f5.DataSet) {
|
|
p := new(common.NftDto)
|
|
p.NetId = q5.ToInt32(ds.GetByName("net_id"))
|
|
p.ContractAddress = ds.GetByName("contract_address")
|
|
p.TokenId = ds.GetByName("token_id")
|
|
p.Payload = map[string]interface{}{}
|
|
q5.DecodeJson(ds.GetByName("event_data"), &p.Payload)
|
|
q5.AppendSlice(&nfts, p)
|
|
})
|
|
GetCacheMgr().GetNfts(nfts)
|
|
{
|
|
for _, val := range nfts {
|
|
p := q5.NewSliceElement(&rspObj.Rows)
|
|
p.Event = val.Payload
|
|
p.Nft = val.NftCache.GetJsonData()
|
|
if cartDb != nil && cartDb.GetGoods(val.NetId, val.ContractAddress, val.TokenId) != nil{
|
|
p.InShopCart = 1
|
|
}
|
|
}
|
|
}
|
|
c.JSON(200, rspObj)
|
|
}
|
|
|
|
func (this *MarketApi) TransactionHistory(c *gin.Context) {
|
|
pageSize := q5.AdjustRangeValue(q5.ToInt32(c.DefaultQuery("page_size", "")), 1, 20)
|
|
cursor := q5.ToInt64(c.DefaultQuery("cursor", ""))
|
|
netId := q5.ToInt64(c.Param("net_id"))
|
|
accountAddress := strings.ToLower(c.Param("account_address"))
|
|
filterType := q5.ToInt32(c.DefaultQuery("type", ""))
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_sale WHERE idx > %d AND net_id = %d `,
|
|
cursor, netId)
|
|
params := []string{}
|
|
subFilters := []f5.DbQueryFilter{}
|
|
{
|
|
if filterType == 1 {
|
|
sql += "AND buyer = ? "
|
|
q5.AppendSlice(¶ms, accountAddress)
|
|
} else if filterType == 2 {
|
|
sql += "AND seller = ? "
|
|
}
|
|
}
|
|
rspObj := struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Page common.StreamPagination `json:"page"`
|
|
Rows []interface{} `json:"rows"`
|
|
}{
|
|
Rows : []interface{}{},
|
|
}
|
|
orderBy := ""
|
|
nfts := []*common.NftDto{}
|
|
f5.GetGoStyleDb().StreamPageQuery(
|
|
constant.BCNFT_DB,
|
|
pageSize,
|
|
cursor,
|
|
sql,
|
|
params,
|
|
f5.GetDbFilter().Comp(subFilters...),
|
|
orderBy,
|
|
func (err error, pagination *f5.StreamPagination) {
|
|
rspObj.Page.FillPage(pagination)
|
|
},
|
|
func (ds *f5.DataSet) {
|
|
p := new(common.NftDto)
|
|
p.NetId = q5.ToInt32(ds.GetByName("net_id"))
|
|
p.ContractAddress = ds.GetByName("contract_address")
|
|
p.TokenId = ds.GetByName("token_id")
|
|
q5.AppendSlice(&nfts, p)
|
|
})
|
|
GetCacheMgr().GetNfts(nfts)
|
|
{
|
|
for _, val := range nfts {
|
|
q5.AppendSlice(&rspObj.Rows, val.NftCache.GetJsonData())
|
|
}
|
|
}
|
|
c.JSON(200, rspObj)
|
|
}
|
|
|
|
func (this *MarketApi) CategoryGoodsNum(c *gin.Context) {
|
|
netId := q5.ToInt64(c.Param("net_id"))
|
|
|
|
rspObj := &struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Data []struct{
|
|
ItemId int32 `json:"item_id"`
|
|
Num int32 `json:"num"`
|
|
} `json:"data"`
|
|
}{}
|
|
q5.NewSlice(&rspObj.Data, 0, 10)
|
|
|
|
sql := fmt.Sprintf(`
|
|
SELECT item_id, COUNT(idx) AS num FROM t_order
|
|
WHERE net_id = %d AND status="%s" AND item_id <> 0 GROUP BY item_id
|
|
`,
|
|
netId,
|
|
jccommon.ORDER_STATUS_ACTIVE)
|
|
params := []string{}
|
|
f5.GetGoStyleDb().RawQuery(
|
|
constant.BCNFT_DB,
|
|
sql,
|
|
params,
|
|
func (err error, ds *f5.DataSet) {
|
|
if err != nil {
|
|
rspObj.ErrCode = 500
|
|
rspObj.ErrMsg = "server internal error"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
for ds.Next() {
|
|
p := q5.NewSliceElement(&rspObj.Data)
|
|
p.ItemId = q5.ToInt32(ds.GetByName("item_id"))
|
|
p.Num = q5.ToInt32(ds.GetByName("num"))
|
|
}
|
|
c.JSON(200, rspObj)
|
|
})
|
|
}
|
|
|
|
func (this *MarketApi) QueryPrice(c *gin.Context) {
|
|
netId := q5.ToInt64(c.DefaultQuery("net_id", ""))
|
|
itemId := q5.ToInt64(c.DefaultQuery("item_id", ""))
|
|
contractAddress := c.DefaultQuery("contract_address", "")
|
|
itemQuality := q5.ToInt64(c.DefaultQuery("quality", ""))
|
|
|
|
itemMeta := mt.Table.Item.GetById(itemId)
|
|
if itemMeta != nil && itemMeta.GetType() != jccommon.ITEM_TYPE_HERO {
|
|
itemQuality = 0
|
|
}
|
|
|
|
rspObj := &struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
LowestPriceGoods *goodsDto `json:"lowest_price_goods"`
|
|
HighestPriceGoods *goodsDto `json:"highest_price_goods"`
|
|
}{}
|
|
|
|
lowestPriceGoodsIdx := int64(0)
|
|
highestPriceGoodsIdx := int64(0)
|
|
{
|
|
var dbErr error
|
|
lowestPrice := ""
|
|
highestPrice := ""
|
|
sql := fmt.Sprintf(`
|
|
SELECT idx, price FROM t_order
|
|
WHERE net_id = %d AND contract_address=? AND status="%s" AND item_id = %d AND hero_quality = %d
|
|
`,
|
|
netId,
|
|
jccommon.ORDER_STATUS_ACTIVE,
|
|
itemId,
|
|
itemQuality)
|
|
params := []string{
|
|
contractAddress,
|
|
}
|
|
f5.GetGoStyleDb().RawQuery(
|
|
constant.BCNFT_DB,
|
|
sql,
|
|
params,
|
|
func (err error, ds *f5.DataSet) {
|
|
dbErr = err
|
|
if err != nil {
|
|
return
|
|
}
|
|
for ds.Next() {
|
|
idx := q5.ToInt64(ds.GetByName("idx"))
|
|
price := ds.GetByName("price")
|
|
if lowestPriceGoodsIdx == 0 ||
|
|
q5.BigIntStrCmp(lowestPrice, price) < 0 {
|
|
lowestPriceGoodsIdx = idx
|
|
lowestPrice = price
|
|
}
|
|
if highestPriceGoodsIdx == 0 ||
|
|
q5.BigIntStrCmp(highestPrice, price) > 0 {
|
|
highestPriceGoodsIdx = idx
|
|
highestPrice = price
|
|
}
|
|
}
|
|
})
|
|
if dbErr != nil {
|
|
rspObj.ErrCode = 500
|
|
rspObj.ErrMsg = "server internal error"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
}
|
|
{
|
|
idxs := []string{}
|
|
if lowestPriceGoodsIdx > 0 {
|
|
q5.AppendSlice(&idxs, q5.ToString(lowestPriceGoodsIdx))
|
|
}
|
|
if highestPriceGoodsIdx > 0 {
|
|
q5.AppendSlice(&idxs, q5.ToString(highestPriceGoodsIdx))
|
|
}
|
|
if len(idxs) <= 0 {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
var dbErr error
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_order
|
|
WHERE idx in (%s)
|
|
`,
|
|
strings.Join(idxs, ","))
|
|
nfts := []*common.NftDto{}
|
|
f5.GetGoStyleDb().RawQuery(
|
|
constant.BCNFT_DB,
|
|
sql,
|
|
[]string{},
|
|
func (err error, ds *f5.DataSet) {
|
|
dbErr = err
|
|
if err != nil {
|
|
return
|
|
}
|
|
for ds.Next() {
|
|
p := new(common.NftDto)
|
|
p.Param1 = q5.ToInt64(ds.GetByName("idx"))
|
|
p.NetId = q5.ToInt32(ds.GetByName("net_id"))
|
|
p.ContractAddress = ds.GetByName("contract_address")
|
|
p.TokenId = ds.GetByName("token_id")
|
|
p.Payload = map[string]interface{}{}
|
|
q5.DecodeJson(ds.GetByName("event_data"), &p.Payload)
|
|
q5.AppendSlice(&nfts, p)
|
|
}
|
|
})
|
|
if dbErr != nil {
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
GetCacheMgr().GetNfts(nfts)
|
|
{
|
|
for _, val := range nfts {
|
|
var p *goodsDto
|
|
p = new(goodsDto)
|
|
p.Event = val.Payload
|
|
p.Nft = val.NftCache.GetJsonData()
|
|
if val.Param1 == lowestPriceGoodsIdx {
|
|
rspObj.LowestPriceGoods = p
|
|
}
|
|
if val.Param1 == highestPriceGoodsIdx {
|
|
rspObj.HighestPriceGoods = p
|
|
}
|
|
}
|
|
}
|
|
c.JSON(200, rspObj)
|
|
}
|
|
}
|
|
|
|
func (this *MarketApi) Goods(c *gin.Context) {
|
|
openId := c.MustGet("open_id").(string)
|
|
cartDb := service.GetShopCartByOpenId(openId)
|
|
|
|
netId := q5.ToInt64(c.Param("net_id"))
|
|
contractAddress := c.Param("contract_address")
|
|
tokenId := c.Param("token_id")
|
|
|
|
rspObj := &struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Data *goodsDto `json:"data"`
|
|
}{}
|
|
|
|
var dbErr error
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_order
|
|
WHERE net_id=? AND contract_address=? AND token_id=? AND status=?
|
|
`)
|
|
nfts := []*common.NftDto{}
|
|
f5.GetGoStyleDb().RawQuery(
|
|
constant.BCNFT_DB,
|
|
sql,
|
|
[]string{
|
|
q5.ToString(netId),
|
|
contractAddress,
|
|
tokenId,
|
|
jccommon.ORDER_STATUS_ACTIVE,
|
|
},
|
|
func (err error, ds *f5.DataSet) {
|
|
dbErr = err
|
|
if err != nil {
|
|
return
|
|
}
|
|
for ds.Next() {
|
|
p := new(common.NftDto)
|
|
p.Param1 = q5.ToInt64(ds.GetByName("idx"))
|
|
p.NetId = q5.ToInt32(ds.GetByName("net_id"))
|
|
p.ContractAddress = ds.GetByName("contract_address")
|
|
p.TokenId = ds.GetByName("token_id")
|
|
p.Payload = map[string]interface{}{}
|
|
q5.DecodeJson(ds.GetByName("event_data"), &p.Payload)
|
|
q5.AppendSlice(&nfts, p)
|
|
}
|
|
})
|
|
if dbErr != nil {
|
|
rspObj.ErrCode = 500
|
|
rspObj.ErrMsg = "server internal error"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
if len(nfts) <= 0 {
|
|
p := new(common.NftDto)
|
|
//p.Param1 = q5.ToInt64(ds.GetByName("idx"))
|
|
p.NetId = q5.ToInt32(netId)
|
|
p.ContractAddress = contractAddress
|
|
p.TokenId = tokenId
|
|
p.Payload = nil
|
|
q5.AppendSlice(&nfts, p)
|
|
}
|
|
GetCacheMgr().GetNfts(nfts)
|
|
{
|
|
for _, val := range nfts {
|
|
var p *goodsDto
|
|
p = new(goodsDto)
|
|
p.Event = val.Payload
|
|
p.Nft = val.NftCache.GetJsonData()
|
|
if cartDb != nil && cartDb.GetGoods(val.NetId, val.ContractAddress, val.TokenId) != nil{
|
|
p.InShopCart = 1
|
|
}
|
|
rspObj.Data = p
|
|
}
|
|
}
|
|
if rspObj.Data == nil {
|
|
rspObj.ErrCode = 1
|
|
rspObj.ErrMsg = "not found"
|
|
c.JSON(200, rspObj)
|
|
return
|
|
}
|
|
c.JSON(200, rspObj)
|
|
}
|