feat: 新增养成管理后台

This commit is contained in:
richjohnrambo
2026-09-19 14:36:30 +08:00
parent 7c8b4d9b7c
commit f6766d3dbd
59 changed files with 3001 additions and 0 deletions
@@ -0,0 +1,324 @@
package sys
import (
"context"
"database/sql"
"errors"
"strconv"
"strings"
"time"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"hotgo/internal/dao"
"hotgo/internal/library/contexts"
"hotgo/internal/library/imtenant"
"hotgo/internal/model/entity"
"hotgo/internal/model/input/sysin"
"hotgo/internal/service"
)
type sSysCultivateContent struct{}
func NewSysCultivateContent() *sSysCultivateContent { return &sSysCultivateContent{} }
func init() { service.RegisterSysCultivateContent(NewSysCultivateContent()) }
func (s *sSysCultivateContent) List(ctx context.Context, in *sysin.CultivateContentListInp) (list []*sysin.CultivateContentListModel, totalCount int, err error) {
tenantID := cultivateTenantID(ctx)
if in.ObjectType == "feed_comment" {
columns := dao.FeedComment.Columns()
mod := dao.FeedComment.Ctx(ctx).Where(columns.TenantId, tenantID)
if in.AuthorId != "" {
mod = mod.Where(columns.AuthorId, in.AuthorId)
}
if in.Status != "" {
mod = mod.Where(columns.Status, in.Status)
}
if in.Keyword != "" {
mod = mod.WhereLike(columns.ContentText, "%"+strings.TrimSpace(in.Keyword)+"%")
}
if len(in.CreatedAt) == 2 {
mod = mod.WhereBetween(columns.CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
}
var rows []*entity.FeedComment
err = mod.Page(in.Page, in.PerPage).OrderDesc(columns.Id).ScanAndCount(&rows, &totalCount, false)
if err != nil {
return nil, 0, gerror.Wrap(err, "获取评论审核列表失败")
}
list = make([]*sysin.CultivateContentListModel, 0, len(rows))
for _, row := range rows {
list = append(list, &sysin.CultivateContentListModel{Id: strconv.FormatUint(row.Id, 10), ObjectType: "feed_comment", PostId: strconv.FormatUint(row.PostId, 10), AuthorId: strconv.FormatUint(row.AuthorId, 10), ContentText: row.ContentText, Status: row.Status, AuditStatus: row.AuditStatus, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt})
}
return
}
columns := dao.FeedPost.Columns()
mod := dao.FeedPost.Ctx(ctx).Where(columns.TenantId, tenantID)
if in.AuthorId != "" {
mod = mod.Where(columns.AuthorId, in.AuthorId)
}
if in.Status != "" {
mod = mod.Where(columns.Status, in.Status)
}
if in.Keyword != "" {
mod = mod.WhereLike(columns.ContentText, "%"+strings.TrimSpace(in.Keyword)+"%")
}
if len(in.CreatedAt) == 2 {
mod = mod.WhereBetween(columns.CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
}
var rows []*entity.FeedPost
err = mod.Page(in.Page, in.PerPage).OrderDesc(columns.Id).ScanAndCount(&rows, &totalCount, false)
if err != nil {
return nil, 0, gerror.Wrap(err, "获取动态审核列表失败")
}
list = make([]*sysin.CultivateContentListModel, 0, len(rows))
for _, row := range rows {
list = append(list, &sysin.CultivateContentListModel{Id: strconv.FormatUint(row.Id, 10), ObjectType: "feed_post", AuthorId: strconv.FormatUint(row.AuthorId, 10), ContentText: row.ContentText, FlowerCode: row.FlowerCode, Visibility: row.Visibility, Status: row.Status, AuditStatus: row.AuditStatus, RejectReason: row.RejectReasonCode, LikeCount: row.LikeCount, CommentCount: row.CommentCount, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt})
}
return
}
func (s *sSysCultivateContent) Review(ctx context.Context, in *sysin.CultivateContentReviewInp) error {
tenantID, operatorID := cultivateTenantID(ctx), contexts.GetUserId(ctx)
return dao.FeedPost.Transaction(ctx, func(txCtx context.Context, tx gdb.TX) error {
if in.ObjectType == "feed_comment" {
return s.reviewComment(txCtx, tx, tenantID, operatorID, in)
}
return s.reviewPost(txCtx, tx, tenantID, operatorID, in)
})
}
func (s *sSysCultivateContent) reviewPost(ctx context.Context, tx gdb.TX, tenantID, operatorID int64, in *sysin.CultivateContentReviewInp) error {
columns := dao.FeedPost.Columns()
var before entity.FeedPost
err := tx.Model(dao.FeedPost.Table()).Where(g.Map{columns.Id: in.ObjectId, columns.TenantId: tenantID}).LockUpdate().Scan(&before)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
if before.Id == 0 {
return gerror.New("动态不存在")
}
now := time.Now().UTC()
data := g.Map{columns.UpdatedAt: now, columns.Version: gdb.Raw(columns.Version + "+1")}
afterStatus := before.Status
switch in.Action {
case "approve":
if before.Status != "reviewing" && before.Status != "rejected" {
return gerror.New("仅待审核或已拒绝动态可审核通过")
}
afterStatus = "published"
data[columns.Status] = afterStatus
data[columns.AuditStatus] = "approved"
data[columns.RejectReasonCode] = ""
if before.PublishedAt == nil {
data[columns.PublishedAt] = now
}
case "reject":
if before.Status != "reviewing" {
return gerror.New("仅待审核动态可拒绝")
}
afterStatus = "rejected"
data[columns.Status] = afterStatus
data[columns.AuditStatus] = "rejected"
data[columns.RejectReasonCode] = in.Reason
case "take_down":
if before.Status != "published" {
return gerror.New("仅已发布动态可下架")
}
afterStatus = "taken_down"
data[columns.Status] = afterStatus
data[columns.TakenDownAt] = now
data[columns.RejectReasonCode] = in.Reason
case "restore":
if before.Status != "taken_down" {
return gerror.New("仅已下架动态可恢复")
}
afterStatus = "published"
data[columns.Status] = afterStatus
data[columns.TakenDownAt] = nil
data[columns.RejectReasonCode] = ""
}
if _, err = tx.Model(dao.FeedPost.Table()).Where(columns.Id, before.Id).Data(data).Update(); err != nil {
return gerror.Wrap(err, "更新动态审核状态失败")
}
if err = writeContentAudit(ctx, tx, tenantID, operatorID, "feed_post", before.Id, before.Status, afterStatus, in.Action, now); err != nil {
return err
}
return writeCultivateOperation(ctx, tx, tenantID, operatorID, "content."+in.Action, "feed_post", in.Id, before, data, in.Reason, now)
}
func (s *sSysCultivateContent) reviewComment(ctx context.Context, tx gdb.TX, tenantID, operatorID int64, in *sysin.CultivateContentReviewInp) error {
columns := dao.FeedComment.Columns()
var before entity.FeedComment
err := tx.Model(dao.FeedComment.Table()).Where(g.Map{columns.Id: in.ObjectId, columns.TenantId: tenantID}).LockUpdate().Scan(&before)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
if before.Id == 0 {
return gerror.New("评论不存在")
}
now := time.Now().UTC()
data := g.Map{columns.UpdatedAt: now}
afterStatus := before.Status
counterDelta := 0
switch in.Action {
case "approve":
if before.Status != "reviewing" && before.Status != "rejected" {
return gerror.New("仅待审核或已拒绝评论可审核通过")
}
afterStatus = "published"
data[columns.Status] = afterStatus
data[columns.AuditStatus] = "approved"
data[columns.PublishedAt] = now
counterDelta = 1
case "reject":
if before.Status != "reviewing" {
return gerror.New("仅待审核评论可拒绝")
}
afterStatus = "rejected"
data[columns.Status] = afterStatus
data[columns.AuditStatus] = "rejected"
data[columns.DeleteReason] = in.Reason
case "take_down":
if before.Status != "published" {
return gerror.New("仅已发布评论可下架")
}
afterStatus = "taken_down"
data[columns.Status] = afterStatus
data[columns.DeleteReason] = in.Reason
counterDelta = -1
case "restore":
if before.Status != "taken_down" {
return gerror.New("仅已下架评论可恢复")
}
afterStatus = "published"
data[columns.Status] = afterStatus
data[columns.DeleteReason] = ""
counterDelta = 1
}
if _, err = tx.Model(dao.FeedComment.Table()).Where(columns.Id, before.Id).Data(data).Update(); err != nil {
return gerror.Wrap(err, "更新评论审核状态失败")
}
if counterDelta != 0 {
postColumns := dao.FeedPost.Columns()
expression := "`comment_count` + 1"
if counterDelta < 0 {
expression = "GREATEST(`comment_count` - 1, 0)"
}
if _, err = tx.Model(dao.FeedPost.Table()).Where(g.Map{postColumns.Id: before.PostId, postColumns.TenantId: tenantID}).Data(g.Map{postColumns.CommentCount: gdb.Raw(expression), postColumns.UpdatedAt: now}).Update(); err != nil {
return gerror.Wrap(err, "更新动态评论数失败")
}
}
if err = writeContentAudit(ctx, tx, tenantID, operatorID, "feed_comment", before.Id, before.Status, afterStatus, in.Action, now); err != nil {
return err
}
return writeCultivateOperation(ctx, tx, tenantID, operatorID, "content."+in.Action, "feed_comment", in.Id, before, data, in.Reason, now)
}
func writeContentAudit(ctx context.Context, tx gdb.TX, tenantID, operatorID int64, objectType string, objectID uint64, before, after, result string, now time.Time) error {
id, err := imtenant.NextSnowflakeID(ctx)
if err != nil {
return err
}
columns := dao.ContentAuditLog.Columns()
_, err = tx.Model(dao.ContentAuditLog.Table()).Data(g.Map{columns.Id: id, columns.TenantId: tenantID, columns.ObjectType: objectType, columns.ObjectId: objectID, columns.AuditSource: "manual", columns.StatusBefore: before, columns.StatusAfter: after, columns.ResultCode: result, columns.RiskLabels: "[]", columns.OperatorId: operatorID, columns.OccurredAt: now, columns.CreatedAt: now}).Insert()
return err
}
func (s *sSysCultivateContent) ReportList(ctx context.Context, in *sysin.CultivateReportListInp) (list []*sysin.CultivateReportListModel, totalCount int, err error) {
columns := dao.FeedReport.Columns()
mod := dao.FeedReport.Ctx(ctx).Where(columns.TenantId, cultivateTenantID(ctx))
if in.ObjectType != "" {
mod = mod.Where(columns.ObjectType, in.ObjectType)
}
if in.Status != "" {
mod = mod.Where(columns.Status, in.Status)
}
if in.ReporterId != "" {
mod = mod.Where(columns.ReporterId, in.ReporterId)
}
if len(in.CreatedAt) == 2 {
mod = mod.WhereBetween(columns.CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
}
var rows []*entity.FeedReport
if err = mod.Page(in.Page, in.PerPage).OrderDesc(columns.Id).ScanAndCount(&rows, &totalCount, false); err != nil {
return nil, 0, gerror.Wrap(err, "获取举报列表失败")
}
list = make([]*sysin.CultivateReportListModel, 0, len(rows))
for _, row := range rows {
list = append(list, &sysin.CultivateReportListModel{Id: strconv.FormatUint(row.Id, 10), ReporterId: strconv.FormatUint(row.ReporterId, 10), ReportedUserId: strconv.FormatUint(row.ReportedUserId, 10), ObjectType: row.ObjectType, ObjectId: row.ObjectId, ReasonCode: row.ReasonCode, Description: row.Description, EvidenceFileIds: row.EvidenceFileIds, SourcePage: row.SourcePage, Status: row.Status, HandlerId: optionalUint(row.HandlerId), HandleResult: row.HandleResult, HandledAt: row.HandledAt, CreatedAt: row.CreatedAt})
}
return
}
func (s *sSysCultivateContent) ReportHandle(ctx context.Context, in *sysin.CultivateReportHandleInp) error {
tenantID, operatorID := cultivateTenantID(ctx), contexts.GetUserId(ctx)
return dao.FeedReport.Transaction(ctx, func(txCtx context.Context, tx gdb.TX) error {
columns := dao.FeedReport.Columns()
var before entity.FeedReport
err := tx.Model(dao.FeedReport.Table()).Where(g.Map{columns.Id: in.ReportId, columns.TenantId: tenantID}).LockUpdate().Scan(&before)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
if before.Id == 0 {
return gerror.New("举报记录不存在")
}
if before.Status == "resolved" || before.Status == "rejected" {
return gerror.New("举报已办结,不能重复处理")
}
now := time.Now().UTC()
status := in.Action
if status == "resolve" {
status = "resolved"
}
data := g.Map{columns.Status: status, columns.HandlerId: operatorID, columns.HandleResult: in.HandleResult, columns.UpdatedAt: now}
if status == "resolved" || status == "rejected" {
data[columns.HandledAt] = now
}
if _, err = tx.Model(dao.FeedReport.Table()).Where(columns.Id, before.Id).Data(data).Update(); err != nil {
return gerror.Wrap(err, "更新举报处理状态失败")
}
return writeCultivateOperation(txCtx, tx, tenantID, operatorID, "report."+in.Action, "feed_report", in.Id, before, data, in.HandleResult, now)
})
}
func (s *sSysCultivateContent) LogList(ctx context.Context, in *sysin.CultivateOperationLogListInp) (list []*sysin.CultivateOperationLogListModel, totalCount int, err error) {
columns := dao.AdminOperationLog.Columns()
mod := dao.AdminOperationLog.Ctx(ctx).Where(columns.TenantId, cultivateTenantID(ctx))
if in.OperatorId != "" {
mod = mod.Where(columns.OperatorId, in.OperatorId)
}
if in.OperationType != "" {
mod = mod.WhereLike(columns.OperationType, "%"+strings.TrimSpace(in.OperationType)+"%")
}
if in.TargetType != "" {
mod = mod.Where(columns.TargetType, in.TargetType)
}
if len(in.CreatedAt) == 2 {
mod = mod.WhereBetween(columns.CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
}
var rows []*entity.AdminOperationLog
if err = mod.Page(in.Page, in.PerPage).OrderDesc(columns.Id).ScanAndCount(&rows, &totalCount, false); err != nil {
return nil, 0, gerror.Wrap(err, "获取操作日志失败")
}
list = make([]*sysin.CultivateOperationLogListModel, 0, len(rows))
for _, row := range rows {
list = append(list, &sysin.CultivateOperationLogListModel{Id: strconv.FormatUint(row.Id, 10), OperatorId: strconv.FormatUint(row.OperatorId, 10), OperationType: row.OperationType, TargetType: row.TargetType, TargetId: row.TargetId, RequestId: row.RequestId, BeforeSnapshot: jsonValue(row.BeforeSnapshot), AfterSnapshot: jsonValue(row.AfterSnapshot), Reason: row.Reason, CreatedAt: row.CreatedAt})
}
return
}
func optionalUint(value uint64) string {
if value == 0 {
return ""
}
return strconv.FormatUint(value, 10)
}
func jsonValue(value *gjson.Json) any {
if value == nil {
return nil
}
return value
}