195 lines
4.6 KiB
Go
195 lines
4.6 KiB
Go
package system
|
|
|
|
import (
|
|
"f5"
|
|
"fmt"
|
|
"main/mt"
|
|
"main/service"
|
|
|
|
"main/constant"
|
|
"main/model/system"
|
|
"net/http"
|
|
"q5"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type NFTApi struct {
|
|
}
|
|
|
|
func (this *NFTApi) OrderQuery(c *gin.Context) {
|
|
type OrderQueryForm struct {
|
|
Order_id string `json:"order_id"`
|
|
Contract_address string `json:"contract_address"`
|
|
Token_id string `json:"token_id"`
|
|
}
|
|
|
|
reqJson := OrderQueryForm{}
|
|
if !checkparam(&reqJson, c) {
|
|
return
|
|
}
|
|
|
|
cursor := q5.ToInt64(c.DefaultQuery("cursor", ""))
|
|
|
|
filterstr := ""
|
|
if reqJson.Order_id != "" {
|
|
filterstr = " order_id = '" + reqJson.Order_id + "' "
|
|
} else if reqJson.Contract_address != "" {
|
|
filterstr = " contract_address = '" + reqJson.Contract_address + "'"
|
|
} else if reqJson.Token_id != "" {
|
|
filterstr = " token_id = '" + reqJson.Token_id + "'"
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": "input one of order_id, contract_address, token_id",
|
|
})
|
|
return
|
|
}
|
|
sql := fmt.Sprintf(`SELECT * FROM t_order WHERE idx > %d AND %s `, cursor, filterstr)
|
|
|
|
query(constant.BCNFT_DB, cursor, sql, c, func(ds *f5.DataSet) interface{} {
|
|
p := new(system.Order)
|
|
p.LoadFromDs(ds)
|
|
return p
|
|
})
|
|
}
|
|
|
|
func (this *NFTApi) SaleQuery(c *gin.Context) {
|
|
type SaleQueryForm struct {
|
|
Order_id string `json:"order_id"`
|
|
Buyer string `json:"buyer"`
|
|
Seller string `json:"seller"`
|
|
}
|
|
|
|
reqJson := SaleQueryForm{}
|
|
if !checkparam(&reqJson, c) {
|
|
return
|
|
}
|
|
|
|
cursor := q5.ToInt64(c.DefaultQuery("cursor", ""))
|
|
filterstr := ""
|
|
if reqJson.Order_id != "" {
|
|
filterstr = " order_id = '" + reqJson.Order_id + "' "
|
|
} else if reqJson.Buyer != "" {
|
|
filterstr = " buyer = '" + reqJson.Buyer + "' "
|
|
} else if reqJson.Seller != "" {
|
|
filterstr = " seller = '" + reqJson.Seller + "' "
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": "input one of order_id, buyer, seller",
|
|
})
|
|
return
|
|
}
|
|
|
|
sql := fmt.Sprintf(`SELECT * FROM t_sale WHERE idx > %d AND %s `, cursor, filterstr)
|
|
|
|
query(constant.BCNFT_DB, cursor, sql, c, func(ds *f5.DataSet) interface{} {
|
|
p := new(system.Sale)
|
|
p.LoadFromDs(ds)
|
|
return p
|
|
})
|
|
}
|
|
|
|
func (this *NFTApi) NFTQuery(c *gin.Context) {
|
|
type NFTQueryForm struct {
|
|
Owner_address string `json:"owner_address"`
|
|
Last_owner_address string `json:"last_owner_address"`
|
|
Net_id int64 `json:"net_id"`
|
|
}
|
|
|
|
reqJson := NFTQueryForm{}
|
|
if !checkparam(&reqJson, c) {
|
|
return
|
|
}
|
|
|
|
cursor := q5.ToInt64(c.DefaultQuery("cursor", ""))
|
|
filterstr := ""
|
|
if reqJson.Owner_address != "" {
|
|
filterstr = " owner_address = '" + reqJson.Owner_address + "' "
|
|
} else if reqJson.Last_owner_address != "" {
|
|
filterstr = " last_owner_address = '" + reqJson.Last_owner_address + "' "
|
|
} else if reqJson.Net_id != 0 {
|
|
filterstr = " net_id = " + q5.ToString(reqJson.Net_id)
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": "input one of owner_address, last_owner_address, net_id",
|
|
})
|
|
return
|
|
}
|
|
|
|
sql := fmt.Sprintf(`SELECT * FROM t_nft WHERE idx > %d AND %s `, cursor, filterstr)
|
|
|
|
query(constant.BCNFT_DB, cursor, sql, c, func(ds *f5.DataSet) interface{} {
|
|
p := new(system.NFT)
|
|
p.LoadFromDs(ds)
|
|
return p
|
|
})
|
|
}
|
|
|
|
func (this *NFTApi) BalanceQuery(c *gin.Context) {
|
|
type BalanceQueryForm struct {
|
|
Account string `json:"account_address"`
|
|
}
|
|
|
|
reqJson := BalanceQueryForm{}
|
|
if !checkparam(&reqJson, c) {
|
|
return
|
|
}
|
|
|
|
selfaccount := false
|
|
if reqJson.Account != "" {
|
|
mt.Table.Chain.Traverse(func(item *mt.Chain) bool {
|
|
if item.GetAccountAddress() == reqJson.Account {
|
|
selfaccount = true
|
|
return false
|
|
}
|
|
|
|
return true
|
|
})
|
|
}
|
|
|
|
type retitem struct {
|
|
Account string `json:"account_address"`
|
|
TokenName string `json:"token_name"`
|
|
ChainBalance string `json:"chain_balance"`
|
|
Recharge int64 `json:"recharge"`
|
|
NetId int32 `json:"net_id"`
|
|
TokenAddress string `json:"-"`
|
|
}
|
|
result := []retitem{}
|
|
|
|
mt.Table.Chain.Traverse(func(item *mt.Chain) bool {
|
|
account := item.GetAccountAddress()
|
|
if selfaccount && account != reqJson.Account {
|
|
return true
|
|
}
|
|
|
|
if reqJson.Account != "" && !selfaccount {
|
|
account = reqJson.Account
|
|
}
|
|
|
|
p := q5.NewSliceElement(&result)
|
|
p.Account = account
|
|
p.NetId = item.GetNetId()
|
|
p.TokenName = item.GetTokenName()
|
|
p.TokenAddress = item.GetTokenAddress()
|
|
|
|
key := fmt.Sprintf("%d$%s", item.GetNetId(), item.GetTokenAddress())
|
|
if selfaccount || reqJson.Account == "" {
|
|
p.ChainBalance, p.Recharge = service.Balance.GetGlobalBalance(key)
|
|
} else {
|
|
p.ChainBalance, p.Recharge = service.Balance.GetAccoutBalance(key, account)
|
|
}
|
|
|
|
return true
|
|
})
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
"data": result,
|
|
})
|
|
}
|