后台“资金流水

This commit is contained in:
yml
2026-05-22 17:43:45 +08:00
parent 7df557d668
commit c9e6ea2b71
28 changed files with 1271 additions and 34 deletions
+25
View File
@@ -23,3 +23,28 @@ type LedgerDTO struct {
Remark string `json:"remark"`
CreatedAt time.Time `json:"created_at"`
}
type AdminLedgerQuery struct {
UserID uint64
OrderID uint64
BizType string
Limit int
}
type AdminLedgerDTO struct {
ID uint64 `json:"id"`
LedgerNo string `json:"ledger_no"`
UserID uint64 `json:"user_id"`
UserPhone string `json:"user_phone"`
UserNickname string `json:"user_nickname"`
OrderID *uint64 `json:"order_id"`
OrderNo string `json:"order_no"`
Direction string `json:"direction"`
Amount float64 `json:"amount"`
BalanceAfter float64 `json:"balance_after"`
BalanceType string `json:"balance_type"`
BizType string `json:"biz_type"`
BizNo string `json:"biz_no"`
Remark string `json:"remark"`
CreatedAt time.Time `json:"created_at"`
}
@@ -2,6 +2,7 @@ package wallet
import (
"errors"
"strconv"
"hfb_sys/backend/internal/middleware"
"hfb_sys/backend/pkg/response"
@@ -45,6 +46,49 @@ func (h *Handler) Ledger(c *gin.Context) {
response.OK(c, gin.H{"items": items})
}
func (h *Handler) AdminLedger(c *gin.Context) {
query, ok := parseAdminLedgerQuery(c)
if !ok {
return
}
items, err := h.service.AdminLedger(query)
if err != nil {
writeWalletError(c, err)
return
}
response.OK(c, gin.H{"items": items})
}
func parseAdminLedgerQuery(c *gin.Context) (AdminLedgerQuery, bool) {
var query AdminLedgerQuery
if raw := c.Query("user_id"); raw != "" {
value, err := strconv.ParseUint(raw, 10, 64)
if err != nil || value == 0 {
response.BadRequest(c, "用户 ID 不正确")
return query, false
}
query.UserID = value
}
if raw := c.Query("order_id"); raw != "" {
value, err := strconv.ParseUint(raw, 10, 64)
if err != nil || value == 0 {
response.BadRequest(c, "订单 ID 不正确")
return query, false
}
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")
return query, true
}
func currentUserID(c *gin.Context) (uint64, bool) {
value, ok := c.Get(middleware.ContextUserID)
if !ok {
@@ -57,6 +57,37 @@ func (r *Repository) Ledger(userID uint64) ([]LedgerDTO, error) {
return items, nil
}
func (r *Repository) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, error) {
limit := query.Limit
if limit <= 0 || limit > 500 {
limit = 200
}
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,
wl.direction, wl.amount, wl.balance_after, wl.balance_type, wl.biz_type, wl.biz_no,
wl.remark, wl.created_at`).
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")
if query.UserID > 0 {
db = db.Where("wl.user_id = ?", query.UserID)
}
if query.OrderID > 0 {
db = db.Where("wl.order_id = ?", query.OrderID)
}
if query.BizType != "" {
db = db.Where("wl.biz_type = ?", query.BizType)
}
var items []AdminLedgerDTO
if err := db.Order("wl.id DESC").Limit(limit).Scan(&items).Error; err != nil {
return nil, err
}
return items, nil
}
func AppendEntries(tx *gorm.DB, entries ...Entry) error {
for _, entry := range entries {
if entry.Amount <= 0 {
@@ -25,3 +25,10 @@ func (s *Service) Ledger(userID uint64) ([]LedgerDTO, error) {
}
return s.repo.Ledger(userID)
}
func (s *Service) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.AdminLedger(query)
}