tenant: extend admin order filters
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
@@ -350,10 +351,19 @@ func (s *order) AdminOrderPage(
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"user_id": lo.FromPtr(filter.UserID),
|
||||
"status": lo.FromPtr(filter.Status),
|
||||
"content_id": lo.FromPtr(filter.ContentID),
|
||||
"tenant_id": tenantID,
|
||||
"user_id": lo.FromPtr(filter.UserID),
|
||||
"username": filter.UsernameTrimmed(),
|
||||
"content_id": lo.FromPtr(filter.ContentID),
|
||||
"content_title": filter.ContentTitleTrimmed(),
|
||||
"type": lo.FromPtr(filter.Type),
|
||||
"status": lo.FromPtr(filter.Status),
|
||||
"created_at_from": filter.CreatedAtFrom,
|
||||
"created_at_to": filter.CreatedAtTo,
|
||||
"paid_at_from": filter.PaidAtFrom,
|
||||
"paid_at_to": filter.PaidAtTo,
|
||||
"amount_paid_min": filter.AmountPaidMin,
|
||||
"amount_paid_max": filter.AmountPaidMax,
|
||||
}).Info("services.order.admin.page")
|
||||
|
||||
filter.Pagination.Format()
|
||||
@@ -365,9 +375,18 @@ func (s *order) AdminOrderPage(
|
||||
if filter.UserID != nil {
|
||||
conds = append(conds, tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.Type != nil {
|
||||
conds = append(conds, tbl.Type.Eq(*filter.Type))
|
||||
}
|
||||
if filter.Status != nil {
|
||||
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))
|
||||
}
|
||||
@@ -380,10 +399,32 @@ func (s *order) AdminOrderPage(
|
||||
if filter.AmountPaidMax != nil {
|
||||
conds = append(conds, tbl.AmountPaid.Lte(*filter.AmountPaidMax))
|
||||
}
|
||||
if filter.ContentID != nil && *filter.ContentID > 0 {
|
||||
|
||||
// 用户关键字:按 users.username 模糊匹配。
|
||||
// 关键点:orders.user_id 与 users.id 一对一,不会导致重复行,无需 group by。
|
||||
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)))
|
||||
}
|
||||
|
||||
// 内容过滤:通过 order_items(以及 contents)关联查询。
|
||||
// 关键点:orders 与 order_items 一对多,join 后必须 group by orders.id 以避免同一订单重复返回。
|
||||
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))
|
||||
conds = append(conds, oiTbl.ContentID.Eq(*filter.ContentID))
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -650,7 +691,9 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
return err
|
||||
}
|
||||
var access models.ContentAccess
|
||||
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
|
||||
if err := tx.Table(models.TableNameContentAccess).
|
||||
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
|
||||
First(&access).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
out.AmountPaid = 0
|
||||
@@ -762,7 +805,9 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
return err
|
||||
}
|
||||
var access models.ContentAccess
|
||||
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
|
||||
if err := tx.Table(models.TableNameContentAccess).
|
||||
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
|
||||
First(&access).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
out.Order = orderModel
|
||||
@@ -829,12 +874,18 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 关键点:上面是 DB 更新;这里同步更新内存对象,避免返回给调用方的状态仍为 created。
|
||||
orderModel.Status = consts.OrderStatusPaid
|
||||
orderModel.PaidAt = now
|
||||
orderModel.UpdatedAt = now
|
||||
if err := s.grantAccess(ctx, tx, params.TenantID, params.UserID, params.ContentID, orderModel.ID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var access models.ContentAccess
|
||||
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
|
||||
if err := tx.Table(models.TableNameContentAccess).
|
||||
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
|
||||
First(&access).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -993,7 +1044,9 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
return err
|
||||
}
|
||||
var access models.ContentAccess
|
||||
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
|
||||
if err := tx.Table(models.TableNameContentAccess).
|
||||
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
|
||||
First(&access).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
out.Order = orderModel
|
||||
@@ -1053,13 +1106,19 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 关键点:上面是 DB 更新;这里同步更新内存对象,避免返回给调用方的状态仍为 created。
|
||||
orderModel.Status = consts.OrderStatusPaid
|
||||
orderModel.PaidAt = now
|
||||
orderModel.UpdatedAt = now
|
||||
|
||||
if err := s.grantAccess(ctx, tx, params.TenantID, params.UserID, params.ContentID, orderModel.ID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var access models.ContentAccess
|
||||
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
|
||||
if err := tx.Table(models.TableNameContentAccess).
|
||||
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
|
||||
First(&access).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user