system_delete_article_logic.go
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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)
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
}