system_delete_article_logic.go 3.5 KB
package article

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/message"
	"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/cmd/discuss/interanl/pkg/gateway/authlib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
	"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 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)
	}
	//文章内容
	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)
	}
	userToken := contextdata.GetUserTokenFromCtx(l.ctx)
	if userToken.CompanyId != article.CompanyId {
		return nil, xerr.NewErrMsg("您没有权限")
	}
	// 获取当前用户信息
	userMe, err := l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{Token: req.AccessToken})
	if err != nil {
		return nil, xerr.NewErrMsgErr("获取当前用户信息失败", err)
	}
	operator := domain.UserSimple{
		Id:        userToken.UserId,
		Name:      userMe.User.NickName,
		Avatar:    userMe.User.Avatar,
		CompanyId: userToken.CompanyId,
		Company:   userMe.CurrentCompany.Name,
	}
	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, conn, article)
		if err != nil {
			return err
		}
		var backup domain.ArticleBackup
		backup.MakeBackup(operator, article, sectionList, "删除")
		_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, conn, &backup)
		if err != nil {
			return xerr.NewErrMsgErr("保存文章内容失败", err)
		}
		//消息通知
		messageLogic := message.NewMiniSystemLogic(l.ctx, l.svcCtx)
		mTime := time.Unix(article.CreatedAt, 0).Format("2006-01-02 15:04")
		err = messageLogic.ArticleDeleted(conn, article.CompanyId, article.AuthorId, mTime, article.Title)
		if err != nil {
			return xerr.NewErrMsgErr("删除失败", 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
}