79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
// 功能描述:当前钱包账单的白名单输出、收支筛选和游标分页;版本:1.0.0。
|
||
package common
|
||
|
||
import (
|
||
"strconv"
|
||
|
||
"git.apinb.com/bsm-sdk/core/errcode"
|
||
"git.apinb.com/bsm-sdk/core/infra"
|
||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// ListWalletBills 仅按当前主体钱包读取,不允许请求参数覆盖归属;旧records接口保持兼容。
|
||
func ListWalletBills(client string) gin.HandlerFunc {
|
||
return func(ctx *gin.Context) {
|
||
direction := ctx.Query("direction")
|
||
if direction != "" && direction != "income" && direction != "expense" {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
var before uint64
|
||
if raw := ctx.Query("cursor"); raw != "" {
|
||
var err error
|
||
before, err = strconv.ParseUint(raw, 10, 64)
|
||
if err != nil || before == 0 {
|
||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||
return
|
||
}
|
||
}
|
||
owner, ok := currentOwner(ctx, client)
|
||
if !ok {
|
||
return
|
||
}
|
||
wallet, err := ensureWallet(impl.DBService, owner)
|
||
if err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
query := impl.DBService.Where("wallet_basic_id = ?", wallet.ID)
|
||
if direction != "" {
|
||
// 旧种子使用in,当前记账使用income;读取兼容,不重写历史资金流水。
|
||
if direction == "income" {
|
||
query = query.Where("direction IN ?", []string{"income", "in"})
|
||
} else {
|
||
query = query.Where("direction = ?", direction)
|
||
}
|
||
}
|
||
if before != 0 {
|
||
query = query.Where("id < ?", before)
|
||
}
|
||
var records []models.WalletRecord
|
||
// 固定按入账序号倒序,新增流水不会导致后续页重复或错位。
|
||
if err := query.Order("id desc").Limit(51).Find(&records).Error; err != nil {
|
||
infra.Response.Error(ctx, err)
|
||
return
|
||
}
|
||
next := ""
|
||
if len(records) > 50 {
|
||
records = records[:50]
|
||
next = strconv.FormatUint(records[49].ID, 10)
|
||
}
|
||
items := make([]gin.H, 0, len(records))
|
||
for _, record := range records {
|
||
flow := record.Direction
|
||
if flow == "in" {
|
||
flow = "income"
|
||
}
|
||
items = append(items, gin.H{
|
||
"identity": record.Identity, "record_no": record.RecordNo,
|
||
"direction": flow, "trade_type": record.TradeType,
|
||
"amount": record.Amount, "fee": record.Fee, "balance_after": record.BalanceAfter,
|
||
"created_at": record.CreatedAt, "pay_channel": record.PayChannel,
|
||
})
|
||
}
|
||
infra.Response.Success(ctx, gin.H{"items": items, "next_cursor": next})
|
||
}
|
||
}
|