51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type WalletLedger struct {
|
|
ID int64 `db:"id"`
|
|
LedgerNo string `db:"ledger_no"`
|
|
BizType string `db:"biz_type"`
|
|
BizNo string `db:"biz_no"`
|
|
EntryType string `db:"entry_type"`
|
|
Direction int `db:"direction"`
|
|
Amount int64 `db:"amount"`
|
|
BeforeAmount int64 `db:"before_amount"`
|
|
AfterAmount int64 `db:"after_amount"`
|
|
Currency string `db:"currency"`
|
|
Remark string `db:"remark"`
|
|
OccurredAt string `db:"occurred_at"`
|
|
}
|
|
|
|
type WalletLedgerPage struct {
|
|
Items []WalletLedger
|
|
Total int64
|
|
}
|
|
|
|
func (s *Store) WalletLedgers(ctx context.Context, tenantID, memberID int64, bizType string, direction, offset, limit int) (*WalletLedgerPage, error) {
|
|
where := ` WHERE tenant_id=? AND member_id=?`
|
|
args := []any{tenantID, memberID}
|
|
if bizType != "" {
|
|
where += ` AND biz_type=?`
|
|
args = append(args, bizType)
|
|
}
|
|
if direction > 0 {
|
|
where += ` AND direction=?`
|
|
args = append(args, direction)
|
|
}
|
|
|
|
var total int64
|
|
if err := s.db.GetContext(ctx, &total, `SELECT COUNT(*) FROM hg_wallet_ledger`+where, args...); err != nil {
|
|
return nil, err
|
|
}
|
|
args = append(args, limit, offset)
|
|
items := make([]WalletLedger, 0)
|
|
err := s.db.SelectContext(ctx, &items, `SELECT id,ledger_no,biz_type,biz_no,entry_type,direction,
|
|
amount,before_amount,after_amount,currency,remark,
|
|
DATE_FORMAT(occurred_at,'%Y-%m-%dT%H:%i:%sZ') AS occurred_at
|
|
FROM hg_wallet_ledger`+where+` ORDER BY occurred_at DESC,id DESC LIMIT ? OFFSET ?`, args...)
|
|
return &WalletLedgerPage{Items: items, Total: total}, err
|
|
}
|