feat: add batch content governance actions
This commit is contained in:
@@ -3240,6 +3240,85 @@ func (s *super) BatchReviewContents(ctx context.Context, operatorID int64, form
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *super) BatchUpdateContentStatus(ctx context.Context, operatorID int64, form *super_dto.SuperContentBatchStatusForm) error {
|
||||
if operatorID == 0 {
|
||||
return errorx.ErrUnauthorized.WithMsg("缺少操作者信息")
|
||||
}
|
||||
if form == nil {
|
||||
return errorx.ErrBadRequest.WithMsg("处置参数不能为空")
|
||||
}
|
||||
|
||||
status := form.Status
|
||||
if status != consts.ContentStatusUnpublished && status != consts.ContentStatusBlocked {
|
||||
return errorx.ErrBadRequest.WithMsg("处置状态非法")
|
||||
}
|
||||
|
||||
// 去重并过滤非法ID,避免批量处置出现空指令。
|
||||
unique := make(map[int64]struct{})
|
||||
contentIDs := make([]int64, 0, len(form.ContentIDs))
|
||||
for _, id := range form.ContentIDs {
|
||||
if id <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := unique[id]; ok {
|
||||
continue
|
||||
}
|
||||
unique[id] = struct{}{}
|
||||
contentIDs = append(contentIDs, id)
|
||||
}
|
||||
if len(contentIDs) == 0 {
|
||||
return errorx.ErrBadRequest.WithMsg("内容ID不能为空")
|
||||
}
|
||||
|
||||
reason := strings.TrimSpace(form.Reason)
|
||||
var contents []*models.Content
|
||||
err := models.Q.Transaction(func(tx *models.Query) error {
|
||||
tbl, q := tx.Content.QueryContext(ctx)
|
||||
list, err := q.Where(tbl.ID.In(contentIDs...)).Find()
|
||||
if err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
if len(list) != len(contentIDs) {
|
||||
return errorx.ErrRecordNotFound.WithMsg("部分内容不存在")
|
||||
}
|
||||
|
||||
// 内容治理批量处置,统一更新状态与更新时间。
|
||||
updates := &models.Content{
|
||||
Status: status,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
if _, err := q.Where(tbl.ID.In(contentIDs...)).Updates(updates); err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
contents = list
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
title := "内容治理结果"
|
||||
detail := "内容已下架"
|
||||
action := "content_unpublish"
|
||||
if status == consts.ContentStatusBlocked {
|
||||
detail = "内容已封禁"
|
||||
action = "content_block"
|
||||
}
|
||||
if reason != "" {
|
||||
detail += ",原因:" + reason
|
||||
}
|
||||
for _, content := range contents {
|
||||
if Notification != nil {
|
||||
_ = Notification.Send(ctx, content.TenantID, content.UserID, string(consts.NotificationTypeAudit), title, detail)
|
||||
}
|
||||
if Audit != nil {
|
||||
Audit.Log(ctx, content.TenantID, operatorID, action, cast.ToString(content.ID), detail)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *super) ContentStatistics(ctx context.Context, filter *super_dto.SuperContentStatisticsFilter) (*super_dto.SuperContentStatisticsResponse, error) {
|
||||
// 统一统计时间范围与粒度,默认最近 7 天。
|
||||
reportFilter := &super_dto.SuperReportOverviewFilter{}
|
||||
|
||||
@@ -600,6 +600,51 @@ func (s *SuperTestSuite) Test_ContentReview() {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperTestSuite) Test_BatchUpdateContentStatus() {
|
||||
Convey("BatchUpdateContentStatus", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
database.Truncate(ctx, s.DB, models.TableNameUser, models.TableNameTenant, models.TableNameContent)
|
||||
|
||||
admin := &models.User{Username: "batch_admin"}
|
||||
owner := &models.User{Username: "batch_owner"}
|
||||
models.UserQuery.WithContext(ctx).Create(admin, owner)
|
||||
|
||||
tenant := &models.Tenant{
|
||||
UserID: owner.ID,
|
||||
Name: "Batch Tenant",
|
||||
Code: "batch",
|
||||
Status: consts.TenantStatusVerified,
|
||||
}
|
||||
models.TenantQuery.WithContext(ctx).Create(tenant)
|
||||
|
||||
content1 := &models.Content{
|
||||
TenantID: tenant.ID,
|
||||
UserID: owner.ID,
|
||||
Title: "Batch Content 1",
|
||||
Status: consts.ContentStatusPublished,
|
||||
}
|
||||
content2 := &models.Content{
|
||||
TenantID: tenant.ID,
|
||||
UserID: owner.ID,
|
||||
Title: "Batch Content 2",
|
||||
Status: consts.ContentStatusUnpublished,
|
||||
}
|
||||
models.ContentQuery.WithContext(ctx).Create(content1, content2)
|
||||
|
||||
err := Super.BatchUpdateContentStatus(ctx, admin.ID, &super_dto.SuperContentBatchStatusForm{
|
||||
ContentIDs: []int64{content1.ID, content2.ID},
|
||||
Status: consts.ContentStatusBlocked,
|
||||
Reason: "违规处理",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
reloaded1, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(content1.ID)).First()
|
||||
reloaded2, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(content2.ID)).First()
|
||||
So(reloaded1.Status, ShouldEqual, consts.ContentStatusBlocked)
|
||||
So(reloaded2.Status, ShouldEqual, consts.ContentStatusBlocked)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperTestSuite) Test_OrderGovernance() {
|
||||
Convey("OrderGovernance", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
|
||||
Reference in New Issue
Block a user