From 58dd41c5374d24b4540c5b866a8cd1f00bc503ef Mon Sep 17 00:00:00 2001 From: scorpiotzh <835598264@qq.com> Date: Fri, 15 Nov 2024 16:37:07 +0800 Subject: [PATCH] feat: fix api get account list --- dao/t_account_info.go | 27 ++++++++++++++++++++++++--- http_server/handle/account_list.go | 8 +++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/dao/t_account_info.go b/dao/t_account_info.go index dfb265c..35873f3 100644 --- a/dao/t_account_info.go +++ b/dao/t_account_info.go @@ -146,11 +146,32 @@ func (d *DbDao) FindAccountListByAddress(chainType common.ChainType, address str return } -func (d *DbDao) FindAccountNameListByAddress(chainType common.ChainType, address, role string) (list []tables.TableAccountInfo, err error) { +func (d *DbDao) FindAccountNameListByAddress(chainType common.ChainType, address, role string, limit, offset int) (list []tables.TableAccountInfo, err error) { if role == "" || role == "owner" { - err = d.db.Select("account,registered_at,expired_at").Where(" owner_chain_type=? AND owner=? AND `status`!=? and expired_at >= ?", chainType, address, tables.AccountStatusOnLock, time.Now().Unix()-90*86400).Find(&list).Error + err = d.db.Select("account,registered_at,expired_at"). + Where(" owner_chain_type=? AND owner=? AND `status`!=? and expired_at >= ?", + chainType, address, tables.AccountStatusOnLock, time.Now().Unix()-90*86400). + Order("account").Limit(limit).Offset(offset).Find(&list).Error } else if role == "manager" { - err = d.db.Select("account,registered_at,expired_at").Where(" manager_chain_type=? AND manager=? AND `status`!=? and expired_at >= ?", chainType, address, tables.AccountStatusOnLock, time.Now().Unix()-90*86400).Find(&list).Error + err = d.db.Select("account,registered_at,expired_at"). + Where(" manager_chain_type=? AND manager=? AND `status`!=? and expired_at >= ?", + chainType, address, tables.AccountStatusOnLock, time.Now().Unix()-90*86400). + Order("account").Limit(limit).Offset(offset).Find(&list).Error + } + return +} + +func (d *DbDao) FindTotalAccountNameListByAddress(chainType common.ChainType, address, role string) (count int64, err error) { + if role == "" || role == "owner" { + err = d.db.Model(tables.TableAccountInfo{}). + Where(" owner_chain_type=? AND owner=? AND `status`!=? and expired_at >= ?", + chainType, address, tables.AccountStatusOnLock, time.Now().Unix()-90*86400). + Count(&count).Error + } else if role == "manager" { + err = d.db.Model(tables.TableAccountInfo{}). + Where(" manager_chain_type=? AND manager=? AND `status`!=? and expired_at >= ?", + chainType, address, tables.AccountStatusOnLock, time.Now().Unix()-90*86400). + Count(&count).Error } return } diff --git a/http_server/handle/account_list.go b/http_server/handle/account_list.go index 19de634..75735e8 100644 --- a/http_server/handle/account_list.go +++ b/http_server/handle/account_list.go @@ -131,7 +131,7 @@ func (h *HttpHandle) doAccountList(ctx context.Context, req *ReqAccountList, api } resp.Total = total } else { - list, err := h.DbDao.FindAccountNameListByAddress(addrHex.ChainType, addrHex.AddressHex, req.Role) + list, err := h.DbDao.FindAccountNameListByAddress(addrHex.ChainType, addrHex.AddressHex, req.Role, req.GetLimit(), req.GetOffset()) if err != nil { log.Error(ctx, "FindAccountListByAddress err:", err.Error(), req.KeyInfo) apiResp.ApiRespErr(http_api.ApiCodeDbError, "find account list err") @@ -147,6 +147,12 @@ func (h *HttpHandle) doAccountList(ctx context.Context, req *ReqAccountList, api } resp.AccountList = append(resp.AccountList, tmp) } + total, err := h.DbDao.FindTotalAccountNameListByAddress(addrHex.ChainType, addrHex.AddressHex, req.Role) + if err != nil { + apiResp.ApiRespErr(http_api.ApiCodeDbError, "search account total err") + return fmt.Errorf("FindTotalAccountNameListByAddress err: %s", err.Error()) + } + resp.Total = total } apiResp.ApiRespOK(resp)