增加流水通知审计分页

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
@@ -7,10 +7,18 @@ import (
)
type Query struct {
ActorID uint64
Action string
BizType string
Limit int
ActorID uint64
Action string
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 LogDTO struct {
+20 -10
View File
@@ -18,17 +18,32 @@ func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
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) List(c *gin.Context) {
query, ok := parseQuery(c)
if !ok {
return
}
items, err := h.service.List(query)
result, err := h.service.List(query)
if err != nil {
writeAuditError(c, err)
return
}
response.OK(c, gin.H{"items": items})
response.OK(c, result)
}
func parseQuery(c *gin.Context) (Query, bool) {
@@ -41,16 +56,11 @@ func parseQuery(c *gin.Context) (Query, bool) {
}
query.ActorID = 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.Action = c.Query("action")
query.BizType = c.Query("biz_type")
page, pageSize := parsePagination(c)
query.Page = page
query.PageSize = pageSize
return query, true
}
@@ -3,6 +3,8 @@ package adminaudit
import (
"time"
"hfb_sys/backend/internal/model"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@@ -15,37 +17,42 @@ func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) List(query Query) ([]LogDTO, error) {
limit := query.Limit
if limit <= 0 || limit > 500 {
limit = 200
}
func (r *Repository) List(query Query) (*PaginatedResult, error) {
db := r.db.Table("audit_logs AS al").
Select(`al.id, al.actor_type, al.actor_id, COALESCE(au.username, '') AS actor_username,
COALESCE(au.nickname, '') AS actor_nickname, al.action, al.biz_type, al.biz_id,
al.ip, al.user_agent, al.detail, al.created_at`).
Joins("LEFT JOIN admin_users AS au ON au.id = al.actor_id AND al.actor_type = ?", "admin")
countDB := r.db.Model(&model.AuditLog{})
if query.ActorID > 0 {
db = db.Where("al.actor_id = ?", query.ActorID)
countDB = countDB.Where("actor_id = ?", query.ActorID)
}
if query.Action != "" {
db = db.Where("al.action = ?", query.Action)
countDB = countDB.Where("action = ?", query.Action)
}
if query.BizType != "" {
db = db.Where("al.biz_type = ?", query.BizType)
countDB = countDB.Where("biz_type = ?", query.BizType)
}
var total int64
if err := countDB.Count(&total).Error; err != nil {
return nil, err
}
offset := (query.Page - 1) * query.PageSize
var rows []auditLogRow
if err := db.Order("al.id DESC").Limit(limit).Scan(&rows).Error; err != nil {
if err := db.Order("al.id DESC").Offset(offset).Limit(query.PageSize).Scan(&rows).Error; err != nil {
return nil, err
}
items := make([]LogDTO, 0, len(rows))
for _, row := range rows {
items = append(items, row.toDTO())
}
return items, nil
return &PaginatedResult{Items: items, Total: total, Page: query.Page, PageSize: query.PageSize}, nil
}
type auditLogRow struct {
@@ -12,7 +12,7 @@ func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List(query Query) ([]LogDTO, error) {
func (s *Service) List(query Query) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
@@ -13,3 +13,10 @@ type NotificationDTO struct {
ReadAt *time.Time `json:"read_at"`
CreatedAt time.Time `json:"created_at"`
}
type PaginatedResult struct {
Items interface{} `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
@@ -18,18 +18,34 @@ func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
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) List(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
items, err := h.service.List(userID)
page, pageSize := parsePagination(c)
result, err := h.service.List(userID, page, pageSize)
if err != nil {
writeNotificationError(c, err)
return
}
response.OK(c, gin.H{"items": items})
response.OK(c, result)
}
func (h *Handler) MarkRead(c *gin.Context) {
@@ -25,16 +25,21 @@ func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) List(userID uint64) ([]NotificationDTO, error) {
func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
var total int64
if err := r.db.Model(&model.Notification{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
return nil, err
}
offset := (page - 1) * pageSize
var rows []model.Notification
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([]NotificationDTO, 0, len(rows))
for _, row := range rows {
items = append(items, toDTO(row))
}
return items, nil
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
}
func (r *Repository) MarkRead(userID uint64, id uint64) error {
@@ -12,11 +12,11 @@ func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List(userID uint64) ([]NotificationDTO, error) {
func (s *Service) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.List(userID)
return s.repo.List(userID, page, pageSize)
}
func (s *Service) MarkRead(userID uint64, id uint64) error {
+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
}