增加流水通知审计分页

This commit is contained in:
yml
2026-05-24 17:51:00 +08:00
parent 03e5d211dc
commit cb636e4e89
21 changed files with 295 additions and 120 deletions
+12 -4
View File
@@ -29,10 +29,18 @@ type LedgerDTO struct {
}
type AdminLedgerQuery struct {
UserID uint64
OrderID uint64
BizType string
Limit int
UserID uint64
OrderID uint64
BizType string
Page int
PageSize int
}
type PaginatedResult struct {
Items interface{} `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
type AdminLedgerDTO struct {
+23 -12
View File
@@ -32,18 +32,34 @@ func (h *Handler) Balance(c *gin.Context) {
response.OK(c, account)
}
func parsePagination(c *gin.Context) (int, int) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
if pageSize > 100 {
pageSize = 100
}
return page, pageSize
}
func (h *Handler) Ledger(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
items, err := h.service.Ledger(userID)
page, pageSize := parsePagination(c)
result, err := h.service.Ledger(userID, page, pageSize)
if err != nil {
writeWalletError(c, err)
return
}
response.OK(c, gin.H{"items": items})
response.OK(c, result)
}
func (h *Handler) Recharge(c *gin.Context) {
@@ -70,12 +86,12 @@ func (h *Handler) AdminLedger(c *gin.Context) {
if !ok {
return
}
items, err := h.service.AdminLedger(query)
result, err := h.service.AdminLedger(query)
if err != nil {
writeWalletError(c, err)
return
}
response.OK(c, gin.H{"items": items})
response.OK(c, result)
}
func parseAdminLedgerQuery(c *gin.Context) (AdminLedgerQuery, bool) {
@@ -96,15 +112,10 @@ func parseAdminLedgerQuery(c *gin.Context) (AdminLedgerQuery, bool) {
}
query.OrderID = value
}
if raw := c.Query("limit"); raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
response.BadRequest(c, "查询条数不正确")
return query, false
}
query.Limit = value
}
query.BizType = c.Query("biz_type")
page, pageSize := parsePagination(c)
query.Page = page
query.PageSize = pageSize
return query, true
}
+22 -12
View File
@@ -45,16 +45,21 @@ func (r *Repository) Account(userID uint64) (*AccountDTO, error) {
return toAccountDTO(account), nil
}
func (r *Repository) Ledger(userID uint64) ([]LedgerDTO, error) {
func (r *Repository) Ledger(userID uint64, page, pageSize int) (*PaginatedResult, error) {
var total int64
if err := r.db.Model(&model.WalletLedger{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
return nil, err
}
offset := (page - 1) * pageSize
var rows []model.WalletLedger
if err := r.db.Where("user_id = ?", userID).Order("id DESC").Limit(100).Find(&rows).Error; err != nil {
if err := r.db.Where("user_id = ?", userID).Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
return nil, err
}
items := make([]LedgerDTO, 0, len(rows))
for _, row := range rows {
items = append(items, toLedgerDTO(row))
}
return items, nil
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
}
func (r *Repository) Recharge(userID uint64, amount float64) (*AccountDTO, error) {
@@ -75,12 +80,7 @@ func (r *Repository) Recharge(userID uint64, amount float64) (*AccountDTO, error
return r.Account(userID)
}
func (r *Repository) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, error) {
limit := query.Limit
if limit <= 0 || limit > 500 {
limit = 200
}
func (r *Repository) AdminLedger(query AdminLedgerQuery) (*PaginatedResult, error) {
db := r.db.Table("wallet_ledger AS wl").
Select(`wl.id, wl.ledger_no, wl.user_id, COALESCE(u.phone, '') AS user_phone,
COALESCE(u.nickname, '') AS user_nickname, wl.order_id, COALESCE(ro.order_no, '') AS order_no,
@@ -89,21 +89,31 @@ func (r *Repository) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, erro
Joins("LEFT JOIN users AS u ON u.id = wl.user_id").
Joins("LEFT JOIN rental_orders AS ro ON ro.id = wl.order_id")
countDB := r.db.Model(&model.WalletLedger{})
if query.UserID > 0 {
db = db.Where("wl.user_id = ?", query.UserID)
countDB = countDB.Where("user_id = ?", query.UserID)
}
if query.OrderID > 0 {
db = db.Where("wl.order_id = ?", query.OrderID)
countDB = countDB.Where("order_id = ?", query.OrderID)
}
if query.BizType != "" {
db = db.Where("wl.biz_type = ?", query.BizType)
countDB = countDB.Where("biz_type = ?", query.BizType)
}
var items []AdminLedgerDTO
if err := db.Order("wl.id DESC").Limit(limit).Scan(&items).Error; err != nil {
var total int64
if err := countDB.Count(&total).Error; err != nil {
return nil, err
}
return items, nil
offset := (query.Page - 1) * query.PageSize
var items []AdminLedgerDTO
if err := db.Order("wl.id DESC").Offset(offset).Limit(query.PageSize).Scan(&items).Error; err != nil {
return nil, err
}
return &PaginatedResult{Items: items, Total: total, Page: query.Page, PageSize: query.PageSize}, nil
}
func AppendEntries(tx *gorm.DB, entries ...Entry) error {
+3 -3
View File
@@ -23,11 +23,11 @@ func (s *Service) Account(userID uint64) (*AccountDTO, error) {
return s.repo.Account(userID)
}
func (s *Service) Ledger(userID uint64) ([]LedgerDTO, error) {
func (s *Service) Ledger(userID uint64, page, pageSize int) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.Ledger(userID)
return s.repo.Ledger(userID, page, pageSize)
}
func (s *Service) Recharge(userID uint64, req RechargeRequest) (*AccountDTO, error) {
@@ -40,7 +40,7 @@ func (s *Service) Recharge(userID uint64, req RechargeRequest) (*AccountDTO, err
return s.repo.Recharge(userID, req.Amount)
}
func (s *Service) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, error) {
func (s *Service) AdminLedger(query AdminLedgerQuery) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}