system_delete_article_logic.go 2.0 KB
package article

import (
	"context"
	"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/contextdata"
	"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 SystemDeleteArticleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	conn   transaction.Conn
}

func NewSystemDeleteArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemDeleteArticleLogic {
	return &SystemDeleteArticleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
		conn:   svcCtx.DefaultDBConn(),
	}
}

func (l *SystemDeleteArticleLogic) SystemDeleteArticle(req *types.SystemArticleDeleteRequest) (resp *types.SystemArticleDeleteResponse, err error) {
	// 文章数据
	article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, l.conn, req.Id)
	if err != nil {
		return nil, xerr.NewErrMsgErr("帖子不存在", err)
	}
	userToken := contextdata.GetUserTokenFromCtx(l.ctx)
	if userToken.CompanyId != article.CompanyId {
		return nil, xerr.NewErrMsg("您没有权限")
	}
	article.DeletedType = domain.ArticleDeletedTypeOperator
	err = transaction.UseTrans(l.ctx, l.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
		//更新删除类型
		_, err = l.svcCtx.ArticleRepository.Update(l.ctx, conn, article)
		if err != nil {
			return err
		}
		_, err = l.svcCtx.ArticleRepository.Delete(l.ctx, l.conn, article)
		if err != nil {
			return err
		}
		return nil
	}, true)
	if err != nil {
		return nil, xerr.NewErrMsgErr("删除失败", err)
	}
	resp = &types.SystemArticleDeleteResponse{
		Id:       article.Id,
		Title:    article.Title,
		AuthorId: article.AuthorId,
	}
	return
}