重构 orm 代码生成逻辑,实现 bill 接口,优化请求字段检查与 list total 查询逻辑
This commit is contained in:
@@ -1 +1,96 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"platform/web/auth"
|
||||
"platform/web/common"
|
||||
q "platform/web/queries"
|
||||
"platform/web/services"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// region ListBill
|
||||
|
||||
type ListBillReq struct {
|
||||
Page int `json:"page" validate:"required"`
|
||||
Size int `json:"size" validate:"required"`
|
||||
BillNo *string `json:"bill_no"`
|
||||
Type *int `json:"type"`
|
||||
Status *int `json:"status"`
|
||||
CreateAfter *time.Time `json:"create_after"`
|
||||
CreateBefore *time.Time `json:"create_before"`
|
||||
}
|
||||
|
||||
// ListBill 获取账单列表
|
||||
func ListBill(c *fiber.Ctx) error {
|
||||
// 检查权限
|
||||
authContext, err := auth.Protect(c, []services.PayloadType{services.PayloadUser}, []string{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 解析请求参数
|
||||
req := new(ListBillReq)
|
||||
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.
|
||||
Where(q.Bill.UserID.Eq(authContext.Payload.Id))
|
||||
|
||||
if req.Status != nil {
|
||||
do = do.Where(q.Bill.Status.Eq(int32(*req.Status)))
|
||||
}
|
||||
if req.Type != nil {
|
||||
do = do.Where(q.Bill.Type.Eq(int32(*req.Type)))
|
||||
}
|
||||
if req.CreateAfter != nil {
|
||||
do = do.Where(q.Bill.CreatedAt.Gte(common.LocalDateTime(*req.CreateAfter)))
|
||||
}
|
||||
if req.CreateBefore != nil {
|
||||
do = do.Where(q.Bill.CreatedAt.Lte(common.LocalDateTime(*req.CreateBefore)))
|
||||
}
|
||||
if req.BillNo != nil && *req.BillNo != "" {
|
||||
do = do.Where(q.Bill.BillNo.Eq(*req.BillNo))
|
||||
}
|
||||
|
||||
bills, err := do.Debug().
|
||||
Preload(q.Bill.Resource, q.Bill.Trade, q.Bill.Refund).
|
||||
Preload(q.Bill.Resource.Pss).
|
||||
Order(q.Bill.CreatedAt.Desc()).
|
||||
Offset(offset).
|
||||
Limit(limit).
|
||||
Find()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var total int64
|
||||
if len(bills) < limit {
|
||||
total = int64(len(bills) + offset)
|
||||
} else {
|
||||
total, err = do.Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(common.PageResp{
|
||||
Total: int(total),
|
||||
Page: req.Page,
|
||||
Size: req.Size,
|
||||
List: bills,
|
||||
})
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
Reference in New Issue
Block a user