84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"platform/web/auth"
|
|
"platform/web/core"
|
|
"platform/web/globals/orm"
|
|
q "platform/web/queries"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// region ListBill
|
|
|
|
type ListBillReq struct {
|
|
core.PageReq
|
|
BillNo *string `json:"bill_no"`
|
|
Type *int `json:"type"`
|
|
CreateAfter *time.Time `json:"create_after"`
|
|
CreateBefore *time.Time `json:"create_before"`
|
|
}
|
|
|
|
// ListBill 获取账单列表
|
|
func ListBill(c *fiber.Ctx) error {
|
|
// 检查权限
|
|
authCtx, err := auth.GetAuthCtx(c).PermitUser()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 解析请求参数
|
|
req := new(ListBillReq)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 查询账单列表
|
|
do := q.Bill.
|
|
Where(q.Bill.UserID.Eq(authCtx.User.ID))
|
|
|
|
if req.Type != nil {
|
|
do.Where(q.Bill.Type.Eq(int32(*req.Type)))
|
|
}
|
|
if req.CreateAfter != nil {
|
|
do.Where(q.Bill.CreatedAt.Gte(orm.LocalDateTime(*req.CreateAfter)))
|
|
}
|
|
if req.CreateBefore != nil {
|
|
do.Where(q.Bill.CreatedAt.Lte(orm.LocalDateTime(*req.CreateBefore)))
|
|
}
|
|
if req.BillNo != nil && *req.BillNo != "" {
|
|
do.Where(q.Bill.BillNo.Eq(*req.BillNo))
|
|
}
|
|
|
|
bills, err := q.Bill.Where(do).
|
|
Preload(q.Bill.Resource, q.Bill.Trade, q.Bill.Refund).
|
|
Preload(q.Bill.Resource.Short, q.Bill.Resource.Long).
|
|
Order(q.Bill.CreatedAt.Desc()).
|
|
Offset(req.GetOffset()).
|
|
Limit(req.GetLimit()).
|
|
Find()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var total int64
|
|
if len(bills) < req.GetLimit() {
|
|
total = int64(len(bills) + req.GetOffset())
|
|
} else {
|
|
total, err = q.Bill.Where(do).Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return c.JSON(core.PageResp{
|
|
Total: int(total),
|
|
Page: req.GetPage(),
|
|
Size: req.GetSize(),
|
|
List: bills,
|
|
})
|
|
}
|
|
|
|
// endregion
|