feat: 添加订单管理功能,支持跨租户分页查询,优化订单列表展示及查询条件
This commit is contained in:
@@ -199,6 +199,224 @@ func (s *order) AdminOrderExportCSV(
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SuperOrderPage 平台侧分页查询订单(跨租户)。
|
||||
func (s *order) SuperOrderPage(ctx context.Context, filter *superdto.OrderPageFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &superdto.OrderPageFilter{}
|
||||
}
|
||||
|
||||
filter.Pagination.Format()
|
||||
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
conds := []gen.Condition{}
|
||||
|
||||
if filter.ID != nil && *filter.ID > 0 {
|
||||
conds = append(conds, tbl.ID.Eq(*filter.ID))
|
||||
}
|
||||
if filter.TenantID != nil && *filter.TenantID > 0 {
|
||||
conds = append(conds, tbl.TenantID.Eq(*filter.TenantID))
|
||||
}
|
||||
if filter.UserID != nil && *filter.UserID > 0 {
|
||||
conds = append(conds, tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.Type != nil && *filter.Type != "" {
|
||||
conds = append(conds, tbl.Type.Eq(*filter.Type))
|
||||
}
|
||||
if filter.Status != nil && *filter.Status != "" {
|
||||
conds = append(conds, tbl.Status.Eq(*filter.Status))
|
||||
}
|
||||
if filter.CreatedAtFrom != nil {
|
||||
conds = append(conds, tbl.CreatedAt.Gte(*filter.CreatedAtFrom))
|
||||
}
|
||||
if filter.CreatedAtTo != nil {
|
||||
conds = append(conds, tbl.CreatedAt.Lte(*filter.CreatedAtTo))
|
||||
}
|
||||
if filter.PaidAtFrom != nil {
|
||||
conds = append(conds, tbl.PaidAt.Gte(*filter.PaidAtFrom))
|
||||
}
|
||||
if filter.PaidAtTo != nil {
|
||||
conds = append(conds, tbl.PaidAt.Lte(*filter.PaidAtTo))
|
||||
}
|
||||
if filter.AmountPaidMin != nil {
|
||||
conds = append(conds, tbl.AmountPaid.Gte(*filter.AmountPaidMin))
|
||||
}
|
||||
if filter.AmountPaidMax != nil {
|
||||
conds = append(conds, tbl.AmountPaid.Lte(*filter.AmountPaidMax))
|
||||
}
|
||||
|
||||
// 买家用户名关键字。
|
||||
if username := filter.UsernameTrimmed(); username != "" {
|
||||
uTbl, _ := models.UserQuery.QueryContext(ctx)
|
||||
query = query.LeftJoin(uTbl, uTbl.ID.EqCol(tbl.UserID))
|
||||
conds = append(conds, uTbl.Username.Like(database.WrapLike(username)))
|
||||
}
|
||||
|
||||
// 租户 code/name 关键字。
|
||||
tenantCode := filter.TenantCodeTrimmed()
|
||||
tenantName := filter.TenantNameTrimmed()
|
||||
if tenantCode != "" || tenantName != "" {
|
||||
tTbl, _ := models.TenantQuery.QueryContext(ctx)
|
||||
query = query.LeftJoin(tTbl, tTbl.ID.EqCol(tbl.TenantID))
|
||||
if tenantCode != "" {
|
||||
conds = append(conds, tTbl.Code.Like(database.WrapLike(tenantCode)))
|
||||
}
|
||||
if tenantName != "" {
|
||||
conds = append(conds, tTbl.Name.Like(database.WrapLike(tenantName)))
|
||||
}
|
||||
}
|
||||
|
||||
// 内容过滤(orders 与 order_items 一对多,需要 group by)。
|
||||
needItemJoin := (filter.ContentID != nil && *filter.ContentID > 0) || filter.ContentTitleTrimmed() != ""
|
||||
if needItemJoin {
|
||||
oiTbl, _ := models.OrderItemQuery.QueryContext(ctx)
|
||||
query = query.LeftJoin(oiTbl, oiTbl.OrderID.EqCol(tbl.ID))
|
||||
|
||||
if filter.ContentID != nil && *filter.ContentID > 0 {
|
||||
conds = append(conds, oiTbl.ContentID.Eq(*filter.ContentID))
|
||||
}
|
||||
if title := filter.ContentTitleTrimmed(); title != "" {
|
||||
cTbl, _ := models.ContentQuery.QueryContext(ctx)
|
||||
query = query.LeftJoin(cTbl, cTbl.ID.EqCol(oiTbl.ContentID))
|
||||
conds = append(conds, cTbl.Title.Like(database.WrapLike(title)))
|
||||
}
|
||||
query = query.Group(tbl.ID)
|
||||
}
|
||||
|
||||
// 排序白名单:避免把任意字符串拼进 SQL 导致注入或慢查询。
|
||||
orderBys := make([]field.Expr, 0, 6)
|
||||
allowedAsc := map[string]field.Expr{
|
||||
"id": tbl.ID.Asc(),
|
||||
"tenant_id": tbl.TenantID.Asc(),
|
||||
"user_id": tbl.UserID.Asc(),
|
||||
"status": tbl.Status.Asc(),
|
||||
"created_at": tbl.CreatedAt.Asc(),
|
||||
"paid_at": tbl.PaidAt.Asc(),
|
||||
"amount_paid": tbl.AmountPaid.Asc(),
|
||||
}
|
||||
allowedDesc := map[string]field.Expr{
|
||||
"id": tbl.ID.Desc(),
|
||||
"tenant_id": tbl.TenantID.Desc(),
|
||||
"user_id": tbl.UserID.Desc(),
|
||||
"status": tbl.Status.Desc(),
|
||||
"created_at": tbl.CreatedAt.Desc(),
|
||||
"paid_at": tbl.PaidAt.Desc(),
|
||||
"amount_paid": tbl.AmountPaid.Desc(),
|
||||
}
|
||||
for _, f := range filter.AscFields() {
|
||||
f = strings.TrimSpace(f)
|
||||
if f == "" {
|
||||
continue
|
||||
}
|
||||
if ob, ok := allowedAsc[f]; ok {
|
||||
orderBys = append(orderBys, ob)
|
||||
}
|
||||
}
|
||||
for _, f := range filter.DescFields() {
|
||||
f = strings.TrimSpace(f)
|
||||
if f == "" {
|
||||
continue
|
||||
}
|
||||
if ob, ok := allowedDesc[f]; ok {
|
||||
orderBys = append(orderBys, ob)
|
||||
}
|
||||
}
|
||||
if len(orderBys) == 0 {
|
||||
orderBys = append(orderBys, tbl.ID.Desc())
|
||||
} else {
|
||||
orderBys = append(orderBys, tbl.ID.Desc())
|
||||
}
|
||||
|
||||
orders, total, err := query.Where(conds...).Order(orderBys...).FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tenantIDs := make([]int64, 0, len(orders))
|
||||
userIDs := make([]int64, 0, len(orders))
|
||||
for _, o := range orders {
|
||||
if o == nil {
|
||||
continue
|
||||
}
|
||||
if o.TenantID > 0 {
|
||||
tenantIDs = append(tenantIDs, o.TenantID)
|
||||
}
|
||||
if o.UserID > 0 {
|
||||
userIDs = append(userIDs, o.UserID)
|
||||
}
|
||||
}
|
||||
tenantIDs = lo.Uniq(tenantIDs)
|
||||
userIDs = lo.Uniq(userIDs)
|
||||
|
||||
tenantMap := make(map[int64]*models.Tenant, len(tenantIDs))
|
||||
if len(tenantIDs) > 0 {
|
||||
tTbl, tQuery := models.TenantQuery.QueryContext(ctx)
|
||||
tenants, err := tQuery.Where(tTbl.ID.In(tenantIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, te := range tenants {
|
||||
if te == nil {
|
||||
continue
|
||||
}
|
||||
tenantMap[te.ID] = te
|
||||
}
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*models.User, len(userIDs))
|
||||
if len(userIDs) > 0 {
|
||||
uTbl, uQuery := models.UserQuery.QueryContext(ctx)
|
||||
users, err := uQuery.Where(uTbl.ID.In(userIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, u := range users {
|
||||
if u == nil {
|
||||
continue
|
||||
}
|
||||
userMap[u.ID] = u
|
||||
}
|
||||
}
|
||||
|
||||
items := lo.Map(orders, func(o *models.Order, _ int) *superdto.SuperOrderItem {
|
||||
if o == nil {
|
||||
return &superdto.SuperOrderItem{}
|
||||
}
|
||||
|
||||
var tenantLite *superdto.OrderTenantLite
|
||||
if te := tenantMap[o.TenantID]; te != nil {
|
||||
tenantLite = &superdto.OrderTenantLite{ID: te.ID, Code: te.Code, Name: te.Name}
|
||||
}
|
||||
|
||||
var buyerLite *superdto.OrderBuyerLite
|
||||
if u := userMap[o.UserID]; u != nil {
|
||||
buyerLite = &superdto.OrderBuyerLite{ID: u.ID, Username: u.Username}
|
||||
}
|
||||
|
||||
return &superdto.SuperOrderItem{
|
||||
ID: o.ID,
|
||||
Tenant: tenantLite,
|
||||
Buyer: buyerLite,
|
||||
Type: o.Type,
|
||||
Status: o.Status,
|
||||
StatusDescription: o.Status.Description(),
|
||||
Currency: o.Currency,
|
||||
AmountOriginal: o.AmountOriginal,
|
||||
AmountDiscount: o.AmountDiscount,
|
||||
AmountPaid: o.AmountPaid,
|
||||
PaidAt: o.PaidAt,
|
||||
RefundedAt: o.RefundedAt,
|
||||
CreatedAt: o.CreatedAt,
|
||||
UpdatedAt: o.UpdatedAt,
|
||||
}
|
||||
})
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PurchaseContentParams 定义“租户内使用余额购买内容”的入参。
|
||||
type PurchaseContentParams struct {
|
||||
// TenantID 租户 ID(多租户隔离范围)。
|
||||
|
||||
Reference in New Issue
Block a user