mini_article_delete_logic.go 3.1 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 MiniArticleDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	conn   transaction.Conn
}

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

func (l *MiniArticleDeleteLogic) MiniArticleDelete(req *types.MiniArticleDeleteRequest) (resp *types.MiniArticleDeleteResponse, 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.UserId != article.AuthorId || userToken.CompanyId != article.CompanyId {
		return nil, xerr.NewErrMsg("您没有权限删除")
	}
	// 检查发布人
	author, err := l.svcCtx.UserRepository.FindOne(l.ctx, l.conn, article.AuthorId)
	if err != nil {
		return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
	}
	companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, l.conn, author.CompanyId)
	if err != nil {
		return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
	}
	articleAuthor := domain.UserSimple{
		Id:        author.Id,
		Name:      author.Name,
		Avatar:    author.Avatar,
		Position:  author.Position,
		Company:   companyInfo.Name,
		CompanyId: author.CompanyId,
	}
	article.DeletedType = domain.ArticleDeletedTypeUser
	//文章内容详情
	queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("articleId", req.Id)
	_, sectionList, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, l.conn, queryOption)
	if err != nil {
		return nil, xerr.NewErrMsgErr("帖子不存在", err)
	}
	var backup domain.ArticleBackup
	backup.MakeBackup(articleAuthor, article, sectionList, "删除")
	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, conn, article)
		if err != nil {
			return err
		}
		//备份
		_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, conn, &backup)
		if err != nil {
			return xerr.NewErrMsgErr("删除失败", err)
		}
		return nil
	}, true)
	if err != nil {
		return nil, xerr.NewErrMsgErr("删除失败", err)
	}
	resp = &types.MiniArticleDeleteResponse{
		Id:    article.Id,
		Title: article.Title,
	}
	return
}