article_security_get_logic.go 1.8 KB
package secuirty

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"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 ArticleSecurityGetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *ArticleSecurityGetLogic) ArticleSecurityGet(req *types.ArticleSecurityGetRequest) (resp *types.ArticleSecurityGetResponse, err error) {
	var (
		conn     = l.svcCtx.DefaultDBConn()
		dm       *domain.ArticleSecurity
		reviewer *domain.User
		article  *domain.Article
		comment  *domain.ArticleComment
	)
	// 货号唯一
	if dm, err = l.svcCtx.ArticleSecurityRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErrMsgErr("不存在", err)
	}
	if dm.Reviewer > 0 {
		if reviewer, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, dm.Reviewer); err != nil {
			return nil, err
		}
	}
	resp = &types.ArticleSecurityGetResponse{
		ArticleSecurity: NewTypesArticleSecurity(dm, reviewer),
	}
	if dm.ContentType == domain.TypeArticle {
		if article, _ = l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, dm.ContentId); article != nil {
			resp.ArticleSecurity.Content.Text = article.Summary
		}
	} else if dm.ContentType == domain.TypeComment {
		if comment, _ = l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, dm.ContentId); comment != nil {
			resp.ArticleSecurity.Content.Text = comment.Content
		}
	}
	return
}