article_security_audit_logic.go 2.1 KB
package secuirty

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/core"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ArticleSecurityAuditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewArticleSecurityAuditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleSecurityAuditLogic {
	return &ArticleSecurityAuditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ArticleSecurityAuditLogic) ArticleSecurityAudit(req *types.ArticleSecurityAuditRequest) (resp *types.ArticleSecurityAuditResponse, err error) {
	var (
		conn = l.svcCtx.DefaultDBConn()
		dm   *domain.ArticleSecurity
	)
	if dm, err = l.svcCtx.ArticleSecurityRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErrMsgErr("不存在", err)
	}
	// 不可编辑判断
	if dm.ReviewStatus != domain.ReviewStatusWait {
		return nil, xerr.NewErrMsgErr("内容已审核", err)
	}
	// 赋值
	if req.Status == 1 {
		dm.ReviewStatus = domain.ReviewStatusPass
	} else {
		dm.ReviewStatus = domain.ReviewStatusFail
	}
	dm.ReviewAt = time.Now().Unix()
	// 更新
	if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
		dm, err = l.svcCtx.ArticleSecurityRepository.UpdateWithVersion(l.ctx, conn, dm)
		if err != nil {
			return err
		}
		if err = core.HandlerSecurityContent(l.ctx, l.svcCtx, conn, core.ContentBody{Id: dm.ContentId, Type: dm.ContentType}, dm.ReviewStatus); err != nil {
			return err
		}
		// 更新文章/评论可见
		return err
	}, true); err != nil {
		return nil, xerr.NewErrMsg("更新失败")
	}
	resp = &types.ArticleSecurityAuditResponse{}
	return
}