统一处理分页参数简化查询接口
This commit is contained in:
@@ -6,15 +6,52 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrResp 定义通用错误响应格式
|
// region req
|
||||||
type ErrResp struct {
|
|
||||||
Message string `json:"message"`
|
type PageReqInter interface {
|
||||||
Error bool `json:"error"`
|
GetPage() int
|
||||||
|
GetSize() int
|
||||||
|
GetOffset() int
|
||||||
|
GetLimit() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type PageReq struct {
|
type PageReq struct {
|
||||||
Page int `json:"page"`
|
RawPage int `json:"page"`
|
||||||
Size int `json:"size"`
|
RawSize int `json:"size"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageReq) GetPage() int {
|
||||||
|
if p.RawPage < 1 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return p.RawPage
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageReq) GetSize() int {
|
||||||
|
if p.RawSize < 1 {
|
||||||
|
return 10
|
||||||
|
}
|
||||||
|
if p.RawSize > 100 {
|
||||||
|
return 100
|
||||||
|
}
|
||||||
|
return p.RawSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageReq) GetOffset() int {
|
||||||
|
return (p.GetPage() - 1) * p.GetSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageReq) GetLimit() int {
|
||||||
|
return p.GetSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region resp
|
||||||
|
|
||||||
|
type ErrResp struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Error bool `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PageResp struct {
|
type PageResp struct {
|
||||||
@@ -24,6 +61,8 @@ type PageResp struct {
|
|||||||
List any `json:"list"`
|
List any `json:"list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// endregion
|
||||||
|
|
||||||
// region LocalDateTime
|
// region LocalDateTime
|
||||||
|
|
||||||
type LocalDateTime time.Time
|
type LocalDateTime time.Time
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ import (
|
|||||||
// region ListBill
|
// region ListBill
|
||||||
|
|
||||||
type ListBillReq struct {
|
type ListBillReq struct {
|
||||||
Page int `json:"page" validate:"required"`
|
common.PageReq
|
||||||
Size int `json:"size" validate:"required"`
|
|
||||||
BillNo *string `json:"bill_no"`
|
BillNo *string `json:"bill_no"`
|
||||||
Type *int `json:"type"`
|
Type *int `json:"type"`
|
||||||
Status *int `json:"status"`
|
Status *int `json:"status"`
|
||||||
@@ -35,14 +34,6 @@ func ListBill(c *fiber.Ctx) error {
|
|||||||
if err := c.BodyParser(req); err != nil {
|
if err := c.BodyParser(req); err != nil {
|
||||||
return err
|
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.
|
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, q.Bill.Trade, q.Bill.Refund).
|
||||||
Preload(q.Bill.Resource.Pss).
|
Preload(q.Bill.Resource.Pss).
|
||||||
Order(q.Bill.CreatedAt.Desc()).
|
Order(q.Bill.CreatedAt.Desc()).
|
||||||
Offset(offset).
|
Offset(req.GetOffset()).
|
||||||
Limit(limit).
|
Limit(req.GetLimit()).
|
||||||
Find()
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
if len(bills) < limit {
|
if len(bills) < req.GetLimit() {
|
||||||
total = int64(len(bills) + offset)
|
total = int64(len(bills) + req.GetOffset())
|
||||||
} else {
|
} else {
|
||||||
total, err = do.Count()
|
total, err = do.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -87,8 +78,8 @@ func ListBill(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
return c.JSON(common.PageResp{
|
return c.JSON(common.PageResp{
|
||||||
Total: int(total),
|
Total: int(total),
|
||||||
Page: req.Page,
|
Page: req.GetPage(),
|
||||||
Size: req.Size,
|
Size: req.GetSize(),
|
||||||
List: bills,
|
List: bills,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ import (
|
|||||||
// region ListResourcePss
|
// region ListResourcePss
|
||||||
|
|
||||||
type ListResourcePssReq struct {
|
type ListResourcePssReq struct {
|
||||||
Page int `json:"page" validate:"required"`
|
common.PageReq
|
||||||
Size int `json:"size" validate:"required"`
|
|
||||||
ResourceNo *string `json:"resource_no"`
|
ResourceNo *string `json:"resource_no"`
|
||||||
Active *bool `json:"active"`
|
Active *bool `json:"active"`
|
||||||
Type *int `json:"type"`
|
Type *int `json:"type"`
|
||||||
@@ -39,14 +38,6 @@ func ListResourcePss(c *fiber.Ctx) error {
|
|||||||
if err := c.BodyParser(req); err != nil {
|
if err := c.BodyParser(req); err != nil {
|
||||||
return err
|
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.Resource.
|
do := q.Resource.
|
||||||
@@ -77,16 +68,16 @@ func ListResourcePss(c *fiber.Ctx) error {
|
|||||||
var resource []*m.Resource
|
var resource []*m.Resource
|
||||||
err = do.Debug().
|
err = do.Debug().
|
||||||
Order(q.ResourcePss.As(q.Resource.Pss.Name()).CreatedAt.Desc()).
|
Order(q.ResourcePss.As(q.Resource.Pss.Name()).CreatedAt.Desc()).
|
||||||
Offset(offset).
|
Offset(req.GetOffset()).
|
||||||
Limit(limit).
|
Limit(req.GetLimit()).
|
||||||
Scan(&resource)
|
Scan(&resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
if len(resource) < limit {
|
if len(resource) < req.GetLimit() {
|
||||||
total = int64(len(resource) + offset)
|
total = int64(len(resource) + req.GetOffset())
|
||||||
} else {
|
} else {
|
||||||
total, err = do.Count()
|
total, err = do.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -96,8 +87,8 @@ func ListResourcePss(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
return c.JSON(common.PageResp{
|
return c.JSON(common.PageResp{
|
||||||
Total: int(total),
|
Total: int(total),
|
||||||
Page: req.Page,
|
Page: req.GetPage(),
|
||||||
Size: req.Size,
|
Size: req.GetSize(),
|
||||||
List: resource,
|
List: resource,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ListWhitelistReq struct {
|
type ListWhitelistReq struct {
|
||||||
Page int `json:"page" validate:"required"`
|
common.PageReq
|
||||||
Size int `json:"size" validate:"required"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListWhitelistResp struct {
|
type ListWhitelistResp struct {
|
||||||
@@ -36,22 +35,14 @@ func ListWhitelist(c *fiber.Ctx) error {
|
|||||||
if err := c.BodyParser(req); err != nil {
|
if err := c.BodyParser(req); err != nil {
|
||||||
return err
|
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.Whitelist.
|
do := q.Whitelist.
|
||||||
Where(q.Whitelist.UserID.Eq(authContext.Payload.Id))
|
Where(q.Whitelist.UserID.Eq(authContext.Payload.Id))
|
||||||
|
|
||||||
list, err := do.
|
list, err := do.
|
||||||
Offset(offset).
|
Offset(req.GetOffset()).
|
||||||
Limit(limit).
|
Limit(req.GetLimit()).
|
||||||
Order(q.Whitelist.CreatedAt.Desc()).
|
Order(q.Whitelist.CreatedAt.Desc()).
|
||||||
Find()
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -59,8 +50,8 @@ func ListWhitelist(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
if len(list) < limit {
|
if len(list) < req.GetLimit() {
|
||||||
total = int64(len(list) + offset)
|
total = int64(len(list) + req.GetOffset())
|
||||||
} else {
|
} else {
|
||||||
total, err = do.Count()
|
total, err = do.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -72,8 +63,8 @@ func ListWhitelist(c *fiber.Ctx) error {
|
|||||||
return c.JSON(common.PageResp{
|
return c.JSON(common.PageResp{
|
||||||
Total: int(total),
|
Total: int(total),
|
||||||
List: list,
|
List: list,
|
||||||
Page: req.Page,
|
Page: req.GetPage(),
|
||||||
Size: req.Size,
|
Size: req.GetSize(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user