feat: add super comment governance and finance oversight
This commit is contained in:
47
backend/app/http/super/v1/comments.go
Normal file
47
backend/app/http/super/v1/comments.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
dto "quyun/v2/app/http/super/v1/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type comments struct{}
|
||||
|
||||
// List comments
|
||||
//
|
||||
// @Router /super/v1/comments [get]
|
||||
// @Summary List comments
|
||||
// @Description List comments across tenants
|
||||
// @Tags Content
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "Page number"
|
||||
// @Param limit query int false "Page size"
|
||||
// @Success 200 {object} requests.Pager{items=[]dto.SuperCommentItem}
|
||||
// @Bind filter query
|
||||
func (c *comments) List(ctx fiber.Ctx, filter *dto.SuperCommentListFilter) (*requests.Pager, error) {
|
||||
return services.Super.ListComments(ctx, filter)
|
||||
}
|
||||
|
||||
// Delete comment
|
||||
//
|
||||
// @Router /super/v1/comments/:id<int>/delete [post]
|
||||
// @Summary Delete comment
|
||||
// @Description Soft delete a comment
|
||||
// @Tags Content
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int64 true "Comment ID"
|
||||
// @Param form body dto.SuperCommentDeleteForm false "Delete form"
|
||||
// @Success 200 {string} string "Deleted"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind id path
|
||||
// @Bind form body
|
||||
func (c *comments) Delete(ctx fiber.Ctx, user *models.User, id int64, form *dto.SuperCommentDeleteForm) error {
|
||||
return services.Super.DeleteComment(ctx, user.ID, id, form)
|
||||
}
|
||||
74
backend/app/http/super/v1/dto/super_comment.go
Normal file
74
backend/app/http/super/v1/dto/super_comment.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package dto
|
||||
|
||||
import "quyun/v2/app/requests"
|
||||
|
||||
// SuperCommentListFilter 超管评论列表过滤条件。
|
||||
type SuperCommentListFilter struct {
|
||||
requests.Pagination
|
||||
// ID 评论ID,精确匹配。
|
||||
ID *int64 `query:"id"`
|
||||
// TenantID 租户ID,精确匹配。
|
||||
TenantID *int64 `query:"tenant_id"`
|
||||
// TenantCode 租户编码,模糊匹配。
|
||||
TenantCode *string `query:"tenant_code"`
|
||||
// TenantName 租户名称,模糊匹配。
|
||||
TenantName *string `query:"tenant_name"`
|
||||
// ContentID 内容ID,精确匹配。
|
||||
ContentID *int64 `query:"content_id"`
|
||||
// ContentTitle 内容标题关键字,模糊匹配。
|
||||
ContentTitle *string `query:"content_title"`
|
||||
// UserID 评论用户ID,精确匹配。
|
||||
UserID *int64 `query:"user_id"`
|
||||
// Username 评论用户用户名/昵称,模糊匹配。
|
||||
Username *string `query:"username"`
|
||||
// Keyword 评论内容关键字,模糊匹配。
|
||||
Keyword *string `query:"keyword"`
|
||||
// Status 评论状态过滤(active/deleted/all)。
|
||||
Status *string `query:"status"`
|
||||
// CreatedAtFrom 创建时间起始(RFC3339)。
|
||||
CreatedAtFrom *string `query:"created_at_from"`
|
||||
// CreatedAtTo 创建时间结束(RFC3339)。
|
||||
CreatedAtTo *string `query:"created_at_to"`
|
||||
// Asc 升序字段(id/created_at/likes)。
|
||||
Asc *string `query:"asc"`
|
||||
// Desc 降序字段(id/created_at/likes)。
|
||||
Desc *string `query:"desc"`
|
||||
}
|
||||
|
||||
// SuperCommentItem 超管评论列表项。
|
||||
type SuperCommentItem struct {
|
||||
// ID 评论ID。
|
||||
ID int64 `json:"id"`
|
||||
// TenantID 租户ID。
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
// TenantCode 租户编码。
|
||||
TenantCode string `json:"tenant_code"`
|
||||
// TenantName 租户名称。
|
||||
TenantName string `json:"tenant_name"`
|
||||
// ContentID 内容ID。
|
||||
ContentID int64 `json:"content_id"`
|
||||
// ContentTitle 内容标题。
|
||||
ContentTitle string `json:"content_title"`
|
||||
// UserID 评论用户ID。
|
||||
UserID int64 `json:"user_id"`
|
||||
// Username 评论用户名称。
|
||||
Username string `json:"username"`
|
||||
// ReplyTo 回复评论ID(0 表示一级评论)。
|
||||
ReplyTo int64 `json:"reply_to"`
|
||||
// Content 评论内容。
|
||||
Content string `json:"content"`
|
||||
// Likes 评论点赞数。
|
||||
Likes int32 `json:"likes"`
|
||||
// IsDeleted 是否已删除。
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
// CreatedAt 创建时间(RFC3339)。
|
||||
CreatedAt string `json:"created_at"`
|
||||
// DeletedAt 删除时间(RFC3339,未删除为空)。
|
||||
DeletedAt string `json:"deleted_at"`
|
||||
}
|
||||
|
||||
// SuperCommentDeleteForm 超管删除评论表单。
|
||||
type SuperCommentDeleteForm struct {
|
||||
// Reason 删除原因(可选,用于审计记录)。
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
177
backend/app/http/super/v1/dto/super_finance.go
Normal file
177
backend/app/http/super/v1/dto/super_finance.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/pkg/consts"
|
||||
)
|
||||
|
||||
// SuperLedgerListFilter 超管资金流水过滤条件。
|
||||
type SuperLedgerListFilter struct {
|
||||
requests.Pagination
|
||||
// ID 流水ID,精确匹配。
|
||||
ID *int64 `query:"id"`
|
||||
// TenantID 租户ID,精确匹配。
|
||||
TenantID *int64 `query:"tenant_id"`
|
||||
// TenantCode 租户编码,模糊匹配。
|
||||
TenantCode *string `query:"tenant_code"`
|
||||
// TenantName 租户名称,模糊匹配。
|
||||
TenantName *string `query:"tenant_name"`
|
||||
// UserID 用户ID,精确匹配。
|
||||
UserID *int64 `query:"user_id"`
|
||||
// Username 用户名/昵称,模糊匹配。
|
||||
Username *string `query:"username"`
|
||||
// OrderID 关联订单ID,精确匹配。
|
||||
OrderID *int64 `query:"order_id"`
|
||||
// Type 流水类型过滤。
|
||||
Type *consts.TenantLedgerType `query:"type"`
|
||||
// AmountMin 金额下限(分)。
|
||||
AmountMin *int64 `query:"amount_min"`
|
||||
// AmountMax 金额上限(分)。
|
||||
AmountMax *int64 `query:"amount_max"`
|
||||
// CreatedAtFrom 创建时间起始(RFC3339)。
|
||||
CreatedAtFrom *string `query:"created_at_from"`
|
||||
// CreatedAtTo 创建时间结束(RFC3339)。
|
||||
CreatedAtTo *string `query:"created_at_to"`
|
||||
// Asc 升序字段(id/created_at/amount)。
|
||||
Asc *string `query:"asc"`
|
||||
// Desc 降序字段(id/created_at/amount)。
|
||||
Desc *string `query:"desc"`
|
||||
}
|
||||
|
||||
// SuperLedgerItem 超管资金流水项。
|
||||
type SuperLedgerItem struct {
|
||||
// ID 流水ID。
|
||||
ID int64 `json:"id"`
|
||||
// TenantID 租户ID。
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
// TenantCode 租户编码。
|
||||
TenantCode string `json:"tenant_code"`
|
||||
// TenantName 租户名称。
|
||||
TenantName string `json:"tenant_name"`
|
||||
// UserID 关联用户ID。
|
||||
UserID int64 `json:"user_id"`
|
||||
// Username 关联用户名。
|
||||
Username string `json:"username"`
|
||||
// OrderID 关联订单ID。
|
||||
OrderID int64 `json:"order_id"`
|
||||
// Type 流水类型。
|
||||
Type consts.TenantLedgerType `json:"type"`
|
||||
// TypeDescription 流水类型描述(用于展示)。
|
||||
TypeDescription string `json:"type_description"`
|
||||
// Amount 变动金额(分)。
|
||||
Amount int64 `json:"amount"`
|
||||
// BalanceBefore 变更前可用余额(分)。
|
||||
BalanceBefore int64 `json:"balance_before"`
|
||||
// BalanceAfter 变更后可用余额(分)。
|
||||
BalanceAfter int64 `json:"balance_after"`
|
||||
// FrozenBefore 变更前冻结余额(分)。
|
||||
FrozenBefore int64 `json:"frozen_before"`
|
||||
// FrozenAfter 变更后冻结余额(分)。
|
||||
FrozenAfter int64 `json:"frozen_after"`
|
||||
// Remark 流水备注说明。
|
||||
Remark string `json:"remark"`
|
||||
// OperatorUserID 操作者用户ID(0 表示系统)。
|
||||
OperatorUserID int64 `json:"operator_user_id"`
|
||||
// BizRefType 业务引用类型(可选)。
|
||||
BizRefType string `json:"biz_ref_type"`
|
||||
// BizRefID 业务引用ID(可选)。
|
||||
BizRefID int64 `json:"biz_ref_id"`
|
||||
// CreatedAt 创建时间(RFC3339)。
|
||||
CreatedAt string `json:"created_at"`
|
||||
// UpdatedAt 更新时间(RFC3339)。
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// SuperBalanceAnomalyFilter 余额异常筛选条件。
|
||||
type SuperBalanceAnomalyFilter struct {
|
||||
requests.Pagination
|
||||
// UserID 用户ID,精确匹配。
|
||||
UserID *int64 `query:"user_id"`
|
||||
// Username 用户名/昵称,模糊匹配。
|
||||
Username *string `query:"username"`
|
||||
// Issue 异常类型(negative_balance/negative_frozen)。
|
||||
Issue *string `query:"issue"`
|
||||
// Asc 升序字段(id/balance/balance_frozen)。
|
||||
Asc *string `query:"asc"`
|
||||
// Desc 降序字段(id/balance/balance_frozen)。
|
||||
Desc *string `query:"desc"`
|
||||
}
|
||||
|
||||
// SuperBalanceAnomalyItem 余额异常项。
|
||||
type SuperBalanceAnomalyItem struct {
|
||||
// UserID 用户ID。
|
||||
UserID int64 `json:"user_id"`
|
||||
// Username 用户名。
|
||||
Username string `json:"username"`
|
||||
// Balance 可用余额(分)。
|
||||
Balance int64 `json:"balance"`
|
||||
// BalanceFrozen 冻结余额(分)。
|
||||
BalanceFrozen int64 `json:"balance_frozen"`
|
||||
// Issue 异常类型标识。
|
||||
Issue string `json:"issue"`
|
||||
// IssueDescription 异常描述说明。
|
||||
IssueDescription string `json:"issue_description"`
|
||||
// CreatedAt 用户创建时间(RFC3339)。
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// SuperOrderAnomalyFilter 订单异常筛选条件。
|
||||
type SuperOrderAnomalyFilter struct {
|
||||
requests.Pagination
|
||||
// ID 订单ID,精确匹配。
|
||||
ID *int64 `query:"id"`
|
||||
// TenantID 租户ID,精确匹配。
|
||||
TenantID *int64 `query:"tenant_id"`
|
||||
// TenantCode 租户编码,模糊匹配。
|
||||
TenantCode *string `query:"tenant_code"`
|
||||
// TenantName 租户名称,模糊匹配。
|
||||
TenantName *string `query:"tenant_name"`
|
||||
// UserID 用户ID,精确匹配。
|
||||
UserID *int64 `query:"user_id"`
|
||||
// Username 用户名/昵称,模糊匹配。
|
||||
Username *string `query:"username"`
|
||||
// Type 订单类型过滤。
|
||||
Type *consts.OrderType `query:"type"`
|
||||
// Issue 异常类型(missing_paid_at/missing_refunded_at)。
|
||||
Issue *string `query:"issue"`
|
||||
// CreatedAtFrom 创建时间起始(RFC3339)。
|
||||
CreatedAtFrom *string `query:"created_at_from"`
|
||||
// CreatedAtTo 创建时间结束(RFC3339)。
|
||||
CreatedAtTo *string `query:"created_at_to"`
|
||||
// Asc 升序字段(id/created_at/amount_paid)。
|
||||
Asc *string `query:"asc"`
|
||||
// Desc 降序字段(id/created_at/amount_paid)。
|
||||
Desc *string `query:"desc"`
|
||||
}
|
||||
|
||||
// SuperOrderAnomalyItem 订单异常项。
|
||||
type SuperOrderAnomalyItem struct {
|
||||
// OrderID 订单ID。
|
||||
OrderID int64 `json:"order_id"`
|
||||
// TenantID 租户ID。
|
||||
TenantID int64 `json:"tenant_id"`
|
||||
// TenantCode 租户编码。
|
||||
TenantCode string `json:"tenant_code"`
|
||||
// TenantName 租户名称。
|
||||
TenantName string `json:"tenant_name"`
|
||||
// UserID 用户ID。
|
||||
UserID int64 `json:"user_id"`
|
||||
// Username 用户名。
|
||||
Username string `json:"username"`
|
||||
// Type 订单类型。
|
||||
Type consts.OrderType `json:"type"`
|
||||
// Status 订单状态。
|
||||
Status consts.OrderStatus `json:"status"`
|
||||
// AmountPaid 实付金额(分)。
|
||||
AmountPaid int64 `json:"amount_paid"`
|
||||
// Issue 异常类型标识。
|
||||
Issue string `json:"issue"`
|
||||
// IssueDescription 异常描述说明。
|
||||
IssueDescription string `json:"issue_description"`
|
||||
// CreatedAt 创建时间(RFC3339)。
|
||||
CreatedAt string `json:"created_at"`
|
||||
// PaidAt 支付时间(RFC3339)。
|
||||
PaidAt string `json:"paid_at"`
|
||||
// RefundedAt 退款时间(RFC3339)。
|
||||
RefundedAt string `json:"refunded_at"`
|
||||
}
|
||||
60
backend/app/http/super/v1/finance.go
Normal file
60
backend/app/http/super/v1/finance.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
dto "quyun/v2/app/http/super/v1/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type finance struct{}
|
||||
|
||||
// List ledgers
|
||||
//
|
||||
// @Router /super/v1/finance/ledgers [get]
|
||||
// @Summary List ledgers
|
||||
// @Description List tenant ledgers across tenants
|
||||
// @Tags Finance
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "Page number"
|
||||
// @Param limit query int false "Page size"
|
||||
// @Success 200 {object} requests.Pager{items=[]dto.SuperLedgerItem}
|
||||
// @Bind filter query
|
||||
func (c *finance) ListLedgers(ctx fiber.Ctx, filter *dto.SuperLedgerListFilter) (*requests.Pager, error) {
|
||||
return services.Super.ListLedgers(ctx, filter)
|
||||
}
|
||||
|
||||
// List balance anomalies
|
||||
//
|
||||
// @Router /super/v1/finance/anomalies/balances [get]
|
||||
// @Summary List balance anomalies
|
||||
// @Description List balance anomalies across users
|
||||
// @Tags Finance
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "Page number"
|
||||
// @Param limit query int false "Page size"
|
||||
// @Success 200 {object} requests.Pager{items=[]dto.SuperBalanceAnomalyItem}
|
||||
// @Bind filter query
|
||||
func (c *finance) ListBalanceAnomalies(ctx fiber.Ctx, filter *dto.SuperBalanceAnomalyFilter) (*requests.Pager, error) {
|
||||
return services.Super.ListBalanceAnomalies(ctx, filter)
|
||||
}
|
||||
|
||||
// List order anomalies
|
||||
//
|
||||
// @Router /super/v1/finance/anomalies/orders [get]
|
||||
// @Summary List order anomalies
|
||||
// @Description List order anomalies across tenants
|
||||
// @Tags Finance
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "Page number"
|
||||
// @Param limit query int false "Page size"
|
||||
// @Success 200 {object} requests.Pager{items=[]dto.SuperOrderAnomalyItem}
|
||||
// @Bind filter query
|
||||
func (c *finance) ListOrderAnomalies(ctx fiber.Ctx, filter *dto.SuperOrderAnomalyFilter) (*requests.Pager, error) {
|
||||
return services.Super.ListOrderAnomalies(ctx, filter)
|
||||
}
|
||||
@@ -24,6 +24,13 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*comments, error) {
|
||||
obj := &comments{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*contents, error) {
|
||||
obj := &contents{}
|
||||
|
||||
@@ -52,6 +59,13 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*finance, error) {
|
||||
obj := &finance{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*notifications, error) {
|
||||
obj := ¬ifications{}
|
||||
|
||||
@@ -83,10 +97,12 @@ func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
assets *assets,
|
||||
auditLogs *auditLogs,
|
||||
comments *comments,
|
||||
contents *contents,
|
||||
coupons *coupons,
|
||||
creatorApplications *creatorApplications,
|
||||
creators *creators,
|
||||
finance *finance,
|
||||
middlewares *middlewares.Middlewares,
|
||||
notifications *notifications,
|
||||
orders *orders,
|
||||
@@ -100,10 +116,12 @@ func Provide(opts ...opt.Option) error {
|
||||
obj := &Routes{
|
||||
assets: assets,
|
||||
auditLogs: auditLogs,
|
||||
comments: comments,
|
||||
contents: contents,
|
||||
coupons: coupons,
|
||||
creatorApplications: creatorApplications,
|
||||
creators: creators,
|
||||
finance: finance,
|
||||
middlewares: middlewares,
|
||||
notifications: notifications,
|
||||
orders: orders,
|
||||
|
||||
@@ -27,10 +27,12 @@ type Routes struct {
|
||||
// Controller instances
|
||||
assets *assets
|
||||
auditLogs *auditLogs
|
||||
comments *comments
|
||||
contents *contents
|
||||
coupons *coupons
|
||||
creatorApplications *creatorApplications
|
||||
creators *creators
|
||||
finance *finance
|
||||
notifications *notifications
|
||||
orders *orders
|
||||
payoutAccounts *payoutAccounts
|
||||
@@ -79,6 +81,19 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
r.auditLogs.List,
|
||||
Query[dto.SuperAuditLogListFilter]("filter"),
|
||||
))
|
||||
// Register routes for controller: comments
|
||||
r.log.Debugf("Registering route: Get /super/v1/comments -> comments.List")
|
||||
router.Get("/super/v1/comments"[len(r.Path()):], DataFunc1(
|
||||
r.comments.List,
|
||||
Query[dto.SuperCommentListFilter]("filter"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /super/v1/comments/:id<int>/delete -> comments.Delete")
|
||||
router.Post("/super/v1/comments/:id<int>/delete"[len(r.Path()):], Func3(
|
||||
r.comments.Delete,
|
||||
Local[*models.User]("__ctx_user"),
|
||||
PathParam[int64]("id"),
|
||||
Body[dto.SuperCommentDeleteForm]("form"),
|
||||
))
|
||||
// Register routes for controller: contents
|
||||
r.log.Debugf("Registering route: Get /super/v1/contents -> contents.List")
|
||||
router.Get("/super/v1/contents"[len(r.Path()):], DataFunc1(
|
||||
@@ -186,6 +201,22 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
r.creators.List,
|
||||
Query[dto.TenantListFilter]("filter"),
|
||||
))
|
||||
// Register routes for controller: finance
|
||||
r.log.Debugf("Registering route: Get /super/v1/finance/anomalies/balances -> finance.ListBalanceAnomalies")
|
||||
router.Get("/super/v1/finance/anomalies/balances"[len(r.Path()):], DataFunc1(
|
||||
r.finance.ListBalanceAnomalies,
|
||||
Query[dto.SuperBalanceAnomalyFilter]("filter"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /super/v1/finance/anomalies/orders -> finance.ListOrderAnomalies")
|
||||
router.Get("/super/v1/finance/anomalies/orders"[len(r.Path()):], DataFunc1(
|
||||
r.finance.ListOrderAnomalies,
|
||||
Query[dto.SuperOrderAnomalyFilter]("filter"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /super/v1/finance/ledgers -> finance.ListLedgers")
|
||||
router.Get("/super/v1/finance/ledgers"[len(r.Path()):], DataFunc1(
|
||||
r.finance.ListLedgers,
|
||||
Query[dto.SuperLedgerListFilter]("filter"),
|
||||
))
|
||||
// Register routes for controller: notifications
|
||||
r.log.Debugf("Registering route: Get /super/v1/notifications -> notifications.List")
|
||||
router.Get("/super/v1/notifications"[len(r.Path()):], DataFunc1(
|
||||
|
||||
@@ -2311,6 +2311,306 @@ func (s *super) ListContents(ctx context.Context, filter *super_dto.SuperContent
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) ListComments(ctx context.Context, filter *super_dto.SuperCommentListFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &super_dto.SuperCommentListFilter{}
|
||||
}
|
||||
|
||||
tbl, q := models.CommentQuery.QueryContext(ctx)
|
||||
|
||||
if filter.ID != nil && *filter.ID > 0 {
|
||||
q = q.Where(tbl.ID.Eq(*filter.ID))
|
||||
}
|
||||
if filter.TenantID != nil && *filter.TenantID > 0 {
|
||||
q = q.Where(tbl.TenantID.Eq(*filter.TenantID))
|
||||
}
|
||||
if filter.ContentID != nil && *filter.ContentID > 0 {
|
||||
q = q.Where(tbl.ContentID.Eq(*filter.ContentID))
|
||||
}
|
||||
if filter.UserID != nil && *filter.UserID > 0 {
|
||||
q = q.Where(tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.Keyword != nil && strings.TrimSpace(*filter.Keyword) != "" {
|
||||
keyword := "%" + strings.TrimSpace(*filter.Keyword) + "%"
|
||||
q = q.Where(tbl.Content.Like(keyword))
|
||||
}
|
||||
|
||||
tenantIDs, tenantFilter, err := s.lookupTenantIDs(ctx, filter.TenantCode, filter.TenantName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tenantFilter {
|
||||
if len(tenantIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.TenantID.In(tenantIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
userIDs, userFilter, err := s.lookupUserIDs(ctx, filter.Username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if userFilter {
|
||||
if len(userIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.UserID.In(userIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
if filter.ContentTitle != nil && strings.TrimSpace(*filter.ContentTitle) != "" {
|
||||
contentTbl, contentQuery := models.ContentQuery.QueryContext(ctx)
|
||||
keyword := "%" + strings.TrimSpace(*filter.ContentTitle) + "%"
|
||||
contentQuery = contentQuery.Where(field.Or(
|
||||
contentTbl.Title.Like(keyword),
|
||||
contentTbl.Description.Like(keyword),
|
||||
contentTbl.Summary.Like(keyword),
|
||||
))
|
||||
if filter.TenantID != nil && *filter.TenantID > 0 {
|
||||
contentQuery = contentQuery.Where(contentTbl.TenantID.Eq(*filter.TenantID))
|
||||
}
|
||||
if tenantFilter {
|
||||
if len(tenantIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
contentQuery = contentQuery.Where(contentTbl.TenantID.In(tenantIDs...))
|
||||
}
|
||||
}
|
||||
contentIDs, err := contentQuery.Select(contentTbl.ID).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
ids := make([]int64, 0, len(contentIDs))
|
||||
for _, content := range contentIDs {
|
||||
ids = append(ids, content.ID)
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.ContentID.In(ids...))
|
||||
}
|
||||
}
|
||||
|
||||
status := ""
|
||||
if filter.Status != nil {
|
||||
status = strings.TrimSpace(*filter.Status)
|
||||
}
|
||||
switch status {
|
||||
case "deleted":
|
||||
q = q.Unscoped().Where(tbl.DeletedAt.IsNotNull())
|
||||
case "all":
|
||||
q = q.Unscoped()
|
||||
default:
|
||||
q = q.Where(tbl.DeletedAt.IsNull())
|
||||
}
|
||||
|
||||
if filter.CreatedAtFrom != nil {
|
||||
from, err := s.parseFilterTime(filter.CreatedAtFrom)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if from != nil {
|
||||
q = q.Where(tbl.CreatedAt.Gte(*from))
|
||||
}
|
||||
}
|
||||
if filter.CreatedAtTo != nil {
|
||||
to, err := s.parseFilterTime(filter.CreatedAtTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if to != nil {
|
||||
q = q.Where(tbl.CreatedAt.Lte(*to))
|
||||
}
|
||||
}
|
||||
|
||||
orderApplied := false
|
||||
if filter.Desc != nil && strings.TrimSpace(*filter.Desc) != "" {
|
||||
switch strings.TrimSpace(*filter.Desc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
case "likes":
|
||||
q = q.Order(tbl.Likes.Desc())
|
||||
}
|
||||
orderApplied = true
|
||||
} else if filter.Asc != nil && strings.TrimSpace(*filter.Asc) != "" {
|
||||
switch strings.TrimSpace(*filter.Asc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID)
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt)
|
||||
case "likes":
|
||||
q = q.Order(tbl.Likes)
|
||||
}
|
||||
orderApplied = true
|
||||
}
|
||||
if !orderApplied {
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
}
|
||||
|
||||
// 评论治理默认排除已删除内容,如需查看已删除请显式选择状态。
|
||||
filter.Pagination.Format()
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
list, err := q.Offset(int(filter.Pagination.Offset())).Limit(int(filter.Pagination.Limit)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
if len(list) == 0 {
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: 0,
|
||||
Items: []super_dto.SuperCommentItem{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
tenantSet := make(map[int64]struct{})
|
||||
userSet := make(map[int64]struct{})
|
||||
contentSet := make(map[int64]struct{})
|
||||
for _, comment := range list {
|
||||
tenantSet[comment.TenantID] = struct{}{}
|
||||
userSet[comment.UserID] = struct{}{}
|
||||
contentSet[comment.ContentID] = struct{}{}
|
||||
}
|
||||
|
||||
tenantIDs = make([]int64, 0, len(tenantSet))
|
||||
for id := range tenantSet {
|
||||
tenantIDs = append(tenantIDs, id)
|
||||
}
|
||||
userIDs = make([]int64, 0, len(userSet))
|
||||
for id := range userSet {
|
||||
userIDs = append(userIDs, id)
|
||||
}
|
||||
contentIDs := make([]int64, 0, len(contentSet))
|
||||
for id := range contentSet {
|
||||
contentIDs = append(contentIDs, id)
|
||||
}
|
||||
|
||||
tenantMap := make(map[int64]*models.Tenant, len(tenantIDs))
|
||||
if len(tenantIDs) > 0 {
|
||||
tenantTbl, tenantQuery := models.TenantQuery.QueryContext(ctx)
|
||||
tenants, err := tenantQuery.Where(tenantTbl.ID.In(tenantIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, tenant := range tenants {
|
||||
tenantMap[tenant.ID] = tenant
|
||||
}
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*models.User, len(userIDs))
|
||||
if len(userIDs) > 0 {
|
||||
userTbl, userQuery := models.UserQuery.QueryContext(ctx)
|
||||
users, err := userQuery.Where(userTbl.ID.In(userIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, user := range users {
|
||||
userMap[user.ID] = user
|
||||
}
|
||||
}
|
||||
|
||||
contentMap := make(map[int64]*models.Content, len(contentIDs))
|
||||
if len(contentIDs) > 0 {
|
||||
contentTbl, contentQuery := models.ContentQuery.QueryContext(ctx)
|
||||
contents, err := contentQuery.Where(contentTbl.ID.In(contentIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, content := range contents {
|
||||
contentMap[content.ID] = content
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]super_dto.SuperCommentItem, 0, len(list))
|
||||
for _, comment := range list {
|
||||
tenant := tenantMap[comment.TenantID]
|
||||
user := userMap[comment.UserID]
|
||||
content := contentMap[comment.ContentID]
|
||||
|
||||
username := ""
|
||||
if user != nil {
|
||||
if user.Username != "" {
|
||||
username = user.Username
|
||||
} else {
|
||||
username = user.Nickname
|
||||
}
|
||||
}
|
||||
|
||||
item := super_dto.SuperCommentItem{
|
||||
ID: comment.ID,
|
||||
TenantID: comment.TenantID,
|
||||
ContentID: comment.ContentID,
|
||||
UserID: comment.UserID,
|
||||
ReplyTo: comment.ReplyTo,
|
||||
Content: comment.Content,
|
||||
Likes: comment.Likes,
|
||||
IsDeleted: comment.DeletedAt.Valid,
|
||||
CreatedAt: s.formatTime(comment.CreatedAt),
|
||||
TenantCode: "",
|
||||
TenantName: "",
|
||||
ContentTitle: "",
|
||||
Username: username,
|
||||
DeletedAt: "",
|
||||
}
|
||||
if tenant != nil {
|
||||
item.TenantCode = tenant.Code
|
||||
item.TenantName = tenant.Name
|
||||
}
|
||||
if content != nil {
|
||||
item.ContentTitle = content.Title
|
||||
}
|
||||
if comment.DeletedAt.Valid {
|
||||
item.DeletedAt = s.formatTime(comment.DeletedAt.Time)
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) DeleteComment(ctx context.Context, operatorID, id int64, form *super_dto.SuperCommentDeleteForm) error {
|
||||
if operatorID == 0 {
|
||||
return errorx.ErrUnauthorized.WithMsg("缺少操作者信息")
|
||||
}
|
||||
|
||||
tbl, q := models.CommentQuery.QueryContext(ctx)
|
||||
comment, err := q.Where(tbl.ID.Eq(id)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errorx.ErrRecordNotFound
|
||||
}
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
// 评论删除采用软删除,便于审计追溯与误删恢复。
|
||||
if _, err := q.Where(tbl.ID.Eq(id)).Delete(); err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
if Audit != nil {
|
||||
reason := ""
|
||||
if form != nil {
|
||||
reason = strings.TrimSpace(form.Reason)
|
||||
}
|
||||
detail := "删除评论"
|
||||
if reason != "" {
|
||||
detail = "删除评论: " + reason
|
||||
}
|
||||
Audit.Log(ctx, comment.TenantID, operatorID, "delete_comment", cast.ToString(id), detail)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *super) UpdateContentStatus(ctx context.Context, tenantID, contentID int64, form *super_dto.SuperTenantContentStatusUpdateForm) error {
|
||||
tbl, q := models.ContentQuery.QueryContext(ctx)
|
||||
_, err := q.Where(tbl.ID.Eq(contentID), tbl.TenantID.Eq(tenantID)).Update(tbl.Status, consts.ContentStatus(form.Status))
|
||||
@@ -5192,6 +5492,519 @@ func (s *super) ListWithdrawals(ctx context.Context, filter *super_dto.SuperOrde
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) ListLedgers(ctx context.Context, filter *super_dto.SuperLedgerListFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &super_dto.SuperLedgerListFilter{}
|
||||
}
|
||||
|
||||
tbl, q := models.TenantLedgerQuery.QueryContext(ctx)
|
||||
if filter.ID != nil && *filter.ID > 0 {
|
||||
q = q.Where(tbl.ID.Eq(*filter.ID))
|
||||
}
|
||||
if filter.TenantID != nil && *filter.TenantID > 0 {
|
||||
q = q.Where(tbl.TenantID.Eq(*filter.TenantID))
|
||||
}
|
||||
if filter.UserID != nil && *filter.UserID > 0 {
|
||||
q = q.Where(tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.OrderID != nil && *filter.OrderID > 0 {
|
||||
q = q.Where(tbl.OrderID.Eq(*filter.OrderID))
|
||||
}
|
||||
if filter.Type != nil && *filter.Type != "" {
|
||||
q = q.Where(tbl.Type.Eq(*filter.Type))
|
||||
}
|
||||
if filter.AmountMin != nil {
|
||||
q = q.Where(tbl.Amount.Gte(*filter.AmountMin))
|
||||
}
|
||||
if filter.AmountMax != nil {
|
||||
q = q.Where(tbl.Amount.Lte(*filter.AmountMax))
|
||||
}
|
||||
|
||||
tenantIDs, tenantFilter, err := s.lookupTenantIDs(ctx, filter.TenantCode, filter.TenantName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tenantFilter {
|
||||
if len(tenantIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.TenantID.In(tenantIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
userIDs, userFilter, err := s.lookupUserIDs(ctx, filter.Username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if userFilter {
|
||||
if len(userIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.UserID.In(userIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
if filter.CreatedAtFrom != nil {
|
||||
from, err := s.parseFilterTime(filter.CreatedAtFrom)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if from != nil {
|
||||
q = q.Where(tbl.CreatedAt.Gte(*from))
|
||||
}
|
||||
}
|
||||
if filter.CreatedAtTo != nil {
|
||||
to, err := s.parseFilterTime(filter.CreatedAtTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if to != nil {
|
||||
q = q.Where(tbl.CreatedAt.Lte(*to))
|
||||
}
|
||||
}
|
||||
|
||||
orderApplied := false
|
||||
if filter.Desc != nil && strings.TrimSpace(*filter.Desc) != "" {
|
||||
switch strings.TrimSpace(*filter.Desc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
case "amount":
|
||||
q = q.Order(tbl.Amount.Desc())
|
||||
}
|
||||
orderApplied = true
|
||||
} else if filter.Asc != nil && strings.TrimSpace(*filter.Asc) != "" {
|
||||
switch strings.TrimSpace(*filter.Asc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID)
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt)
|
||||
case "amount":
|
||||
q = q.Order(tbl.Amount)
|
||||
}
|
||||
orderApplied = true
|
||||
}
|
||||
if !orderApplied {
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
}
|
||||
|
||||
// 超管资金流水用于跨租户排查收支与异常变化。
|
||||
filter.Pagination.Format()
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
list, err := q.Offset(int(filter.Pagination.Offset())).Limit(int(filter.Pagination.Limit)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
tenantSet := make(map[int64]struct{})
|
||||
userSet := make(map[int64]struct{})
|
||||
for _, ledger := range list {
|
||||
if ledger.TenantID > 0 {
|
||||
tenantSet[ledger.TenantID] = struct{}{}
|
||||
}
|
||||
if ledger.UserID > 0 {
|
||||
userSet[ledger.UserID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
tenantIDs = make([]int64, 0, len(tenantSet))
|
||||
for id := range tenantSet {
|
||||
tenantIDs = append(tenantIDs, id)
|
||||
}
|
||||
userIDs = make([]int64, 0, len(userSet))
|
||||
for id := range userSet {
|
||||
userIDs = append(userIDs, id)
|
||||
}
|
||||
|
||||
tenantMap := make(map[int64]*models.Tenant, len(tenantIDs))
|
||||
if len(tenantIDs) > 0 {
|
||||
tenantTbl, tenantQuery := models.TenantQuery.QueryContext(ctx)
|
||||
tenants, err := tenantQuery.Where(tenantTbl.ID.In(tenantIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, tenant := range tenants {
|
||||
tenantMap[tenant.ID] = tenant
|
||||
}
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*models.User, len(userIDs))
|
||||
if len(userIDs) > 0 {
|
||||
userTbl, userQuery := models.UserQuery.QueryContext(ctx)
|
||||
users, err := userQuery.Where(userTbl.ID.In(userIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, user := range users {
|
||||
userMap[user.ID] = user
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]super_dto.SuperLedgerItem, 0, len(list))
|
||||
for _, ledger := range list {
|
||||
item := super_dto.SuperLedgerItem{
|
||||
ID: ledger.ID,
|
||||
TenantID: ledger.TenantID,
|
||||
UserID: ledger.UserID,
|
||||
OrderID: ledger.OrderID,
|
||||
Type: ledger.Type,
|
||||
TypeDescription: ledger.Type.Description(),
|
||||
Amount: ledger.Amount,
|
||||
BalanceBefore: ledger.BalanceBefore,
|
||||
BalanceAfter: ledger.BalanceAfter,
|
||||
FrozenBefore: ledger.FrozenBefore,
|
||||
FrozenAfter: ledger.FrozenAfter,
|
||||
Remark: ledger.Remark,
|
||||
OperatorUserID: ledger.OperatorUserID,
|
||||
BizRefType: ledger.BizRefType,
|
||||
BizRefID: ledger.BizRefID,
|
||||
CreatedAt: s.formatTime(ledger.CreatedAt),
|
||||
UpdatedAt: s.formatTime(ledger.UpdatedAt),
|
||||
TenantCode: "",
|
||||
TenantName: "",
|
||||
Username: "",
|
||||
}
|
||||
if tenant := tenantMap[ledger.TenantID]; tenant != nil {
|
||||
item.TenantCode = tenant.Code
|
||||
item.TenantName = tenant.Name
|
||||
}
|
||||
if user := userMap[ledger.UserID]; user != nil {
|
||||
if user.Username != "" {
|
||||
item.Username = user.Username
|
||||
} else {
|
||||
item.Username = user.Nickname
|
||||
}
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) ListBalanceAnomalies(ctx context.Context, filter *super_dto.SuperBalanceAnomalyFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &super_dto.SuperBalanceAnomalyFilter{}
|
||||
}
|
||||
|
||||
tbl, q := models.UserQuery.QueryContext(ctx)
|
||||
if filter.UserID != nil && *filter.UserID > 0 {
|
||||
q = q.Where(tbl.ID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.Username != nil && strings.TrimSpace(*filter.Username) != "" {
|
||||
keyword := "%" + strings.TrimSpace(*filter.Username) + "%"
|
||||
q = q.Where(field.Or(tbl.Username.Like(keyword), tbl.Nickname.Like(keyword)))
|
||||
}
|
||||
|
||||
issue := ""
|
||||
if filter.Issue != nil {
|
||||
issue = strings.TrimSpace(*filter.Issue)
|
||||
}
|
||||
switch issue {
|
||||
case "negative_balance":
|
||||
q = q.Where(tbl.Balance.Lt(0))
|
||||
case "negative_frozen":
|
||||
q = q.Where(tbl.BalanceFrozen.Lt(0))
|
||||
default:
|
||||
q = q.Where(field.Or(tbl.Balance.Lt(0), tbl.BalanceFrozen.Lt(0)))
|
||||
}
|
||||
|
||||
orderApplied := false
|
||||
if filter.Desc != nil && strings.TrimSpace(*filter.Desc) != "" {
|
||||
switch strings.TrimSpace(*filter.Desc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
case "balance":
|
||||
q = q.Order(tbl.Balance.Desc())
|
||||
case "balance_frozen":
|
||||
q = q.Order(tbl.BalanceFrozen.Desc())
|
||||
}
|
||||
orderApplied = true
|
||||
} else if filter.Asc != nil && strings.TrimSpace(*filter.Asc) != "" {
|
||||
switch strings.TrimSpace(*filter.Asc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID)
|
||||
case "balance":
|
||||
q = q.Order(tbl.Balance)
|
||||
case "balance_frozen":
|
||||
q = q.Order(tbl.BalanceFrozen)
|
||||
}
|
||||
orderApplied = true
|
||||
}
|
||||
if !orderApplied {
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
}
|
||||
|
||||
// 余额异常用于发现负余额或冻结异常的账号。
|
||||
filter.Pagination.Format()
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
users, err := q.Offset(int(filter.Pagination.Offset())).Limit(int(filter.Pagination.Limit)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
items := make([]super_dto.SuperBalanceAnomalyItem, 0, len(users))
|
||||
for _, user := range users {
|
||||
itemIssue := "negative_balance"
|
||||
itemDesc := "可用余额为负"
|
||||
if user.Balance < 0 && user.BalanceFrozen < 0 {
|
||||
itemIssue = "negative_balance"
|
||||
itemDesc = "可用余额与冻结余额均为负"
|
||||
} else if user.BalanceFrozen < 0 {
|
||||
itemIssue = "negative_frozen"
|
||||
itemDesc = "冻结余额为负"
|
||||
}
|
||||
|
||||
username := user.Username
|
||||
if username == "" {
|
||||
username = user.Nickname
|
||||
}
|
||||
items = append(items, super_dto.SuperBalanceAnomalyItem{
|
||||
UserID: user.ID,
|
||||
Username: username,
|
||||
Balance: user.Balance,
|
||||
BalanceFrozen: user.BalanceFrozen,
|
||||
Issue: itemIssue,
|
||||
IssueDescription: itemDesc,
|
||||
CreatedAt: s.formatTime(user.CreatedAt),
|
||||
})
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) ListOrderAnomalies(ctx context.Context, filter *super_dto.SuperOrderAnomalyFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &super_dto.SuperOrderAnomalyFilter{}
|
||||
}
|
||||
|
||||
tbl, q := models.OrderQuery.QueryContext(ctx)
|
||||
if filter.ID != nil && *filter.ID > 0 {
|
||||
q = q.Where(tbl.ID.Eq(*filter.ID))
|
||||
}
|
||||
if filter.TenantID != nil && *filter.TenantID > 0 {
|
||||
q = q.Where(tbl.TenantID.Eq(*filter.TenantID))
|
||||
}
|
||||
if filter.UserID != nil && *filter.UserID > 0 {
|
||||
q = q.Where(tbl.UserID.Eq(*filter.UserID))
|
||||
}
|
||||
if filter.Type != nil && *filter.Type != "" {
|
||||
q = q.Where(tbl.Type.Eq(*filter.Type))
|
||||
}
|
||||
|
||||
tenantIDs, tenantFilter, err := s.lookupTenantIDs(ctx, filter.TenantCode, filter.TenantName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tenantFilter {
|
||||
if len(tenantIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.TenantID.In(tenantIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
userIDs, userFilter, err := s.lookupUserIDs(ctx, filter.Username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if userFilter {
|
||||
if len(userIDs) == 0 {
|
||||
q = q.Where(tbl.ID.Eq(-1))
|
||||
} else {
|
||||
q = q.Where(tbl.UserID.In(userIDs...))
|
||||
}
|
||||
}
|
||||
|
||||
if filter.CreatedAtFrom != nil {
|
||||
from, err := s.parseFilterTime(filter.CreatedAtFrom)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if from != nil {
|
||||
q = q.Where(tbl.CreatedAt.Gte(*from))
|
||||
}
|
||||
}
|
||||
if filter.CreatedAtTo != nil {
|
||||
to, err := s.parseFilterTime(filter.CreatedAtTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if to != nil {
|
||||
q = q.Where(tbl.CreatedAt.Lte(*to))
|
||||
}
|
||||
}
|
||||
|
||||
issue := ""
|
||||
if filter.Issue != nil {
|
||||
issue = strings.TrimSpace(*filter.Issue)
|
||||
}
|
||||
zeroTime := time.Time{}
|
||||
missingPaid := field.Or(tbl.PaidAt.IsNull(), tbl.PaidAt.Lte(zeroTime))
|
||||
missingRefund := field.Or(tbl.RefundedAt.IsNull(), tbl.RefundedAt.Lte(zeroTime))
|
||||
switch issue {
|
||||
case "missing_paid_at":
|
||||
q = q.Where(tbl.Status.Eq(consts.OrderStatusPaid), missingPaid)
|
||||
case "missing_refunded_at":
|
||||
q = q.Where(tbl.Status.Eq(consts.OrderStatusRefunded), missingRefund)
|
||||
default:
|
||||
q = q.Where(field.Or(
|
||||
field.And(tbl.Status.Eq(consts.OrderStatusPaid), missingPaid),
|
||||
field.And(tbl.Status.Eq(consts.OrderStatusRefunded), missingRefund),
|
||||
))
|
||||
}
|
||||
|
||||
orderApplied := false
|
||||
if filter.Desc != nil && strings.TrimSpace(*filter.Desc) != "" {
|
||||
switch strings.TrimSpace(*filter.Desc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
case "amount_paid":
|
||||
q = q.Order(tbl.AmountPaid.Desc())
|
||||
}
|
||||
orderApplied = true
|
||||
} else if filter.Asc != nil && strings.TrimSpace(*filter.Asc) != "" {
|
||||
switch strings.TrimSpace(*filter.Asc) {
|
||||
case "id":
|
||||
q = q.Order(tbl.ID)
|
||||
case "created_at":
|
||||
q = q.Order(tbl.CreatedAt)
|
||||
case "amount_paid":
|
||||
q = q.Order(tbl.AmountPaid)
|
||||
}
|
||||
orderApplied = true
|
||||
}
|
||||
if !orderApplied {
|
||||
q = q.Order(tbl.ID.Desc())
|
||||
}
|
||||
|
||||
filter.Pagination.Format()
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
list, err := q.Offset(int(filter.Pagination.Offset())).Limit(int(filter.Pagination.Limit)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
tenantSet := make(map[int64]struct{})
|
||||
userSet := make(map[int64]struct{})
|
||||
for _, order := range list {
|
||||
if order.TenantID > 0 {
|
||||
tenantSet[order.TenantID] = struct{}{}
|
||||
}
|
||||
if order.UserID > 0 {
|
||||
userSet[order.UserID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
tenantIDs = make([]int64, 0, len(tenantSet))
|
||||
for id := range tenantSet {
|
||||
tenantIDs = append(tenantIDs, id)
|
||||
}
|
||||
userIDs = make([]int64, 0, len(userSet))
|
||||
for id := range userSet {
|
||||
userIDs = append(userIDs, id)
|
||||
}
|
||||
|
||||
tenantMap := make(map[int64]*models.Tenant, len(tenantIDs))
|
||||
if len(tenantIDs) > 0 {
|
||||
tenantTbl, tenantQuery := models.TenantQuery.QueryContext(ctx)
|
||||
tenants, err := tenantQuery.Where(tenantTbl.ID.In(tenantIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, tenant := range tenants {
|
||||
tenantMap[tenant.ID] = tenant
|
||||
}
|
||||
}
|
||||
|
||||
userMap := make(map[int64]*models.User, len(userIDs))
|
||||
if len(userIDs) > 0 {
|
||||
userTbl, userQuery := models.UserQuery.QueryContext(ctx)
|
||||
users, err := userQuery.Where(userTbl.ID.In(userIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
for _, user := range users {
|
||||
userMap[user.ID] = user
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]super_dto.SuperOrderAnomalyItem, 0, len(list))
|
||||
for _, order := range list {
|
||||
itemIssue := "missing_paid_at"
|
||||
itemDesc := "已支付但缺失支付时间"
|
||||
if order.Status == consts.OrderStatusRefunded && order.RefundedAt.IsZero() {
|
||||
itemIssue = "missing_refunded_at"
|
||||
itemDesc = "已退款但缺失退款时间"
|
||||
}
|
||||
|
||||
tenant := tenantMap[order.TenantID]
|
||||
user := userMap[order.UserID]
|
||||
username := ""
|
||||
if user != nil {
|
||||
if user.Username != "" {
|
||||
username = user.Username
|
||||
} else {
|
||||
username = user.Nickname
|
||||
}
|
||||
}
|
||||
|
||||
item := super_dto.SuperOrderAnomalyItem{
|
||||
OrderID: order.ID,
|
||||
TenantID: order.TenantID,
|
||||
UserID: order.UserID,
|
||||
Type: order.Type,
|
||||
Status: order.Status,
|
||||
AmountPaid: order.AmountPaid,
|
||||
Issue: itemIssue,
|
||||
IssueDescription: itemDesc,
|
||||
CreatedAt: s.formatTime(order.CreatedAt),
|
||||
PaidAt: "",
|
||||
RefundedAt: "",
|
||||
TenantCode: "",
|
||||
TenantName: "",
|
||||
Username: username,
|
||||
}
|
||||
|
||||
if tenant != nil {
|
||||
item.TenantCode = tenant.Code
|
||||
item.TenantName = tenant.Name
|
||||
}
|
||||
if !order.PaidAt.IsZero() {
|
||||
item.PaidAt = s.formatTime(order.PaidAt)
|
||||
}
|
||||
if !order.RefundedAt.IsZero() {
|
||||
item.RefundedAt = s.formatTime(order.RefundedAt)
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *super) ListCoupons(ctx context.Context, filter *super_dto.SuperCouponListFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &super_dto.SuperCouponListFilter{}
|
||||
|
||||
@@ -220,6 +220,122 @@ func (s *SuperTestSuite) Test_WithdrawalApproval() {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperTestSuite) Test_CommentGovernance() {
|
||||
Convey("Comment Governance", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
database.Truncate(ctx, s.DB, models.TableNameComment, models.TableNameContent, models.TableNameTenant, models.TableNameUser)
|
||||
|
||||
owner := &models.User{Username: "owner_comment"}
|
||||
commenter := &models.User{Username: "commenter"}
|
||||
admin := &models.User{Username: "admin_comment"}
|
||||
models.UserQuery.WithContext(ctx).Create(owner, commenter, admin)
|
||||
|
||||
tenant := &models.Tenant{UserID: owner.ID, Code: "t-comment", Name: "Comment Tenant", Status: consts.TenantStatusVerified}
|
||||
models.TenantQuery.WithContext(ctx).Create(tenant)
|
||||
|
||||
content := &models.Content{
|
||||
TenantID: tenant.ID,
|
||||
UserID: owner.ID,
|
||||
Title: "Comment Content",
|
||||
Description: "Desc",
|
||||
}
|
||||
models.ContentQuery.WithContext(ctx).Create(content)
|
||||
|
||||
Convey("should list comments", func() {
|
||||
comment := &models.Comment{
|
||||
TenantID: tenant.ID,
|
||||
UserID: commenter.ID,
|
||||
ContentID: content.ID,
|
||||
Content: "Nice work",
|
||||
}
|
||||
models.CommentQuery.WithContext(ctx).Create(comment)
|
||||
|
||||
filter := &super_dto.SuperCommentListFilter{
|
||||
Pagination: requests.Pagination{Page: 1, Limit: 10},
|
||||
}
|
||||
res, err := Super.ListComments(ctx, filter)
|
||||
So(err, ShouldBeNil)
|
||||
So(res.Total, ShouldEqual, 1)
|
||||
items := res.Items.([]super_dto.SuperCommentItem)
|
||||
So(items[0].ContentTitle, ShouldEqual, "Comment Content")
|
||||
So(items[0].Username, ShouldEqual, commenter.Username)
|
||||
})
|
||||
|
||||
Convey("should delete comment", func() {
|
||||
comment := &models.Comment{
|
||||
TenantID: tenant.ID,
|
||||
UserID: commenter.ID,
|
||||
ContentID: content.ID,
|
||||
Content: "Spam content",
|
||||
}
|
||||
models.CommentQuery.WithContext(ctx).Create(comment)
|
||||
|
||||
err := Super.DeleteComment(ctx, admin.ID, comment.ID, &super_dto.SuperCommentDeleteForm{Reason: "spam"})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
deleted, err := models.CommentQuery.WithContext(ctx).Unscoped().Where(models.CommentQuery.ID.Eq(comment.ID)).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(deleted.DeletedAt.Valid, ShouldBeTrue)
|
||||
|
||||
filter := &super_dto.SuperCommentListFilter{
|
||||
Pagination: requests.Pagination{Page: 1, Limit: 10},
|
||||
}
|
||||
res, err := Super.ListComments(ctx, filter)
|
||||
So(err, ShouldBeNil)
|
||||
So(res.Total, ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperTestSuite) Test_FinanceAnomalies() {
|
||||
Convey("Finance Anomalies", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
database.Truncate(ctx, s.DB, models.TableNameOrder, models.TableNameTenant, models.TableNameUser)
|
||||
|
||||
user := &models.User{Username: "finance_user", Balance: -100}
|
||||
models.UserQuery.WithContext(ctx).Create(user)
|
||||
|
||||
tenant := &models.Tenant{UserID: user.ID, Code: "t-fin", Name: "Finance Tenant", Status: consts.TenantStatusVerified}
|
||||
models.TenantQuery.WithContext(ctx).Create(tenant)
|
||||
|
||||
order := &models.Order{
|
||||
TenantID: tenant.ID,
|
||||
UserID: user.ID,
|
||||
Type: consts.OrderTypeRecharge,
|
||||
Status: consts.OrderStatusPaid,
|
||||
AmountOriginal: 100,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 100,
|
||||
IdempotencyKey: "anomaly-paid",
|
||||
}
|
||||
models.OrderQuery.WithContext(ctx).Create(order)
|
||||
|
||||
Convey("should list balance anomalies", func() {
|
||||
filter := &super_dto.SuperBalanceAnomalyFilter{
|
||||
Pagination: requests.Pagination{Page: 1, Limit: 10},
|
||||
}
|
||||
res, err := Super.ListBalanceAnomalies(ctx, filter)
|
||||
So(err, ShouldBeNil)
|
||||
So(res.Total, ShouldEqual, 1)
|
||||
items := res.Items.([]super_dto.SuperBalanceAnomalyItem)
|
||||
So(items[0].UserID, ShouldEqual, user.ID)
|
||||
So(items[0].Issue, ShouldEqual, "negative_balance")
|
||||
})
|
||||
|
||||
Convey("should list order anomalies", func() {
|
||||
filter := &super_dto.SuperOrderAnomalyFilter{
|
||||
Pagination: requests.Pagination{Page: 1, Limit: 10},
|
||||
}
|
||||
res, err := Super.ListOrderAnomalies(ctx, filter)
|
||||
So(err, ShouldBeNil)
|
||||
So(res.Total, ShouldEqual, 1)
|
||||
items := res.Items.([]super_dto.SuperOrderAnomalyItem)
|
||||
So(items[0].OrderID, ShouldEqual, order.ID)
|
||||
So(items[0].Issue, ShouldEqual, "missing_paid_at")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperTestSuite) Test_TenantHealth() {
|
||||
Convey("TenantHealth", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
|
||||
Reference in New Issue
Block a user