统一处理分页参数简化查询接口

This commit is contained in:
2025-04-12 11:48:39 +08:00
parent ca6e160bef
commit 438a55cc3e
4 changed files with 66 additions and 54 deletions

View File

@@ -13,8 +13,7 @@ import (
// region ListBill
type ListBillReq struct {
Page int `json:"page" validate:"required"`
Size int `json:"size" validate:"required"`
common.PageReq
BillNo *string `json:"bill_no"`
Type *int `json:"type"`
Status *int `json:"status"`
@@ -35,14 +34,6 @@ func ListBill(c *fiber.Ctx) error {
if err := c.BodyParser(req); err != nil {
return err
}
if req.Page < 1 {
req.Page = 1
}
if req.Size < 1 {
req.Size = 10
}
var offset = (req.Page - 1) * req.Size
var limit = req.Size
// 查询账单列表
do := q.Bill.
@@ -68,16 +59,16 @@ func ListBill(c *fiber.Ctx) error {
Preload(q.Bill.Resource, q.Bill.Trade, q.Bill.Refund).
Preload(q.Bill.Resource.Pss).
Order(q.Bill.CreatedAt.Desc()).
Offset(offset).
Limit(limit).
Offset(req.GetOffset()).
Limit(req.GetLimit()).
Find()
if err != nil {
return err
}
var total int64
if len(bills) < limit {
total = int64(len(bills) + offset)
if len(bills) < req.GetLimit() {
total = int64(len(bills) + req.GetOffset())
} else {
total, err = do.Count()
if err != nil {
@@ -87,8 +78,8 @@ func ListBill(c *fiber.Ctx) error {
return c.JSON(common.PageResp{
Total: int(total),
Page: req.Page,
Size: req.Size,
Page: req.GetPage(),
Size: req.GetSize(),
List: bills,
})
}