增加用户中token分页查询

This commit is contained in:
xxq250 2024-09-09 09:18:59 +08:00
parent 1129ebd409
commit a2995a6c2c
5 changed files with 124 additions and 1 deletions

View File

@ -618,6 +618,30 @@ func (_OpenSource *OpenSourceCaller) SelectUserBalance(opts *bind.CallOpts, user
return *ret0, *ret1, *ret2, err
}
func (_OpenSource *OpenSourceCaller) SelectUserBalanceByPage(opts *bind.CallOpts, username string, token_name string, start *big.Int, page_num *big.Int) ([]string, []string, []*big.Int, *big.Int, error) {
var (
ret0 = new([]string)
ret1 = new([]string)
ret2 = new([]*big.Int)
ret3 = new(*big.Int)
)
out := &[]interface{}{
ret0,
ret1,
ret2,
ret3,
}
err := _OpenSource.contract.Call(opts, out, "selectUserBalance", username, token_name, start, page_num)
return *ret0, *ret1, *ret2, *ret3, err
}
// SelectUserBalance is a free data retrieval call binding the contract method 0x233cbaf7.
//
// Solidity: function selectUserBalance(string username, string token_name) constant returns(string[], string[], uint256[])
func (_OpenSource *OpenSourceSession) SelectUserBalanceByPage(username string, token_name string, start *big.Int, page_num *big.Int) ([]string, []string, []*big.Int, *big.Int, error) {
return _OpenSource.Contract.SelectUserBalanceByPage(&_OpenSource.CallOpts, username, token_name, start, page_num)
}
// SelectUserBalance is a free data retrieval call binding the contract method 0x233cbaf7.
//
// Solidity: function selectUserBalance(string username, string token_name) constant returns(string[], string[], uint256[])

View File

@ -167,7 +167,43 @@ contract OpenSource {
}
return (username_list, token_name_list, balance_list);
}
}
// select single user balance by page
function selectUserBalance(string memory username, string memory token_name, int256 start, int256 page_num)
public
view
returns (string[] memory, string[] memory, uint256[] memory, uint256)
{
Table user_table = tableFactory.openTable(USER_TABLE);
Condition condition = user_table.newCondition();
condition.EQ("token_name", token_name);
Entries entries = user_table.select(username, condition);
if (start + page_num > entries.size()) {
page_num = entries.size() - start;
if (page_num < 0) {
page_num = 0;
}
}
uint256 total_count = uint256(entries.size());
string[] memory username_list = new string[](uint256(entries.size()));
string[] memory token_name_list = new string[](uint256(entries.size()));
uint256[] memory balance_list = new uint256[](uint256(entries.size()));
for (int256 i = 0; i < entries.size(); ++i) {
Entry entry = entries.get(i);
username_list[uint256(i)] = entry.getString("user");
token_name_list[uint256(i)] = entry.getString("token_name");
balance_list[uint256(i)] = entry.getUInt("balance");
}
return (username_list, token_name_list, balance_list, total_count);
}
// add user balance
function addUserBalance(string memory username, string memory token_name, uint256 amount)

View File

@ -27,3 +27,9 @@ type ResponseUserBalance struct {
UserBalance
Response
}
type ResponseUserBalanceByPage struct {
Response
UserBalanceList
TotalCount uint64 `json:"total_count"`
}

View File

@ -152,6 +152,8 @@ func HandleAllRoutes(ctx *macaron.Context, opt api.FiscoBcos, logger *log.Logger
repo.SelectIssueCommentAllInfo(ctx, opt.IssueCommentID, logger)
case "query user balance of single repo":
user.SelectUserBalance(ctx, opt.Username, opt.TokenName, logger)
case "query user and token balance of repos by page":
user.SelectUserBalanceByPage(ctx, opt.Username, opt.TokenName, opt.Page, opt.Pagenum, logger)
case "query user balance of all repos":
user.SelectUserAllBalance(ctx, opt.Username, logger)
case "query user balance of all repos by page":

View File

@ -186,6 +186,61 @@ func SelectUserBalance(ctx *macaron.Context, username string, tokenName string,
})
}
// select user all balance by page
func SelectUserBalanceByPage(ctx *macaron.Context, username string, tokenName string, page int64, page_num int64, logger *log.Logger) {
if username == "" || tokenName == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)
return
}
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
// load the contract
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
usernames, tokenNames, balances, total_count, err := openSourceSession.SelectUserBalanceByPage(username, tokenName, big.NewInt(page), big.NewInt(page_num)) // call select API
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
if len(usernames) == 0 {
ctx.JSON(http.StatusOK, api.ResUserNotExisted)
return
}
var userBalancelist api.UserBalanceList
for i := 0; i < len(tokenNames); i++ {
userBalancelist = append(userBalancelist, &api.UserBalance{User: api.User{Username: username, TokenName: tokenNames[i]}, Balance: balances[i].Uint64()})
fmt.Printf("username: %v, token_name: %v, balance: %v, total: %v \n", username, tokenNames[i], balances[i], total_count)
}
fmt.Printf("username: %v, token_name: %v, balance: %v \n", usernames[0], tokenNames[0], balances[0])
ctx.JSON(http.StatusOK, &structs.ResponseUserBalanceByPage{
UserBalanceList: userBalancelist,
Response: structs.Response{
Status: 0,
Message: "query success!",
},
TotalCount: total_count.Uint64(),
})
}
func AddUserAmount(ctx *macaron.Context, username string, tokenName string, amount uint64, logger *log.Logger) {
if username == "" || tokenName == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)