|
|
package article
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"github.com/samber/lo"
|
|
|
"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"
|
|
|
|
|
|
"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 SystemArticleRestoreLogic struct {
|
|
|
logx.Logger
|
|
|
ctx context.Context
|
|
|
svcCtx *svc.ServiceContext
|
|
|
}
|
|
|
|
|
|
func NewSystemArticleRestoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemArticleRestoreLogic {
|
|
|
return &SystemArticleRestoreLogic{
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
ctx: ctx,
|
|
|
svcCtx: svcCtx,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (l *SystemArticleRestoreLogic) SystemArticleRestore(req *types.SystemArticleRestoreRequest) (resp *types.SystemArticleRestoreResponse, err error) {
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
backup, err := l.svcCtx.ArticleBackupRepository.FindOne(l.ctx, conn, req.Id)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取编辑历史记录失败", err)
|
|
|
}
|
|
|
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, backup.ArticleId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取文章失败", err)
|
|
|
}
|
|
|
article.Version = article.Version + 1
|
|
|
article.Images = backup.Images
|
|
|
article.Title = backup.Title
|
|
|
articleSections := make([]domain.ArticleSection, 0)
|
|
|
lo.ForEach(backup.Section, func(item domain.ArticleSection, index int) {
|
|
|
articleSections = append(articleSections, domain.ArticleSection{
|
|
|
Id: item.Id,
|
|
|
CompanyId: item.CompanyId,
|
|
|
Version: article.Version,
|
|
|
ArticleId: article.Id,
|
|
|
Content: item.Content,
|
|
|
SortBy: item.SortBy,
|
|
|
})
|
|
|
})
|
|
|
//获取当前用户信息
|
|
|
userToken := contextdata.GetUserTokenFromCtx(l.ctx)
|
|
|
userMe, err := l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{Token: req.AccessToken})
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取当前用户信息失败", err)
|
|
|
}
|
|
|
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
//保存文章
|
|
|
_, err = l.svcCtx.ArticleRepository.Update(ctx, c, article)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("恢复文章版本失败", err)
|
|
|
}
|
|
|
//保存段落
|
|
|
updateSectionIds := []int64{}
|
|
|
for _, item := range articleSections {
|
|
|
section, err := l.svcCtx.ArticleSectionRepository.FindOne(ctx, c, item.Id)
|
|
|
if err == nil && section.Id > 0 {
|
|
|
section.Content = item.Content
|
|
|
section.SortBy = item.SortBy
|
|
|
section.Version = item.Version
|
|
|
_, err = l.svcCtx.ArticleSectionRepository.Update(ctx, c, section)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("恢复文章版本失败", err)
|
|
|
}
|
|
|
} else {
|
|
|
_, err = l.svcCtx.ArticleSectionRepository.Insert(ctx, c, &item)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("恢复文章版本失败", err)
|
|
|
}
|
|
|
}
|
|
|
updateSectionIds = append(updateSectionIds, item.Id)
|
|
|
}
|
|
|
if len(updateSectionIds) > 0 {
|
|
|
err = l.svcCtx.ArticleSectionRepository.DeleteBy(ctx, c, domain.NewQueryOptions().WithKV("articleId", article.Id).WithKV("notIds", updateSectionIds))
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("保存文章内容失败", err)
|
|
|
}
|
|
|
}
|
|
|
//备份数据
|
|
|
newBackUp := article.MakeBackup(domain.UserSimple{
|
|
|
Id: userToken.UserId,
|
|
|
Name: userMe.User.NickName,
|
|
|
Avatar: userMe.User.Avatar,
|
|
|
CompanyId: userToken.CompanyId,
|
|
|
Company: userMe.CurrentCompany.Name,
|
|
|
}, articleSections)
|
|
|
newBackUp.Action = "恢复"
|
|
|
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, newBackUp)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("恢复文章版本失败", err)
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
resp = &types.SystemArticleRestoreResponse{
|
|
|
Id: req.Id,
|
|
|
ArticleId: article.Id,
|
|
|
}
|
|
|
return
|
|
|
} |
...
|
...
|
|