|
|
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/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 SystemUpdateArticleLogic struct {
|
|
|
logx.Logger
|
|
|
ctx context.Context
|
|
|
svcCtx *svc.ServiceContext
|
|
|
}
|
|
|
|
|
|
func NewSystemUpdateArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUpdateArticleLogic {
|
|
|
return &SystemUpdateArticleLogic{
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
ctx: ctx,
|
|
|
svcCtx: svcCtx,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleUpdateRequest) (resp *types.SystemArticleUpdateResponse, err error) {
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("帖子不存在", err)
|
|
|
}
|
|
|
//TODO 获取图片的尺寸大小
|
|
|
images := []domain.Image{}
|
|
|
for _, val := range req.Images {
|
|
|
images = append(images, domain.Image{
|
|
|
Url: val,
|
|
|
Width: 0,
|
|
|
Height: 0,
|
|
|
})
|
|
|
}
|
|
|
article.Title = req.Title
|
|
|
article.Version = article.Version + 1
|
|
|
article.Images = images
|
|
|
article.WhoRead = req.WhoRead
|
|
|
article.WhoReview = req.WhoReview
|
|
|
article.TargetUser = domain.ArticleTarget(req.TargetUser)
|
|
|
article.Location = domain.Location{
|
|
|
Longitude: req.Location.Longitude,
|
|
|
Latitude: req.Location.Latitude,
|
|
|
Descript: req.Location.Descript,
|
|
|
}
|
|
|
//文章内容
|
|
|
articleSections := []domain.ArticleSection{}
|
|
|
lo.ForEach(req.Section, func(item types.ArticleSection, index int) {
|
|
|
articleSections = append(articleSections, domain.ArticleSection{
|
|
|
Id: item.Id,
|
|
|
CompanyId: article.CompanyId,
|
|
|
Version: article.Version,
|
|
|
ArticleId: article.Id,
|
|
|
Content: item.Content,
|
|
|
SortBy: index + 1,
|
|
|
})
|
|
|
})
|
|
|
//设置内容概要
|
|
|
if len(req.Section) > 0 {
|
|
|
// 截取内容 30个字
|
|
|
runeNumber := 0 //字数
|
|
|
stringIndex := 0 //字符串长度
|
|
|
for i := range req.Section[0].Content {
|
|
|
if runeNumber > 30 {
|
|
|
break
|
|
|
}
|
|
|
runeNumber += 1
|
|
|
stringIndex = i
|
|
|
}
|
|
|
article.Summary = req.Section[0].Content[0:stringIndex]
|
|
|
}
|
|
|
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
_, err = l.svcCtx.ArticleRepository.Update(l.ctx, conn, article)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("保存文章失败", err)
|
|
|
}
|
|
|
//文章内容
|
|
|
updateSectionIds := []int64{}
|
|
|
for _, item := range articleSections {
|
|
|
if item.Id > 0 {
|
|
|
section, err := l.svcCtx.ArticleSectionRepository.FindOne(ctx, c, item.Id)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("获取文章段落内容失败", err)
|
|
|
}
|
|
|
section.Content = item.Content
|
|
|
section.ArticleId = item.ArticleId
|
|
|
section.Version = item.Version
|
|
|
section.SortBy = item.SortBy
|
|
|
section.CompanyId = item.CompanyId
|
|
|
_, 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)
|
|
|
}
|
|
|
}
|
|
|
//备份数据
|
|
|
backup := article.MakeBackup(article.Author, articleSections)
|
|
|
backup.Action = "编辑"
|
|
|
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, backup)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("保存文章内容失败", err)
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
return
|
|
|
} |
...
|
...
|
|