This commit is contained in:
11
backend_v1/app/http/admin/dto/order.go
Normal file
11
backend_v1/app/http/admin/dto/order.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package dto
|
||||
|
||||
import "quyun/v2/app/requests"
|
||||
|
||||
type OrderListQuery struct {
|
||||
*requests.Pagination
|
||||
|
||||
Keyword *string `query:"keyword"`
|
||||
OrderNumber *string `query:"order_number"`
|
||||
UserID *int64 `query:"user_id"`
|
||||
}
|
||||
@@ -1,19 +1,14 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/admin/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
type OrderListQuery struct {
|
||||
OrderNumber *string `query:"order_number"`
|
||||
UserID *int64 `query:"user_id"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
type orders struct{}
|
||||
|
||||
@@ -22,27 +17,12 @@ type orders struct{}
|
||||
// @Summary 订单列表
|
||||
// @Tags Admin Orders
|
||||
// @Produce json
|
||||
// @Param pagination query requests.Pagination false "分页参数"
|
||||
// @Param query query OrderListQuery false "筛选条件"
|
||||
// @Success 200 {object} requests.Pager{items=services.OrderListItem} "成功"
|
||||
// @Param query query dto.OrderListQuery false "筛选条件"
|
||||
// @Success 200 {object} requests.Pager{items=services.OrderListItem} "成功"
|
||||
// @Router /admin/v1/orders [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *orders) List(
|
||||
ctx fiber.Ctx,
|
||||
pagination *requests.Pagination,
|
||||
query *OrderListQuery,
|
||||
) (*requests.Pager, error) {
|
||||
conds := []gen.Condition{}
|
||||
if query.OrderNumber != nil {
|
||||
conds = append(conds, models.OrderQuery.OrderNo.Eq(*query.OrderNumber))
|
||||
}
|
||||
|
||||
if query.UserID != nil {
|
||||
conds = append(conds, models.OrderQuery.UserID.Eq(*query.UserID))
|
||||
}
|
||||
|
||||
return services.Orders.List(ctx, pagination, conds...)
|
||||
func (ctl *orders) List(ctx fiber.Ctx, query *dto.OrderListQuery) (*requests.Pager, error) {
|
||||
return services.Orders.List(ctx, query)
|
||||
}
|
||||
|
||||
// Refund
|
||||
|
||||
@@ -6,6 +6,7 @@ package admin
|
||||
|
||||
import (
|
||||
"go.ipao.vip/gen/field"
|
||||
"quyun/v2/app/http/admin/dto"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
@@ -80,10 +81,9 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
))
|
||||
// Register routes for controller: orders
|
||||
r.log.Debugf("Registering route: Get /admin/v1/orders -> orders.List")
|
||||
router.Get("/admin/v1/orders"[len(r.Path()):], DataFunc2(
|
||||
router.Get("/admin/v1/orders"[len(r.Path()):], DataFunc1(
|
||||
r.orders.List,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[OrderListQuery]("query"),
|
||||
Query[dto.OrderListQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /admin/v1/orders/:id/refund -> orders.Refund")
|
||||
router.Post("/admin/v1/orders/:id/refund"[len(r.Path()):], Func1(
|
||||
|
||||
@@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/http/admin/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
|
||||
@@ -25,19 +27,33 @@ type OrderListItem struct {
|
||||
type orders struct{}
|
||||
|
||||
// List 订单列表(支持按订单号模糊查询、按用户过滤)。
|
||||
func (m *orders) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
conds ...gen.Condition,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
func (m *orders) List(ctx context.Context, filter *dto.OrderListQuery) (*requests.Pager, error) {
|
||||
filter.Pagination.Format()
|
||||
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
|
||||
orders, cnt, err := query.
|
||||
Where(conds...).
|
||||
Order(tbl.ID.Desc()).
|
||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
query = query.Order(tbl.ID.Desc())
|
||||
|
||||
if filter.OrderNumber != nil && *filter.OrderNumber != "" {
|
||||
query = query.Where(tbl.OrderNo.Eq(*filter.OrderNumber))
|
||||
}
|
||||
|
||||
if filter.Keyword != nil && *filter.Keyword != "" {
|
||||
query = query.Where(tbl.OrderNo.Like(database.WrapLike(*filter.Keyword)))
|
||||
|
||||
// query post ids
|
||||
ids, _ := Posts.FilterIdsByKeyword(ctx, *filter.Keyword)
|
||||
if len(ids) > 0 {
|
||||
ids = lo.Uniq(ids)
|
||||
query = query.Or(tbl.PostID.In(ids...))
|
||||
}
|
||||
}
|
||||
|
||||
if filter.UserID != nil && *filter.UserID > 0 {
|
||||
query = query.Where(tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
|
||||
orders, cnt, err := query.FindByPage(int(filter.Pagination.Offset()), int(filter.Pagination.Limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to list orders")
|
||||
}
|
||||
@@ -72,7 +88,7 @@ func (m *orders) List(
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
Pagination: *filter.Pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -167,3 +168,9 @@ func (m *posts) Count(ctx context.Context, conds ...gen.Condition) (int64, error
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// FilterIdsByKeyword filter posts ids by title keywords
|
||||
func (m *posts) FilterIdsByKeyword(ctx context.Context, keyword string) ([]int64, error) {
|
||||
tbl, query := models.PostQuery.QueryContext(ctx)
|
||||
return query.Select(tbl.ID).Where(tbl.Title.Like(database.WrapLike(keyword))).PluckIDs()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user